How to run a multi indicator strategy in bt?
See original GitHub issueI 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:
- Created 4 years ago
- Reactions:1
- Comments:8
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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:
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()