modify
Download scriptThis script is designed to automate the security of your active trading positions by automatically attaching a "Stop Loss" to your first open market order. It serves as a safety net, ensuring that if the market moves against you, your trade will close at a predetermined point to limit potential losses.
How the Logic Works
1. Scanning for Active Trades When the script is executed, it begins by scanning all of your currently active trades in the market. It looks specifically for "Buy" or "Sell" orders, ignoring any other types of pending transactions.
2. Identifying the Target The script is programmed to focus on the first eligible trade it encounters in your list. Once it identifies a Buy or Sell position, it performs the following actions based on the direction of that trade:
- For Buy Orders: It calculates a safety exit point slightly below the current market price (specifically 20 points below the current Bid).
- For Sell Orders: It calculates a safety exit point slightly above the current market price (specifically 20 points above the current Ask).
3. Automated Modification Once the script determines the correct price for the Stop Loss, it attempts to update your open trade instantly. To ensure this happens successfully in a fast-moving market, the script includes a "refresh" loop. If the market moves so quickly that the price changes during the modification process, the script will automatically grab the newest price and try again until the safety limit is successfully applied.
4. Completion After the first valid trade is updated with its new Stop Loss, the script stops. It provides a confirmation message to your terminal to let you know the action was completed or to notify you if an error occurred during the process.
//+------------------------------------------------------------------+
//| 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;
//----
total=OrdersTotal();
point=MarketInfo(Symbol(),MODE_POINT);
//----
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==OP_BUY || cmd==OP_SELL)
{
//---- modify first market order
while(true)
{
if(cmd==OP_BUY) stop_loss=Bid-20*point;
else stop_loss=Ask+20*point;
result=OrderModify(OrderTicket(),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)
OrderPrint();
break;
}
}
else { Print( "Error when order select ", GetLastError()); break; }
}
//----
return(0);
}
//+------------------------------------------------------------------+
Comments