Need help on Live Renko chart

 

Hi, I am using this renko chart. there are couple of issues. need help with that.

1. Sometime its visible on all time frames, some time not.

2. I am trying make an EA using the indicator but not able to copy the MA buffers.

Pls help to resolve these errors.


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

//|                                                        Renko.mq5 |

//|                                            Copyright 2012, Rone. |

//|                                            rone.sergey@gmail.com |

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

#property copyright "Copyright 2012, Rone."

#property link      "rone.sergey@gmail.com"

#property version   "1.00"

#property description "The Renko chart. Regardless of whether what a time frame is used for the chart on which the indicator is attached, "

#property description "the Renko is calculated by the closing prices of М1 time frame."

//--- indicator settings

#property indicator_chart_window

#property indicator_buffers 9

#property indicator_plots   4

//--- plot Renko

#property indicator_label1  "Renko Open;Renko High;Renko Low;Renko Close"

#property indicator_type1   DRAW_COLOR_CANDLES

#property indicator_color1  clrRed,clrLime

#property indicator_style1  STYLE_SOLID

#property indicator_width1  1

//--- Plot 2: EMA1

#property indicator_type2   DRAW_LINE

#property indicator_color2  Lime

#property indicator_width2  2

#property indicator_label2  "EMA1"



//--- Plot 3: EMA2

#property indicator_type3   DRAW_LINE

#property indicator_color3  Red

#property indicator_width3  2

#property indicator_label3  "EMA2"



//--- Plot 4: EMA3

#property indicator_type4   DRAW_LINE

#property indicator_color4  Aqua

#property indicator_width4  2

#property indicator_label4  "EMA3"



//--- input parameters

input int      InpBoxSize = 100; // Box size (in pips)

//--- indicator buffers

double         BoxOpenBuffer[];

double         BoxHighBuffer[];

double         BoxLowBuffer[];

double         BoxCloseBuffer[];

double         BoxColors[];

double         ExtEMA1Buffer[];

double         ExtEMA2Buffer[];

double         ExtEMA3Buffer[];

double         ExtColorBuffer[];

//---

double         boxSize;

//--- EMA periods

input int      EMA1Period = 5;

input int      EMA2Period = 13;

input int      EMA3Period = 21;

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

//| Custom indicator initialization function                         |

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

int OnInit() {

//---

   if ( InpBoxSize < 50 ) {

      boxSize = 50 *_Point;

      Print("Incorrect parameter InpBoxSize = ", InpBoxSize, ". Value equal to 50 will be used.");

   } else {

      boxSize = InpBoxSize * _Point;

   }

//--- indicator buffers mapping

   SetIndexBuffer(0, BoxOpenBuffer, INDICATOR_DATA);

   SetIndexBuffer(1, BoxHighBuffer, INDICATOR_DATA);

   SetIndexBuffer(2, BoxLowBuffer, INDICATOR_DATA);

   SetIndexBuffer(3, BoxCloseBuffer, INDICATOR_DATA);

   SetIndexBuffer(4, BoxColors, INDICATOR_COLOR_INDEX);

   SetIndexBuffer(5, ExtEMA1Buffer, INDICATOR_DATA);

   SetIndexBuffer(6, ExtEMA2Buffer, INDICATOR_DATA);

   SetIndexBuffer(7, ExtEMA3Buffer, INDICATOR_DATA);

   SetIndexBuffer(8, ExtColorBuffer, INDICATOR_DATA);

   

   //--- initialize EMA buffers with zero

   ArrayInitialize(ExtEMA1Buffer, 0.0);

   ArrayInitialize(ExtEMA2Buffer, 0.0);

   ArrayInitialize(ExtEMA3Buffer, 0.0);

   

//---

   PlotIndexSetDouble(0, PLOT_EMPTY_VALUE, 0.0);

//---

   IndicatorSetInteger(INDICATOR_LEVELS, 1);

   IndicatorSetInteger(INDICATOR_LEVELCOLOR, clrGray);

   IndicatorSetInteger(INDICATOR_LEVELSTYLE, STYLE_SOLID);

//---

   IndicatorSetInteger(INDICATOR_DIGITS, _Digits);

//---

   IndicatorSetString(INDICATOR_SHORTNAME, "Renko ("+(string)boxSize+")");  

//---

   return(0);

}

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

//| Custom indicator iteration function                              |

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

int OnCalculate(const int rates_total,

                const int prev_calculated,

                const int begin,

                const double &price[])

{

//---

   double RenkoBuffer[], BackupBuffer[], Close[];

   int m1RatesTotal = Bars(_Symbol, PERIOD_M1);

   int renkoShift = 0;

   int barShift;

//---

   for ( ; ArrayResize(RenkoBuffer, m1RatesTotal) == -1 &&

      ArrayResize(BackupBuffer, m1RatesTotal) == -1 &&

      ArrayResize(Close, m1RatesTotal) == -1; m1RatesTotal /= 2 );

//---

   if ( CopyClose(_Symbol, PERIOD_M1, 0, m1RatesTotal, Close) != m1RatesTotal ) {

      Print("Failed to copy history data for the М1 time frame. Error #", GetLastError());

      return(0);

   }

   RenkoBuffer[renkoShift] = Close[0];

   renkoShift += 1;

//---

   for ( barShift = 1; MathAbs(Close[barShift]-Close[0]) <= boxSize; barShift++ );

  

   for ( ; Close[barShift] > RenkoBuffer[renkoShift] + boxSize; ) {

      renkoShift += 1;

      RenkoBuffer[renkoShift] = RenkoBuffer[renkoShift-1] + boxSize;

   }

   for ( ; Close[barShift] < RenkoBuffer[renkoShift] - boxSize;  ) {

      renkoShift += 1;

      RenkoBuffer[renkoShift] = RenkoBuffer[renkoShift-1] - boxSize;

   }

//---

   for ( ; barShift < m1RatesTotal; barShift++ ) {

      if ( renkoShift > ArraySize(RenkoBuffer) - 100 ) {

         ArrayCopy(BackupBuffer, RenkoBuffer);

         ArrayResize(RenkoBuffer, ArraySize(RenkoBuffer) + m1RatesTotal);

         ArrayCopy(RenkoBuffer, BackupBuffer, 0, 0, renkoShift+1);

         ArrayResize(BackupBuffer, ArraySize(BackupBuffer) + m1RatesTotal);

         ArrayResize(ExtEMA1Buffer, m1RatesTotal);

         ArrayResize(ExtEMA2Buffer, m1RatesTotal);

         ArrayResize(ExtEMA3Buffer, m1RatesTotal);

      }

      //---

      double lastClose = Close[barShift];

      

      //---

      if ( RenkoBuffer[renkoShift] > RenkoBuffer[renkoShift-1] ) {

         if ( lastClose > RenkoBuffer[renkoShift] + boxSize ) {

            for ( ; lastClose > RenkoBuffer[renkoShift] + boxSize; ) {

               renkoShift += 1;

               RenkoBuffer[renkoShift] = RenkoBuffer[renkoShift-1] + boxSize;

            }

         } else if ( lastClose < RenkoBuffer[renkoShift] - 2 * boxSize ) {

            renkoShift += 1;

            RenkoBuffer[renkoShift] = RenkoBuffer[renkoShift-1] - 2 * boxSize;

            for ( ; lastClose < RenkoBuffer[renkoShift-1] - boxSize; ) {

               renkoShift += 1;

               RenkoBuffer[renkoShift] = RenkoBuffer[renkoShift-1] - boxSize;

            }

         }

      }

      //---

      if ( RenkoBuffer[renkoShift] < RenkoBuffer[renkoShift-1] ) {

         if ( lastClose < RenkoBuffer[renkoShift] - boxSize ) {

            for ( ; lastClose < RenkoBuffer[renkoShift] - boxSize; ) {

               renkoShift += 1;

               RenkoBuffer[renkoShift] = RenkoBuffer[renkoShift-1] - boxSize;

            }

         } else if ( lastClose > RenkoBuffer[renkoShift] + 2 * boxSize ) {

            renkoShift += 1;

            RenkoBuffer[renkoShift] = RenkoBuffer[renkoShift-1] + 2 * boxSize;

            for ( ; lastClose > RenkoBuffer[renkoShift] + boxSize; ) {

               renkoShift += 1;

               RenkoBuffer[renkoShift] = RenkoBuffer[renkoShift-1] + boxSize;

            }

         }

      }

   }

//---

   ArrayInitialize(BoxOpenBuffer, 0.0);

   ArrayInitialize(BoxHighBuffer, 0.0);

   ArrayInitialize(BoxLowBuffer, 0.0);

   ArrayInitialize(BoxCloseBuffer, 0.0);     

//---

   if ( renkoShift > rates_total - 100 ) {

      for ( int i = 0; i <= rates_total - 100; i++ ) {

         RenkoBuffer[i] = RenkoBuffer[i+renkoShift-(rates_total-100)];                          

      }

      renkoShift = rates_total - 100;

//---

   for ( int i = 2; i <= renkoShift; i++ ) {

      int bar = rates_total - renkoShift - 1 + i;

      double current = RenkoBuffer[i];

      double previous = RenkoBuffer[i-1];

      double penult = RenkoBuffer[i-2];

      //---

      if ( current > previous ) {

         BoxColors[bar] = 1;

         BoxCloseBuffer[bar] = BoxHighBuffer[bar] = current;

         if ( previous > penult ) {

            BoxOpenBuffer[bar] = BoxLowBuffer[bar] = previous;

         } else if ( previous < penult ) {            

            BoxOpenBuffer[bar] = BoxLowBuffer[bar] = current - boxSize;

         }

      } else if ( current < previous ) {

         BoxColors[bar] = 0;

         BoxCloseBuffer[bar] = BoxLowBuffer[bar] = current;

         if ( previous < penult ) {

            BoxOpenBuffer[bar] = BoxHighBuffer[bar] = previous;

         } else if ( previous > penult ) {

            BoxOpenBuffer[bar] = BoxHighBuffer[bar] = current + boxSize;

         }

      }

   }



   // Initialize EMA buffers if necessary

   if (ArraySize(ExtEMA1Buffer) < rates_total) {

      ArrayResize(ExtEMA1Buffer, rates_total);

      ArrayResize(ExtEMA2Buffer, rates_total);

      ArrayResize(ExtEMA3Buffer, rates_total);

   }

   

   // Calculate EMA based on Renko close buffer

   double k1 = 2.0 / (EMA1Period + 1);

   double k2 = 2.0 / (EMA2Period + 1);

   double k3 = 2.0 / (EMA3Period + 1);   

   

   // Calculate EMA

   for (int i = 1; i < rates_total; i++) {

       double ha_close = BoxCloseBuffer[i];

       double ha_open = BoxOpenBuffer[i];

              

       // Calculate EMA 1

       ExtEMA1Buffer[i] = (k1 * ha_close) + (1 - k1) * ExtEMA1Buffer[i - 1];

   

       // Calculate EMA 2

       ExtEMA2Buffer[i] = (k2 * ha_close) + (1 - k2) * ExtEMA2Buffer[i - 1];



       // Calculate EMA 3

       ExtEMA3Buffer[i] = (k3 * ha_close) + (1 - k3) * ExtEMA3Buffer[i - 1];

             

      //--- Set candle color based on Heiken Ashi open and close

      if(ha_open < ha_close)

         ExtColorBuffer[i]=0.0; // set color Lime

      else

         ExtColorBuffer[i]=1.0; // set color Red 

      }            

   }

//---

   IndicatorSetDouble(INDICATOR_LEVELVALUE, 0, price[rates_total-1]);

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

   return(rates_total);

}
 

Don't re-event the wheel (why): https://www.mql5.com/en/search#!keyword=Renko&module=mql5_module_codebase.

Learn to search before trying to code!

 
Carl Schreiber #:

Don't re-event the wheel (why): https://www.mql5.com/en/search#!keyword=Renko&module=mql5_module_codebase.

Learn to search before trying to code!

I did that already. i am trying to develop a strategy based on MA