//+------------------------------------------------------------------+
//|                                                                  |
//|                                                                  |
//+------------------------------------------------------------------+
//Tony's Two Candle trading system 
//http://www.forexfactory.com/showthread.php?t=172963

#property copyright ""
#property link      ""

#property indicator_chart_window
#property indicator_buffers    6
#property indicator_color1     Navy
#property indicator_color2     Blue
#property indicator_color3     Navy


#property indicator_color4     Yellow
#property indicator_color5      Red
#property indicator_color6    Yellow


//--


extern int TimeFrame=60;

extern int entrylvlPoints     = 40;
extern int SLlvlPoints        = 150;
extern int TPlvlPoints        = 250;


double buffer1[];
double buffer2[];
double buffer3[];
double buffer4[];
double buffer5[];
double buffer6[];

double buffer7[];
double buffer8[];

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+

int init()
{
      IndicatorBuffers(6); 

  
   SetIndexBuffer(0,buffer1);
   SetIndexBuffer(1,buffer2);
   SetIndexBuffer(2,buffer3);
   SetIndexBuffer(3,buffer4);
   SetIndexBuffer(4,buffer5);
   SetIndexBuffer(5,buffer6);


   SetIndexBuffer(6,buffer7);
   SetIndexBuffer(7,buffer8);

   SetIndexStyle(0,DRAW_ARROW);  SetIndexArrow(0,233); SetIndexEmptyValue(0,EMPTY_VALUE);
   SetIndexStyle(1,DRAW_ARROW);  SetIndexArrow(1,167); SetIndexEmptyValue(1,EMPTY_VALUE);
   SetIndexStyle(2,DRAW_ARROW);  SetIndexArrow(2,159); SetIndexEmptyValue(2,EMPTY_VALUE);


   SetIndexStyle(3,DRAW_ARROW);  SetIndexArrow(3,234); SetIndexEmptyValue(3,EMPTY_VALUE);
   SetIndexStyle(4,DRAW_ARROW);  SetIndexArrow(4,167); SetIndexEmptyValue(4,EMPTY_VALUE);
   SetIndexStyle(5,DRAW_ARROW);  SetIndexArrow(5,159); SetIndexEmptyValue(5,EMPTY_VALUE);

      TimeFrame = MathMax(TimeFrame,Period());

   IndicatorShortName("2consB " +TimeFrame);  

   SetIndexLabel(0,"bullentry");
   SetIndexLabel(3,"bearentry");

//   


   return(0);
}
int deinit()
{
   return(0);
}

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+

int start()
{
   int counted_bars = IndicatorCounted();
   int limit, i;
   
   if (counted_bars < 0) return (-1);
   if (counted_bars > 0) counted_bars--;
   
   limit = Bars - counted_bars;
   if (TimeFrame != Period())
      limit = MathMax(limit, TimeFrame / Period());

   for (i = limit; i >= 0; i--)
   {
      int y = iBarShift(NULL, TimeFrame, Time[i]);
   
      buffer1[i] = EMPTY_VALUE;
      buffer2[i] = EMPTY_VALUE;
      buffer3[i] = EMPTY_VALUE;
      buffer4[i] = EMPTY_VALUE;
      buffer5[i] = EMPTY_VALUE;
      buffer6[i] = EMPTY_VALUE;

      double cl = iClose(NULL, TimeFrame, y);
      double op = iOpen(NULL, TimeFrame, y);
      double cl1 = iClose(NULL, TimeFrame, y + 1);
      double op1 = iOpen(NULL, TimeFrame, y + 1);
      double cl2 = iClose(NULL, TimeFrame, y + 2);
      double op2 = iOpen(NULL, TimeFrame, y + 2);

      double hi = iHigh(NULL, TimeFrame, y);
      double lo = iLow(NULL, TimeFrame, y);
      double hi1 = iHigh(NULL, TimeFrame, y + 1);
      double lo1 = iLow(NULL, TimeFrame, y + 1);
   
      string message;
   
      if ((hi - op) > entrylvlPoints * Point && cl > op && cl1 > op1 && cl2 < op2)
      {
         buffer1[i] = op + entrylvlPoints * Point;
         buffer2[i] = op - SLlvlPoints * Point;
         buffer3[i] = op + TPlvlPoints * Point;
      
         message = "Long Signal On " + Symbol() + " At Timeframe " + TimeFrame;
         AlertOnce(message, 0, TimeFrame);

         // Add blue vertical line at starting point
         if (buffer1[i] != 0)
            DrawVerticalLine(i, Blue);
      } 
 
      if ((lo - op) < -entrylvlPoints * Point && cl < op && cl1 < op1 && cl2 > op2)
      {
         buffer4[i] = op - entrylvlPoints * Point;
         buffer5[i] = op + SLlvlPoints * Point;
         buffer6[i] = op - TPlvlPoints * Point;
      
         message = "Short Signal On " + Symbol() + " At Timeframe " + TimeFrame;
         AlertOnce(message, 0, TimeFrame);

         // Add red vertical line at starting point
         if (buffer3[i] != 0)
            DrawVerticalLine(i, Red);
      }
   }
   
   return 0;
}

void DrawVerticalLine(int barIndex, color lineColor)
{
   datetime time = Time[barIndex];
   string lineName = "VerticalLine_" + IntegerToString(barIndex);

   ObjectCreate(lineName, OBJ_VLINE, 0, time, 0);
   ObjectSet(lineName, OBJPROP_COLOR, lineColor);
   ObjectSet(lineName, OBJPROP_STYLE, STYLE_SOLID);
   ObjectSet(lineName, OBJPROP_WIDTH, 1);
}



bool AlertOnce(string alert_msg, int ref, int tf)
{  
int i = 0;


  static int LastAlert[1000];
   
  while (i < 1000)
{
 if (ref == i)
  {
           if( LastAlert[i] == 0 || LastAlert[i] < iBars(NULL,tf) )
        {
          Alert(alert_msg);
           LastAlert[i] = iBars(NULL,tf);
           return (1);
        }
      break;
 }
   
 i = i + 1;
  }
   
}
