query past values

Posted By: Paul_der_Zweite

query past values - 11/22/18 16:59

Sorry i'm a newbie ...
I have a small problem, which is probably very easy to solve:

How can I query past values, here the indicator value KAMA from before n candles correctly and then use it again?
My state of knowledge: I define a series (here: vars kama = KAMA(Price,10) and can then address a certain point in time n (shift or candles in the past) with series[n] (here: kama[n]).

The following part of my script:
Code:
vars Price = series(priceClose());
vars kama = KAMA(Price,10);
var close_kama(int n)
 {
	return  kama[n] / priceClose(n);
 }

vars cycle = DominantPeriod(Price,100);
var cycle_multi = 2;
var close_res(int n)
{
	return priceClose(n) / HH(cycle[n]*cycle_multi,n+1);
}

var close_sup(int n)
{
	return priceClose(n) / LL(cycle[n]*cycle_multi,1);
}


causes the following error:
Code:
Error 111: Crash in function: close_kama() at bar 0



What am I doing wrong?
Everything?
grin
Posted By: Dalla

Re: query past values - 11/22/18 17:52

Wrap your indicators in a series, like you did with Price.
Like so,

series(KAMA(Price,10));
Posted By: Paul_der_Zweite

Re: query past values - 11/22/18 18:51

Hi, thank you so much for your answer:

I changed the script as follows:
Code:
vars Price = series(priceClose());
vars kama = series(KAMA(Price,10));
var close_kama(int n)
 {
	return  kama[n] / priceClose(n);
 }

vars cycle = series(DominantPeriod(Price,100));
var cycle_multi = 2;
var close_res(int n)
{
	return priceClose(n) / HH(cycle[n]*cycle_multi,n+1);
}

var close_sup(int n)
{
	return priceClose(n) / LL(cycle[n]*cycle_multi,n+1);
}


But the error output remains the same.
Posted By: Dalla

Re: query past values - 11/22/18 22:05

Can you paste all of your code? there is no run function here
Posted By: Paul_der_Zweite

Re: query past values - 11/22/18 23:14

Sure, here's the whole script:
Code:
#include <r.h>

vars Price = series(priceClose());
vars kama = series(KAMA(Price,10));
var close_kama(int n)
 {
	return  kama[n] / priceClose(n);
 }

vars cycle = series(DominantPeriod(Price,100));
var cycle_multi = 2;
var close_res(int n)
 {
	return priceClose(n) / HH(cycle[n]*cycle_multi,n+1);
 }

var close_sup(int n)
 {
	return priceClose(n) / LL(cycle[n]*cycle_multi,n+1);
 }
///////////////////////////////////////////////////////////////////////
function run()
{
	StartDate = 20160101;
	BarPeriod = 120;
	LookBack = 100;
	WFOPeriod = 22*288;
	DataSplit = 90;
	NumCores = -1;
	if(is(TRADEMODE|TESTMODE))
		Hedge = 5; 
		else
		Hedge = 2;  // full hedging in train mode

	
	set(RULES|LOGFILE|TESTNOW|PLOTNOW);
	//set(PARAMETERS|LOGFILE|TESTNOW|PLOTNOW);

	//LifeTime = 140;
	
	//Capital = 1000;		// reinvestment mode
	//Margin = 0.01 * OptimalF * Capital * sqrt(1 + ProfitClosed/Capital);
	//Stop = MedPrice(1) + 3 * ATR(14); 
	//TakeProfit = 3 * ATR(14);
	//TrailStep = 3 * ATR(14);
  	//Trail = ATR(14);
	//TrailLock = 80;
	
  
///////////////////////////////////////////////////////////	
	if(adviseLong(NEURAL,0,
		close_kama(1),close_kama(2),close_kama(3),close_kama(4),
		close_res(1),close_res(2),close_res(3),close_res(4),
		close_sup(1),close_sup(2),close_sup(3),close_sup(4)) > 0.5) 
		enterLong();

		if(adviseShort() > 0.5) 
		   enterShort();

	PlotWidth = 800;
	PlotHeight1 = 340;
	ColorUp = ColorDn = ColorWin = ColorLoss = 0;
}

Posted By: Dalla

Re: query past values - 11/23/18 19:56

You need to place your series inside the run function, that's the function that will run on every bar.
Posted By: Paul_der_Zweite

Re: query past values - 11/25/18 17:34

Mmh, the basic idea of the script or the basic structure comes from financial-hacker.com. The original script can be downloaded there [x] and looks like this:

Code:
///////////////////////////////////////////////////////////////////////
#include <r.h>

var change(int n)
{
	return scale((priceClose(0) - priceClose(n))/priceClose(0),100)/100;
}

var range(int n)
{
	return scale((HH(n) - LL(n))/priceClose(0),100)/100;
}

///////////////////////////////////////////////////////////////////////

function run()
{
	StartDate = 20140601;
	BarPeriod = 60;	// 1 hour
	LookBack = 100;

	WFOPeriod = 252*24; // 1 year
	DataSplit = 90;
	NumCores = -1;

	set(RULES|LOGFILE|TESTNOW|PLOTNOW);
	Spread = RollLong = RollShort = Commission = Slippage = 0;
	LifeTime = 3;
	if(Train) Hedge = 2;
	
///////////////////////////////////////////////////////////	
	if(adviseLong(NEURAL+BALANCED,0,
		change(1),change(2),change(3),change(4),
		range(1),range(2),range(3),range(4)) > 0.5) 
		enterLong();
	if(adviseShort() > 0.5) 
		enterShort();

	PlotWidth = 800;
	PlotHeight1 = 340;
	ColorUp = ColorDn = ColorWin = ColorLoss = 0;
}



The corresponding part of the script runs in a run function, but some functions are outside this function, but are queried from it.
Not okay?

Not okay?


[x] http://financial-hacker.com/scripts2016.zip
Posted By: AndrewAMD

Re: query past values - 11/25/18 22:20

Not okay. Do not use series() outside of run(). Use it inside run() instead.
Posted By: Paul_der_Zweite

Re: query past values - 11/26/18 12:11

Okay, thank you very much for your answers. I got it. grin
© 2024 lite-C Forums