//+------------------------------------------------------------------+
//|                                                     day open.mq4 |
//|                        Copyright 2014, MetaQuotes Software Corp. |
//|                                              http://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2014, MetaQuotes Software Corp."
#property link      "http://www.mql5.com"
#property version   "1.00"
#property strict
extern double LOT=0.1;
extern int Profit=500;
extern int Loss=200;
extern int Op_gap=200;
int bars;
int Barcurrentopen;
bool trade;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
trade=false;
bars=iBars(Symbol(),PERIOD_D1);
Barcurrentopen=(iOpen(Symbol(),PERIOD_D1),bars);
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
 
if (bars!=iBars(Symbol(),PERIOD_D1)){bars=iBars(Symbol(),PERIOD_D1);trade=true;}

if (trade){
            OrderSend(Symbol(),OP_BUYSTOP,LOT,(Ask<=Barcurrentopen+Op_gap*Point),2,0,0,NULL,0123456,0,clrRed);
            OrderSend(Symbol(),OP_SELLSTOP,LOT,(Bid>=Barcurrentopen+Op_gap*Point),2,0,0,NULL,0123456,0,clrRed);
            trade=false;
          }
   
  int tot=OrdersTotal();
  
  for(int i=0; i<=tot;i++)
  {OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
   if (OrderSymbol()==Symbol() &&OrderMagicNumber()==0123456)
      {
       if(OrderType()==OP_BUY){if (Bid>= OrderOpenPrice()+Profit*Point)OrderClose(OrderTicket(),LOT,Bid,2,clrAqua);}
       if(OrderType()==OP_BUY){if (Bid<= OrderOpenPrice()-Loss*Point)OrderClose(OrderTicket(),LOT,Bid,2,clrAqua);}
       
      
       if(OrderType()==OP_SELL){if (Ask<= OrderOpenPrice()-Profit*Point)OrderClose(OrderTicket(),LOT,Ask,2,clrAqua);}
       if(OrderType()==OP_SELL){if (Ask>= OrderOpenPrice()+Loss*Point)OrderClose(OrderTicket(),LOT,Ask,2,clrAqua);}
      }
  
  }
  
  }
//+------------------------------------------------------------------+
