Confusion about the TimeFrame Parameter in Predefined Indicator Functions

 
Hi everyone!

I trying to code a type of "Envelope" that would use ATR to adjust its "deviation" from the Base SMA.
All Arrays present in the code have been Set as Index Buffers and are declared as dynamic.
Here my attemp at it:
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 BarLimit = rates_total - prev_calculated;
   if(!prev_calculated) BarLimit -= 2;
   for(int i = 0 ; i <= BarLimit ; i++){

      // - Calculate SMA & ATR Values
      SMA[i] = iMA (_Symbol,TimeFrame0,Period0,0,MODE_SMA,PRICE_MEDIAN,i);
      ATR[i] = (iATR(_Symbol,TimeFrame1,Period1,i+1)) * Multiplier1;
      
      // - Calculate COG Main Lines
      COG00[i] = NormalizeDouble(SMA[i]+(ATR[i]/2),_Digits);
      COG01[i] = NormalizeDouble(SMA[i]-(ATR[i]/2),_Digits);
   }
   return(rates_total);
}

Everything works fine if I input the Current Chart's TimeFrame in Inputs TimeFrame0 & TimeFrame1.
However, if I want the COG (envelope) to be calculated with H4 SMA (via TimeFrame0) and then display it on the H1 Chart, or any other combination of TimeFrame0 input that is not the current chart's TF, it really shifts the whole envelope and even makes it go off chart.
The ATR Value also changes at every bar on the H1 even if I input H4 Timeframe. Shouldn't it stay the same for 4 consecutive H1 bars? (especially since I am selecting the ATR value of the closed bar i+1?)

If you think I should add any info to this topic please let me know; it is my first post here and I am not used to the website.

Thanks in advance for any help!

Envelopes - Trend Indicators - MetaTrader 5 Help
Envelopes - Trend Indicators - MetaTrader 5 Help
  • www.metatrader5.com
Envelopes Technical Indicator is formed with two Moving Averages , one of which is shifted upward and another one is shifted downward. The...
 
If "TimeFrame0" and "TimeFrame1" are different, you need to use "iBarShift" to adjust the time of the bars in each time frame.
 
   for(int i = 0 ; i <= BarLimit ; i++){
      SMA[i] = iMA (_Symbol,TimeFrame0,Period0,0,MODE_SMA,PRICE_MEDIAN,i);

You are mixing apples and oranges.