Modify not closing all but only first open trade

To add comments, please log in or register
Mounir Chikri
Mounir Chikri  

i have the following code how to modify not close all but only the first trade? thanks!

void TrailingRules()
    double current_profit = Profit();

    if (Count(0) + Count(1) == 0)max_usd = 0;

    if (current_profit >= TrailingStartUsd && max_usd == 0)max_usd = current_profit;

    if (max_usd > 0 && current_profit > max_usd)max_usd = current_profit;

    if (max_usd > 0 && current_profit <= max_usd - TrailingInUsd)
        max_usd = 0;

William Roeder
William Roeder  
Mounir Chikri: i have the following code how to modify not close all but only the first trade?
  1. When you post code please use the CODE button (Alt-S)! (For large amounts of code, attach it.) Please edit your (original) post.
              General rules and best pratices of the Forum. - General - MQL5 programming forum
              Messages Editor

  2. The code you posted does not close any trades. Your problem is in your called functions. Possibly:
    In the presence of multiple orders (one EA multiple charts, multiple EAs, manual trading,) while you are waiting for the current operation (closing, deleting, modifying) to complete, any number of other operations on other orders could have concurrently happened and changed the position indexing:
    1. For non-FIFO (US brokers,) (or the EA only opens one order per symbol,) you can simply count down in a position loop, and you won't miss orders. Get in the habit of always counting down.
                Loops and Closing or Deleting Orders - MQL4 and MetaTrader 4 - MQL4 programming forum
      For FIFO (US brokers,) and you (potentially) process multiple orders per symbol, you must count up and on a successful operation, reprocess all positions (set index to -1 before continuing.)
    2. and check OrderSelect in case earlier positions were deleted.
                What are Function return values ? How do I use them ? - MQL4 and MetaTrader 4 - MQL4 programming forum
                Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
    3. and if you (potentially) process multiple orders, must call RefreshRates() after server calls if you want to use, on the next order / server call, the Predefined Variables (Bid/Ask) or (be direction independent and use) OrderClosePrice().
To add comments, please log in or register