Scripts: Modify SL TP

 

Modify SL TP:

This script is used to modify Stop Loss and Take Profit.

Modify SL TP

Author: Ziheng Zhuang

 

Any one of StopLoss and TakeProfit,if you do not want to change it,just set it to 0.

Let's say,If you do not want to change the stop loss price of positions,just set StopLoss Pips to 0.

Also,if you do not want to change the take profit price of positions,  just set TakeProfit Pips to 0.

 
Ziheng Zhuang:

Any one of StopLoss and TakeProfit,if you do not want to change it,just set it to 0.

Let's say,If you do not want to change the stop loss price of positions,just set StopLoss Pips to 0.

Also,if you do not want to change the take profit price of positions,  just set TakeProfit Pips to 0.


the main problem for this script when you make any change in take profit will change all the positions fall the symbols in same time

if you have EURUSD, GBPJPY will change all of them the same amount

 
Hany Elkhatib:

the main problem for this script when you make any change in take profit will change all the positions fall the symbols in same time

if you have EURUSD, GBPJPY will change all of them the same amount


This script is used to modify the Stop Loss and Take Profit of the position of the current chart.

all positions of the current chart will be updated with the same SL/TP.

 

Hi Ziheng,

Can you make the entry as Price instead of pips?

Thanks.

 
Suleymann:

Hi Ziheng,

Can you make the entry as Price instead of pips?

Thanks.


//+------------------------------------------------------------------+
//|                                                 Modify SL TP.mq4 |
//|                                          Copyright 2017,fxMeter. |
//|                            https://www.mql5.com/en/users/fxmeter |
//+------------------------------------------------------------------+
#property copyright "Copyright 2017,fxMeter."
#property link      "https://www.mql5.com/en/users/fxmeter"
#property version   "1.00"
#property strict
#property show_inputs
#include <stdlib.mqh>
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
enum ENUM_ORDERTYPE
  {
   BUY=0,
   SELL=1
  };

input ENUM_ORDERTYPE SelectOrderType=BUY;
input double InpStopLoss=0.0; // StopLoss in Price
input double InpTakeProfit=0.0; //TakeProfit in Price
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
//---
   double  TakeProfit=0.0;
   double  StopLoss=0.0;
   int     Slippage=0.0;
   color   clr=clrNONE;

   TakeProfit =InpStopLoss;
   StopLoss   =InpTakeProfit;

   if(OrdersTotal()==0)  return;

   if(!IsTradeAllowed()){Alert("Autotrade is NOT allowed.");  return;}

   if(StopLoss==0 && TakeProfit==0){Alert(" No SL/TP need to be modified");return;}

   double tpbuy = NormalizeDouble(TakeProfit,Digits);
   double slbuy = NormalizeDouble(StopLoss,Digits);

   double tpsell = NormalizeDouble(TakeProfit,Digits);
   double slsell = NormalizeDouble(StopLoss,Digits);

   double sl=0.0,tp=0.0;

   if(StopLoss>0 || TakeProfit>0)
     {
      for(int i=OrdersTotal()-1;i>=0;i--)
        {
         if(!OrderSelect(i,SELECT_BY_POS,MODE_TRADES))break;
         if(OrderSymbol()==Symbol() && OrderType()<2)
           {
            if(OrderType()==0 && SelectOrderType==BUY)
              {
               if(StopLoss  !=0)sl = slbuy;else sl =OrderStopLoss();   sl=NormalizeDouble(sl,Digits);
               if(TakeProfit!=0)tp = tpbuy;else tp=OrderTakeProfit(); tp=NormalizeDouble(tp,Digits); clr=clrBlue;
               if(!OrderModify(OrderTicket(),OrderOpenPrice(),sl,tp,0,clr))
                  Print("OrderModify Error: ",ErrorDescription(GetLastError()));
              }
            else if(OrderType()==1 && SelectOrderType==SELL)
              {
               if(StopLoss  !=0) sl=slsell;else sl=OrderStopLoss();    sl=NormalizeDouble(sl,Digits);
               if(TakeProfit!=0) tp=tpsell;else tp=OrderTakeProfit();  tp=NormalizeDouble(tp,Digits);  clr=clrRed;
               if(!OrderModify(OrderTicket(),OrderOpenPrice(),sl,tp,0,clr))
                  Print("OrderModify Error: ",ErrorDescription(GetLastError()));
              }
           }
        }//for
     }

  }
//+------------------------------------------------------------------+
 

Thanks a lot Ziheng.

 
very much thank you mr ziheng
 
Automated-Trading:

Modify SL TP:

Author: Ziheng Zhuang

How can i get this script??
 
This script you shared modify only of the current symbol.

Can you make a script to modify SL and TP from all open and pending orders to all symbols?
 
Natanael Meneses:
This script you shared modify only of the current symbol.

Can you make a script to modify SL and TP from all open and pending orders to all symbols?


It is possible,but the inputs could not be suitable for all symbols.

Let's say, StopLoss = 30 Pips, it is suitable for forex symbols ,but not for CFD.


Modify_SL_TP  has a bug and corrected,please update.
Modify_SL_TP_With Price is newly added
Modify_SL_TP_ALL_Symbol is newly added.
Reason: