I am writing a strategy for backtesting. Following is the sample code of what I am using:
protected override void OnBarUpdate() {[INDENT]if (BarsInProgress != 0)[/INDENT][INDENT=2]return;[/INDENT][INDENT] if (CurrentBars[0] < 1)[/INDENT][INDENT=2]return;[/INDENT][INDENT] if (Position.MarketPosition == MarketPosition.Long) {[/INDENT][INDENT=2]Print("Inside MarketPosition " + Position.MarketPosition);[/INDENT][INDENT=2]//Condition to trigger sell goes here[/INDENT][INDENT]}[/INDENT] [INDENT]else {[/INDENT][INDENT=2]Print("Inside MarketPosition " + Position.MarketPosition); if (/*condition to buy goes here*/) {[/INDENT][INDENT=3]ContSize = x; Print("Long condition met"); EnterLong(ContSize, "Long");[/INDENT][INDENT=2] }[/INDENT][INDENT] }[/INDENT] } protected override void OnOrderUpdate(Order order, double limitPrice, double stPrice, int quantity, int filled, double averageFillPrice, OrderState orderState, DateTime time, ErrorCode error, string comment) {[INDENT]Print("Order " + order.Name + " is updated with state " + orderState + " Quantity " + quantity + " Error Code " + error);[/INDENT] }
Inside MarketPosition Flat Inside MarketPosition Flat Inside MarketPosition Flat Inside MarketPosition Flat Long condition met Order Long is updated with state Submitted Quantity 21 Error Code NoError Order Long is updated with state Accepted Quantity 21 Error Code NoError Order Long is updated with state Working Quantity 21 Error Code NoError Order Long is updated with state Filled Quantity 21 Error Code NoError Execution Order Name Long and Size 21 and Price 2293.51 and state Filled Execution order name Long Filled Quantity 21 Setting stop order for stopPrice 2247.6398 Order StopLong is updated with state Submitted Quantity 21 Error Code NoError Order StopLong is updated with state Accepted Quantity 21 Error Code NoError Order StopLong is updated with state Working Quantity 21 Error Code NoError Inside MarketPosition Long Inside MarketPosition Long Inside MarketPosition Flat Long condition met Order Long is updated with state Submitted Quantity 39 Error Code NoError Order Long is updated with state Accepted Quantity 39 Error Code NoError Order Long is updated with state Working Quantity 39 Error Code NoError Order Long is updated with state Filled Quantity 39 Error Code NoError Execution Order Name Long and Size 39 and Price 1270.51 and state Filled Execution order name Long Filled Quantity 39 Setting stop order for stopPrice 1245.0998 Order StopLong is updated with state Submitted Quantity 39 Error Code NoError Order StopLong is updated with state Accepted Quantity 39 Error Code NoError Order StopLong is updated with state Working Quantity 39 Error Code NoError Inside MarketPosition Long Order ExitLong is updated with state Submitted Quantity 39 Error Code NoError Order ExitLong is updated with state Accepted Quantity 39 Error Code NoError Order ExitLong is updated with state Working Quantity 39 Error Code NoError Order ExitLong is updated with state CancelPending Quantity 39 Error Code NoError Order ExitLong is updated with state CancelSubmitted Quantity 39 Error Code NoError Order ExitLong is updated with state Cancelled Quantity 39 Error Code NoError ExitLong Cancelled Order StopLong is updated with state Filled Quantity 39 Error Code NoError Execution Order Name StopLong and Size 39 and Price 1245.1 and state Filled StopLong is executed with Quantity 39 Inside MarketPosition Flat [B]Long condition met[/B] Order Long is updated with state Submitted Quantity 39 Error Code NoError Order Long is updated with state Accepted Quantity 39 Error Code NoError [B]Order Long is updated with state Working Quantity 39 Error Code NoError[/B] [B]Inside MarketPosition Flat[/B] [B]Long condition met [/B] Inside MarketPosition Flat Inside MarketPosition Flat [B]Long condition met [/B] . . Inside MarketPosition Flat [B]Long condition met [/B] . .
Interestingly this stops working after a certain time frame. For example I wanted to run the backtest from 1999 - 2020
On the first few run it was giving me the results from 1999 - 2012 and when I changed the time frame and I was able to run from 2013 - 2020 and see the results.
After a while, it started giving me results only from 1999 - 2003 and if I change the time frame to 2003 - 2020 I was able to see the results for this time period successfully.
Why does it stop at random times?
I would like to optimize the strategy from 1999 - 2020 but because I have to break it into more than one time period the optimized parameters are different for both the time period, which is obvious. How do I fix this problem?
Thanks for your help.
Yogesh
Comment