//+------------------------------------------------------------------+
//|                       Golden test.mq4                            |
//|                       Copyright © 2011, StarLimit Software Corp. |
//|                       Created 01/01/2011  5:45PM                 |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2011, StarLimit Software Corp."
#property link      ""

#property indicator_chart_window
#property indicator_buffers 7
#property indicator_color1 Aqua
#property indicator_color2 Red
#property indicator_color3 Aqua
#property indicator_color4 Red
#property indicator_color5 Aqua
#property indicator_color6 Red

extern int RISK = 6;
extern bool autoset=true;
extern int TimeFrame1=0,TimeFrame2=0,TimeFrame3=0;
extern bool SingleTF=false;
extern bool showinfo=true;

double high3,high1,high2,low3,low1,low2;
double BuyBuffer1[],BuyBuffer2[],BuyBuffer3[];
double SellBuffer1[],SellBuffer2[],SellBuffer3[];
int GoldenBuy = 0;
int GoldenSell = 0;
 string auto =" FALSE";
bool first=true;

int sli_20[3],per[3];
//double TempBuffer_0[];
double TempBuffer[];

int init()
{
   IndicatorBuffers( 7 );
   
   SetIndexStyle(0, DRAW_ARROW, STYLE_SOLID, 2);   
   SetIndexArrow(0, 159);
   SetIndexBuffer(0, BuyBuffer1);
   SetIndexStyle(1, DRAW_ARROW, STYLE_SOLID, 2);
   SetIndexArrow(1, 159);
   SetIndexBuffer(1, SellBuffer1);
   SetIndexStyle(2, DRAW_ARROW, STYLE_SOLID, 4);   
   SetIndexArrow(2, 159);
   SetIndexBuffer(2, BuyBuffer2);
   SetIndexStyle(3, DRAW_ARROW, STYLE_SOLID, 4);
   SetIndexArrow(3, 159);
   SetIndexBuffer(3, SellBuffer2);
   SetIndexStyle(4, DRAW_ARROW, STYLE_SOLID, 6);   
   SetIndexArrow(4, 159);
   SetIndexBuffer(4, BuyBuffer3);
   SetIndexStyle(5, DRAW_ARROW, STYLE_SOLID, 6);
   SetIndexArrow(5, 159);
   SetIndexBuffer(5, SellBuffer3);
   
   SetIndexLabel(0,"Resistance_1");
   SetIndexLabel(1,"Support_1");
   SetIndexLabel(2,"Resistance_2");
   SetIndexLabel(3,"Support_2");
   SetIndexLabel(4,"Resistance_3");
   SetIndexLabel(5,"Support_3");
   SetIndexEmptyValue(0,0.0);
   SetIndexEmptyValue(1,0.0);
   SetIndexEmptyValue(2,0.0);
   SetIndexEmptyValue(3,0.0);
   SetIndexEmptyValue(4,0.0);
   SetIndexEmptyValue(5,0.0);

   SetIndexBuffer(6, TempBuffer);
   
     if( autoset)
  { 
   auto=" TRUE";
    if(Period()==1)
   {TimeFrame1=1;TimeFrame2=5;TimeFrame3=15;}     //1, 5, 30
   else if(Period()==5)
   {TimeFrame1=5;TimeFrame2=15;TimeFrame3=60;}
   
   else if(Period()==15)
   {TimeFrame1=15;TimeFrame2=60;TimeFrame3=240;}
   
   else if(Period()==30)
   {TimeFrame1=30;TimeFrame2=240;TimeFrame3=1440;}
   
   else if(Period()==60)
   {TimeFrame1=60;TimeFrame2=240;TimeFrame3=1440;}
   
   else if(Period()==240)
   {TimeFrame1=240;TimeFrame2=1440;TimeFrame3=10080;}
   
   else if(Period()==1440)
   {TimeFrame1=1440;TimeFrame2=10080;TimeFrame3=43200;}
   else if(Period()==10080)
   {TimeFrame1=10080;TimeFrame2=43200;TimeFrame3=43200;}
  } 
   per[0]=TimeFrame1;
   per[1]=TimeFrame2;
   per[2]=TimeFrame3;
   
   
  if(SingleTF)
  {
   per[0]=TimeFrame1;
   per[1]=TimeFrame1;
   per[2]=TimeFrame1;
   showinfo=false;
  }
  first=true;
  string name="MTF Golden Finger("+per[0]+","+per[1]+","+per[2]+")";
  IndicatorShortName(name);
   return (0);
}
/////////////////////////////////////////////////// 
int deinit()
{
  Comment(" ");
   return (0);
}

int start()
{
  string name="MTF Golden Finger("+per[0]+","+per[1]+","+per[2]+")";
  IndicatorShortName(name);
   double ld_0;
   double ld_8;
   int l_count_16;
   int li_20;
   int li_24;
   int li_28;
   double ld_32;
   double ld_40;
   double ld_48;
   double ld_56;
   //double TempBuffer[500][2];
   double ld_68 = 10;
   double ld_76 = 70;
   double ld_84 = 30;
   int counted_bars = IndicatorCounted();
   ld_68 = RISK * 2 + 3;
   ld_76 = RISK + 67;
   ld_84 = 33 - RISK;
   double l_period_96 = ld_68;
   
   if(counted_bars < 0)
      return (-1);
   
   if(counted_bars > 0)
      counted_bars--;
  // if(first)
     int limit = Bars - counted_bars - 1;
   //else limit =40;
   
for(int a=0;a<=3;a++)    // M1 ,M5, M15, M30 ,H1   get all data values
{  
   for(int shift1 = limit; shift1 > 0; shift1--)
    {
      
      if((GoldenBuy != 0 || GoldenSell != 0) && shift1 <= 1)
  //     Comment(shift1 + " GoldenBuy=" + GoldenBuy + " GoldenSell=" + GoldenSell);
   //   Comment("GoldFingerB >>> "+ BuyBuffer1[shift1] + "\n\nGoldFingerS >>> "+ SellBuffer1[shift1]);
      li_20 = shift1;
    
     datetime time=iTime(NULL,Period(),shift1);
    sli_20[a]=iBarShift(NULL,per[a],time,true);
      
      ld_32 = 0;
      ld_40 = 0;

      for(li_20 = shift1; li_20 <= shift1 + 9; li_20++)  
        ld_40 += MathAbs(iHigh(NULL,per[a],sli_20[a]) - iLow(NULL,per[a],sli_20[a]));     

     // ld_40 += MathAbs(High[li_20] - Low[li_20]);     
      ld_32 = ld_40 / 10.0;
      li_20 = shift1;
      l_count_16 = 0;

      while(li_20 < shift1 + 9 && l_count_16 < 1) 
      {
       //  if(MathAbs(Open[li_20] - (Close[li_20 + 1])) >= 2.0 * ld_32)  
           if(MathAbs(iOpen(NULL,per[a],sli_20[a]) - iClose(NULL,per[a],sli_20[a]+1)) >= 2.0 * ld_32)  
            l_count_16++;
         li_20++;
      }

      if(l_count_16 >= 1)  
         li_24 = li_20;
      else
         li_24 = -1;

      li_20 = shift1;
      l_count_16 = 0;

      while(li_20 < shift1 + 6 && l_count_16 < 1) 
      {
         //if(MathAbs(Close[li_20 + 3] - Close[li_20]) >= 4.6 * ld_32)
         if(MathAbs(iClose(NULL,per[a],sli_20[a]+3) - iClose(NULL,per[a],sli_20[a])) >= 4.6 * ld_32)
            l_count_16++;
         li_20++;
      }

      if(l_count_16 >= 1)  
         li_28 = li_20;
      else
         li_28 = -1;

      if(li_24 > -1)       
         l_period_96 = 3;
      else
         l_period_96 = ld_68;

      if(li_28 > -1)
         l_period_96 = 4;
      else
         l_period_96 = ld_68;

      ld_0 = 100 - MathAbs(iWPR(NULL,per[a], l_period_96, sli_20[a]));   // iWPR 
      //TempBuffer[shift1][0] = shift1;
      TempBuffer[shift1] = ld_0;
      ld_48 = 0;
      ld_56 = 0;
      ld_8 = 0;

      if(ld_0 < ld_84) 
       {
         for(int li_112 = 1;
             TempBuffer[shift1 + li_112] >= ld_84
             && TempBuffer[shift1 + li_112] <= ld_76; li_112++)
              {
              }
         if(TempBuffer[shift1 + li_112] > ld_76)
         {
            ld_8 = iHigh(NULL,per[a],sli_20[a]) + ld_32 / 2.0;
            ld_48 = ld_8;
         }
      }

      if(ld_0 > ld_76)
      {
         for(li_112 = 1;
             TempBuffer[shift1 + li_112] >= ld_84
             && TempBuffer[shift1 + li_112] <= ld_76; li_112++)
              {
              }
         if(TempBuffer[shift1 + li_112] < ld_84)
           {
            ld_8 = iLow(NULL,per[a],sli_20[a]) - ld_32 / 2.0;
            ld_56 = ld_8;
           }
      }

      if(ld_56 != 0.0 && GoldenBuy == FALSE)
      {
        switch(a)
        {
         case 0:
            BuyBuffer1[shift1] = ld_56 - 1.0 * Point;
            low1=BuyBuffer1[shift1];
            break;
         case 1:
            BuyBuffer2[shift1] = ld_56 - 1.0 * Point;
            low2=BuyBuffer2[shift1];
            break;
         case 2:
            BuyBuffer3[shift1] = ld_56 - 1.0 * Point;
            low3=BuyBuffer3[shift1];
            break;
         }   
         GoldenBuy = TRUE;
         GoldenSell = FALSE;
         if(limit <= 2)
            Alert(Symbol(), " M", per[a], "  Golden Finger BUY ");
      }

      if(ld_48 != 0.0 && GoldenSell == FALSE) 
      {
         switch(a)
        {
         case 0:
            SellBuffer1[shift1] = ld_48 + 1.0 * Point;
            high1=SellBuffer1[shift1];
            break;
         case 1:
            SellBuffer2[shift1] = ld_48 + 1.0 * Point;
            high2=SellBuffer2[shift1];
            break;
         case 2:
            SellBuffer3[shift1] = ld_48 + 1.0 * Point;
            high3=SellBuffer3[shift1];
            break;
         } 
         GoldenSell = TRUE;
         GoldenBuy = FALSE;
         if(limit <= 2)
            Alert(Symbol(), " M", per[a], "  Golden Finger SELL ");
      }
 if(showinfo)        Comment(" \n\n\n Multi Golden Finger \n\n AUTHOR: StarLimit03 \n\n CREATED :  01/01/2011.... \n\n AUTOSET TIMEFRAME IS SET TO",auto,"\n OWN  TIMEFRAME= ",TimeFrame1,"\n HIGH1 TIMEFRAME= ",TimeFrame2,"\n HIGH2 TIMEFRAME= ",TimeFrame3,
      "\n\n Last High on ",TimeFrame1," is ",high1,".....Last Low is ",low1,
      "\n Last High on ",TimeFrame2," is ",high2,".....Last Low is ",low2,
      "\n Last High on ",TimeFrame3," is ",high3,".....Last Low is ",low3,
      "\n\n Buying Price is ; ",Ask,"...........Selling Price is : ",Bid);
        
   }
 }
   return (0);
}

