static variable

Posted By: tomaslolo

static variable - 05/31/19 18:32

Hi all, I want to create a custom RSI indicator that just plots besides price series.

As you know RSI values go from 0 to 100 so if I want to plot it together with price series I need to multiply its value. So I want to create a multiplier.

This multiplier would get the first RSI value of the series and first priceClose value and divide them, so it will store a double variable (var?). As this variable must remain unchanged has to be declared as static (global?).

It will also take the first value of the series so has to be calculated at FIRSTRUN.

Obviously I´m doing it wrong as I get a few different error messages and I´m stucked. This is my code:
Code:
// PRUEBA DE MULTIPLICADOR ////////////////

var multiplier=0.0;

function run() 
{
	StartDate = 20160101; // 20160101
	//EndDate = 20170301;
	EndDate = 20181231;
	BarPeriod = 5;	// 1 hour
	LookBack = 200;

	asset("EUR/USD"); //"EUR/USD"
	
	if(is(FIRSTRUN)){
	vars myRSI=series(RSI(priceClose,100));
	static var multiplier=(priceClose(0)/myRSI[0]);
	}
	
	vars myRSI=series(RSI(priceClose,100));
	//var multiplier=priceClose(0)/myRSI[0]; this one would change multiplier value at every new bar, so delete it.
	vars myPrice=series(myRSI[0]* multiplier);
	

// plot signals and thresholds

   plot("myPrice", myPrice, 0, RED);
	
	PlotWidth = 600;
	PlotHeight1 = 300;
	set(PLOTNOW);
}



Any clues?

Thank you in advanced.
Posted By: Smon

Re: static variable - 06/01/19 11:10

This might put you back on track:

Code:
// PRUEBA DE MULTIPLICADOR ////////////////

var multiplier=0.0;

function run() 
{
	StartDate = 20160101; // 20160101
	//EndDate = 20170301;
	EndDate = 20181231;
	BarPeriod = 5;	// 1 hour
	LookBack = 200;

	asset("EUR/USD"); //"EUR/USD"
	
	
	static var multiplier=1;

	vars Price = series(priceClose());
	
	var myRSI = RSI(Price, 100);
	
	static int neverplotted = 1;
	
	if(!is(LOOKBACK) && neverplotted) 
	{
		neverplotted = 0;
		printf("nmyRSI = %.1f, multiplier before set = %.1f, priceClose() = %.5f", myRSI, multiplier, priceClose());
		multiplier=priceClose()/myRSI;
		printf("nmultiplier after set = %.5f", multiplier);
	}
	
	//var multiplier=priceClose(0)/myRSI[0]; this one would change multiplier value at every new bar, so delete it.
	vars myPrice=series(myRSI*multiplier);
	
	//if(!is(INITRUN)) printf("nmultiplier = %.5f, Price[0] = %.5f, myRSI = %.0f, myPrice[0] = %.5f", multiplier, Price[0], myRSI, myPrice[0]);
	

// plot signals and thresholds

   plot("myPrice", myPrice, 0, RED);
	
	PlotWidth = 600;
	PlotHeight1 = 300;
	set(PLOTNOW);
}

Posted By: tomaslolo

Re: static variable - 06/03/19 21:10

It does.

Thank you very much!
© 2024 lite-C Forums