//+------------------------------------------------------------------+
//|zMACallX                                                          |
//|Get EMA from X-minute chart and place on x-second                 |
//|11/05/09                                                          |
//+------------------------------------------------------------------+
#property copyright "z"
#property link      "z"
#property indicator_chart_window
//#property indicator_separate_window  // for other symbol with different scaling place in separate window 
#property indicator_buffers 1
#property indicator_color1 Yellow
//extern string sWhatSymbol = "XXXYYY";   // you can make this an extern 
string sWhatSymbol = "EURUSD";   // on/off-line chart built by period converter does not have special prefix
int iMAPeriod = 3;  // you can make these externs 
int iMAShift = 2;
int iChartPeriod = 3;
double maNOW;
double maNOWPrev;
double MABuf[];
bool bFirst=True;
int prevPeriod=0;
int initBars=0;
int prevBars=0;
int iRtn=0;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
{
   SetIndexStyle(0,DRAW_LINE,0,1);
   SetIndexBuffer(0,MABuf); 
   SetIndexLabel(0,"MA1");
   return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
{
   //Initialize
   //save time of right-most bar
   if(Bars < 50) {
      Print ("zMACallX not enough data., bars: ", Bars);
      return;
   }
   // some off-line charts have funky name
   sWhatSymbol = Symbol();
   //if(StringSubstr(sWhatSymbol,0,1) != "!") {
   //  Print ("zMA1CallX - Error. Expecting a tick or seconds chart.");
   //  return;
   //}
   //sWhatSymbol = StringSubstr(sWhatSymbol,2,99);
   if(iBars(sWhatSymbol,iChartPeriod) < 50) {  
      Print ("zMACallX not enough X-min data., bars: ", iBars(sWhatSymbol,iChartPeriod));
      return;
   }
   if(Period() != prevPeriod) {
      prevPeriod = Period();
      bFirst = True;
   }
   if(IndicatorCounted() == 0) bFirst = True;
   if(bFirst == True)
   {
      initBars = Bars;
      prevBars = Bars;
      //calculate momentum on loaded bars
      int kklmt = Bars-20;   //allow for lookback
      for(int kk=kklmt; kk>=1; kk--)
      {
         iRtn = Calc_MA1(kk);
      }
      bFirst = False;
   }
   //Initialize End
   //
   //All bars (full and tick)
   if(Bars <= initBars) return;        //are we past initially loaded bars   
   if(Bars == prevBars)
   {              //still filling out current bar
      //Print("continuation bar.", TimeToStr(CurTime()),"  bars: ",Bars,TimeToStr(Time[0]));
   }
   else
   {                              //first tick of new bar, process prior bar
      //Print("new bar.", TimeToStr(CurTime()),"  bars: ",Bars,TimeToStr(Time[0]));
      //iRtn = Calc_MA1(1);     //this is not working
      for(kk=5; kk>=1; kk--)   //recalc a few
      {
         iRtn = Calc_MA1(kk);
      }
   }
   prevBars = Bars;
   return(0);
}   

int Calc_MA1(int i)
{
   int iBarOnX = iBarShift(sWhatSymbol,iChartPeriod,Time[i],false);
   maNOW = iMA(sWhatSymbol,iChartPeriod,iMAPeriod,iMAShift,MODE_EMA,PRICE_CLOSE,iBarOnX);
   if(maNOW == EMPTY_VALUE) {
      maNOW = maNOWPrev;
   }
   MABuf[i] = maNOW;
   maNOWPrev = maNOW;
   return(0);
}
//+------------------------------------------------------------------+