I have question regarding backtesting:
My strategy generates entry/exit signals for the same trading day for more contract months. However, I'de like to avoid to trade the same time range in different contract months.
Example:
- let's say strategy generates entry on 20/7/2011 and exit on 12/8/2011 for ZS 01-12 and also for ZS 11-11
- in ZS 11-11 there are also another trades (entry 24/1/2011 exit 1/2/2011)
- NT backtests ZS 01-12 first, then backtests ZS 11-11
I tried to check, if time of next trade (ZS 11-11 20/7/2011...12/8/2011) is not overlapping with trades already finished in another contract months (ZS 01-12 20/7/2011...12/8/2011).
Code created based on NT 7 manual:
private void CheckPrevTrades() // entry signal generated, check overlapping with trades already finished
{
if ( Performance.AllTrades.Count > 0 && Position.MarketPosition == MarketPosition.Flat ) // at least one trade finished, no position currently opened
{
foreach (Trade trade in Performance.AllTrades) // go through all finished trades
{
int checkFlatEntryDate = DateTime.Compare(Time[0].Date.Date, trade.Entry.Time.Date); // compare current trading day with previous trade entry date
int checkFlatExitDate = DateTime.Compare(Time[0].Date, trade.Exit.Time.Date); // compare current trading day with previous trade exit date
if (checkFlatEntryDate >= 0 && checkFlatExitDate <= 0) // check overlapping, if current date is bigger/equal to entry date and smaller/equal to exit date, we cannot continue to open the trade
{
// logic for overlapping here
}
.
.
}
}
}
The problem: when NT backtests ZS 11-11 after ZS 01-12, in the list of finished trades (i.e. Performance.AllTrades), there is only contr. month ZS 11-11 with entry 24/1/2011 and exit 1/2/2011.
In the list, there's no trade ZS 01-12 entry 20/7/2011 exit12/8/2011. So, the trade for ZS 11-11, entry 20/7/2011 exit12/8/2011 is opened despite I don't want the trade to open.
Can you hint me, how to resolve it?
Thanks in advance.
BR.
Q.
Comment