Having trouble making a MTF Moving Average. Tried a few things, but still struggling.

 
Some one please help me out. Much thanks appreciated!
#property indicator_chart_window
#property indicator_buffers 7
#property indicator_plots   1
//--- plot Label1
#property indicator_label1  "Label1"
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrRed
#property indicator_style1  STYLE_SOLID
#property indicator_width1  3

input int                MA_Period=14;         
input int                MA_Shift=0;  
input ENUM_TIMEFRAMES   TimeFrame=0;         
input ENUM_MA_METHOD     MA_Method=MODE_EMA;
input ENUM_APPLIED_PRICE MA_Price=PRICE_CLOSE; 
int            MA_handle;
int            TFMA_handle;

//--- indicator buffers
double         Label1Buffer[];
double         MACalcBuffer[];
double         buffer_open[],buffer_high[],buffer_low[],buffer_close[];
double         TFMA_Buffer[];

//-----------------------------------------------------------------------------
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,Label1Buffer,INDICATOR_DATA);
   SetIndexBuffer(1,MACalcBuffer,INDICATOR_CALCULATIONS);
   
   SetIndexBuffer(2,buffer_open,INDICATOR_DATA);
   SetIndexBuffer(3,buffer_high,INDICATOR_DATA);
   SetIndexBuffer(3,buffer_low,INDICATOR_DATA);
   SetIndexBuffer(4,buffer_close,INDICATOR_DATA);
   SetIndexBuffer(5,TFMA_Buffer,INDICATOR_CALCULATIONS);
   
   //--- get MA handle
   MA_handle=iMA(Symbol(),0,MA_Period,0,MODE_EMA,PRICE_CLOSE);
   
  // TFMA_handle=ChartPeriod(TimeFrame);
  TFMA_handle=iMA(Symbol(),TimeFrame,MA_Period,0,MODE_EMA,PRICE_CLOSE);
  //TFMA_handle=IndicatorCreate(NULL,TimeFrame,IND_MA,0,);
   
   return(0);
   
//---
   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 limit;
   if (prev_calculated == 0)
       limit = prev_calculated + MA_Period + 1;
      else
       limit = prev_calculated - 1;
       
      
   int copied=CopyBuffer(MA_handle,0,0,rates_total,Label1Buffer);
   if(copied<=0) return(0);// copying failed - throw away
   //Print ("rates total ",rates_total," prev_calc ",prev_calculated," limit ",limit," copied ", copied);
   
   /*int MTFcopied=*/CopyBuffer(TFMA_handle,0,0,rates_total,TFMA_Buffer);
    //  if(MTFcopied<=0) return (0);
  
       /* buffer_open[i]=open[i];
         buffer_high[i]=high[i];
         buffer_low[i]=low[i];
         buffer_close[i]=close[i];*/
   
//--- return value of prev_calculated for next call
   return(rates_total);
  }
 
  1. Compute your limit. That is the current chart's index. Use iBarShift(TimeFrame) to find the index on the TimeFrame chart. Get that bar's time and use iBarShift(PERIOD_CURRENT) to find the real limit. You have to re-process all bars that are part of TimeFrame bar zero, the stair step effect.

  2. In your loop, for each i, use iBarShift to find the index on the TimeFrame chart and get the value from there.

  3. Compute your lookbacks correctly. For TFMA it is not MA_Period it is MA_Period * PeriodSeconds(TimeFrame) / PeriodSeconds(PERIOD_CURRENT)
              How to do your lookbacks correctly.
Reason: