Modify_All_TP_SL

Author: � Tecciztecatl 2016
Modify_All_TP_SL
Orders Execution
Checks for the total of open ordersIt can change open orders parameters, due to possible stepping strategy
0 Views
0 Downloads
0 Favorites
Modify_All_TP_SL
//+------------------------------------------------------------------+
//|                                             Modify_All_TP_SL.mq4 |
//|                                                  © Tecciztecatl  |
//+------------------------------------------------------------------+
#property copyright     "© Tecciztecatl 2016"
#property link          "https://www.mql5.com/en/users/tecciztecatl"
#property version       "1.00"
#property description   "The script modifies all orders (market and pending) on the symbol Take Profit and Stop Loss."
#property script_show_inputs
#property strict

input double TakeProfit=0; //Take Profit (price)
input double StopLoss=0;   //Stop Loss (price)

double tp,sl;
int i,i2,Ticket;
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
   if(OrdersTotal()>0)
     {
      for(i=OrdersTotal()-1; i>=0; i--)
        {
         i2=false; while(i2==false && !IsStopped())i2=OrderSelect(i,SELECT_BY_POS,MODE_TRADES);

         tp=NormalizeDouble(TakeProfit,_Digits);
         sl=NormalizeDouble(StopLoss,_Digits);

         if(OrderSymbol()==Symbol())
           {
            Ticket=OrderTicket();
            if(tp<=0) tp=OrderTakeProfit();
            if(sl<=0) sl=OrderStopLoss();
            if(OrderType()==OP_SELL || OrderType()==OP_SELLLIMIT || OrderType()==OP_SELLSTOP)
              {
               if(tp>OrderOpenPrice()) tp=OrderTakeProfit();
               if(sl<OrderOpenPrice()) sl=OrderStopLoss();
               Ord_Modify();
              }
            if(OrderType()==OP_BUY || OrderType()==OP_BUYLIMIT || OrderType()==OP_BUYSTOP)
              {
               if(tp<OrderOpenPrice()) tp=OrderTakeProfit();
               if(sl>OrderOpenPrice()) sl=OrderStopLoss();
               Ord_Modify();
              }
           }
        }
     }
   return;
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void Ord_Modify()
  {
   ResetLastError();
   if(OrderCloseTime()==0)
     {
      i2=false;
      while(!i2 && !IsStopped())
        {
         i2=OrderModify(Ticket,OrderOpenPrice(),sl,tp,0,clrNONE);
         if(i2==false)
           {
            Print("Error modifying orders, error # "+IntegerToString(GetLastError()));
            while(!RefreshRates() && !IsStopped())
               Sleep(100);
           }
        }
     }
  }
//+------------------------------------------------------------------+

Comments