Modify Position

Download script
Author: Copyright � 2004, MetaQuotes Software Corp.
Modify Position

This script is designed to automatically manage and update the profit-taking and risk-mitigation levels for your active market trades. Instead of requiring you to manually adjust every trade after it has been opened, this tool streamlines the process by applying specific price targets based on predefined values.

How the Logic Works

1. Input Retrieval The script begins by looking for two specific pieces of information stored in your global settings: your desired Stop Loss (the point where you want to exit a losing trade) and your Take Profit (the goal price for a winning trade). If these values are not set, the script will notify you and stop, ensuring it doesn't make changes based on missing data.

2. Identifying Active Trades The tool scans all of your currently open positions on the market. It specifically looks for standard "Buy" or "Sell" orders, ignoring other types of pending orders (like limits or stops).

3. Automated Price Calculation Once it identifies an active trade, the script performs a real-time calculation based on the current market price:

  • For Buy Orders: It calculates the exit points relative to the current "Bid" price, placing the Stop Loss below the market price and the Take Profit above it.
  • For Sell Orders: It calculates the exit points relative to the current "Ask" price, placing the Stop Loss above the market price and the Take Profit below it.

4. Execution and Reliability The script attempts to apply these new levels to your trade immediately. A key feature of this logic is its persistence: if the market price moves rapidly while the script is trying to update the order (which can sometimes cause a temporary technical rejection), the script is programmed to refresh the data and try again automatically until the modification is successfully confirmed.

5. Completion The process continues through your open trades until it has finished updating them, providing a clear log of the actions taken and notifying you if any specific errors occur during the process.

Orders Execution
Checks for the total of open ordersIt can change open orders parameters, due to possible stepping strategy
13 Views
0 Downloads
0 Favorites
Modify Position
//+------------------------------------------------------------------+
//|                                                       modify.mq4 |
//|                      Copyright © 2004, MetaQuotes Software Corp. |
//|                                       http://www.metaquotes.net/ |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2004, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net/"

//+------------------------------------------------------------------+
//| script "modify first market order"                               |
//+------------------------------------------------------------------+
int start()
  {
   bool   result;
   double take_profit,stop_loss,point;
   int    cmd,total,error;
//----
   double sl=GlobalVariableGet("StopLoss");
   double tp=GlobalVariableGet("TakeProfit");
   if(sl==0 || tp==0)
     {
      Print("Error!");
      return(0);
     }
//----
   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-sl*point;
                  take_profit=Bid+tp*point;
                 }
               else
                 {
                  stop_loss=Ask+sl*point;
                  take_profit=Ask-tp*point;
                 }
               result=OrderModify(OrderTicket(),0,stop_loss,take_profit,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);
  }
//+------------------------------------------------------------------+

Comments