question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to run a multi indicator strategy in bt?

See original GitHub issue

I want to run a multi indicator strategy. I want to long a stock when stock price > sma and also RSI > 70 else short the stock if stock price < sma and RSI <30 . I have tried this way

import bt
import numpy as np

# download data
data = bt.get('aapl', start='2018-01-01')

# calculate moving average DataFrame using pandas' rolling_mean
import pandas as pd
# a rolling mean is a moving average, right?
data['sma'] = data.rolling(50).mean()


import talib as ta

d=data['aapl'].values.reshape(len(data))
data['rsi']=(ta.RSI(d))

signal=(data['aapl']>data['sma']) & (data['rsi']>70)

s = bt.Strategy('multi', [bt.algos.SelectWhere(signal),
                               bt.algos.WeighEqually(),
                               bt.algos.Rebalance()])

# now we create the Backtest
t = bt.Backtest(s, data)

res = bt.run(t)

This doesn’t work?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:8

github_iconTop GitHub Comments

1reaction
davidhuang123commented, Jul 12, 2019

It seems it’s easier to dissect your strategy into 2 strategies. You can write 2 script for each and combine them to see the results .

On Wed, Jul 10, 2019 at 13:17 fightthepower notifications@github.com wrote:

@daviduang123 Sorry for the late reply and also thank you for the code. But can I ask one doubt. In the sample SMA strategy http://pmorissette.github.io/bt/examples.html#sma-strategy. The signal signal = data > sma gives true when price is greater than sma

2019-06-18 True 2019-06-19 True 2019-06-20 True 2019-06-21 True 2019-06-24 True 2019-06-25 True 2019-06-26 True 2019-06-27 True 2019-06-28 True 2019-07-01 True 2019-07-02 True 2019-07-03 True 2019-07-05 True 2019-07-08 True 2019-07-09 True

But in my case data[‘signal’]=(data[‘aapl’]>data[‘sma’]) & (data[‘rsi’]>70) signal will only be true when rsi is greater than 70 and when it decreases below 70 it becomes False.

What I want is I should buy the aapl stock when rsi is 70 and sell it when it reach rsi 30 a long position. Then when rsi is below 30 go for short that is sell at rsi<30 and buy back this at 70

— You are receiving this because you commented.

Reply to this email directly, view it on GitHub https://github.com/pmorissette/bt/issues/183?email_source=notifications&email_token=AC255UYCNV2QUB6UEGRK7FLP6VWF5A5CNFSM4H6VEUH2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGODZSKEDI#issuecomment-509911565, or mute the thread https://github.com/notifications/unsubscribe-auth/AC255U2IXIOIKPYXMBRHPUDP6VWF5ANCNFSM4H6VEUHQ .

1reaction
davidhuang123commented, Jul 8, 2019

Try below, I guess you need to feed the right format so that the library understand.

import bt import numpy as np import pandas as pd import talib as ta import copy #import copy function

download data

data = bt.get(‘aapl’, start=‘2018-01-01’)

calculate moving average DataFrame using pandas’ rolling_mean

data[‘sma’] = data.rolling(50).mean()

d=data[‘aapl’].values.reshape(len(data)) data[‘rsi’]=(ta.RSI(d))

data = data.fillna(0) data[‘signal’]=(data[‘aapl’]>data[‘sma’]) & (data[‘rsi’]>70)

tw_data = copy.deepcopy(data) #make copy of data without association #Assign fixed weight to column tw_data[‘aapl’]=data[‘signal’]

#drop column Unused data=data.drop([‘sma’,‘rsi’,‘signal’], axis=1) tw_data=tw_data.drop([‘sma’,‘rsi’,‘signal’],axis=1)

s = bt.Strategy(‘multi’, [bt.algos.SelectWhere(tw_data), bt.algos.WeighEqually(), bt.algos.Rebalance()])

now we create the Backtest

t = bt.Backtest(s, data)

res = bt.run(t)

%matplotlib inline res.plot()

Read more comments on GitHub >

github_iconTop Results From Across the Web

Multiple Data Strategy - Backtrader
Create a CrossOver indicator using Yahoo's close price and the Moving Average. And then execute the buy/sell orders on Data Source 1 ...
Read more >
How to combine several strategies in vectorBT? - YouTube
In this video, I show how you can combine several strategies into one signal in VectorBT*Support project on Patreon* ...
Read more >
bt Package — bt 0.2.10 documentation
For example, if we have a strategy that is currently long 100% one security, and the weighing Algo sets the new weight to...
Read more >
Flexible Backtesting with BT - Medium
BT is a flexible backtesting framework for Python used to test quantitative trading strategies. This framework allows you to easily create ...
Read more >
Backtesting a strategy based on simple moving average
In this example, we present two possible approaches: building a trading strategy, using a signal ( bt.Signal ) or defining a full strategy...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found