mhallsmoore / qstrader

QuantStart.com - QSTrader backtesting simulation engine.
https://www.quantstart.com/qstrader/
MIT License
2.93k stars 855 forks source link

IBSimulatedExecutionHandler #236

Closed MagicEthan closed 8 months ago

MagicEthan commented 6 years ago

I was curious about the execution mechanism so I run the buy and hold example. I found the commission in calculate_ib_commission() seems not in accordance with https://www.interactivebrokers.co.uk/en/index.php?f=1590&p=stocks1:

commission = min( 0.5 * fill_price * quantity, max(1.0, 0.005 * quantity) )

which should be: commission = min( 0.005 * fill_price * quantity, max(1.0, 0.005 * quantity) )

First I thought this might be something with the PriceParser. So without any change in the codes, I just changed the SPY.csv and let the close price be 0.25, set the default quantity in position sizer as 1000, and it gives:

timestamp | ticker | action | quantity | exchange | price | commission 2000-01-03 00:00:00 | SPY | BOT | 1000 | ARCA | 0.25 | 5

in which the commission should be 1.25 as an example on the IB website. So I changed the codes to keep consistent with IB. However, it still gives:

timestamp | ticker | action | quantity | exchange | price | commission 2000-01-03 00:00:00 | SPY | BOT | 1000 | ARCA | 0.25 | 5

So why?

Well, because the fill price has been parsed by the multiplier before calculating the commission. If you debug the code, you will find the fill_price here is 2500000 instead of 0.25. So we need to rewrite the codes as:

commission = min( 0.005 * PriceParser.display(fill_price) * quantity, max(1.0, 0.005 * quantity) )

Now it gives the correct commission:

timestamp | ticker | action | quantity | exchange | price | commission 2000-01-03 00:00:00 | SPY | BOT | 1000 | ARCA | 0.25 | 1.25

I'm not sure how much speed it improves by using Price Parser, but this parser really results in some problems. My colleague and I just removed the price parser to use the original price. Be careful guys.

mhallsmoore commented 6 years ago

Hi @MagicEthan,

Thanks for pointing out these issues.

Just to let you know that our team are working on a complete rebuild of QSTrader from scratch that does away with the PriceParser and has fixed the commission issue you mention above.

Cheers,

-Mike.