I'm looking at adding some automation to a manual trading system and I need to reference the recent highs and lows on the ZigZag indicator. Before I get blasted... yes, I know that it repaints the previous point but that's why I'm looking to reference points before this.
From looking at the code it appears that ZigZag stores the price of each point in buffer0 where i is a standard count of bars back from now. I've seen a couple of support and resistance indicators that reference this but they call all the bars back to "limit" and it's very cpu hungry. I only need the most recent few points so I was trying to call from bar 1 working up (i++) rather than down.
I've written the following code to try to populate 5 price doubles with the price of the last 5 high and low points held by the ZigZag indicator.
Code: Select all
PointsFound = 0; // may not need this as reset it just before exiting loop
NewPointFound=False; // this is a bool I'll use later in the EA
for( int i=1; i<=MaxBars; i++) //trying to do this forwards to save cpu
{
if(i==(MaxBars) && d1==0) // I'm trying to limit the # of bars but I might cut it too fine so error if so.
{
Print("Error: MaxBars reached without plotting all zz points");
}
double zz = iCustom(Symbol(),0,"ZigZag",varExtDepth,varExtDeviation,varExtBackstep,0,i);
if(zz==0)continue; // no zz point at this bar, move on to the next i
if(zz!=0) // we have found a zz point at bar i
{
PointsFound++;
if(zz==d1 && PointsFound==1)break; // we've already plotted point, wait for a new point to be drawn
if(zz!=d1 && PointsFound==1)
{
NewPointFound=true;
d1=zz;
}
if(PointsFound==2)d2=zz;
if(PointsFound==3)d3=zz;
if(PointsFound==4)d4=zz;
if(PointsFound==5)
{
d5=zz;
PointsFound=0; // reset for the next time
break; // exit here and wait for the next new bar.
}
}//if(zz!=0)
}//for( int i=1; i<=MaxBars; i++)The problem I've got is that this code seems to only half work. Sometimes it's great and the 5 variables are perfect. Other times, the 5 variables all have valid zz points but some of the points have been missed out in between. So the variables 1 to 5 are actually populated with zz points 1,2,4,6,8 for example. With the zigzag indicator on a craptester chart alongside this code in an EA I can see the missed points but I have no idea why they are not populating correctly. Yes, I have the inputs the same in the indicator and the EA.
Can anyone spot the error? Does anyone know of a better way to get the values out - without having to look back through all the bars each time!?
Any help appreciated.
Thanks
Bruster.