//+------------------------------------------------------------------+
//|                                                    gy_ma_pos.mq4 |
//|                                                                  |
//+------------------------------------------------------------------+

#property indicator_chart_window

extern int ma_type=MODE_EMA;
extern int max_bars=3000;

int TFs[10]={ 0, 1, 5, 15, 30, 60, 240, 1440, 10080, 43200 };
string TFns[10]={ "", "M1", "M5", "M15", "M30", "H1", "H4", "D1", "W1", "MN" };

string comm_ma_pos="";    

datetime m1=0, t1, t2;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {

   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----

//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
{
   if (iTime(Symbol(),PERIOD_M1,0)!=m1)
   {
      m1=iTime(Symbol(),PERIOD_M1,0);
      t1=TimeCurrent();
      comm_ma_pos="";
      for (int x1=1;x1<10;x1++)
      {
         MApos(TFs[x1], x1);
      }
      Comment(comm_ma_pos);
      t2=TimeCurrent();
      Print("seconds: ", t2-t1);
   }
   return(0);
}


void MApos(int tfc, int tfnr)
{
   double pr=Ask;
   int nb=iBars(Symbol(), tfc)-1;
   if (nb>max_bars) { nb=max_bars; }
   for (int mp=2;mp<nb;mp++)
   {
      double p1 = iMA(Symbol(), tfc, mp, 0, ma_type, PRICE_CLOSE, 0);
      double p2 = iMA(Symbol(), tfc, mp+1, 0, ma_type, PRICE_CLOSE, 0);
      double ph=MathMax(p1, p2);
      double pl=MathMin(p1, p2);
      if ((pr>=pl) && (pr<=ph)) 
      {
         comm_ma_pos=comm_ma_pos+"\n"+Symbol()+"/"+TFns[tfnr]+" period: "+DoubleToStr(mp,0);
         Print("MAPOS ",Symbol(),"/",tfc,"/",mp," sma: ",DoubleToStr(p1,Digits),"/",DoubleToStr(p2,Digits)," ph: ", DoubleToStr(ph,Digits)," pl: ", DoubleToStr(pl,Digits)," pr: ",DoubleToStr(pr,Digits));
      }
   }
   return(0);
}

