MiniXC / simple-back

A simple daily python backtester that works out of the box.
Mozilla Public License 2.0
59 stars 12 forks source link

Slippage #13

Closed MiniXC closed 4 years ago

MiniXC commented 4 years ago

As this backtester is built for open and close data and not intra-day data, results will be wrong to a certain extent as it is rarely possible to buy exactly at open or close price. Adding a SlippageModel that calculates a price drift would be easy, but coming up with an accurate SlippageModel and determining if it is accurate is not.

SlippageModel

1) random value from truncated normal distribution with open/close price as mean scaled by low/high

given current_price
slippage_price = random value from (gaussian: mu: current_price, sigma: x)
// x (sigma) can be set by user and determines how fat-tailed the distribution will be
if absolute value slippage_price > 1:
  set slippage_price so abs value = 1
if slippage_price > 0:
  slippage_price = slippage_price * high
if slippage_price < 0:
  slippage_price = slippage_price * low

disadvantages

1D0BE commented 4 years ago

Will ask an Algotrading Proffessional about that.

Slippage is a tricky thing to get right. Additionally, there is the difficultly to incorporate different kind of orders ("market", "limit", "aggressive"). Once we got that, the real problems start with incorporating slippage based on order amount and position size. This would lead to having to simulate other market partizipants.

What I am trying to say is that there is no perfect model for market-induced order losses, other than the market itself.

Having stated that, I think that introducing randomness based on volume is probably the best way to go. Something like

slippage in percent = random.normal(loc=0.002, scale=??) / (volume * some_scaling_factor)

would probably suffice.

Quick online search yielded probably something like around approximatly unstably 0.2% as µ. No idea about the stddev. Will look into that.

MiniXC commented 4 years ago

Quantopian uses 0.05% per default, not as a normal distribution but on every trade, like a fixed commission. Correct me if I'm wrong, but I think this is quite conservative for small-cap algo-trading, especially in the age of brokers that do not actually execute the order (e.g. Robinhood, Alpaca) - so simple-back will use this as lower bound. This way people also know what to expect when testing their algorithm in quantopian. The configuration method will be .slippage with no argument defaulting to quantopians 0.05%

MiniXC commented 4 years ago

image And this is what it looks like visually.