Get price in array set like with this RSI example

 

Hello, I have been trying to get the price in an array like I have managed to do with the RSI. I can only seem to get the current price, but I would like to call it with price[i]. Is this possible?


//+------------------------------------------------------------------------------+
//|                                                      GetIndicatorBuffers.mqh |
//|                                                       Copyright 2010, DC2008 |
//|                                       https://login.mql5.com/ru/users/DC2008 |
//+------------------------------------------------------------------------------+
#property copyright "Copyright 2010, DC2008"
#property link      "https://login.mql5.com/ru/users/DC2008"
//+------------------------------------------------------------------------------+
//| copying indicator values to an array based on the index order                |
//+------------------------------------------------------------------------------+
bool CopyBufferAsSeries(
   int handle,      // indicator handle
   int bufer,       // number of the indicator buffer
   int start,       // from where to start
   int number,      // how many to copy
   bool asSeries,   // array index order
   double &M[]      // array to which data will be copied
)
  {
//--- filling the M array with current indicator values
   if(CopyBuffer(handle,bufer,start,number,M)<=0)
      return(false);
//--- set the M array index order
//--- if asSeries=true, the M array index order is as time series
//--- if asSeries=false, the M array has default index order
   ArraySetAsSeries(M,asSeries);
//---
   return(true);
  }




//---- arrays for indicators
double      RSI[];                // array for the indicator iRSI
//---- handles for indicators
int         RSI_handle;           // handle of the indicator iRSI




//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {


//--- creation of the indicator iRSI
   RSI_handle=iRSI(NULL,0,21,PRICE_CLOSE);

//--- report if there was an error in object creation
   if(RSI_handle<0)
     {
      Print("The creation of iRSI has failed: Runtime error =",GetLastError());
      //--- forced program termination
      return(-1);
     }
   return(0);
  }

//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+



//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnTick()
  {

   MqlRates rates[20];
   if(CopyRates(Symbol(),PERIOD_M1,0,1,rates)!=1) { /*error processing */ };

   Alert(rates[0].close);



//Get price data
   double current_bid = SymbolInfoDouble(_Symbol, SYMBOL_BID);
   double current_ask = SymbolInfoDouble(_Symbol, SYMBOL_ASK);




//Get RSI Data
   if(!CopyBufferAsSeries(RSI_handle,0,0,100,true,RSI))
      return;
   Alert(RSI[0]);

   Alert(current_ask);


  }
//+------------------------------------------------------------------+
Sergey Pavlov
  • www.mql5.com
Опубликовал статью Создание многомодульных советников Язык программирования MQL позволяет реализовать концепцию модульного проектирования торговых стратегий. В статье показан пример создания многомодульного советника, состоящего из отдельно скомпилированных файловых модулей. Выставил продукт Индикатор использует в качестве исходных...
 

Use CopyClose or CopyRates. Even better you can use the stardard library's higher lever abstractions to avoid creating bugs and drastically reduce development time. 

#include <indicators/indicators.mqh>

CIndicators  g_indicators;
CiRSI       *g_rsi;
CiClose     *g_close;

int OnInit() {
    g_close = new CiClose();
    g_rsi = new CiRSI();
    bool setup = (
        g_close.Create(_Symbol, PERIOD_CURRENT)
        && g_rsi.Create(_Symbol, PERIOD_CURRENT, 21, PRICE_CLOSE)
        && g_indicators.Add(g_close)
        && g_indicators.Add(g_rsi)
    );
    if (!setup)
        return INIT_FAILED;
    return INIT_SUCCEEDED;
}

void OnTick() {
    g_indicators.Refresh();
    
    if (g_close.GetData(0) > g_close.GetData(1) && g_rsi.Main(0) > g_rsi.Main(1)) {
        Comment("Direction up");
    }
    else if (g_close.GetData(0) < g_close.GetData(1) && g_rsi.Main(0) < g_rsi.Main(1)) {
        Comment("Direction down");
    }
    else {
        Comment("Direction unknown");
    }
}
 
nicholi shen:

Use CopyClose or CopyRates. Even better you can use the stardard library's higher lever abstractions to avoid creating bugs and drastically reduce development time. 

This is exactly what I was looking for, thank you!!!
Reason: