我正在尝试通过使用其网页上bt
提供的示例之一使用该库对交易策略进行回测(由于该功能不起作用,我没有检索所提供的数据。但这是我正在使用的代码(我知道有些处理很混乱,但它完成了工作):bt.get()
df
import bt, pandas as pd, yfinance as yf, datetime as dt, os, requests
from pandas_datareader import data as pdr
yf.pdr_override
ticks = ['AAPL','MSFT','C','GS','GE']
data = tdaph(ticks[0])
data = data.set_index('datetime')
data[ticks[0]] = data['close']
data = data.drop('close',axis=1)
for i in range(1,len(ticks)):
aa = tdaph(ticks[i])
aa = aa.set_index('datetime')
data[ticks[i]] = aa['close']
print('done')
data.index = pd.to_datetime(data.index)
data = data.astype('float')
sma = data.rolling(50).mean()
class SelectWhere(bt.Algo):
def __init__(self, signal):
self.signal = signal
def __call__(self, target):
if target.now in self.signal.index:
sig = self.signal.loc[target.now]
selected = list(sig.index[sig])
target.temp['selected'] = selected
print('hecho')
s = bt.Strategy('above50sma', [SelectWhere(data > sma),
bt.algos.WeighEqually(),
bt.algos.Rebalance()])
print('strat hecha')
t = bt.Backtest(strategy=s,data=data)
print('backtest hecho')
res = bt.run(t)
但是在运行该bt.run()
函数时,我收到以下 RuntimeWarning 消息:
Warning (from warnings module):
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/ffn/core.py", line 2299
res = np.divide(er.mean(), std)
RuntimeWarning: invalid value encountered in true_divide
关于可能是什么问题的任何想法?提前感谢任何人。