Modify_v2
Download scriptThis script is designed to automate a specific safety adjustment for active market orders within the MetaTrader platform. Its primary function is to identify open positions and automatically attach a "Stop Loss" safety net to the first valid trade it encounters.
Operational Workflow
1. Scanning for Active Trades When the script is executed, it begins by scanning through all currently open trades on your account. It looks specifically for standard market orders—specifically "Buy" or "Sell" positions—while ignoring other types of transactions (such as pending orders).
2. Dynamic Stop Loss Calculation Once the script identifies a valid trade, it determines the appropriate safety distance based on the current real-time price of the asset:
- For Buy Positions: It calculates a protection level slightly below the current market price.
- For Sell Positions: It calculates a protection level slightly above the current market price. The script uses a consistent buffer (a set number of "points" or minimum price movements) to ensure the stop loss is not placed too close to the entry price, providing a small margin for market volatility.
3. Automated Execution and Error Handling The script then attempts to update the order with these new parameters. Because market prices move rapidly, the script includes a "refresh" mechanism: if the trade cannot be updated because the price changed during the millisecond the command was sent, the script will automatically refresh the data and try again until the protection is successfully applied.
4. Completion and Reporting After the modification is successful, the script provides a status update to the platform's log, confirming the details of the trade before it finishes its task. The process stops as soon as the first applicable market order has been updated.
//+------------------------------------------------------------------+
//| modify.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_confirm
//+------------------------------------------------------------------+
//| script "modify first market order" |
//+------------------------------------------------------------------+
int start()
{
bool result;
double stop_loss,point;
int cmd,total,nTicket;
//----
total=OrdersTotal();
point=MarketInfo(Symbol(),MODE_POINT);
//----
for(int i=0; i<total; i++)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
{
cmd=OrderType();
//---- buy or sell orders are considered
if(cmd==OP_BUY || cmd==OP_SELL)
{
//---- print selected order
OrderPrint();
//---- modify first market order
while(true)
{
if(cmd==OP_BUY) stop_loss=Bid-20*point;
else stop_loss=Ask+20*point;
nTicket=OrderTicket();
result=OrderModify(nTicket,0,stop_loss,0,0,CLR_NONE);
if(result!=TRUE) Print("LastError = ", GetLastError());
if(result==135) RefreshRates();
else break;
}
//---- print modified order (it still selected after modify)
if(OrderSelect(nTicket,SELECT_BY_TICKET)) OrderPrint();
break;
}
}
else { Print( "Error when order select ", GetLastError()); break; }
}
//----
return(0);
}
//+------------------------------------------------------------------+
Comments