modify_profit_target

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

This automated script is designed to streamline the management of active trading positions by automatically adjusting your profit targets based on specific user-defined preferences. Instead of manually calculating and updating each trade, this tool scans your open positions and handles the mathematical adjustments for you.

Core Functionality

The script works by reviewing all currently open trades in your trading account. It follows a systematic process to identify which trades need modification based on two primary criteria:

  • Trade Direction: You can specify whether the script should act on "Buy" orders, "Sell" orders, or both.
  • Currency Pair: The script focuses exclusively on the specific currency pair you are currently viewing, ensuring it doesn't accidentally interfere with trades on other markets.

How the Automation Works

Once the script identifies a matching trade, it performs the following logic:

  1. Target Calculation: It looks at the price at which you originally entered the market (the Open Price). Based on the "New Target" value you provide (measured in pips), it calculates a new exit point.
    • For Buy trades, it adds the target amount to your entry price to set a profit goal above your entry.
    • For Sell trades, it subtracts the target amount from your entry price to set a profit goal below your entry.
  2. Preservation of Risk: The script is designed to be "non-destructive" regarding your risk management. It automatically detects your existing Stop Loss (the price at which the trade closes if the market moves against you) and carries that value over to the new order, ensuring your safety limits remain unchanged.
  3. Automatic Execution: The tool attempts to push these updates to the broker's server instantly. If the market is moving too quickly and the update fails due to a price fluctuation, the script includes a "refresh" mechanism that retries the modification until it is successfully confirmed.

Summary for the User

Essentially, this tool allows you to take an existing position and instantly "reset" its profit goal to a specific distance from your entry point. It automates the arithmetic and the manual clicking required to move a profit target, while keeping your original risk (Stop Loss) exactly as it was.

Orders Execution
Checks for the total of open ordersIt can change open orders parameters, due to possible stepping strategy
12 Views
0 Downloads
0 Favorites
modify_profit_target
//+------------------------------------------------------------------+
//|                                         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_target = 10;

//#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) profit_target=OrderOpenPrice()+new_target*gd_point;
               else            profit_target=OrderOpenPrice()-new_target*gd_point;
               stop_loss = OrderStopLoss(); //save current stop loss
               result=OrderModify(OrderTicket(),0,OrderStopLoss(),profit_target,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