Weird custom indicator behaviour?

 

Hi!

I am struggling to make a simple indicator, probably by the lack of coding experience. My excuses for my silly mistakes!

I am trying to get a indicator, similar to the AO, to show a bar of a different timeframe. So for example: I have the chart on a daily timeframe. But I want the indicator to show the 8th bar of the hourly timeframe on the daily timeframe. 

I've tried the following code: 

//--- indicator parameters
int InpFastSMA=5;    // Fast SMA Period
int InpSlowSMA=34;   // Slow SMA Period

//--- indicator buffers
double    ExtAoBuffer[];
//--- right input parameters flag
bool      ExtParameters=true;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit(void)
{
   IndicatorDigits(Digits+1);
//--- drawing settings
   SetIndexStyle(0,DRAW_HISTOGRAM);
   SetIndexDrawBegin(0,InpFastSMA);
//--- indicator buffers mapping
   SetIndexBuffer(0,ExtAoBuffer);
//--- name for DataWindow and indicator subwindow label
   IndicatorShortName("Awsome indicator test");
   SetIndexLabel(0,"Awsome Indicator test");

   ExtParameters=true;
//--- initialization done
   return(INIT_SUCCEEDED);
}

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 i,limit;
//---
   if(rates_total<=0 || !ExtParameters)
      return(0);
//--- last counted bar will be recounted
   limit=rates_total-prev_calculated;
   if(prev_calculated>0)
      limit++;
//--- macd counted in the 1-st buffer
   for(i=0; i<limit; i++)
      ExtAoBuffer[i]=iMA(NULL,60,InpFastSMA,0,MODE_SMA,PRICE_CLOSE,i*24-8)-iMA(NULL,60,InpSlowSMA,0,MODE_SMA,PRICE_CLOSE,i*24-8);
//--- done
   return(rates_total);
  }
//+---------------------

It kinda works, or atleast shows the right thing sometimes... See the pictures below:

It works at times, but it wont work in a backtest for example. Or whenever you zoom out too much...

What the indicator should do:

Show x bar of a lower timeframe on a higher timeframe. So for example the 8th bar of the hourly timeframe within the 24 bars that are in one daily bar. This means that there is a quite choppy indicator that on the daily timeframe. 

Thanks in advance!
Daan

Documentation on MQL5: Constants, Enumerations and Structures / Chart Constants / Chart Timeframes
Documentation on MQL5: Constants, Enumerations and Structures / Chart Constants / Chart Timeframes
  • www.mql5.com
Chart Timeframes - Chart Constants - Constants, Enumerations and Structures - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 

Please don't post randomly in any section. Your question is not related to the section you posted.

MT4/mql4 has it's own section on the forum.

Your topic has been moved to correct section, please don't create another duplicate topic.

 
Fernando Carreiro #:

Please don't post randomly in any section. Your question is not related to the section you posted.

MT4/mql4 has it's own section on the forum.

Your topic has been moved to correct section, please don't create another duplicate topic.

My Excuses! 

 
DaanKouwen_SF #:
      ExtAoBuffer[i]=iMA(NULL,60,InpFastSMA,0,MODE_SMA,PRICE_CLOSE,i*24-8)-iMA(NULL,60,InpSlowSMA,0,MODE_SMA,PRICE_CLOSE,i*24-8);
  1. Do not hard code numbers. Use the proper enumerations.

  2. i*24-8 A) assumes your chart's period (D1) vs PERIOD_H1. Don't assume. B) Assumes that all bars exist. They don't. C) trying to read eight (8) hours into the future. Use iBarShift to get the shift of other timeframes.

  3. On MT4: Unless the current chart is that specific symbol(s)/TF(s) referenced, you must handle 4066/4073 errors before accessing candle/indicator values.
              Download history in MQL4 EA - MQL4 programming forum - Page 3 #26.4 (2019)

 
William Roeder #:
  1. Do not hard code numbers. Use the proper enumerations.

  2. i*24-8 A) assumes your chart's period (D1) vs PERIOD_H1. Don't assume. B) Assumes that all bars exist. They don't. C) trying to read eight (8) hours into the future. Use iBarShift to get the shift of other timeframes.

  3. On MT4: Unless the current chart is that specific symbol(s)/TF(s) referenced, you must handle 4066/4073 errors before accessing candle/indicator values.
              Download history in MQL4 EA - MQL4 programming forum - Page 3 #26.4 (2019)

Thank you William for your reply!


I have the following code, which doesn't seem to work. I have made a couple of mistakes I suppose... 

   for(i=0; i<limit; i++)
   {
      // Thank you mr William for your help so far!
      int shift = iBarShift(_Symbol,PERIOD_M5,iTime(_Symbol,PERIOD_M30,i),false);
      
      Comment("Shift: " + shift); // Shift seems to give the right shift value
      
      // Fill the buffer with the right value
      ExtAoBuffer[i]=iMA(NULL,PERIOD_M5,InpFastSMA,0,MODE_SMA,PRICE_CLOSE,i-shift)-
                     iMA(NULL,PERIOD_M5,InpSlowSMA,0,MODE_SMA,PRICE_CLOSE,i-shift);
   }

I seem to get the right shift, not completely sure, but it seems like it. I get the right value of the AO on (for example) the 30 minute timeframe, from the last bar of the 5 minute timeframe. But this only works on the last bar, see attached pictures to see the results. 


I've not tried to implement the error handling yet, because I am still struggling with the basics. I will try to implement this when the basic indicator is working. 

I hope you can help me with this problem. I've always been struggling with indicators because of problems (lack of knowledge) like this. 


Thanks in advance!

Daan

Files:
1.png  29 kb
2.png  33 kb
 

Is there anyone who might be able to help me further? 

I am only trading EURUSD, so does that mean I won't have to handle these  4066/4073  errors? 

Thanks in advance!

Runtime Errors - Codes of Errors and Warnings - Constants, Enumerations and Structures - MQL4 Reference
Runtime Errors - Codes of Errors and Warnings - Constants, Enumerations and Structures - MQL4 Reference
  • docs.mql4.com
Runtime Errors - Codes of Errors and Warnings - Constants, Enumerations and Structures - MQL4 Reference
Reason: