Need help with my code

 

Hello coders, I've written the code below but it's not giving me the desired results. The code entails finding the profit of all of all current trades then closing them after a certain percentage of account equity has been achieved. Kindly review it and help me write the function to solve the problem.

double TradePercent()
  {
     double profit = 0;
      
      
     for(int i = OrdersTotal() - 1; i >= 0; i--)
         {
            if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
              {
                 profit += OrderProfit();

              }
         } 
      double profitpercentage = (profit / AccountBalance() ) * 100;
      Comment("The profit is " , profitpercentage, "\n"); 
      
      
    return(0);  
  }
 

What is the problem? I tested your exact code as is, and it gave the right percentage.

 
Jesse Phipps #:

What is the problem? I tested your exact code as is, and it gave the right percentage.

Ok, then my my strategy tester has a problem. I'll check it out. Kindly help me how I can add a function to close all current trades after the profit percentage has been reached 
 
Ernest Akoyi #:
Ok, then my my strategy tester has a problem. I'll check it out. Kindly help me how I can add a function to close all current trades after the profit percentage has been reached 
Loop through and use this:
 
Daniel Cioca #:
Loop through and use this:

Thank you

 
Daniel Cioca #: Loop through

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 and order count:

  1. For non-FIFO (non-US brokers), (or the EA only opens one order per symbol), you can simply count down, in an index loop, and you won't miss orders. Get in the habit of always counting down.
              Loops and Closing or Deleting Orders - MQL4 programming forum

  2. For In First Out (FIFO rules — US brokers), and you (potentially) process multiple orders per symbol, you must find the earliest order (count up), close it, and on a successful operation, reprocess all positions (from zero).
              CloseOrders by FIFO Rules - Strategy Tester - MQL4 programming forum - Page 2 #16
              MetaTrader 5 platform beta build 2155: MQL5 scope, global Strategy Tester and built-in Virtual Hosting updates - Best Expert Advisors - General - MQL5 programming forum #1.11

  3. and check OrderSelect in case later positions were deleted.
              What are Function return values ? How do I use them ? - MQL4 programming forum
              Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles

  4. 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 instead, be direction independent and just use OrderClosePrice().

Reason: