This script is designed to automate the management of active "Buy" orders within your trading platform. Its primary purpose is to allow you to quickly update or set your Stop Loss and Take Profit levels across multiple positions simultaneously, ensuring your risk management remains consistent.
How the Logic Works
1. Market Awareness and Compatibility Before performing any actions, the script automatically detects the specific pricing structure of your broker (identifying whether the currency pair uses 3-digit or 5-digit pricing). This ensures that when you enter a distance in "pips," the software translates that correctly into the exact numerical value required by the broker.
2. Order Identification The script scans through all currently open trades in your account. It specifically filters these to identify only two things:
- Trades that belong to the specific currency pair you are currently viewing.
- Trades that are "Buy" orders.
3. Calculation Logic For every qualifying Buy order found, the script determines the new target prices based on three prioritized rules:
- Relative Distance: If you provide a number of pips, it calculates the price by adding or subtracting that amount from the current market price.
- Specific Price: If you provide an exact price level instead of a pip distance, it uses that specific numerical value.
- Current Status: If no new instructions are provided, it maintains the existing Stop Loss and Take Profit levels currently on the trade.
4. Safety and Validation Checks To prevent errors that could cause your trade to be rejected by the broker, the script performs a "safety check" before applying any changes. It verifies that the proposed Stop Loss or Take Profit is not too close to the current market price (violating the broker's minimum distance rules) and is positioned on the correct side of the trade.
5. Execution If the new prices pass all safety checks, the script automatically modifies the open orders. If the modification fails for any reason (such as a sudden shift in market price), the script will continuously refresh the data and attempt to apply the update again until it succeeds or encounters a permanent restriction. If a move is impossible due to broker rules, it will provide an alert notification to inform you of the conflict.
//+------------------------------------------------------------------+
//| Copyright © 2011, Matus German, matusgerman@gmail.com |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2012 Matus German www.MTexperts.net"
#property show_inputs
extern string descr = "*** Modify BUY 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_BUY)
{
RefreshRates();
sl=0; tp=0;
if(slPoints>0)
sl=Bid-slPoints;
else if(SLprice>0)
sl=SLprice;
else
sl=OrderStopLoss();
if(tpPoints>0)
tp=Bid+tpPoints;
else if(TPprice>0)
tp=TPprice;
else
tp=OrderTakeProfit();
if(Bid-sl<=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(tp-Bid<=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)
{
RefreshRates();
if(Bid-sl<=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(tp-Bid<=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