//+------------------------------------------------------------------+
//|                                                      ProjectName |
//|                                      Copyright 2018, CompanyName |
//|                                       http://www.companyname.net |
//+------------------------------------------------------------------+
#property copyright "https://www.forexfactory.com/chenxee"
#property strict

#define buy 888
#define sell -888

input double    lot=0.01;

int magic=88888888;

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---

//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---

  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {

//---
   if(IsTradeAllowed()==false)
      return;

   if(Bars<3)
      return;

   MqlDateTime current;
   TimeToStruct(TimeCurrent(),current);

   if(current.sec>3)
     {
      return;
     }

   int c=0;
   int ticket;

   for(int i=0; i<OrdersTotal(); i++)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false)
         break;
      if(OrderSymbol()!=Symbol() || OrderMagicNumber()!=magic)
         continue;

      int type=OrderType();
      if(type==OP_BUY || type==OP_SELL)
         c++;
     }

   if(c>0)
     {
      return;
     }

   int operation=signal();

   double tp=20*Point;
   double sl=50*Point;

   if(operation==buy)
     {
      ticket=OrderSend(Symbol(),OP_BUY,lot,Ask,3,0,Ask+tp,"",magic,0,Blue);
     }
   if(operation==sell)
     {
      ticket=OrderSend(Symbol(),OP_SELL,lot,Bid,3,0,Bid-tp,"",magic,0,Red);
     }

  }
//+------------------------------------------------------------------+

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int signal()
  {

   double l1=High[1]-Low[1];
   double l2=High[2]-Low[2];

   if(l2>0 && l1/l2>=2 && High[1]<High[2] && Low[1]<Low[2])
     {
      double d1=MathAbs(Low[1]-Close[1]);
      double d2=MathAbs(Low[2]-Close[2]);

      if(d2>0 && d1/d2>=2)
        {
         return(buy);
        }
     }


   if(l2>0 && l1/l2>=2 && High[1]>High[2] && Low[1]>Low[2])
     {
      double d1=MathAbs(High[1]-Close[1]);
      double d2=MathAbs(High[2]-Close[2]);

      if(d2>0 && d1/d2>=2)
        {
         return(sell);
        }

     }

   return 0;
  }
//+------------------------------------------------------------------+
