SmartQuant Discussion
http://www.smartquant.com/forums/

Trade Price Error in Backtest
http://www.smartquant.com/forums/viewtopic.php?f=60&t=11021
Page 1 of 2

Author:  M101 [ Fri Nov 13, 2015 1:04 pm ]
Post subject:  Trade Price Error in Backtest

Hi Smartquant,

I’m backtesting a futures contract (in OQ 2014) that has a minimum tick size of .25, however the backtest results are showing my trades closed at prices that are not multiples of .25 (such as 2030.27 and 2030.68).

Any idea why this is happening?

(FYI: I checked the historical data I uploaded for the symbol and its all in multiples of .25 as shown in attached 1.jpg. I also made sure the TickSize was set to 0.25 in the Properties tab as shown in attached screenshot 2.jpg)


Thanks!

Attachments:
2.jpg
2.jpg [ 20.79 KiB | Viewed 6552 times ]
1.jpg
1.jpg [ 71.39 KiB | Viewed 6552 times ]

Author:  Dr. Anton Fokin [ Fri Nov 13, 2015 3:04 pm ]
Post subject:  Re: Trade Price Error in Backtest

I guess if you are talking about limit orders and have FillAtLimitPrice option set to true, then the simulator fills you with limit order price and doesn't take tick size into account. So I suggest you set limit price in accordance with tick size.

Regards,
Anton

Author:  M101 [ Mon Nov 16, 2015 3:59 am ]
Post subject:  Re: Trade Price Error in Backtest

I see.

Can you please type out the exact code I'd need to write in order set a limit order’s limit price to be whatever the best bid price is at the time the limit order is placed?

Would really appreciate the example.

Thanks!

Author:  Dr. Anton Fokin [ Mon Nov 16, 2015 11:33 am ]
Post subject:  Re: Trade Price Error in Backtest

Hi,

I am not sure your question is relevant to our discussion above and perhaps I am missing something but I guess

BuyLimit(100, Instrument.Bid.Price)

that's it.

Instrument.Bid property holds the last best bid for the instrument.

Regards,
Anton

Author:  M101 [ Mon Nov 16, 2015 10:29 pm ]
Post subject:  Re: Trade Price Error in Backtest

That solved it, thanks!

Author:  M101 [ Fri Nov 20, 2015 6:18 am ]
Post subject:  Re: Trade Price Error in Backtest

False alarm, I did not figure out how to fix this after all:)

I ran a backtest of the "BollingerBands" Solution (that came with OQ 2014) on a Future with a minimum tick size of 0.25, but the backtest results show certain trades closed at prices that are not multiples of 0.25. Can you please type out the exact code I'd write to get the mentioned "BollingerBands" solution to recognize the Future it is trading has a minimum Tick size of 0.25?

(FYI: I'm not sure if the code to do this is something I should add to the Scenario.cs file, MyStrategy.cs file or somewhere else.)

Thanks

Author:  Dr. Anton Fokin [ Fri Nov 20, 2015 8:17 am ]
Post subject:  Re: Trade Price Error in Backtest

Hi,

I think all you need to do is to round sma.Last to 0.25 in

if (Position.Side == PositionSide.Long)
exitOrder = SellLimitOrder(Instrument, Qty, sma.Last, "Exit");
else
exitOrder = BuyLimitOrder(Instrument, Qty, sma.Last, "Exit");

in BollingerBands example.

Regards,
Anton

Author:  M101 [ Fri Nov 20, 2015 1:11 pm ]
Post subject:  Re: Trade Price Error in Backtest

How do I round sma.Last to 0.25?

Author:  Valentin Lebedev [ Fri Nov 20, 2015 5:59 pm ]
Post subject:  Re: Trade Price Error in Backtest

Hello, M101.

You may round sma.Last to 0.25 this way:

Math.Round(sma.Last/0.25)*0.25

Author:  M101 [ Sat Nov 21, 2015 5:23 am ]
Post subject:  Re: Trade Price Error in Backtest

Thanks Valentine.

Can you please show me how/where exactly I'd type that into the Scenario.cs file?

The more details the better:)

Author:  Valentin Lebedev [ Sat Nov 21, 2015 1:45 pm ]
Post subject:  Re: Trade Price Error in Backtest

You have to edit MyStrategy.cs file.

First, add new parameter before constructor:
Code:
[Parameter]
public double TickSize = 0.25;

public MyStrategy(Framework framework, string name)
   : base(framework, name)

Then, add new method, you may add it to the end of the MyStrategy class body:
Code:
private double ShrinkPrice(double price)
{
   return Math.Round(price/TickSize)*TickSize;
}

Finally, edit UpdateExitLimit method:
Code:
if (Position.Side == PositionSide.Long)
   exitOrder = SellLimitOrder(Instrument, Qty, ShrinkPrice(sma.Last), "Exit");
else
   exitOrder = BuyLimitOrder(Instrument, Qty, ShrinkPrice(sma.Last), "Exit");

Send(exitOrder);

Author:  Valentin Lebedev [ Sat Nov 21, 2015 1:59 pm ]
Post subject:  Re: Trade Price Error in Backtest

Another way, you may use TickSize parameter in Instrument. So you don't need to add your own, and ShrinkPrice method will be changed to:
Code:
private double ShrinkPrice(double price)
{
   return Math.Round(price/Instrument.TickSize)*Instrument.TickSize;
}

Author:  M101 [ Mon Nov 23, 2015 7:02 am ]
Post subject:  Re: Trade Price Error in Backtest

Thanks Valentin, that definitely got all the backtest results to be multiples of 0.25!

However, the backtest results are showing certain limit orders were filled for prices that did not exist at the time. For instance, the backtest results show a Buy limit order was filled at 2086.75 while best Ask price at the time was 2087.25.

Any idea why this is happening?

Author:  Valentin Lebedev [ Mon Nov 23, 2015 5:22 pm ]
Post subject:  Re: Trade Price Error in Backtest

Please explain how may I reproduce this issue.

Author:  Dr. Anton Fokin [ Mon Nov 23, 2015 5:49 pm ]
Post subject:  Re: Trade Price Error in Backtest

Hi,

I've mentioned in the beginning of this thread that if you

have FillAtLimitPrice option set to true, then the simulator fills you with limit order price

This means there can be a fill that doesn't correspond to any bid/ask in the simulated market data. Try to set this option to false in the scenario or in the execution simulator properties in the OQ IDE and see what happens.

Regards,
Anton

Page 1 of 2 All times are UTC + 3 hours
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/