//+------------------------------------------------------------------+
//|                                                     G.Daylee.mq4 |
//|                                                           Glebov |
//|                                                             2015 |
//+------------------------------------------------------------------+
#property copyright "Glebov"
#property link      "2015"
#property version   "1.00"
#property strict

int tic, step = 30, mag = 0, trel_stop = 70, trel_step = 40, ocnt;
double lot = 0.01, sl, min, max;
datetime lasttradetame;
bool new_bar = false;


//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
bool rabota = false;
void OnTick()
  {
     static datetime dd;
     if (dd != iTime(NULL,PERIOD_D1,0)) //новый бар!
     {
        dd  = iTime(NULL,PERIOD_D1,0);
        max = iHigh(NULL,PERIOD_D1,1);
        min = iLow (NULL,PERIOD_D1,1);
        rabota = true;
     }
     if(rabota == true && TimeHour(TimeCurrent()) >= 23 && TimeMinute(TimeCurrent()) >= 55) 
     {
        rabota = false; close();
     }
     
     if (!rabota) return;
     ocnt = c_tr();
     if(ocnt <= 0 && Ask > max + step*_Point)
     {
        tic = OrderSend(NULL, OP_BUY,  lot, Ask, 50, 0, 0, NULL, mag, 0, Blue);
     }
     else
     if(ocnt <= 0 && Bid < min - step*_Point)
     {
        tic = OrderSend(NULL, OP_SELL, lot, Bid, 50, 0, 0, NULL, mag, 0, Red);
     }
}
//+------------------------------------------------------------------+
void close()
{
   for(int i = OrdersTotal()-1; i>=0; i--)
   {
      if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
      {
         if(OrderMagicNumber() == mag) 
         {
            if(OrderType() == OP_SELL) bool oc = OrderClose(OrderTicket(), OrderLots(), Ask, 50, clrNONE);
            else if(OrderType() == OP_BUY) bool oc = OrderClose(OrderTicket(), OrderLots(), Bid, 50, clrNONE);
         }
      }
   }
}
//+------------------------------------------------------------------+
int c_tr()
{
   int c = 0;
   for(int i = 0; i<OrdersTotal(); i++)
   {
      if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
      {
         if(OrderMagicNumber() == mag) c ++;
      }
   }
   return(c); 
}
