Zero values in BW FractalHigh series - workaround?

Posted By: distribution

Zero values in BW FractalHigh series - workaround? - 10/03/20 13:07

I'm using the Bill Williams fractalHigh series. It works well when a fractal is found - PriceH1FractalHigh[0]. However when I go to access the
previous fractal to the one just found - PriceH1FractalHigh[1] it returns a 0 value. Looking at the code FractalHigh form indicators.c
this makes sense - when a fractal is not found a zero is returned. But as you can imagine this is not ideal, as it makes it difficult to access
the previous fractal high - I have to search through the series until I find the next non-zero value. This is tedious and it feels like a Zorro anti-pattern.

I've attached some code below which illustrates the issue.

Any suggestions on a workaround ?


Code
#define VERBOSE	3

void fractalH1TF() {

  vars PriceH1FractalHigh = series(FractalHigh(series(priceHigh()),3));
  printf("FOUND PriceH1FractalHigh[0] = %.5f  PriceH1FractalHigh[1] = %.5f \n", PriceH1FractalHigh[0], PriceH1FractalHigh[1]);
  plotGraph("HTFFractalHigh", 1, 1.0002*PriceH1FractalHigh[0],TRIANGLE4, 0xE5DEDD);

}


function run()
{
  set(PLOTNOW, TICKS);
  PlotScale = 8;
  Verbose = VERBOSE;

  BarPeriod = 60; //H1

  LookBack = 24;
  StartDate = 20170102;
  EndDate = 20170201;

  fractalH1TF();

  }




BW fractal function def taken from indicators.c

Code
var FractalHigh(var* Data,int Period)
{
	if(Period < 3) return *Data;
	int Center = (Period-1)/2;
	if(MaxIndex(Data,Period) == Center)
		return Data[Center];
	else return 0.;
}
Posted By: kalmar

Re: Zero values in BW FractalHigh series - workaround? - 10/03/20 22:27

I guess the variables should be static here like in the example below

Code
function run()	
{	
StartDate = 20160101;	
EndDate = 20160331;	
BarPeriod = 1440;	
	
vars Price = series(price());	
vars Highs = series(priceHigh());	
vars Lows = series(priceLow());	
	
var fHigh = FractalHigh(Highs, 7);	// short term highs and lows
var fLow = FractalLow(Lows, 7);	
static var fHighCurrent, fLowCurrent;	// fractal values are retained between runs ...
if (fHigh != 0) fHighCurrent = fHigh;	// ... and are changed whenever a non-zero value is returned by FractalHigh() ...
if (fLow !=0) fLowCurrent = fLow;	// ...  or FractalLow()
	
plot("Recent High", fHighCurrent, MAIN|DOT, BLUE);	
plot("Recent Low", fLowCurrent, MAIN|DOT, RED);	
}	
Posted By: distribution

Re: Zero values in BW FractalHigh series - workaround? - 10/04/20 13:28

I appreciate the response Kalmar. I'd like fHighCurrent and fLowCurrent to be a series.

Code below, but Zorro crashes. Have I missed something ?

Code

function run()
{
set(PLOTNOW, TICKS);
PlotScale = 8;

StartDate = 20160101;
EndDate = 20160331;
BarPeriod = 60;
LookBack = 3;

vars Price = series(price());
vars Highs = series(priceHigh());
vars Lows = series(priceLow());

var fHigh = FractalHigh(Highs, 3);	// short term highs and lows
var fLow = FractalLow(Lows, 3);
static vars fLowCurrent = series(), fHighCurrent = series();	// fractal values are retained between runs ...


if(fHigh != 0){
  fHighCurrent[0] = fHigh;
  plot("Recent High", fHighCurrent[0], MAIN|DOT, BLUE);
  printf(" fHighCurrent[0] = %.5f, fHighCurrent[1] = %.5f \n ", fHighCurrent[0], fHighCurrent[1]);
}

if(fLow !=0){
  fLowCurrent[0] = fLow;
  plot("Recent Low", fLowCurrent[0], MAIN|DOT, RED);
  printf(" fLowCurrent[0] = %.5f, fLowCurrent[1] = %.5f \n ", fLowCurrent[0], fLowCurrent[1]);
  
}

}

Posted By: danatrader

Re: Zero values in BW FractalHigh series - workaround? - 10/04/20 13:35

You have to shift static series manually.
Static series should be defined globally, not within the run function.

https://zorro-trader.com/manual/en/shift.htm


https://zorro-trader.com/manual/en/series.htm

Static series (defined with a negative length) are not automatically shifted, but can be shifted by script with the shift function. Script-shifted static series can be used for excluding certain time periods from indicators, such as out-of-market hours or periods with high or low volatility. They can also be used for shifting a series by arriving price ticks in the tick function. Not shifted static series can store additional asset-dependent variables. Usage examples can be found in the SpecialBars script or in the SAR code in indicators.c.
Posted By: distribution

Re: Zero values in BW FractalHigh series - workaround? - 10/05/20 18:34

Thanks for the feedback.

The following works well. However, I can't seem to get the fHighCurrent & fLowCurrent to work as Global vars..
Even if I place them in a seperate function outside of run.

Code
// Uncomment for crash.
//vars fHighCurrent = series(0, -LookBack);
//vars fLowCurrent = series(0, -LookBack);

function run()
 {
    set(PLOTNOW, TICKS);
    PlotScale = 8;

    StartDate = 20160101;
    EndDate = 20160331;
    BarPeriod = 60;
    LookBack = 50;

    vars Price = series(price());
    vars Highs = series(priceHigh());
    vars Lows = series(priceLow());
    var fHigh = FractalHigh(Highs, 3);	// short term highs and lows
    var fLow = FractalLow(Lows, 3);

    vars fHighCurrent = series(0, -LookBack);
    vars fLowCurrent = series(0, -LookBack);

    if(fHigh != 0){
      shift(fHighCurrent, fHigh, LookBack);
      plot("Recent High", fHighCurrent[0], MAIN|DOT, BLUE);
      printf(" fHighCurrent[0] = %.5f, fHighCurrent[1] =  %.5f  \n ", fHighCurrent[0], fHighCurrent[1]);
    }

    if(fLow !=0){
      shift(fLowCurrent, fLow, LookBack);
      plot("Recent Low", fLowCurrent[0], MAIN|DOT, RED);
      printf(" fLowCurrent[0] = %.5f, fLowCurrent[1] = %.5f \n ", fLowCurrent[0],fLowCurrent[1]);
    }
}

© 2024 lite-C Forums