Skip to content

How to add slippage? #128

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
jiangjiu opened this issue Aug 8, 2020 · 5 comments
Closed

How to add slippage? #128

jiangjiu opened this issue Aug 8, 2020 · 5 comments
Labels
duplicate This issue or pull request already exists question Not a bug, but a FAQ entry

Comments

@jiangjiu
Copy link

jiangjiu commented Aug 8, 2020

Expected Behavior

https://www.backtrader.com/docu/slippage/slippage/

backtrader can set a slippage via percentage or fixed value like 0.01

Actual Behavior

not a feature

thinking about this feature please?

@jiangjiu
Copy link
Author

jiangjiu commented Aug 8, 2020

Slippage can greatly affect returns

@kernc
Copy link
Owner

kernc commented Aug 8, 2020

Would something like this work?

SPREAD = .0002
SLIPPAGE = .0001

...

bt = Backtest(..., commission=SPREAD + SLIPPAGE)

@jiangjiu
Copy link
Author

jiangjiu commented Aug 9, 2020

good idea, well, commission can add this slippage percent,but can not set fixed value

@kernc
Copy link
Owner

kernc commented Aug 9, 2020

Related issue: #113.

@kernc kernc changed the title 【Feature Request】how to add a slippage? How to add slippage? Aug 9, 2020
@kernc kernc added question Not a bug, but a FAQ entry duplicate This issue or pull request already exists labels Aug 9, 2020
@kernc kernc closed this as completed Aug 9, 2020
@ibaris
Copy link

ibaris commented Aug 22, 2024

How would be to add the a random variable during the _process_orders to simulate slippage? One would need 2 variables:

  • slippage range : Tuple[float, float] = Indicates the slippage range of the close price like [0.98, 1.03]. This would mean the price could range between 98% and 103%.
  • slippage probability: float = A probability that a slippage will appear, e.g. 0.5 which says, that 50% of the time you experience a slippage.

Here is the basic idea:

import numpy as np

slippage_probability = 0.25
slippage_range = (0.98, 1.03)
close = 105

if np.random.rand() < slippage_probability:
    close *= np.random.uniform(slippage_range[0], slippage_range[1])

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
duplicate This issue or pull request already exists question Not a bug, but a FAQ entry
Projects
None yet
Development

No branches or pull requests

3 participants