//+------------------------------------------------------------------+
//|                                                       All MA.mq4 |
//|                                                           mladen |
//+------------------------------------------------------------------+
#property copyright   "mladen"
#property link        "mladenfx@gmail.com"

//
//
//
//
//

#property indicator_separate_window
#property indicator_buffers 4
#property indicator_color1 Red
#property indicator_color2 Lime

//
//
//
//
//

extern string forSymbol            = "";
extern int    MAperiod1            = 60;
extern int    MAperiod2            = 20;
extern int    MAMethod             = 1;
extern int    AppliedPrice         = 0;
extern string __                   = "Chose timeframes";
extern string timeFrames           = "H4;H1";
extern int    barsPerTimeFrame     = 115;
extern bool   shiftRight           = False;
extern bool   currentFirst         = True; 
extern int    BarWidth             = 3;
extern color  BarUpColor           = LimeGreen; 
extern color  BarDownColor         = PaleVioletRed; 
extern color  WickColor            = Gray; 
extern color  txtColor             = Silver; 
extern color  separatorColor       = DimGray; 
extern string UniqueID             = "All time frames MA";

//
//
//
//
//
//

double ExtMapBuffer1[];
double ExtMapBuffer2[];
double ExtMapBuffer3[];
double ExtMapBuffer4[];

//
//
//
//
//

string shortName;
string labels[];
int    periods[];
int    Shift; 
int    prices;
int    window;


//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+

int init()
{
      if (shiftRight) Shift = 1;
      else            Shift = 0;
      if (forSymbol == "") forSymbol=Symbol();
         barsPerTimeFrame = MathMax(barsPerTimeFrame,15);
         shortName = UniqueID+" "+forSymbol+" ("+MAperiod1+","+MAperiod2+")";
                     IndicatorShortName(shortName);
   
      //
      //
      //
      //
      //

      SetIndexBuffer(0,ExtMapBuffer1);
      SetIndexBuffer(1,ExtMapBuffer2);
      SetIndexBuffer(2,ExtMapBuffer3);
      SetIndexBuffer(3,ExtMapBuffer4);
      SetIndexShift(0,Shift*(barsPerTimeFrame+1));
      SetIndexShift(1,Shift*(barsPerTimeFrame+1));
      SetIndexShift(2,Shift*(barsPerTimeFrame+1));
      SetIndexShift(3,Shift*(barsPerTimeFrame+1));
      SetIndexLabel(0,"MA "+MAperiod1);
      SetIndexLabel(1,"MA "+MAperiod2);
      SetIndexLabel(2,NULL);
      SetIndexLabel(3,NULL);
      SetIndexStyle(2,DRAW_NONE);
      SetIndexStyle(3,DRAW_NONE);
   
      //
      //
      //
      //
      //
      
      timeFrames = StringTrimLeft(StringTrimRight(timeFrames));
      if (StringSubstr(timeFrames,StringLen(timeFrames),1) != ";")
                       timeFrames = StringConcatenate(timeFrames,";");

         //
         //
         //
         //
         //                                   
            
         int s = 0;
         int i = StringFind(timeFrames,";",s);
         int time;
         string current;
            while (i > 0)
            {
               current = StringSubstr(timeFrames,s,i-s);
               time    = stringToTimeFrame(current);
               if (time > 0) {
                     ArrayResize(labels ,ArraySize(labels)+1);
                     ArrayResize(periods,ArraySize(periods)+1);
                                 labels[ArraySize(labels)-1] = timeFrameToString(time); 
                                 periods[ArraySize(periods)-1] = time; }
                                 s = i + 1;
                                     i = StringFind(timeFrames,";",s);
            }
      
      //
      //
      //
      //
      //

      if(currentFirst)
         for (i=1;i<ArraySize(periods);i++)
         if (Period()==periods[i])
            {
               string tmpLbl = labels[i];
               int    tmpPer = periods[i];
               
               //
               //
               //
               //
               //
               
               for (int k=i ;k>0; k--)
                     {
                        labels[k]  = labels[k-1];
                        periods[k] = periods[k-1];
                     }                     
               labels[0]  = tmpLbl;
               periods[0] = tmpPer;
            }
   return(0);
}

int deinit()
{
   for(int l=0;l<ArraySize(periods);l++) {
         ObjectDelete(shortName+l);
         ObjectDelete(shortName+l+"label");
      }         
   DeletePrices();      
   return(0);
}

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+

int start()
{
     double max;
     double min; 
     string separator;
     int    k=0;


     //
     //
     //
     //
     //
     
     window=WindowFind(shortName);
     DeletePrices();      
            for(int p=0; p<ArraySize(periods);p++)
                  {
                     for(int i=0; i<barsPerTimeFrame;i++,k++)
                        {
                           if (MAperiod1 > 0) ExtMapBuffer1[k] = iMA(forSymbol,periods[p],MAperiod1,0,MAMethod,AppliedPrice,i);
                           if (MAperiod2 > 0) ExtMapBuffer2[k] = iMA(forSymbol,periods[p],MAperiod2,0,MAMethod,AppliedPrice,i);
                           DrawPrice(periods[p],i,k);
                        }                           
                        ExtMapBuffer1[k] = EMPTY_VALUE;
                        ExtMapBuffer2[k] = EMPTY_VALUE;
                        k += 1;
                           
                           //
                           //
                           //
                           //
                           //
                           
                           separator = shortName+p;
                           if(ObjectFind(separator)==-1)
                              ObjectCreate(separator,OBJ_TREND,window,0,0);
                              ObjectSet(separator,OBJPROP_TIME1,barTime(k-Shift*(barsPerTimeFrame+1)-1));
                              ObjectSet(separator,OBJPROP_TIME2,barTime(k-Shift*(barsPerTimeFrame+1)-1));
                              ObjectSet(separator,OBJPROP_COLOR ,separatorColor);
                              ObjectSet(separator,OBJPROP_WIDTH ,2);
                           separator = shortName+p+"label";
                           if(ObjectFind(separator)==-1)
                              ObjectCreate(separator,OBJ_TEXT,window,0,0);
                              ObjectSet(separator,OBJPROP_TIME1,barTime(k-Shift*(barsPerTimeFrame+1)-5));
                              ObjectSet(separator,OBJPROP_ANGLE,270);
                              ObjectSetText(separator,labels[p],9,"Arial",txtColor);
                  }
      SetIndexDrawBegin(0,Bars-k);
      SetIndexDrawBegin(1,Bars-k);
      
      //
      //
      //
      //
      //
      
      max =       0.00;
      min = 9999999.99;
      for(p=0; p<ArraySize(periods);p++)
         {
            max = MathMax(iHigh(forSymbol,periods[p],iHighest(forSymbol,periods[p],MODE_HIGH,barsPerTimeFrame,0)),max);
            min = MathMin( iLow(forSymbol,periods[p], iLowest(forSymbol,periods[p],MODE_LOW ,barsPerTimeFrame,0)),min);
         }
      for(p=0; p<ArraySize(periods);p++)
         {
            separator = shortName+p;
               ObjectSet(separator,OBJPROP_PRICE1,max*1.2);
               ObjectSet(separator,OBJPROP_PRICE2,min);
            separator = shortName+p+"label";
               ObjectSet(separator,OBJPROP_PRICE1,(min+max)/2);
         }
      for(i=k;i>=0 ;i--)
         {
            ExtMapBuffer3[i] = max;
            ExtMapBuffer4[i] = min;
         }
      

      //
      //
      //
      //
      //
    
   return(0);
}

//+------------------------------------------------------------------+
//+                                                                  +
//+------------------------------------------------------------------+
//
//
//
//
//

int barTime(int a)
{
   if(a<0)
         return(Time[0]+Period()*60*MathAbs(a));
   else  return(Time[a]);   
}

//
//
//
//
//

void DeletePrices()
{
   for(;prices>0;prices--) {
         ObjectDelete(shortName+"prices"+prices);
         ObjectDelete(shortName+"prices"+prices+"body");
      }         
}

void DrawPrice(int period, int shift,int ovearAll)
{
   datetime time  = barTime(ovearAll);
   double   high  = iHigh (forSymbol,period,shift);
   double   low   = iLow  (forSymbol,period,shift);
   double   open  = iOpen (forSymbol,period,shift);
   double   close = iClose(forSymbol,period,shift);
   string name;


   
   prices += 1;
   name    = shortName+"prices"+prices;
      ObjectCreate(name,OBJ_TREND,window,time,high,time,low);
         ObjectSet(name,OBJPROP_COLOR,WickColor);
         ObjectSet(name,OBJPROP_RAY  ,false);
      
   //
   //
   //
   //
   //
         
   prices += 1;
   name    = shortName+"prices"+prices+"body";
      ObjectCreate(name,OBJ_TREND,window,time,open,time,close);
         ObjectSet(name,OBJPROP_WIDTH,BarWidth);
         ObjectSet(name,OBJPROP_RAY  ,false);
         if (open<close)
               ObjectSet(name,OBJPROP_COLOR,BarUpColor);
         else  ObjectSet(name,OBJPROP_COLOR,BarDownColor);
}


//-------------------------------------------------------------------
//
//-------------------------------------------------------------------
//
//
//
//
//

string sTfTable[] = {"M1","M5","M15","M30","H1","H4","D1","W1","MN"};
int    iTfTable[] = {1,5,15,30,60,240,1440,10080,43200};

//
//
//
//
//

int toInt(double value) { return(value); }
int stringToTimeFrame(string tfs)
{
   tfs = stringUpperCase(tfs);
   int max = ArraySize(iTfTable)-1, add=0;
   int nxt = (StringFind(tfs,"NEXT1")>-1); if (nxt>0) { tfs = ""+Period(); add=1; }
       nxt = (StringFind(tfs,"NEXT2")>-1); if (nxt>0) { tfs = ""+Period(); add=2; }
       nxt = (StringFind(tfs,"NEXT3")>-1); if (nxt>0) { tfs = ""+Period(); add=3; }

      //
      //
      //
      //
      //
         
      for (int i=max; i>=0; i--)
         if (tfs==sTfTable[i] || tfs==""+iTfTable[i]) return(iTfTable[toInt(MathMin(max,i+add))]);
                                                      return(Period());
}
string timeFrameToString(int tf)
{
   for (int i=ArraySize(iTfTable)-1; i>=0; i--) 
         if (tf==iTfTable[i]) return(sTfTable[i]);
                              return("");
}

//
//
//
//
//

string stringUpperCase(string str)
{
   string   s = str;

   for (int length=StringLen(str)-1; length>=0; length--)
   {
      int char = StringGetChar(s, length);
         if((char > 96 && char < 123) || (char > 223 && char < 256))
                     s = StringSetChar(s, length, char - 32);
         else if(char > -33 && char < 0)
                     s = StringSetChar(s, length, char + 224);
   }
   return(s);
}