This script is designed as an automated utility for MetaTrader users to manage open "Sell" orders efficiently. Its primary purpose is to automatically adjust the Stop Loss (the price at which a trade closes to prevent further loss) and the Take Profit (the target price at which a trade closes to lock in gains) across multiple positions simultaneously.
How the Logic Works
1. Environment Detection The script begins by identifying the specific rules of your trading broker. It automatically detects if your broker uses "fractional" pricing (common in 3-digit or 5-digit currency pairs). This ensures that when you input a value in "pips," the tool calculates the correct price movement accurately based on your account's decimal configuration.
2. Order Filtering Rather than affecting every open trade, the script scans your active portfolio to identify only two specific criteria:
- Trades associated with the current currency pair being viewed.
- Positions that are "Sell" orders.
3. Dynamic Price Calculation For every eligible Sell order found, the script determines the new target prices based on three different methods of input:
- Distance-based: If you provide a number of pips, it calculates the price relative to the current market asking price.
- Price-based: If you provide a specific price value, it uses that exact figure.
- Default behavior: If no new values are provided, it keeps the existing settings from the original order.
4. Safety & Compliance Checks Before applying any changes, the script performs a "safety check" against the broker's minimum distance rules (Stop Levels). This prevents the trade from being rejected by the server if your proposed Stop Loss or Take Profit is too close to the current market price. If a requested change violates these rules, the tool will trigger an alert notification to inform you of the error.
5. Execution Once all parameters are validated, the script updates the orders in your account. It continuously attempts to push these updates to the server until they are successfully confirmed, ensuring that your risk management levels are applied correctly across all qualifying trades at once.
//+------------------------------------------------------------------+
//| Copyright © 2011, Matus German, matusgerman@gmail.com |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2012 Matus German www.MTexperts.net"
#property show_inputs
#include <WinUser32.mqh>
extern string descr = "*** Modify SELL orders ***";
extern double SLpip = 0;
extern double TPpip = 0;
extern double SLprice = 0;
extern double TPprice = 0;
double pips2dbl, pips2point, minDistance,
slPoints, tpPoints,
itotal,
sl, tp;
// script modifies stop loss and take profit
int start()
{
minDistance=MarketInfo(Symbol(),MODE_STOPLEVEL)*Point;
if (Digits == 5 || Digits == 3) // Adjust for five (5) digit brokers.
{
pips2dbl = Point*10; pips2point = 10;
}
else
{
pips2dbl = Point; pips2point = 1;
}
slPoints=SLpip*pips2dbl;
tpPoints=TPpip*pips2dbl;
itotal=OrdersTotal();
for(int icnt=itotal-1;icnt>=0;icnt--)
{
OrderSelect(icnt, SELECT_BY_POS, MODE_TRADES);
if (OrderSymbol()==Symbol() && OrderType()==OP_SELL)
{
RefreshRates();
sl=0; tp=0;
if(slPoints>0)
sl=Ask+slPoints;
else if(SLprice>0)
sl=SLprice;
else
sl=OrderStopLoss();
if(tpPoints>0)
tp=Ask-tpPoints;
else if(TPprice>0)
tp=TPprice;
else
tp=OrderTakeProfit();
if(sl-Ask<=MarketInfo(Symbol(),MODE_STOPLEVEL)*Point && sl>0) // check broker stop levels
{
Alert("Stop Loss is too close to market price or on wrong side!!!");
return(0);
}
if(Ask-tp<=MarketInfo(Symbol(),MODE_STOPLEVEL)*Point && tp>0)
{
Alert("Take Profit is too close to market price or on wrong side!!!");
return(0);
}
if(OrderTakeProfit()!=tp || OrderStopLoss()!=sl)
{
while(OrderModify(OrderTicket(),OrderOpenPrice(),sl,tp,0,Green)==false)
{
if(sl-Ask<=MarketInfo(Symbol(),MODE_STOPLEVEL)*Point && sl>0) // check broker stop levels
{
Alert("Stop Loss is too close to market price or on wrong side!!!");
return(0);
}
if(Ask-tp<=MarketInfo(Symbol(),MODE_STOPLEVEL)*Point && tp>0)
{
Alert("Take Profit is too close to market price or on wrong side!!!");
return(0);
}
}
}
}
}
return(0);
}
//+------------------------------------------------------------------+
Comments