Help reading the ZigZag...

Post Reply
Bruster400
Trader
Posts: 192
Joined: Tue Sep 24, 2013 3:19 pm

Help reading the ZigZag...

Post by Bruster400 »

Hi,

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.
afeudale
Trader
Posts: 59
Joined: Mon Aug 08, 2016 1:23 pm

Help reading the ZigZag...

Post by afeudale »

Just read through your post quickly, but I struggled with a similar issue (getting values from a range trading indicator) and iCustom just wasn't cutting it.

Instead, I wrote some code where it scanned the trendlines themselves, then found which trendline was most recent and got the value directly, rather than the iCustom call. It works great now.

Basically this (replace the ZigZag line name where it shows LRC):

Code: Select all

for(i=0;i<100;i++)
{
   if(ObjectFind("LRC#"+i)==0){HighestRange=i;};
}

ObjectGet("LRC#"+HighestRange,OBJPROP_PRICE2);
Hope it helps.
User avatar
renexxxx
Trader
Posts: 860
Joined: Sat Dec 31, 2011 3:48 am

Help reading the ZigZag...

Post by renexxxx »

Hi Bruster,

I don't understand your code ... but I would do something like this:

Code: Select all

void getZigZag( double &myZigZag[], int lookBack = 5 ) {

   // Reserve space and initialize the myZigZag-array
   ArrayResize( myZigZag, lookBack ); 
   ArrayInitialize( myZigZag, 0.0 );
   
   double zigZagValue = 0.0;
   int    zigZagIndex = 0; 
   int    myShift     = 0;
   while(true) {
      zigZagValue = iCustom( _Symbol, _Period, "ZigZag", 0, myShift );
      if ( zigZagValue > 0.0 ) {
         myZigZag[zigZagIndex] = zigZagValue;
         zigZagIndex++;
         if ( zigZagIndex == lookBack ) break;
      }
      myShift++;
   }
}
You can test this with the attached script.
You do not have the required permissions to view the files attached to this post.
Bruster400
Trader
Posts: 192
Joined: Tue Sep 24, 2013 3:19 pm

Help reading the ZigZag...

Post by Bruster400 »

renexxxx » Tue Nov 15, 2016 8:22 am wrote: I don't understand your code ...
Lol!! :lol: That just shows how far away from getting this right I was!!

In truth, I had the right idea of scanning back through the points and recording the values as they appeared (>0) but I was clearly going about it the wrong way. Dropping them into an array until it's full is eminently more sensible. Thanks very much for your help renexxxx!

afeudale, Thanks for you help too, really appreciate it! I had tried looking at object lines using a separate indicator but I want to keep this as simple (and cpu easy) as possible so I need to look for the zigzag points directly.
ruktoa
Posts: 5
Joined: Fri Aug 10, 2012 8:50 am

Help reading the ZigZag...

Post by ruktoa »

Here is my changed version of "ZigZag" indicator.
  • omit meaningless input parameter "Deviation"
  • not calculate Bar[0]
  • low CPU usage (no redundant loop calculations)
  • store zigzag values to Bar[Bars-1] and Bar[Bars-2] (if "BUF_INFO" enabled)
  • not exactly the same zigzag points as MetaQuotes' "ZigZag"
You do not have the required permissions to view the files attached to this post.
Bruster400
Trader
Posts: 192
Joined: Tue Sep 24, 2013 3:19 pm

Help reading the ZigZag...

Post by Bruster400 »

ruktoa » Tue Nov 29, 2016 2:45 pm wrote:Here is my changed version of "ZigZag" indicator.
  • omit meaningless input parameter "Deviation"
Thanks for this version. I knew that the Metaquotes zigzag was incorrectly coded due to the deviation input not working. However, that doesn't mean that it's meaningless. Check out this thread by Big Be » Mon Dec 17, 2012 4:19 am for an in-depth review of the intended purpose of the indicator and what the deviation input is supposed to do. He's also posted a corrected version of the indicator which is now the one I use.
Post Reply

Return to “Coders Hangout”