Getting last trade date in custom backtest code

In order to do some date range calculation, I need to get last date of last trade from a custom backtest code.

I found a way to get it, but it introduces unwanted side effect. Here is what I put in custom backtest code where it will loop on all trades:

// iterate through closed trades first 
   for( trade = bo.GetFirstTrade(); trade; trade = bo.GetNextTrade() ) 

		// Get last trade entry time date
		if (IsEmpty( bo.GetNextTrade() ))
			lastTradeDateTime = trade.EntryDateTime;


Problem is that bo.GetNextTrade() evaluation triggers a jump to next trade resulting of half of the trades being skipped from processing (additional code not shown).

Is there a way to passively evaluate isEmpty( bo.GetNextTrade() ) ?
Or any other way to get date of last trade in a custom backtest code?

If you just want the entry date of the last trade which was closed (which may not be the same as the latest entry date for any closed trade), then you can simply use:

lastTradeDateTime = trade.EntryDateTime;

Because that will overwrite your lastTradeDateTime each time through the loop. Therefore, when you exit the loop that variable will hold the last value it was assigned, which is the value from the last trade.


Simple and brilliant, why can't i come up with such simplicity....

1 Like