modify_stop_loss
Download scriptThis script is designed to automate the adjustment of specific open trades on your trading platform. It allows you to quickly modify the "Stop Loss" levels for a group of positions based on their original opening price, ensuring they align with a predefined distance in pips.
Core Functionality
The script works by scanning through all your currently active market orders. It filters these orders based on two primary criteria:
- Trade Direction: You can choose whether the script should only affect "Buy" positions or "Sell" positions.
- Currency Pair: The script identifies and only modifies trades that match the specific symbol (currency pair) you are currently viewing.
How the Calculation Works
Once the script identifies the correct trades, it calculates a new price level for the Stop Loss based on your input:
- For Buy Positions: It takes the price at which you entered the trade and subtracts your chosen number of pips. This sets a safety net below your entry point.
- For Sell Positions: It takes the price at which you entered the trade and adds your chosen number of pips. This sets a safety net above your entry point.
Execution Logic
To ensure the script operates reliably, it follows these internal steps:
- Automated Identification: It automatically detects how many trades are open and loops through each one individually to check if it meets your criteria (Type and Symbol).
- Persistent Updating: The script includes a "retry" mechanism. If it encounters a minor market fluctuation while trying to update the price, it refreshes the data and attempts the modification again until the change is successfully confirmed by the server.
- Preservation of Profit Targets: While the script moves your Stop Loss, it is programmed to leave your existing "Take Profit" levels untouched, ensuring your intended exit strategy remains intact.
User Impact
Essentially, this tool acts as a "bulk editor." Instead of manually clicking into every individual trade to set your risk parameters, you can run this script once to instantly synchronize the Stop Loss levels for all relevant positions to a specific distance from your entry price.
//+------------------------------------------------------------------+
//| modify_profit_target.mq4 |
//| Copyright © 2004, MetaQuotes Software Corp. |
//| http://www.metaquotes.net/ |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2004, MetaQuotes Software Corp."
#property link "http://www.metaquotes.net/"
#property show_inputs
extern string Trade_Type = "Close Trade Type (0=Buying 1=Selling)";
extern int trade_type_selection = OP_BUY; //default is to close buy trades
extern string New_Target = "New target (in pips - relative to open price)";
extern int new_stop_loss = 20;
//#include <WinUser32.mqh>
//+------------------------------------------------------------------+
//| script "modify first market order" |
//+------------------------------------------------------------------+
int start()
{
bool result;
double profit_target, stop_loss;
int cmd,total,error;
double gd_point;
//----
total=OrdersTotal();
gd_point = SetPoint();
//----
for(int i=0; i<total; i++)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
{
//---- print selected order
OrderPrint();
cmd=OrderType();
//---- buy or sell orders are considered
if((cmd==trade_type_selection)&& (OrderSymbol()==Symbol()))
{
//---- modify market order
while(true)
{
if(cmd==OP_BUY) stop_loss=OrderOpenPrice()-new_stop_loss*gd_point;
else stop_loss=OrderOpenPrice()+new_stop_loss*gd_point;
profit_target = OrderTakeProfit(); //save current profit targert
result=OrderModify(OrderTicket(),0,stop_loss,OrderTakeProfit(),0,CLR_NONE);
if(result!=TRUE) { error=GetLastError(); Print("LastError = ",error); }
else error=0;
if(error==135) RefreshRates();
else break;
}
//---- print modified order (it still selected after modify)
OrderPrint();
//break;
}
}
else { Print( "Error when order select ", GetLastError()); break; }
}
//----
return(0);
}
//+------------------------------------------------------------------+
double SetPoint() {
double ld_point;
if (Digits < 4) ld_point = 0.01;
else ld_point = 0.0001;
return (ld_point);
}
Comments