//+------------------------------------------------------------------+
//|                                           cosztrailingstopem.mq4 |
//|                        Copyright 2018, MetaQuotes Software Corp. |
//|                                             
https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2018, MetaQuotes Software Corp."
#property link      "
https://www.mql5.com"
#property version   "1.00"
#property strict
extern int MagicNumber = 16701;
extern int Slippage = 3;
extern double LotSize = 0.05;
int counter = 0;
extern int distance_between_HighLow_and_Stoploss = 0; // mozna tutaj przesunac stop loss w pipsach od jakiegos poziomu 10 = 1 pips
extern int SL_POINT = 35;
extern int TP_POINT =39;
int startTS = 20;
int TS = 10;
int step = 20;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
      MqlTick lastTick;
      if (Hour() == 14 && Minute() == 01) {
         // open order
         if(SymbolInfoTick(Symbol(),lastTick)) {
            //Print( lastTick.bid );
            if (lastTick.bid > iOpen(Symbol(), PERIOD_H1, 3)) {
               sendBuyOrder();
            } else {
               sendSellOrder();
            }
         }
         
      }
      if (Hour() == 23 && Minute() == 57) {
         // close order
         manuallyCloseOrder();
      }
      
         for(int i=OrdersTotal(); i>=0; i--) {
      if(OrderSelect(i, SELECT_BY_POS)==true) {
         if (OrderType() == 0) {
            double n = NormalizeDouble((Ask - OrderOpenPrice()) * getSymbolScale(), 2);  
            //Print( n );
            double m = NormalizeDouble((Ask - OrderStopLoss()) * getSymbolScale(), 2); 
            //Print( " m " + m ); 
            if (NormalizeDouble(((Bid - OrderOpenPrice()) * getSymbolScale()), 2) > startTS) {
               if (m > step) {
                  double new_sl = NormalizeDouble(OrderStopLoss()+(MarketInfo(Symbol(),MODE_STOPLEVEL)+TS*10)*Point,Digits);
                  OrderModify(OrderTicket(), OrderOpenPrice(), new_sl, OrderTakeProfit(), 0, clrNONE);
               }
            } 
         } else if (OrderType() == 1) {
            double n = NormalizeDouble((OrderOpenPrice() - Ask) * getSymbolScale(), 2); 
            //Print( n );
            double m = NormalizeDouble((OrderStopLoss() - Ask) * getSymbolScale(), 2); 
            //Print( " m " + m );
            if (NormalizeDouble(((OrderOpenPrice() - Ask) * getSymbolScale()), 2) > startTS) {
               if (m > step) {
                  double new_sl = NormalizeDouble(OrderStopLoss()-(MarketInfo(Symbol(),MODE_STOPLEVEL)+TS*10)*Point,Digits); 
                  OrderModify(OrderTicket(), OrderOpenPrice(), new_sl, OrderTakeProfit(), 0, clrNONE);
               }
            }
         }         
      }
   }
}
   
      //Print( iOpen(Symbol(), PERIOD_D1, 1) );
      //Print( iLow(Symbol(), PERIOD_D1, 1) );
      //Print( iHigh(Symbol(), PERIOD_D1, 1) );
      
   
//+------------------------------------------------------------------+
//| ChartEvent function                                              |
//+------------------------------------------------------------------+
void OnChartEvent(const int id, const long &lparam, const double &dparam, const string &sparam) {}
//+------------------------------------------------------------------+
void sendBuyOrder() {
   if (OrdersTotal() > 0) return;
   double minstoplevel_SL = MarketInfo(Symbol(),MODE_STOPLEVEL)+SL_POINT*10;
   double minstoplevel_TP = MarketInfo(Symbol(),MODE_STOPLEVEL)+TP_POINT*10;
   double stoploss = NormalizeDouble(Bid-minstoplevel_SL*Point,Digits);
   double takeprofit = NormalizeDouble(Bid+minstoplevel_TP*Point,Digits);
   OrderSend(Symbol(), OP_BUY, LotSize, Ask, Slippage, stoploss, takeprofit,"Buy[" + counter +"]", MagicNumber, 0, clrGreen);
   counter++;
}
void sendSellOrder() {
   if (OrdersTotal() > 0) return;
   double minstoplevel_SL = MarketInfo(Symbol(),MODE_STOPLEVEL)+SL_POINT*10;
   double minstoplevel_TP = MarketInfo(Symbol(),MODE_STOPLEVEL)+TP_POINT*10;
   double stoploss = NormalizeDouble(Ask+minstoplevel_SL*Point,Digits);
   double takeprofit = NormalizeDouble(Ask-minstoplevel_TP*Point,Digits);
   OrderSend(Symbol(), OP_SELL, LotSize, Bid, Slippage, stoploss, takeprofit,"Sell[" + counter +"]", MagicNumber, 0, clrRed);
   counter++;
}
void manuallyCloseOrder() {
   for(int i=OrdersTotal(); i>=0; i--) {
      if(OrderSelect(i, SELECT_BY_POS)==true) {
         if (OrderType() == 0) {
            OrderClose(OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 0, clrNONE);
         } else if (OrderType() == 1) {
            OrderClose(OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), 0, clrNONE);
         }
      }
   }
}
double getSymbolScale() {
   double res = 0;
   if (Point == 0.00001) { // EURUSD, GBPUSD etc
      res = 10000; // ok
   } else if (Point == 0.001) { // USDJPPY etc
      res = 1000; // 
   } else if (Point == 0.01) { // DAX
      res = 100; // 
   }
   return res;
}