values of the indicators in the previous candles

 

How can I access the values of the indicators in the previous candles in MQL5??

 
aparhizkar:

How can I access the values of the indicators in the previous candles in MQL5??

  • Initialize indicator handle (into OnInit function)
  • CopyBuffer from indicator handle (into OnTick/OnCalculate)
 
aparhizkar:

How can I access the values of the indicators in the previous candles in MQL5??

//+------------------------------------------------------------------+
//|                                                          EMA.mq5 |
//|                                                   Copyright 2022 |
//|                  https://www.mql5.com/en/users/rajeshnait/seller |
//+------------------------------------------------------------------+
#property copyright "Copyright 2022"
#property link      "https://www.mql5.com/en/users/rajeshnait/seller"
#property version   "1.00"
#property indicator_chart_window
#property indicator_plots 0

input group             "MA settings"
input ENUM_TIMEFRAMES      Inp_MA_period        = PERIOD_CURRENT; // MA: timeframe
input int                  Inp_MA_ma_period     = 21;             // MA: averaging period
input int                  Inp_MA_ma_shift      = 0;              // MA: horizontal shift
input ENUM_MA_METHOD       Inp_MA_ma_method     = MODE_EMA;      // MA: smoothing type
input ENUM_APPLIED_PRICE   Inp_MA_applied_price = PRICE_CLOSE; // MA: type of price

double EMA[];
int MA;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit() {
//--- indicator buffers mapping

   ArrayResize(EMA,2);
   ArraySetAsSeries(EMA,true);
   MA = iMA(_Symbol, Inp_MA_period, Inp_MA_ma_period, Inp_MA_ma_shift, Inp_MA_ma_method, Inp_MA_applied_price);
//---
   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[]) {
//---

   CopyBuffer(MA,0,0,2,EMA);
   Print(EMA[1]); // 1 means previous candle

//--- return value of prev_calculated for next call
   return(rates_total);
}

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