Calculating Sum of Sma from different TF inside indicator | MT5

 

Hello Everyone,

By ready the instructions of this tutorial  I tried to sum SMA's of different Time Frames inside to one new indicator and I take current value but the line is straight (not assembled to the previous bars).

The code to set up is this

//+------------------------------------------------------------------+
//|                                                       SumSma.mq5 |
//|                        Copyright 2019, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2019, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property indicator_chart_window
#property indicator_buffers 7
#property indicator_plots   1
//--- plot SumSma
#property indicator_label1  "SumSma"
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrRed
#property indicator_style1  STYLE_SOLID
#property indicator_width1  2


input int MAPeriod = 9;
input int MAShift = 0; 

//--- indicator buffers
double         SumSmaBuffer[];
double         MA_Array_1m[],MA_Array_5m[],MA_Array_10m[],MA_Array_15m[],MA_Array_30m[],MA_Array_1h[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,SumSmaBuffer,INDICATOR_DATA);
   PlotIndexSetInteger(0, PLOT_SHIFT, MAShift);
   PlotIndexSetInteger(0, PLOT_DRAW_BEGIN, 1);   
//---
   return(INIT_SUCCEEDED);
  }

and the code to calculate is this:

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 bar;
   double Sum, SMA;
   
   for(bar = 0; bar < rates_total; bar++)
    {
         int MA_Handle_1m = iMA(_Symbol,PERIOD_M1,MAPeriod,0,MODE_SMA,PRICE_CLOSE);
         int MA_Handle_5m = iMA(_Symbol,PERIOD_M5,MAPeriod,0,MODE_SMA,PRICE_CLOSE);
         int MA_Handle_10m = iMA(_Symbol,PERIOD_M10,MAPeriod,0,MODE_SMA,PRICE_CLOSE);
         int MA_Handle_15m = iMA(_Symbol,PERIOD_M15,MAPeriod,0,MODE_SMA,PRICE_CLOSE);
         int MA_Handle_30m = iMA(_Symbol,PERIOD_M30,MAPeriod,0,MODE_SMA,PRICE_CLOSE);
         int MA_Handle_1h = iMA(_Symbol,PERIOD_H1,MAPeriod,0,MODE_SMA,PRICE_CLOSE);
         
         ArraySetAsSeries( MA_Array_1m,true );
         ArraySetAsSeries( MA_Array_5m,true );
         ArraySetAsSeries( MA_Array_10m,true );
         ArraySetAsSeries( MA_Array_15m,true );
         ArraySetAsSeries( MA_Array_30m,true );
         ArraySetAsSeries( MA_Array_1h,true );
         
         CopyBuffer( MA_Handle_1m,0,0,1,MA_Array_1m );
         CopyBuffer( MA_Handle_5m,0,0,1,MA_Array_5m );
         CopyBuffer( MA_Handle_10m,0,0,1,MA_Array_10m );
         CopyBuffer( MA_Handle_15m,0,0,1,MA_Array_15m );
         CopyBuffer( MA_Handle_30m,0,0,1,MA_Array_30m );
         CopyBuffer( MA_Handle_1h,0,0,1,MA_Array_1h );
         
         double ma1 = NormalizeDouble(MA_Array_1m[0],4);
         double ma2 = NormalizeDouble(MA_Array_5m[0],4);
         double ma3 = NormalizeDouble(MA_Array_10m[0],4);
         double ma4 = NormalizeDouble(MA_Array_15m[0],4);
         double ma5 = NormalizeDouble(MA_Array_30m[0],4);
         double ma6 = NormalizeDouble(MA_Array_1h[0],4);
    
         Sum = ((ma1 + ma2 + ma3 + ma4 + ma5 + ma6)/6);
         SMA = NormalizeDouble(Sum,4);
         Comment(SMA);      
         SumSmaBuffer[bar] = SMA;
    }
   
   return(rates_total);
  }
//+------------------------------------------------------------------+



What i am doing wrong ?

 

You can find it out yourself quite easily!

Just go with the debugger (F5) through your code and let the debugger show the various values even the time of time[bar] and at the very top _LastError!

 
Carl Schreiber:

You can find it out yourself quite easily!

Just go with the debugger (F5) through your code and let the debugger show the various values even the time of time[bar] and at the very top _LastError!

Thanks @Carl Schreiber for your answer...
I cannot see any error through debugging ...

 
MamRa:

Thanks @Carl Schreiber for your answer...
I cannot see any error through debugging ...

The idea of using the debugger, is to step through your code, line by line, and see how the variables change. For example, when you step over this line : "         CopyBuffer( MA_Handle_1m,0,0,1,MA_Array_1m );", you can add "MA_Array_10m[0]" to watch and see how or if the value changes.

But first, I suggest some code shifting, split this segment:

         int MA_Handle_1m = iMA(_Symbol,PERIOD_M1,MAPeriod,0,MODE_SMA,PRICE_CLOSE);
         int MA_Handle_5m = iMA(_Symbol,PERIOD_M5,MAPeriod,0,MODE_SMA,PRICE_CLOSE);
         int MA_Handle_10m = iMA(_Symbol,PERIOD_M10,MAPeriod,0,MODE_SMA,PRICE_CLOSE);
         int MA_Handle_15m = iMA(_Symbol,PERIOD_M15,MAPeriod,0,MODE_SMA,PRICE_CLOSE);
         int MA_Handle_30m = iMA(_Symbol,PERIOD_M30,MAPeriod,0,MODE_SMA,PRICE_CLOSE);
         int MA_Handle_1h = iMA(_Symbol,PERIOD_H1,MAPeriod,0,MODE_SMA,PRICE_CLOSE);

into two parts, with first part (as follows) in the global declaration part:

int MA_Handle_1m,MA_Handle_5m,MA_Handle_10m,MA_Handle_15m,MA_Handle_30m,MA_Handle_1h;

and these into the OnInit() function:

         MA_Handle_1m = iMA(_Symbol,PERIOD_M1,MAPeriod,0,MODE_SMA,PRICE_CLOSE);
         MA_Handle_5m = iMA(_Symbol,PERIOD_M5,MAPeriod,0,MODE_SMA,PRICE_CLOSE);
         MA_Handle_10m = iMA(_Symbol,PERIOD_M10,MAPeriod,0,MODE_SMA,PRICE_CLOSE);
         MA_Handle_15m = iMA(_Symbol,PERIOD_M15,MAPeriod,0,MODE_SMA,PRICE_CLOSE);
         MA_Handle_30m = iMA(_Symbol,PERIOD_M30,MAPeriod,0,MODE_SMA,PRICE_CLOSE);
         MA_Handle_1h = iMA(_Symbol,PERIOD_H1,MAPeriod,0,MODE_SMA,PRICE_CLOSE);

because you need to obtain the handles of the indicators just once, which will also initiate computation. Then in OnCalculate, you just use CopyBuffer to obtain the computed values of specific bars.

Then fire up your debugger and check how the variables change over time (*hint* your 'bar' variable doesn't seem to be used fruitfully, and don't forget that the same 'bar' cannot be used across different timeframe, except bar 0 which refers to the latest non-complete bar, if that is what you want).


https://www.mql5.com/en/articles/654

Debugging MQL5 Programs
Debugging MQL5 Programs
  • www.mql5.com
This article is intended primarily for the programmers who have already learned the language but have not fully mastered the program development yet. It highlights the key issues that every developer deals with when debugging a program. So, what is debugging? Debugging is a stage in program development meant for detecting and removing program...
 
MamRa:

Hello Everyone,

By ready the instructions of this tutorial  I tried to sum SMA's of different Time Frames inside to one new indicator and I take current value but the line is straight (not assembled to the previous bars).

The code to set up is this

and the code to calculate is this:



What i am doing wrong ?

For sure, the line is straight, you are always copying the same values :

         CopyBuffer( MA_Handle_1m,0,0,1,MA_Array_1m );
         CopyBuffer( MA_Handle_5m,0,0,1,MA_Array_5m );
         CopyBuffer( MA_Handle_10m,0,0,1,MA_Array_10m );
         CopyBuffer( MA_Handle_15m,0,0,1,MA_Array_15m );
         CopyBuffer( MA_Handle_30m,0,0,1,MA_Array_30m );
         CopyBuffer( MA_Handle_1h,0,0,1,MA_Array_1h );

Anyway, you will need to synchronize your values between timeframes.

Reason: