My code is outputting(printing) results not as expected.

 

Hello , i built this code trying to check the results of the EA's in my account based on their magic numbers. i loop through the trades from the oldest to the newest for each magic number and perform calculations in order to calculate values e0 , e1 ,e2 . i will share the code and then explain the problem.


void Test()
  {
   int totalOrders = OrdersHistoryTotal();
   int magicNumbers[];
   ArrayResize(magicNumbers, totalOrders);
   int magicNumberCount = 0;
   int magicNumbersWritten[];


   for(int i = 0; i < totalOrders; i++)
     {
      if(OrderSelect(i, SELECT_BY_POS, MODE_HISTORY))
        {
         int currentMagicNumber = OrderMagicNumber();
         bool isUnique = true;

         for(int j = 0; j < magicNumberCount; j++)
           {
            if(magicNumbers[j] == currentMagicNumber)
              {
               isUnique = false;
               break;
              }
           }

         if(isUnique)
           {
            magicNumbers[magicNumberCount] = currentMagicNumber;
            magicNumberCount++;
           }
        }
     }

   for(int i = 0; i < magicNumberCount; i++)
     {
      int currentMagicNumber = magicNumbers[i];
      double e0 = 0.0;
      double e1 = 0.0;
      double e2 = 0.0;


      for(int j = 0; j < totalOrders; j++)
        {
         if(OrderSelect(j, SELECT_BY_POS, MODE_HISTORY))
           {
            if(OrderMagicNumber() == currentMagicNumber)
              {
               // Calculate e0, add all trades with specific magic number to e0
               e0 += OrderProfit();

               // Calculate e1, negative then positive trade
               if(j >= 1)
                 {
                  double pl2 = 0.0;
                  double pl3 = 0.0;

                  if(OrderSelect(j - 1, SELECT_BY_POS, MODE_HISTORY))
                    {
                     pl2 = OrderProfit();
                    }
                  if(OrderSelect(j - 2, SELECT_BY_POS, MODE_HISTORY))
                    {
                     pl3 = OrderProfit();
                    }

                  if(pl3 < 0 && pl2 > 0)
                    {
                     e1 += OrderProfit();
                    }
                else 

                  if(pl3 > 0 && pl2 < 0)
                    {
                     e2 += OrderProfit();
                    }
                 }
// some code
}

in this code if the trades of EA with magic number = 1 , are as following from oldest to newest:
trade 1 : 1.5$ 
trade 2 : -1 $
trade 3 = 2.4
trade 4 = 1.5
e0 should return the total of the trades of each magic number and its working properly , so e0 = 1.5 + (-1) + 2.4 + 1.5
e1 should if the previous 2 trades ( shift 1 and 2 ) are negative then positive. if yes add the PL of current trade to e1. in this example: e1=1.5 , when loop reaches trade 4 it checks trade with shift 1 : 2.4(>0) and trade with shift 2:-1(<0) so its negative then positive trade.
in my EA its not returning the value of e1 correctly for unknown reasons. the same with e2 (e2 is positive then negative sequence)

 

If your code doesn't do what i should use the debugger to find out why and where:

Code debugging:  https://www.metatrader5.com/en/metaeditor/help/development/debug
Error Handling and Logging in MQL5:  https://www.mql5.com/en/articles/2041
Tracing, Debugging and Structural Analysis of Source Code, scroll down to: "Launching and Debuggin": https://www.mql5.com/en/articles/272

Code debugging - Developing programs - MetaEditor Help
  • www.metatrader5.com
MetaEditor has a built-in debugger allowing you to check a program execution step by step (by individual functions). Place breakpoints in the code...
 
Carl Schreiber #:

If your code doesn't do what i should use the debugger to find out why and where:

Code debugging:  https://www.metatrader5.com/en/metaeditor/help/development/debug
Error Handling and Logging in MQL5:  https://www.mql5.com/en/articles/2041
Tracing, Debugging and Structural Analysis of Source Code, scroll down to: "Launching and Debuggin": https://www.mql5.com/en/articles/272

My Code is working normally and printing the values , but some of the values are not calculated as expected.

 
nidalzd: i loop through the trades from the oldest to the newest for each magic number

MT4:

  1. Do not assume history has only closed orders.
              OrderType() == 6, 7 in the history pool? - MQL4 programming forum #4 and #5 (2017)

  2. Do not assume history is ordered by date, it's not.
              Could EA Really Live By Order_History Alone? (ubzen) - MQL4 programming forum (2012)
              Taking the last profit and storing it in a variable | MQL4 - MQL4 programming forum #3 (2020)

  3. Total Profit is OrderProfit() + OrderSwap() + OrderCommission(). Some brokers don't use the Commission/Swap fields. Instead, they add balance entries. (Maybe related to Government required accounting/tax laws.)
              "balance" orders in account history - Day Trading Techniques - MQL4 programming forum (2017)

    Broker History
    FXCM
    Commission - «TICKET»
    Rollover - «TICKET»

    >R/O - 1,000 EUR/USD @0.52

    #«ticket»  N/A
    OANDA
    Balance update
    Financing (Swap: One entry for all open orders.)

Reason: