MT5 back tester -- how to capture final trade when "position closed due end of test"

 

When backtesting, I often have an ongoing trade when the tester ends. It shows up with the message "end of test." So far so good.

end of test

I want to capture that last profit/loss and swap so that I can calculate certain statistical values accurately. In the above trade, I would consider this a "win," as though I actually had closed the position like the Tester did.

I first looked to OnTradeTransaction() event,but this event does not appear capture this final tester-close trade, i.e. there is no final DEAL_ENTRY_OUT.

My next thought was to capture the profit/loss and swap in the OnTester() event. Something like this:

// Called right before OnDeinit.
// . . . the Tester event that is automatically generated after a history testing of an Expert Advisor on the chosen interval is over.
double OnTester()
{
   Print(__FUNCTION__);
   
   Print(StringFormat(  "Current profit/loss [$%.2f] swap [$%.2f]", 
                        PositionGetDouble(POSITION_PROFIT), 
                        PositionGetDouble(POSITION_SWAP)
                     ));
   
   return 0.0;
}

The journal entry shows that I am darn close—the swap is exact, and the position is close ($649.02 vs. $643.97) to what the tester told me.


Journal entry

I could probably live with this; but it's not exact.

So . . . any thoughts on how/where I can capture the final profit/loss and swap at the end of a back test?

 
Anthony Garot:

When backtesting, I often have an ongoing trade when the tester ends. It shows up with the message "end of test." So far so good.

I want to capture that last profit/loss and swap so that I can calculate certain statistical values accurately. In the above trade, I would consider this a "win," as though I actually had closed the position like the Tester did.

I first looked to OnTradeTransaction() event,but this event does not appear capture this final tester-close trade, i.e. there is no final DEAL_ENTRY_OUT.

My next thought was to capture the profit/loss and swap in the OnTester() event. Something like this:

The journal entry shows that I am darn close—the swap is exact, and the position is close ($649.02 vs. $643.97) to what the tester told me.

I could probably live with this; but it's not exact.

So . . . any thoughts on how/where I can capture the final profit/loss and swap at the end of a back test?

Maybe, you can consider adding a "Stop Date/Time" to the EA, so that it will not enter any new trades after that, but allow the test period to go beyond that. This way, when the test ends, there will be no open trades and the results will be "clean".

 
#include <MT4Orders.mqh> // https://www.mql5.com/en/code/16006

double OnTester()
{
  if (OrderSelect(OrdersHistoryTotal() - 1, SELECT_BY_POS, MODE_HISTORY))
     Print(StringFormat("Current profit/loss [$%.2f] swap [$%.2f]", OrderProfit(), OrderSwap()));
     
  return(0);
}

or

double OnTester()
{
  if (HistorySelect(TimeCurrent(), INT_MAX) && HistoryDealsTotal())
  {
     const ulong Ticket = HistoryDealGetTicket(HistoryDealsTotal() - 1);
     
     Print(StringFormat("Current profit/loss [$%.2f] swap [$%.2f]",
                        HistoryDealGetDouble(Ticket, DEAL_PROFIT),
                        HistoryDealGetDouble(Ticket, DEAL_SWAP)));
  }
     
  return(0);
}
 
fxsaber:


This worked perfectly! Thanks!

Reason: