Download MetaTrader 5

Custom Indicator working on CHART and from SCRIPT - BUT NOT FROM EA

To add comments, please log in or register
Sameer Shariff
431
Sameer Shariff  

Hi Guys

I have a custom indicator, which works fine when I attach it to a chart. When I attempt to get the values from the indicator, they are retrieved correctly as per the chart, when getting it from a script. If I use the same code that works in the script from an EA - I get values, but they are not the same values as the chart.

OnInit

   SetIndexBuffer(0, Buffer2);
   SetIndexBuffer(1, Buffer2);
   SetIndexDrawBegin(0, FastPeriod);
   SetIndexDrawBegin(1, SlowPeriod);

OnCalculate

   //--- make sure we have enough bars for the calculation
   if (rates_total <= SlowPeriod)
      return(0);

   //--- first calculation or number of bars was changed
   if (prev_calculated == 0)
   {
      ArrayInitialize(Buffer1, 0);
      ArrayInitialize(Buffer2, 0);
   }

   //--- calculate some method for each uncalculated bar
   for (int shift = Bars - 1 - MathMax(SlowPeriod, prev_calculated); shift >= 0; shift--)
   {
      Buffer1[shift] = SomeMethod(PERIOD_H1, FastPeriod, shift);
      Buffer2[shift] = SomeMethod(PERIOD_D1, SlowPeriod, iBarShift(instrument, PERIOD_D1, time[shift]));
   }

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

Can anyone spot what the problem is?

Like I said, shows fine on chart and can get correct value through iCustom from script attached to chart. But I don't get the same values from the EA.

PLEASE HELP!

Ziheng Zhuang
6225
Ziheng Zhuang  
   //--- return value of prev_calculated for next call
   return (rates_total);
Sameer Shariff
431
Sameer Shariff  
Ziheng Zhuang:

Thanks for the reply. Tried that. Did not resolve the problem.
Ziheng Zhuang
6225
Ziheng Zhuang  
Sameer Shariff:

Thanks for the reply. Tried that. Did not resolve the problem.

   for (int shift = Bars - 1 - MathMax(SlowPeriod, prev_calculated); shift >= 0; shift--)
   {
      Buffer1[shift] = SomeMethod(PERIOD_H1, FastPeriod, iBarShift(instrument, PERIOD_H1, time[shift]));
      Buffer2[shift] = SomeMethod(PERIOD_D1, SlowPeriod, iBarShift(instrument, PERIOD_D1, time[shift]));
   }

   //--- return value of prev_calculated for next call
   return (rates_total);
Sameer Shariff
431
Sameer Shariff  
Ziheng Zhuang:

Still not working. Shift is already in H1 that's why not converting it. This indicator can only be used on H1 chart.
whroeder1
16574
whroeder1  
Ziheng Zhuang:
   return (rates_total);
  1. If you do that, you have to test prev_calculated for zero to set your starting point, i.e. Bars-1 vs Bars - prev_calculated.
  2. See How to do your lookbacks correctly.


Sameer Shariff: Still not working. Shift is already in H1 that's why not converting it. This indicator can only be used on H1 chart.
  1. Do the iBarShift and it can be used on any chart.
  2. Your question was about the EA, why haven't you posted that code. Detailed explanation of iCustom - MQL4 forum
Sameer Shariff
431
Sameer Shariff  
whroeder1:
  1. If you do that, you have to test prev_calculated for zero to set your starting point, i.e. Bars-1 vs Bars - prev_calculated.
  2. See How to do your lookbacks correctly.


  1. Do the iBarShift and it can be used on any chart.
  2. Your question was about the EA, why haven't you posted that code. Detailed explanation of iCustom - MQL4 forum


Hi Whroeder

This is how I am accessing the indicator values from EA:

   buffer1Now = iCustom(NULL, PERIOD_H1, "CDI", 0, 0);
   buffer1Before = iCustom(NULL, PERIOD_H1, "CDI", 0, 1);
   buffer2Now = iCustom(NULL, PERIOD_H1, "CDI", 1, 0);
   buffer2Before = iCustom(NULL, PERIOD_H1, "CDI", 1, 1);

This code works as expected from a script - but not from the EA.

To add comments, please log in or register