How To Get Indicator Value From a Different Timeframe in Mql5

 

Hello ,

in MQL5 , i want to get the value of an indicator for a certain Shift from a different timeframe than the current chart , this is my code when i test it i get error 4806 and the buffer is not copied , how do i do that ?

Thanks .

#property indicator_chart_window
#property indicator_buffers 1
#property indicator_color1 Red

int IndicatorHandle =0;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   IndicatorHandle = iOsMA(_Symbol,PERIOD_M30,12,26,9,PRICE_CLOSE);
//---
   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[])
  {
//---
   double OsMaS3 = IndicatorValue(IndicatorHandle,3,0);
   
   Comment("OsMa S3 = ",OsMaS3);
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+

double IndicatorValue(int handle,int shift,int buffer_num=0)
  {
   double buffer[];
   if(CopyBuffer(handle,buffer_num,shift,1,buffer)!=1)
     {
      Print(StringFormat("Error in IndicatorValue(%d,%d,%d): cannot copy value",handle,shift,buffer_num));
      return(0.0);
     }
   return(buffer[0]);
  }
 
bahtah7:

Hello ,

in MQL5 , i want to get the value of an indicator for a certain Shift from a different timeframe than the current chart , this is my code when i test it i get error 4806 and the buffer is not copied , how do i do that ?

Thanks .

Check that you have PERIOD_M30 data... and use BarsCalculated(handle) to confirm that calculation has been completed before calling CopyBuffer().

 
Seng Joo Thio:

Check that you have PERIOD_M30 data... and use BarsCalculated(handle) to confirm that calculation has been completed before calling CopyBuffer().

i made the function look like this and i get a wrong fix value no matter what i change in shift

double IndicatorValue(int handle,int shift,int buffer_num=0)
  {
   double buffer[];
   ArrayResize(buffer,shift+1,0);
   if(BarsCalculated(handle)!=-1)
   {
   if(CopyBuffer(handle,buffer_num,shift,1,buffer)!=1)
     {
      Print(StringFormat("Error in IndicatorValue(%d,%d,%d): cannot copy value",handle,shift,buffer_num));
      return(0.0);
     }
   }
   return(buffer[0]);
  }
 
bahtah7:

i made the function look like this and i get a wrong fix value no matter what i change in shift

There is no need to call ArrayResize() as CopyBuffer() will do it.

The wrong fixed value is likely due to uninitialized array data (because CopyBuffer() failed to get data after it resized the array...) - not a problem.

I suspect it's something to do with the market being closed at the moment, because there's value when you do a debug run with history data. If that's the case, you can't do much other than wait till monday.

 
Seng Joo Thio:

There is no need to call ArrayResize() as CopyBuffer() will do it.

The wrong fixed value is likely due to uninitialized array data (because CopyBuffer() failed to get data after it resized the array...) - not a problem.

I suspect it's something to do with the market being closed at the moment, because there's value when you do a debug run with history data. If that's the case, you can't do much other than wait till monday.

Ok Thank you very much .

 
bahtah7:

Ok Thank you very much .

It just crossed my mind - u can also use EventSetTimer(1) and OnTimer() to mimic the arrival of ticks during weekend! Just put the first two lines of your OnCalculate() into OnTimer() will do, like this:

int OnInit()
{
        :
        EventSetTimer(1);
        :
}

void OnDeinit(const int reason)
{
        EventKillTimer();
}

void OnTimer()
{
   double OsMaS3 = IndicatorValue(IndicatorHandle,3,0);
   Comment ("OsMa S3 = ",OsMaS3);
}
 
Seng Joo Thio:
It just crossed my mind - u can also use EventSetTimer(1) and OnTimer() to mimic the arrival of ticks during weekend! Just put the first two lines of your OnCalculate() into OnTimer() will do, like this:
Great That worked well . except i get wrong values for the last 3 shifts i think that's because the market is closed .
 
bahtah7:
Great That worked well . except i get wrong values for the last 3 shifts i think that's because the market is closed .

That's weird, because I'm able to get the right values...

I can set my chart to any timeframe, but the output values are always consistently correct and based on PERIOD_M30...

so it's probably some other bugs in your code... LOL

 
Seng Joo Thio:

That's weird, because I'm able to get the right values...

I can set my chart to any timeframe, but the output values are always consistently correct and based on PERIOD_M30...

so it's probably some other bugs in your code... LOL

Yes this is weird . but the only thing i changed is i removed the  ' : ' sign before and after EventSetTimer(1) ,  because i got an error ':' - unexpected token . that's the only thing i changed it may be because of that ?

 
bahtah7:

Yes this is weird . but the only thing i changed is i removed the  ' : ' sign before and after EventSetTimer(1) ,  because i got an error ':' - unexpected token . that's the only thing i changed it may be because of that ?

No, those : just means "something before" and "something after", because that line is to be inserted to your existing codes...

Ok, here's my complete test code - see if you get the right values:

#property indicator_chart_window
#property indicator_buffers 1
#property indicator_color1 Red

int IndicatorHandle =0;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
   IndicatorHandle = iOsMA(_Symbol,PERIOD_M30,12,26,9,PRICE_CLOSE);
   EventSetTimer(1);
//---
   return(INIT_SUCCEEDED);
  }
  
void OnDeinit(const int reason)
{
   EventKillTimer();
}

void OnTimer()
{
   double OsMaS3 = IndicatorValue(IndicatorHandle,1,0);
   Comment ("OsMa S3 = ",OsMaS3);
}

//+------------------------------------------------------------------+
//| 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[])
  {
//---
   double OsMaS3 = IndicatorValue(IndicatorHandle,1,0);
   Comment ("OsMa S3 = ",OsMaS3);
   
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+

double IndicatorValue(int handle,int shift,int buffer_num=0)
  {
   int availableBars = BarsCalculated(handle);
   if (availableBars<0)
      return (DBL_MAX);

   int numToCopy = 1;
   double buffer[];
   if(CopyBuffer(handle,buffer_num,shift,numToCopy,buffer)<MathMin(numToCopy,availableBars))
   {
      Print(StringFormat("Error in IndicatorValue(%d,%d,%d): cannot copy value",handle,shift,buffer_num));
      return(DBL_MAX);
   }

   return(buffer[0]);
  }
 
Seng Joo Thio:

No, those : just means "something before" and "something after", because that line is to be inserted to your existing codes...

Ok, here's my complete test code - see if you get the right values:


i tested your code on M30 and the value of shift 1 in the comment is 6.4 but shift 1 in Data Windows is 0.0000642 , i changed the code to get shift 4 and i get it right it still the last shifts 1, 2, and 3 that get wrong values .

Reason: