Navigation


 Publications


 Go back to the forum

Forex Stock Exchange Forum    

Order Manager MT5 Script

gandra | Published on the fri Oct 11, 2024 2:24 pm | 94 Views

Description of the Manage Positions Script Usage


This script is designed to manage positions in MetaTrader5, specifically by closing open positions based on certain conditions or modifying Stop Loss (SL) and Take Profit (TP) values for selected positions.

 


Input Parameters for Position Closing

Input Parameters for SL/TP Modification


Key Features

   - ClosePositions: This function loops through all open positions and closes positions that match the specified criteria (e.g., all positions, profitable, losing, specific magic number, or symbol).

   - ModifySLTP: This function modifies the Stop Loss and Take Profit values for both buy and sell positions based on the input `PositionTypeToModify`. If the new SL or TP is not specified (set to `0.0`), the existing SL or TP is preserved.

Usage

   Close Positions

     - Set the desired closing criteria using the `CloseAllPositions`, `CloseProfitablePositions`, `CloseLosingPositions`, and `MagicNumber` inputs.

     - If you want to manage positions only for certain symbols, provide them in the `SymbolName` input (e.g., "EURUSD, GBPUSD").

     - Run the script, and it will attempt to close the matching positions.


    Modify Stop Loss and Take Profit

     - Set the `PositionTypeToModify` to either `POSITION_BUY` or `POSITION_SELL` to specify which type of position to modify.

     - Set the `TakeProfitPrice` and/or `StopLossPrice` to the desired levels. Leave either at `0.0` if you don’t want to modify that particular value.

     - Run the script, and it will modify the SL/TP for all open positions accordingly.


 Detailed Feedback

   - The script prints detailed logs during execution. It provides feedback on the success or failure of closing positions and modifying SL/TP values. For example:

     - "Successfully closed position: Ticket 12345, Symbol EURUSD, Profit 50.00."

     - "Modified BUY: Ticket 12345, Symbol EURUSD, SL: 1.2000 -> 1.2050, TP: 1.3000 -> 1.3050."

 

Execution Flow

   - When the script is executed, it first checks if any closing criteria (like closing all, profitable, or losing positions) are activated. If so, it calls the `ClosePositions()` function.

   - Next, if valid `TakeProfitPrice` or `StopLossPrice` values are provided, it modifies the SL/TP for all positions using the `ModifySLTP()` function.

   - The script exits after processing the positions and logs the outcome of the actions.


How to Use


   Close All Positions:

     - Set `CloseAllPositions = true`.

     - Leave the other options (`CloseProfitablePositions`, `CloseLosingPositions`, etc.) as false or `0`.

     - Run the script, and all open positions will be closed, regardless of their profit or loss status.


    Close Only Profitable Positions:

     - Set `CloseProfitablePositions = true`.

     - Run the script, and only positions that are currently in profit will be closed.


    Close Only Losing Positions:

     - Set `CloseLosingPositions = true`.

     - Run the script, and only positions that are currently in a loss will be closed.


   Close Positions by Magic Number:

     - Set `MagicNumber` to the desired magic number (e.g., `12345`).

     - Run the script, and all positions associated with that magic number will be closed.


    Close Positions by Symbol:

     - Set `SymbolName` to a comma-separated list of symbols (e.g., "EURUSD, GBPUSD, XAUUSD").

     - Run the script, and only positions that match the specified symbols will be closed.


    Modify Stop Loss and Take Profit:

     - Set `PositionTypeToModify` to either `POSITION_BUY` or `POSITION_SELL` to specify which type of positions to modify.

     - Set `TakeProfitPrice` and/or `StopLossPrice` to the desired values. For example:

       - `TakeProfitPrice = 1.3050`: This will set the TP of all positions to `1.3050`.

       - `StopLossPrice = 1.2050`: This will set the SL of all positions to `1.2050`.

     - Run the script, and it will modify the SL/TP values accordingly. If either `TakeProfitPrice` or `StopLossPrice` is set to `0.0`, it will preserve the current value for that parameter.


 Combined Closing and SL/TP Modification

   - You can combine both actions in one run. For example, you can set `CloseProfitablePositions = true` and `StopLossPrice = 1.2050` to both close profitable positions and modify the SL of the remaining ones.


Example Scenarios

   Scenario 1: Close All Positions and Modify SL/TP

     - `CloseAllPositions = true`

     - `TakeProfitPrice = 1.3000`

     - `StopLossPrice = 1.2000`

     Result: All positions will be closed, and for any open positions that remain (if any), their SL will be updated to `1.2000`, and their TP will be updated to `1.3000`.


    Scenario 2: Close Profitable Positions on EURUSD Only

     - `CloseProfitablePositions = true`

     - `SymbolName = "EURUSD"`

     - Result: Only profitable positions on EURUSD will be closed. Positions on other symbols will remain open.

  Scenario 3: Modify Take Profit to 1.2500 for All Positions:

     - `TakeProfitPrice = 1.2500`

     - Result: The Take Profit of all open positions will be updated to `1.2500`. Stop Loss values will remain unchanged.


Notes

    - If you specify both `CloseProfitablePositions = true` and `CloseLosingPositions = true`, both profitable and losing positions will be closed.

    - Positions will only be modified if they meet the conditions you set for closing or SL/TP updates. If no conditions are met, the script will not take any action.

    - The script will handle multiple symbols and magic numbers, allowing for precise management of specific positions.

This script is ideal for traders who want a simple and flexible way to manage their open positions and adjust SL/TP levels quickly in MetaTrader 5.

You can find the script at the following link on the MQL5 market online store.

All my Apps: LINK

About the author