Modify_All_TP_SL
Download scriptThis tool is designed to streamline your trading workflow by allowing you to update the Take Profit and Stop Loss levels for all active positions and pending orders on a specific currency pair or asset simultaneously. Instead of manually adjusting each individual trade one by one, this script automates the process to ensure consistency across your entire portfolio.
How it Works
The logic follows a systematic approach to ensure your risk management settings are applied correctly without disrupting your current trades:
1. Identifying Target Orders When you run the script, it first scans all currently open trades in your account. It specifically looks for orders that belong to the symbol (the specific currency pair or asset) you are currently viewing. This ensures that the script doesn't accidentally modify trades on other assets you might be monitoring.
2. Input Recognition and Defaults The script looks at the price levels you have entered as inputs:
- Take Profit: Your desired target price to close a trade for profit.
- Stop Loss: Your safety price to exit a trade to prevent further losses.
If you leave one of these values empty (set to zero), the script is intelligent enough to keep the existing value from that specific order rather than overwriting it with a blank entry.
3. Smart Price Validation To prevent "invalid order" errors—which often happen if a stop loss or take profit is placed at a price that is technically impossible for the market to execute—the script performs an automated check:
- For Buy Orders: It ensures your Take Profit is higher than the entry price and your Stop Loss is lower. If the numbers you entered are mathematically backward, the script reverts to the original values to maintain the integrity of the trade.
- For Sell Orders: It ensures your Take Profit is lower than the entry price and your Stop Loss is higher.
4. Execution and Error Handling Once the script confirms that the prices are valid, it communicates with the broker's server to update the orders. It includes a built-in "retry" mechanism: if the connection flickers or the server is momentarily busy, the script will automatically refresh the data and attempt to apply the modification again until it succeeds or the operation is finished.
Key Benefits for the User
- Efficiency: Quickly synchronize your risk management across multiple positions in a single click.
- Safety: Prevents manual entry errors by validating that your profit targets and stop losses are on the correct sides of the entry price.
- Precision: Automatically rounds the prices to the correct number of decimal places required by your specific broker.
//+------------------------------------------------------------------+
//| 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