//+------------------------------------------------------------------+
//|                                                                  |
//|                                                                  |
//+------------------------------------------------------------------+
//Tony's Two Candle trading system 
//http://www.forexfactory.com/showthread.php?t=172963

#property copyright ""
#property link      ""



//--

input int                            MagicMumber           = 917382;         
input ENUM_TIMEFRAMES                TimeFrame             = PERIOD_H1;
input double                         Lots                  = 0.01;
input int                            entrylvlPoints        = 40;
input int                            SLlvlPoints           = 150;
input int                            TPlvlPoints           = 250;

//+------------------------------------------------------------------+
void ZeroPrevent()
 { 
      string AcctCurrency = AccountCurrency();
       
      if(AcctCurrency == ""||AccountInfoInteger(ACCOUNT_LOGIN)<=0)
          return; 
 }
//+------------------------------------------------------------------+
int init()
{
                  ZeroPrevent();
   return(0);
}
int deinit()
{
   return(0);
}

//+------------------------------------------------------------------+
int start()
{   
                  ZeroPrevent();
                  
   double cl =iClose(NULL,TimeFrame,0);
   double op =iOpen (NULL,TimeFrame,0);

   double cl1 =iClose(NULL,TimeFrame,1);
   double op1 =iOpen (NULL,TimeFrame,1);
   double cl2 =iClose(NULL,TimeFrame,2);
   double op2 =iOpen (NULL,TimeFrame,2);


   double hi =iHigh (NULL,TimeFrame,0);
   double lo =iLow  (NULL,TimeFrame,0);
   double hi1 =iHigh(NULL,TimeFrame,1);
   double lo1 =iLow (NULL,TimeFrame,1);
   
   if ((hi - op)>entrylvlPoints*Point && cl > op && cl1 > op1 && cl2 < op2)
      {
      AlertOnce("Buy",0, TimeFrame);
      } 
      
   if ((lo-op)<-entrylvlPoints*Point && cl < op && cl1 < op1 && cl2 > op2)
      {
      AlertOnce("Sell",0, TimeFrame);
      }
   return(0);
}
//+------------------------------------------------------------------+
int AlertOnce(string What, int ref, int tf)
{  
int i = 0;
int res = 0;


  static int LastAlert[1000];
   
  while (i < 1000)
{
 if (ref == i)
  {
           if( LastAlert[i] == 0 || LastAlert[i] < iBars(NULL,tf) )
        {
      if(What=="Buy")
       {
      if(OrderSend(_Symbol,
                   OP_BUY,
                   Lots,
                   Ask,
                   0,
                   NormalizeDouble(Ask-SLlvlPoints*_Point,_Digits),
                   NormalizeDouble(Bid+TPlvlPoints*_Point,_Digits),
                   NULL,
                   MagicMumber,
                   0,
                   clrNONE)>0)
        {
       Print("Order Buy sucess!");
        }
       else
        {
       Print("Order Buy failed! Error:",GetLastError());
        }
        }
        
      if(What=="Sell")
       {
      if(OrderSend(_Symbol,
                   OP_SELL,
                   Lots,
                   Bid,
                   0,
                   NormalizeDouble(Bid+SLlvlPoints*_Point,_Digits),
                   NormalizeDouble(Ask-TPlvlPoints*_Point,_Digits),
                   NULL,
                   MagicMumber,
                   0,
                   clrNONE)>0)
        {
       Print("Order Sell sucess!");
        }
       else
        {
       Print("Order Sell failed! Error:",GetLastError());
        }
        }
           LastAlert[i] = iBars(NULL,tf);
           res=1;
        }
      break;
 }
   
 i = i + 1;
  }
   
           return (res);
}
//+------------------------------------------------------------------+
