iMAOnArray

 

Hello,

I'd like to add a moving average to ATR (avergae true range) chart. It looks like this:

- ATR is blue line

- MA is red line

MA of ATR


The problem is I don't know to to read the data in MQL.

Here is what I did.

#property copyright"Copyright 2019, MetaQuotes Software Corp."
#property link      "https://www.mql4.com"
#property version   "1.00"//#property strict
#property indicator_chart_window
staticdatetime lastTradeBarTime;

double atr[];
double ma[];

//+------------------------------------------------------------------+//| Custom indicator initialization function                         |//+------------------------------------------------------------------+intOnInit()
{
   
   lastTradeBarTime = Time[1];
   
   return(INIT_SUCCEEDED);
   
}
//+------------------------------------------------------------------+//| Custom indicator iteration function                              |//+------------------------------------------------------------------+intOnCalculate(constint rates_total,
                constint prev_calculated,
                constdatetime &time[],
                constdouble &open[],
                constdouble &high[],
                constdouble &low[],
                constdouble &close[],
                constlong &tick_volume[],
                constlong &volume[],
                constint &spread[])
{
   
   int lookback = 1000;

   for(int i = Bars - 1 - MathMax(lookback, prev_calculated); i >= 0; --i){
      atr[i] = iATR(NULL, 0, 14, i);
      ma[i]  = iMAOnArray(atr, 0, 21, 0, MODE_SMMA, i);
   }
   
   for(i = Bars - 1 - MathMax(lookback, prev_calculated); i >= 0; --i){
  
        Print("[", TimeMonth(Time[i]), "-", TimeDay(Time[i]), "][", TimeHour(Time[i]),":", TimeMinute(Time[i]), "][", i, "] ",
                                    "atr: ",  DoubleToStr(atr[i], _Digits),
                                    ", ma: ",  DoubleToStr(ma[i], _Digits)
        );      
      
   }
   
   return(rates_total);

}

Surely I get zeros in output to console:

EURUSD,M5: [6-7][17:55][15] atr: 0.00000, ma: 0.00000


I have never done it before. Can someone please help me to understand it?

 
llaabbss:

I'd like to add a moving average to ATR (avergae true range) chart. It looks like this:

- ATR is blue line

- MA is red line

The problem is I don't know to to read the data in MQL.

Here is what I did.

Surely I get zeros in output to console:

I have never done it before. Can someone please help me to understand it?

Your codes seemed to have some lines and spaces missing (wondered how you tested...). I added them and you can now see lines drawn and values computed.

#property copyright"Copyright 2019, MetaQuotes Software Corp."
#property link      "https://www.mql4.com"
#property version   "1.00"//#property strict
#property indicator_separate_window
#property indicator_buffers 2
#property indicator_plots   2

#property indicator_type1   DRAW_LINE
#property indicator_color1  clrDodgerBlue
#property indicator_width1  1

#property indicator_type2   DRAW_LINE
#property indicator_color2  clrOrange
#property indicator_width2  1

static datetime lastTradeBarTime;

double atr[];
double ma[];

//+------------------------------------------------------------------+//| Custom indicator initialization function                         |//+------------------------------------------------------------------+intOnInit()
int OnInit()
{
   SetIndexBuffer(0,atr);
   SetIndexBuffer(1,ma);
   
   lastTradeBarTime = Time[1];
   
   return(INIT_SUCCEEDED);
   
}
//+------------------------------------------------------------------+//| Custom indicator iteration function                              |//+------------------------------------------------------------------+intOnCalculate(constint rates_total,
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 lookback = 1000;

   for(int i = Bars - 1 - MathMax(lookback, prev_calculated); i >= 0; --i){
      atr[i] = iATR(NULL, 0, 14, i);
      ma[i]  = iMAOnArray(atr, 0, 21, 0, MODE_SMMA, i);
   }
   
   for(i = Bars - 1 - MathMax(lookback, prev_calculated); i >= 0; --i){
  
        Print("[", TimeMonth(Time[i]), "-", TimeDay(Time[i]), "][", TimeHour(Time[i]),":", TimeMinute(Time[i]), "][", i, "] ",
                                    "atr: ",  DoubleToStr(atr[i], _Digits),
                                    ", ma: ",  DoubleToStr(ma[i], _Digits)
        );      
      
   }
   
   return(rates_total);

}
 
Seng Joo Thio:

Your codes seemed to have some lines and spaces missing (wondered how you tested...). I added them and you can now see lines drawn and values computed.

Dear friend,

Thank you! It really helped.

Sorry for wrong formatting, OnInit() and OnCalculate() actually were indicated, but formatting was incorrect.

I didn't plan to have this indicator plotted, that is why there were no indicators in that code.

But what really helped was SetIndexBuffer() for each of indicators.

Thanks a lot!