//+------------------------------------------------------------------+
//|                                                        order.mq4 |
//|                                                           GumRai |
//|                                                             none |
//+------------------------------------------------------------------+
#property copyright "GumRai"
#property link      "none"
#property version   "1.00"
#property strict
#property script_show_inputs
//--- input parameters
input double SL_in_Currency=10;
input double TP_in_Currency=10;
input double BE_in_Currency=5;
input double BE_Trigger_Currency=2;
input bool   Chart_Trades_Only=true;

#include <stdlib.mqh>
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnTick()
  {
//---

   for(int x=OrdersTotal()-1;x>=0;x--)
     {
      if(!OrderSelect(x,SELECT_BY_POS,MODE_TRADES))
        {
         Print("Error with OrderSelect-",ErrorDescription(GetLastError()));
         continue;
        }
      if(!(OrderType()==OP_BUY || OrderType()==OP_SELL))
         continue;
      if(Chart_Trades_Only && OrderSymbol()!=Symbol())
         continue;
      double SiC=0;
      string ordrsym = OrderSymbol();
      double priceBid = MarketInfo(ordrsym,MODE_BID);
      double priceAsk = MarketInfo(ordrsym,MODE_ASK);
      if(BE_in_Currency<=0||((OrderType()==OP_BUY&&priceBid<OrderOpenPrice())||(OrderType()==OP_SELL&&priceAsk>OrderOpenPrice()))||OrderStopLoss()==0)
       SiC=SL_in_Currency;
       /*
       */
      else
      if(BE_in_Currency>0&&OrderType()==OP_BUY&&priceBid>OrderOpenPrice()&&OrderProfit()>BE_in_Currency+BE_Trigger_Currency)
       SiC=-BE_in_Currency;
      else
      if(BE_in_Currency>0&&OrderType()==OP_SELL&&priceAsk<OrderOpenPrice()&&OrderProfit()>BE_in_Currency+BE_Trigger_Currency)
       SiC=-BE_in_Currency;
      double tick_size=SymbolInfoDouble(OrderSymbol(),SYMBOL_TRADE_TICK_SIZE);
      double pnt=SymbolInfoDouble(OrderSymbol(),SYMBOL_POINT);
      double minfromcurrentprice=SymbolInfoInteger(OrderSymbol(),SYMBOL_TRADE_STOPS_LEVEL)*pnt;
      double loss_for_1_lot=SymbolInfoDouble(OrderSymbol(),SYMBOL_TRADE_TICK_VALUE);  //Loss/Gain for a 1 tick move with 1 lot
      double loss_for_lot_size_this_trade=loss_for_1_lot*OrderLots();    //Loss/Gain for a 1 tick move with trade lot size
      double SL_ticks=SiC/loss_for_lot_size_this_trade;
      double SL_points=MathFloor(SL_ticks*tick_size/pnt)*pnt;
      double TP_ticks=TP_in_Currency/loss_for_lot_size_this_trade;
      double TP_points=MathFloor(TP_ticks*tick_size/pnt)*pnt;

      if(OrderType()==OP_SELL)
        {
         SL_points= (-SL_points);
         TP_points= (-TP_points);
        }

      double new_sl=NormalizeDouble(OrderOpenPrice()-SL_points,_Digits);
      if(MathAbs(new_sl-OrderOpenPrice()) -minfromcurrentprice<pnt)
        {
         // Alert(OrderSymbol()," Order ",OrderTicket()," SL too close to current price");
         new_sl=OrderStopLoss();
        }
      double new_tp=NormalizeDouble(OrderOpenPrice()+TP_points,_Digits);
      if(MathAbs(new_tp-OrderOpenPrice()) -minfromcurrentprice<pnt)
        {
         // Alert(OrderSymbol()," Order ",OrderTicket()," TP too close to current price");
         new_tp=OrderTakeProfit();
        }
      /**/
      if(OrderStopLoss()!=0&&((OrderType()==OP_BUY&&OrderStopLoss()>OrderOpenPrice())||
         (OrderType()==OP_SELL&&OrderStopLoss()<OrderOpenPrice())))
       return;
      if((MathAbs(new_sl-OrderStopLoss())>=pnt || MathAbs(new_tp-OrderTakeProfit())>=pnt))
       {
       if(new_sl!=OrderStopLoss()||new_tp!=OrderTakeProfit())
        {
         if(OrderModify(OrderTicket(),OrderOpenPrice(),new_sl,new_tp,0,0))
          {
         Print("Order Modified success!");
          }
         else
          {
         Print("Order Modify failed!", GetLastError());
         Sleep(500);
          }
         } 
       }
     }
  }
//+------------------------------------------------------------------+
