number of bars since the last zig-zag swing

 

I'm looking for other ways to define cycle length to dynamically adjust indicator values, and because DFT, Goertzel, Corona, MESA, .. don't really do it for me I thought of a more simple idea: if you take the zigzag indicator (that shows you the swing highs and swing lows) then you pretty much see on the chart what the perfect trader would have done in the past. Off course there is no way of coming even close to this, and this indicator does change its past values on a live chart, so it is in no way a good indicator for trading (maybe for system optimization). But what if you could track the number of bars since the last swing high/low? This would effectively show you how many bars there are in a cycle. Then all you have to do is use this value as a setting (i.e. for momentum or RSI) as a number for the look-back period (or half the number, depending on the indicator).

So you I want to create an indicator that shows the number of bars since the last swing high/low: since the last turn, and show this as a graph in the separate chart window.

Files:
zigzag.mq4  7 kb
 
MrH:

I'm looking for other ways to define cycle length to dynamically adjust indicator values, and because DFT, Goertzel, Corona, MESA, .. don't really do it for me I thought of a more simple idea: if you take the zigzag indicator (that shows you the swing highs and swing lows) then you pretty much see on the chart what the perfect trader would have done in the past. Off course there is no way of coming even close to this, and this indicator does change its past values on a live chart, so it is in no way a good indicator for trading (maybe for system optimization). But what if you could track the number of bars since the last swing high/low? This would effectively show you how many bars there are in a cycle. Then all you have to do is use this value as a setting (i.e. for momentum or RSI) as a number for the look-back period (or half the number, depending on the indicator).

So you I want to create an indicator that shows the number of bars since the last swing high/low: since the last turn, and show this as a graph in the separate chart window.


Take a look in the codebase, there is a good indicator calle AutoFibAutoTrend (or similar like this)

There is the ZigZag inside for drwaing the trend-channel. i guess, this part you can use!

double swing.value[4]={0,0,0,0};
      datetime swing.date[4]={0,0,0,0};
      int found=0;
      double tmp=0;
      int i=0;
      while(found<4){
         if(iCustom(Symbol(),period,"ZigZag",12,5,3,0,i)!=0){
            swing.value[found]=iCustom(Symbol(),period,"ZigZag",12,5,3,0,i);
            swing.date[found]=iTime(Symbol(),period,i);
            found++;
         }
         i++;
      }
 
EADeveloper:


Take a look in the codebase, there is a good indicator calle AutoFibAutoTrend (or similar like this)

There is the ZigZag inside for drwaing the trend-channel. i guess, this part you can use!


Can't you simply use the zigzag code and store the bar number in a constant, and then increase this with every bar that doesn't make a swing?
 
EADeveloper:


Take a look in the codebase, there is a good indicator calle AutoFibAutoTrend (or similar like this)

There is the ZigZag inside for drwaing the trend-channel. i guess, this part you can use!

ZigZag repaints the last value, so i (as you see in the above code) get the last 4 values, but for drawing i use only the 3 which does not repaint. Due to this the AutoFibAutoTrend might is a bit late for some swings but works nice in true trends or for breakouts...
 
zzuegg:
ZigZag repaints the last value, so i (as you see in the above code) get the last 4 values, but for drawing i use only the 3 which does not repaint. Due to this the AutoFibAutoTrend might is a bit late for some swings but works nice in true trends or for breakouts...

I don't get why this code doesn't work: It shows what I wanted to do (display the number of bars since the last turn of the zig zag indicator), but only the direction of the indicator is wrong: the lines is going down instead of going up.. why is this so?

//+------------------------------------------------------------------+

//| empty.mq4 |

//| Copyright © 2009, MetaQuotes Software Corp. |

//| http://www.metaquotes.net |

//+------------------------------------------------------------------+

#property copyright "Copyright © 2009, MetaQuotes Software Corp."

#property link "http://www.metaquotes.net"


#property indicator_separate_window

#property indicator_buffers 2

#property indicator_color1 Red

#property indicator_color2 Black

#property indicator_width1 3

#property indicator_width2 2

double ExtMapBuffer0[];

double ExtMapBuffer1[];

//+------------------------------------------------------------------+

//| Custom indicator initialization function |

//+------------------------------------------------------------------+

int init()

{

//---- indicators

SetIndexBuffer(0,ExtMapBuffer0);

SetIndexBuffer(1,ExtMapBuffer1);

SetIndexStyle(0,DRAW_LINE,EMPTY);

SetIndexStyle(1,DRAW_LINE,EMPTY);

//----

return(0);

}

//+------------------------------------------------------------------+

//| Custom indicator deinitialization function |

//+------------------------------------------------------------------+

int deinit()

{

//----

//----

return(0);

}

//+------------------------------------------------------------------+

//| Custom indicator iteration function |

//+------------------------------------------------------------------+

int start()

{

int limit;

double middle, upper, lower;

int counted_bars=IndicatorCounted();

//---- check for possible errors

if(counted_bars<0) return(-1);

//---- last counted bar will be recounted

if(counted_bars>0) counted_bars--;

limit=Bars-counted_bars;

double a=0;

for(int i=0; i<limit; i++)

{


ExtMapBuffer0[i]=iCustom(0,0,"zigzag",12,5,3,0,i);


if(iCustom(0,0,"zigzag",12,5,3,0,i)!=0)

{

a=0;

}

else

{

a++;

}


ExtMapBuffer1[i]=a;

}


return(0);

}

 

Because you are iterating backwards trough history.

change this:

for(int i=0; i<limit; i++)

to this:

for(int i=limit;i>=0;i--)
 
zzuegg:

Because you are iterating backwards trough history.

change this:

to this:


Thanks zzuegg
 
Reason: