0 Views
0 Downloads
0 Favorites
ZigZag RSI
//+------------------------------------------------------------------+

//|                                                   ZigZag Rsi.mq4 |

//|                          Copyright 2015,  Dodonov Vitaly (mql_5) |

//|                                             "https://ellizii.ru" |

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

#property copyright "Copyright 2015, Dodonov Vitaly (mql_5)"

#property link      "https://ellizii.ru"

#property version   "1.10"

#property description "ZigZag Rsi"

#property indicator_chart_window

#property indicator_buffers 1

#property indicator_plots   1

//--- plot ZZ_RSI

#property indicator_label1  "ZZ_RSI"

#property indicator_type1   DRAW_SECTION

#property indicator_color1  clrRed

#property indicator_style1  STYLE_SOLID

#property indicator_width1  1

//--- input parameters

input int      period=14; // RSI Period

//--- indicator buffers

double         ZZ_RSIBuffer[];

double         r1Buffer[];

double         r2Buffer[];

double         mass[];

double         rsi[];

//--- indicator variables

int index[];

bool up,dw;

int lowest,highest,rsi_handle;

string symb;

ENUM_TIMEFRAMES timeframe;

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

//| Custom indicator initialization function                         |

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

int OnInit()

  {

   timeframe = Period();

   symb = Symbol();

//--- indicator buffers mapping

   SetIndexBuffer(0,ZZ_RSIBuffer);

   ArraySetAsSeries(ZZ_RSIBuffer,true);

   ArraySetAsSeries(r1Buffer,true);

   ArraySetAsSeries(r2Buffer,true);

   ArraySetAsSeries(mass,true);

  // ArraySetAsSeries(rsi,true);

   up=false;dw=false;

   

   rsi_handle = iRSI(symb,timeframe,period,PRICE_CLOSE);

//---

   return(INIT_SUCCEEDED);

  }

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

//| Custom indicator iteration function                              |

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

int OnCalculate(const int rates_total,

                const int prev_calculated,

                const datetime &time[],

                const double &open[],

                const double &high[],

                const double &low[],

                const double &close[],

                const long &tick_volume[],

                const long &volume[],

                const int &spread[])

  {int i=0; if(prev_calculated==0)i=rates_total-2-period;else i=1;

  ArrayResize(r1Buffer,rates_total);

  ArrayResize(r2Buffer,rates_total);

  ArrayResize(mass,rates_total);

  ArraySetAsSeries(low,true);

  ArraySetAsSeries(high,true);

//---

     while(i>=0){

    CopyBuffer(rsi_handle,0,i+1,2,rsi);

    ZZ_RSIBuffer[i]=EMPTY_VALUE;

    

    

    if(up && rsi[0]<30 && rsi[1]>30){dw=true;up=false;ArrayResize(index,ArraySize(index)+1); index[ArraySize(index)-1]=i;}

    else if(dw && rsi[0]>70 && rsi[1]<70){up=true;dw=false;ArrayResize(index,ArraySize(index)+1); index[ArraySize(index)-1]=i;}

    else if(!up && !dw && rsi[0]<30 && rsi[1]>30){dw=true;ArrayResize(index,ArraySize(index)+1); index[ArraySize(index)-1]=i;}

    else if(!up && !dw && rsi[0]>70 && rsi[1]<70){up=true;ArrayResize(index,ArraySize(index)+1); index[ArraySize(index)-1]=i;}

    

    if(up && !dw){r1Buffer[i]=1;r2Buffer[i]=EMPTY_VALUE;}

    else if(dw && !up){r2Buffer[i]=1;r1Buffer[i]=EMPTY_VALUE;}

    

    if(up && ArraySize(index)>=2 && i>0){

    lowest = iLowest(symb,timeframe,MODE_LOW,index[ArraySize(index)-2]-index[ArraySize(index)-1]+1,index[ArraySize(index)-1]+1);

    ZZ_RSIBuffer[lowest]=low[lowest];}

    else if(dw && ArraySize(index)>=2 && i>0){

    highest = iHighest(symb,timeframe,MODE_HIGH,index[ArraySize(index)-2]-index[ArraySize(index)-1]+1,index[ArraySize(index)-1]+1);

    ZZ_RSIBuffer[highest]=high[highest];

    }

    

   i--;}

//--- return value of prev_calculated for next call

   return(rates_total);

  }

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

Comments