Skip to content
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

how i want buy at a and sell at b #396

Open
hy2014 opened this issue Nov 27, 2022 · 1 comment
Open

how i want buy at a and sell at b #396

hy2014 opened this issue Nov 27, 2022 · 1 comment

Comments

@hy2014
Copy link

hy2014 commented Nov 27, 2022

see the '' signal = data > sma '' in example, but if i have a indicator, when indicator < 10, i buy it; indicator > 50, sold it.
how to implement this example.

@Matoran
Copy link

Matoran commented Feb 1, 2023

from https://pmorissette.github.io/bt/examples.html#sma-crossover-strategy

## download some data & calc SMAs
data = bt.get('spy', start='2010-01-01')
sma50 = data.rolling(50).mean()
sma200 = data.rolling(200).mean()

## now we need to calculate our target weight DataFrame
# first we will copy the sma200 DataFrame since our weights will have the same strucutre
tw = sma200.copy()
# set appropriate target weights
tw[sma50 > sma200] = 1.0
tw[sma50 <= sma200] = -1.0
# here we will set the weight to 0 - this is because the sma200 needs 200 data points before
# calculating its first point. Therefore, it will start with a bunch of nulls (NaNs).
tw[sma200.isnull()] = 0.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants