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

Simplification of long short strategy like in SIT R #78

Closed
ghost opened this issue Oct 28, 2016 · 12 comments
Closed

Simplification of long short strategy like in SIT R #78

ghost opened this issue Oct 28, 2016 · 12 comments

Comments

@ghost
Copy link

ghost commented Oct 28, 2016

I have used systemic investor toolbox (SIT) package in R and its very good for backtesting. In that package creating signal for long, short or long-short strategy is very easy.

#long only strategy
signal<-ifelse(RSI3 < 30 & CCI20 > -290 & CCI20 < -100 & DEMA10c > -40 & DEMA10c < -20,1,NA)

#short only strategy
signal<-ifelse(RSI3 >30 & CCI20 > -290 & CCI20 > -100 & DEMA10c < -40 & DEMA10c > -20,-1,NA)

#long and short strategy
signal<-ifelse(RSI3 < 30 & CCI20 > -290 & CCI20 < -100 & DEMA10c > -40 & DEMA10c < -20,1,-1)

and then pass the signal to a backtest function

data$weight[] = NA
data$weight[] = signal
models$result = bt.run.share(data, clean.signal=T, trade.summary = TRUE)

It would be easier if bt also has similar simplified functions.

@pmorissette
Copy link
Owner

Hey @potholiday,

This is also easy with bt. Please see the WeighTarget or SelectWhere algos. You will basically use Pandas to create a signal / weight matrix that will be passed to the strategy.

Cheers,
Phil

@ghost
Copy link
Author

ghost commented Feb 17, 2017

Hey thanks for the reply . I have tried this code #59 and I am getting the same error. Any idea why its happening.

@pmorissette
Copy link
Owner

Hey @potholiday ,

Can you try running this code:

import bt
import pandas as pd

data = bt.get('HYG', start = '2000-01-01')

sma = data.rolling(200).mean()
plot = bt.merge(data,sma).plot(figsize=(15,5))

signal = data > sma

s = bt.Strategy('above200sma', [
        bt.algos.SelectWhere(data > sma),
        bt.algos.WeighEqually(), 
        bt.algos.Rebalance()])

t = bt.Backtest(s, data)
res = bt.run(t)
res.display()

This works fine on my end. Also, have you updated to the latest version of bt? You can run:

pip install --upgrade bt

Let me know if you still run into the issue.

Cheers,
Phil

@ghost
Copy link
Author

ghost commented Feb 18, 2017

Thank you it worked without upgrading. Can I ask you one more help? The above code is a long only strategy

s = bt.Strategy('above200sma', [
        bt.algos.SelectWhere(data > sma),
        bt.algos.WeighEqually(), 
        bt.algos.Rebalance()])

for short strategy I can use this

signal = data < sma

s = bt.Strategy('abovesma200', [
        bt.algos.SelectWhere(data < sma),
        bt.algos.WeighEqually(), 
        bt.algos.Rebalance()])

what kind of changes needed for a long and short strategy

long signal= data> sma  
short signal  data<sma

@ghost
Copy link
Author

ghost commented Feb 19, 2017

@pmorissette Can you please answer my last question that is what is the right way to get both long and short signals together and pass it to the strategy in bt.

The doc for SelectWhere is not useful
http://pmorissette.github.io/bt/bt.html#bt.core.Algo

@pmorissette
Copy link
Owner

Hey @potholiday,

The simplest way is to use bt.algos.WeighTarget. It's similar to SelectWhere, but you end up setting the target weights directly.

See http://pmorissette.github.io/bt/examples.html#sma-crossover-strategy - I think this is what you are looking for. Basically, you tell bt what you want your portfolio to look like at each point in time (weights), and it will take care of the rebalancing.

Cheers,
Phil

@ghost
Copy link
Author

ghost commented Feb 20, 2017

Ok so is this correct . its not showing any error.

import bt
import pandas as pd

data = bt.get('HYG', start = '2000-01-01')

sma = data.rolling(200).mean()
plot = bt.merge(data,sma).plot(figsize=(15,5))

tw = sma.copy()


tw[data> sma] = 1.0
tw[data <= sma] = -1.0
tw[sma.isnull()] = 0.0

cross = bt.Strategy('ma_cross', [bt.algos.WeighTarget(tw),
                                    bt.algos.Rebalance()])
t = bt.Backtest(cross, data)

res = bt.run(t)

res.display()

And also what is the best way to pass multiple parameters

import bt
import pandas as pd

data = bt.get('HYG', start = '2000-01-01')

sma50 = data.rolling(50).mean()
sma200 = data.rolling(200).mean()
plot = bt.merge(data,sma200).plot(figsize=(15,5))

tw = sma200.copy()


tw[data> sma50&data>sma200] = 1.0
tw[data <= sma50|data<=sma200] = -1.0
tw[sma200.isnull()] = 0.0

cross = bt.Strategy('ma_cross', [bt.algos.WeighTarget(tw),
                                    bt.algos.Rebalance()])
t = bt.Backtest(cross, data)

res = bt.run(t)

res.display()

Tried this but its showing error

TypeError: ufunc 'bitwise_and' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''
TypeErrorTraceback (most recent call last)

If I want to get Best Month,Best Year,Yearly Mean,Total Return ,Max Drawdown as a separate variable or as a list what should I do. Tried this res.display()[1] but no luck.

Sorry for asking this many doubts but their are not many good tutorials available for bt

@pmorissette
Copy link
Owner

You will have to put ( ) around each condition. This is a pandas thing. For the stats, see res.stats. Once again, to extract individual stats, refer to the pandas documentation.

Hope this helps,
Phil

@ghost
Copy link
Author

ghost commented Feb 21, 2017

Thank you for clearing my doubts and taking your valuable time for explaining it. Can you please suggest any community or forum where bt is actively discussed such a way that I can ask these trivial doubts there. Github is a development platform and I think I should avoid asking such questions here.

@pmorissette
Copy link
Owner

Hey @potholiday,

I am not aware of any such places. I just wrote this code a while back and put it up on Github for others to use / report bugs / submit PRs, etc.

My advice, if you are interested in this library, is to get comfortable with Python, pandas and numpy. You can also browse through bt's code to get an idea on how the library works (if you want to write your own algos).

You might want to check out zipline, another python backtesting framework used to power Quantopian. I'm sure this project has a bigger community / more tutorials, but might not have the same features / structure as bt.

Hope this helps,
Phil

@ghost
Copy link
Author

ghost commented Feb 22, 2017

I have tired many other back-testing modules in python before; quantopian, backtrader, pyalgotrader are a few but I find your backtester bt to be simpler to code comparing to others. This really is an awesome backtester but its really sad that not many people doesn't know about it.

Can I give you a small recommendation, As well as using this example can you add much simpler code to your example page. Your example code is a little bit complex for an average programmer like me but the below code I can understand easily.

import bt
import pandas as pd

data = bt.get('HYG', start = '2000-01-01')

sma50 = data.rolling(50).mean()
sma200 = data.rolling(200).mean()
plot = bt.merge(data,sma200).plot(figsize=(15,5))

tw = sma200.copy()


tw[(data> sma50)&(data>sma200)] = 1.0
tw[(data <= sma50)|(data<=sma200)] = -1.0
tw[sma200.isnull()] = 0.0

cross = bt.Strategy('ma_cross', [bt.algos.WeighTarget(tw),
                                    bt.algos.Rebalance()])
t = bt.Backtest(cross, data)

res = bt.run(t)

res.display()
res.stats

Any way keep up the good work and once more thank you for taking your valuable time

@pmorissette
Copy link
Owner

Hey @potholiday,

Thanks for the kind words and I'm glad to hear you like bt! I will add your example to the documentation website as I'm sure it will help others.

Thanks again,
Phil

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

1 participant