Need help: Moving Average calculation on indicator buffer's data

 

hi

i want to add EMA13 AND SMA50 on indicator buffer's data for renko.mq5 , https://www.mql5.com/en/code/1299

i use this :  incmaonarray.mqh , test_maonarray.mq5 ( https://www.mql5.com/en/code/623 )

and i modify the renko as attached " renko_2.mq5 " , as below :

high lighted line add or changed.

//+------------------------------------------------------------------+
//|                                                        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_separate_window
#property indicator_buffers 9
#property indicator_plots   3
//--- plot Renko
#property indicator_label1  "Renko Open;Renko High;Renko Low;Renko Close"
#property indicator_type1   DRAW_COLOR_CANDLES
#property indicator_color1  clrRed,clrBlue
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1
//--- input parameters
input int      InpBoxSize = 30; // Box size (in pips)
//--- indicator buffers
double         BoxOpenBuffer[];
double         BoxHighBuffer[];
double         BoxLowBuffer[];
double         BoxCloseBuffer[];
double         BoxColors[];
//---
double         boxSize;    
//----------------------------------------------------------------------------------------------------------//
//----------------------------------------------------------------------------------------------------------//
//------------------------------------------------------------------------------------------ Test_MAOnArray.mq5
//---  INDICATOR_data_store

//+----------------------------------------+
//--- plot Label2
#property indicator_label2  "Label2"
#property indicator_type2   DRAW_LINE
#property indicator_color2  clrYellow
#property indicator_style2  STYLE_SOLID
#property indicator_width2  1
//--- input parameters
input int            MAPeriod_1    =  13;
input ENUM_MA_METHOD MAMethod_1    =  MODE_EMA;
//--- indicator buffers
double MABuffer_1[];
double data_1[];
#include <IncMAOnArray.mqh>
CMAOnArray ma;
//+----------------------------------------+
//--- plot Label3
#property indicator_label3  "Label3"
#property indicator_type3   DRAW_LINE
#property indicator_color3  clrRed
#property indicator_style3  STYLE_SOLID
#property indicator_width3  1
//--- input parameters
input int            MAPeriod_2    =  50;
input ENUM_MA_METHOD MAMethod_2    =  MODE_SMA;
//--- indicator buffers
double MABuffer_2[];
double data_2[];
//#include <IncMAOnArray.mqh>
CMAOnArray ma2;



//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit() {
//---
   if ( InpBoxSize < 30 ) {
      boxSize = 30 *_Point;
      Print("Incorrect parameter InpBoxSize = ", InpBoxSize, ". Value equal to 30 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);
//---
   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_2 ("+(string)boxSize+")");   
//---
//----------------------------------------------------------------------------------------------------------//
//------------------------------------------------------------------------------------------ Test_MAOnArray.mq5
ma.Init(MAPeriod_1,MAMethod_1);
//--- indicator buffers mapping
   SetIndexBuffer(5,MABuffer_1,INDICATOR_DATA);
   SetIndexBuffer(7,data_1,INDICATOR_CALCULATIONS);
   PlotIndexSetInteger(1,PLOT_DRAW_BEGIN,ma.BarsRequired());
   PlotIndexSetString(1,PLOT_LABEL,ma.Name());
//+----------------------------------------+
ma2.Init(MAPeriod_2,MAMethod_2);
//--- indicator buffers mapping
   SetIndexBuffer(6,MABuffer_2,INDICATOR_DATA);
   SetIndexBuffer(8,data_2,INDICATOR_CALCULATIONS);
   PlotIndexSetInteger(2,PLOT_DRAW_BEGIN,ma2.BarsRequired());
   PlotIndexSetString(2,PLOT_LABEL,ma2.Name());
   
//---

   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);
      }
      //---
      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;
         }
      }
   }
   
//---
   IndicatorSetDouble(INDICATOR_LEVELVALUE, 0, price[rates_total-1]);
//--- return value of prev_calculated for next call
//----------------------------------------------------------------------------------------------------------//
   int start_1;
   if(prev_calculated>0)
     {
      start_1=prev_calculated-1;
     }
   else
     {
      start_1=0;
     }
   for(int i=start_1;i<rates_total;i++)
     {
      data_1[i]=BoxCloseBuffer[i];
     }
   ma.Solve(rates_total,prev_calculated,data_1,MABuffer_1);
//+----------------------------------------+
   int start_2;
   if(prev_calculated>0)
     {
      start_2=prev_calculated-1;
     }
   else
     {
      start_2=0;
     }
   for(int i=start_2;i<rates_total;i++)
     {
      data_2[i]=BoxCloseBuffer[i];
     }
   ma2.Solve(rates_total,prev_calculated,data_2,MABuffer_2);




//----------------------------------------------------------------------------------------------------------//

  
  Comment(""
          ,"\n BoxHighBuffer[0]=",NormalizeDouble(BoxHighBuffer[rates_total-1],7)
          ,"\n BoxHighBuffer[1]=",NormalizeDouble(BoxHighBuffer[rates_total-2],7)
          ,"\n "
          ,"\n BoxOpenBuffer[0]=",NormalizeDouble(BoxOpenBuffer[rates_total-1],7)
          ,"\n BoxOpenBuffer[1]=",NormalizeDouble(BoxOpenBuffer[rates_total-2],7)
          ,"\n "
          ,"\n BoxLowBuffer[0]=",NormalizeDouble(BoxLowBuffer[rates_total-1],7)
          ,"\n BoxLowBuffer[1]=",NormalizeDouble(BoxLowBuffer[rates_total-2],7)
          ,"\n "
          ,"\n BoxCloseBuffer[0]=",NormalizeDouble(BoxCloseBuffer[rates_total-1],7)
          ,"\n BoxCloseBuffer[1]=",NormalizeDouble(BoxCloseBuffer[rates_total-2],7)
          ,"\n "
          ,"\n MABuffer_1[0]=",NormalizeDouble(MABuffer_1[rates_total-1],7)
          ,"\n MABuffer_1[1]=",NormalizeDouble(MABuffer_1[rates_total-2],7)
          
          
          );
  
  
   return(rates_total);
}
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//----------------------------------------------------------------------------------------------------------//
//********************************************************************************************************************



and there is 2 problem i have:

1) there is some different in value between it above code " renko_2 "   and when i use renko with my setting (3 pip) " renko_1 " and attach to subwindow 2 " Moving Average " Applay to First indicator's data as below :


&


if you use them will see the diff. in value.

-----

2) in both case after some time they have problem if dont refresh the chart or dont change time fram.

can you please help,

Renko
Renko
  • votes: 25
  • 2012.12.27
  • Serhii Ivanenko
  • www.mql5.com
The renko (renko) chart in a separate subwindow. No matter on what chart timeframe the indicator is attached, the renko is drawn on close price of М1 timeframe.
Files:
renko_2.mq5  12 kb
renko_1.mq5  8 kb
 

May be 'Applay to First indicator's data' applays ma not to the close buffer.

Try to change this part:

for(int i=start_1;i<rates_total;i++)
     {
      data_1[i]=BoxCloseBuffer[i]; 
}

Try  BoxOpenBuffer, BoxHighBuffer, BoxLowBuffer, BoxCloseBuffer.

So you will know to what buffer applays ma when you drag it into subwindow.

 
Integer:

May be 'Applay to First indicator's data' applays ma not to the close buffer.

Try to change this part:

Try  BoxOpenBuffer, BoxHighBuffer, BoxLowBuffer, BoxCloseBuffer.

So you will know to what buffer applays ma when you drag it into subwindow.

thanks to reply,

using " BoxOpenBuffer " get the same value, but must refresh or change chart, and after some time they have diff.

?!

and still have 2 problem .

 
TIMisthebest:

thanks to reply,

using " BoxOpenBuffer " get the same value, but must refresh or change chart, and after some time they have diff.

?!

and still have 2 problem .

 

 

For ma you use proper cicle (recalculating only one new bar). But for renko on each tick recalculated several bars. 

So we need to explore the renko cicle and recalculate ma in the same bars.

Solve start_1 и start_2  in another way and calling ma1.Solve and ma2.Solve set prev_calculated=0. But it is not good, it is impossible to limit ma solve range (ma will be solved for all bars every tick)

 

Yet it is necessary to clean the arrays for ma when the indicator is reset.

 if(prev_calculated==0){

ArrayInitialize(MABuffer_1,0);

ArrayInitialize(MABuffer_2,0);

}

-
 
Integer:

For ma you use proper cicle (recalculating only one new bar). But for renko on each tick recalculated several bars. 

So we need to explore the renko cicle and recalculate ma in the same bars.

Solve start_1 и start_2  in another way and calling ma1.Solve and ma2.Solve set prev_calculated=0. But it is not good, it is impossible to limit ma solve range (ma will be solved for all bars every tick)


Yet it is necessary to clean the arrays for ma when the indicator is reset.

THANK YOU,

i will try another method, ...

 

Hi


I would also like to add EMA in indicator buffer data to renko.mq5 ...

I'm also trying to use "BoxOpenBuffer" but just fucniona correctly the moment I compile. After some time, when the chart updates, I'm having problems ...

I believe the problem is that the MA is recalculated every bar for 1 minute, but  the Renko is recalculated on several bars.

Already have a solution?


Thanks

Need help: Moving Average calculation on indicator buffer's data
Need help: Moving Average calculation on indicator buffer's data
  • www.mql5.com
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.
Reason: