//+------------------------------------------------------------------+
//|                                                    Alligator.mq4 |
//|                      Copyright © 2004, MetaQuotes Software Corp. |
//|                                       http://www.metaquotes.net/ |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2004, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net/"

#property indicator_chart_window
#property indicator_buffers 3
#property indicator_color1  Blue
#property indicator_color2  Red
#property indicator_color3  Lime

//
//
//
//
//

extern string  TimeFrame   = "Current time frame";
extern int     JawsPeriod  = 13;
extern int     JawsShift   = 8;
extern int     TeethPeriod = 8;
extern int     TeethShift  = 5;
extern int     LipsPeriod  = 5;
extern int     LipsShift   = 3;
extern int     MaMode      = MODE_SMMA;
extern int     MaPrice     = PRICE_MEDIAN;

//
//
//
//
//

double BlueBuffer[];
double RedBuffer[];
double LimeBuffer[];

string indicatorFileName;
bool   calculateValue;
bool   returnBars;
int    timeFrame;

//
//
//
//
//
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//

int init()
{
   SetIndexBuffer(0,BlueBuffer);SetIndexLabel(0,"Gator Jaws");
   SetIndexBuffer(1,RedBuffer); SetIndexLabel(1,"Gator Teeth");
   SetIndexBuffer(2,LimeBuffer);SetIndexLabel(2,"Gator Lips");
   
      //
      //
      //
      //
      //
     
         indicatorFileName = WindowExpertName();
         calculateValue    = (TimeFrame=="calculateValue"); if (calculateValue) return(0);
         returnBars        = (TimeFrame=="returnBars");     if (returnBars)     return(0);
         timeFrame         = stringToTimeFrame(TimeFrame);
        
         SetIndexShift(0,JawsShift  * timeFrame/Period());  
         SetIndexShift(1,TeethShift * timeFrame/Period());  
         SetIndexShift(2,LipsShift  * timeFrame/Period());  
     
     //
     //
     //
     //
     //
   
     IndicatorShortName(timeFrameToString(timeFrame)+"   Alligator");
return(0);
}

//+------------------------------------------------------------------+
//| Bill Williams' Alligator                                         |
//+------------------------------------------------------------------+
int start()
{
   int limit,i,counted_bars = IndicatorCounted();

   if(counted_bars<0) return(-1);
   if(counted_bars>0) counted_bars--;
       limit = MathMin(Bars - counted_bars,Bars-1);
       if (returnBars) { BlueBuffer[0] = limit+1; return(0); }
       
   //
   //
   //
   //
   //
   
   if (calculateValue || timeFrame == Period())
   {
     for (i = limit; i >= 0 ; i--)
     {
         BlueBuffer[i] = iMA(NULL,0,JawsPeriod, 0,MaMode,MaPrice,i);
         RedBuffer[i]  = iMA(NULL,0,TeethPeriod,0,MaMode,MaPrice,i);
         LimeBuffer[i] = iMA(NULL,0,LipsPeriod, 0,MaMode,MaPrice,i);
     }
   return(0);
   }
   
   //
  //
  //
  //
  //
   
  limit = MathMax(limit,MathMin(Bars-1,iCustom(NULL,timeFrame,indicatorFileName,"returnBars",0,0)*timeFrame/Period()));
  for (i=limit; i>=0; i--)
  {   
     int y = iBarShift(NULL,timeFrame,Time[i]);
        BlueBuffer[i] = iCustom(NULL,timeFrame,indicatorFileName,"calculateValue",JawsPeriod,JawsShift,TeethPeriod,TeethShift,LipsPeriod,LipsShift,MaMode,MaPrice,0,y);
        RedBuffer[i]  = iCustom(NULL,timeFrame,indicatorFileName,"calculateValue",JawsPeriod,JawsShift,TeethPeriod,TeethShift,LipsPeriod,LipsShift,MaMode,MaPrice,1,y);
        LimeBuffer[i] = iCustom(NULL,timeFrame,indicatorFileName,"calculateValue",JawsPeriod,JawsShift,TeethPeriod,TeethShift,LipsPeriod,LipsShift,MaMode,MaPrice,2,y);
  }
   return(0);
}

//
//
//
//
//

string sTfTable[] = {"M1","M5","M15","M30","H1","H4","D1","W1","MN"};
int    iTfTable[] = {1,5,15,30,60,240,1440,10080,43200};

//
//
//
//
//

int stringToTimeFrame(string tfs)
{
   tfs = stringUpperCase(tfs);
   for (int i=ArraySize(iTfTable)-1; i>=0; i--)
         if (tfs==sTfTable[i] || tfs==""+iTfTable[i]) return(MathMax(iTfTable[i],Period()));
                                                      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 tchar = StringGetChar(s, length);
         if((tchar > 96 && tchar < 123) || (tchar > 223 && tchar < 256))
                     s = StringSetChar(s, length, tchar - 32);
         else if(tchar > -33 && tchar < 0)
                     s = StringSetChar(s, length, tchar + 224);
   }
   return(s);
}

