作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我是 backtrader 的新手,我有一个大问题。我想开始我的策略(只是一个简单的 GoldenCross 策略)。这个 GoldenCross.py 脚本如下所示:
import math
import backtrader as bt
class GoldenCross(bt.Strategy):
params = (("fast", 50),
("slow", 200),
("order percentage", 0.95),
("ticker", "AAPL"))
def __init__(self):
self.fast_moving_average = self.bt.indicators.SmoothedMovingAverage(
self.data.close,
period=self.p.fast,
plotname="50 day moving average")
self.slow_moving_average = self.bt.indicators.SmoothedMovingAverage(
self.data.close,
period=self.p.slow,
plotname="200 day moving average")
self.crossover = self.bt.indicators.crossover(self.fast_moving_average, self.slow_moving_average)
def next(self):
pass
import os, sys, argparse
import pandas as pd
import backtrader as bt
from Strategien.GoldenCross import GoldenCross
import datetime
cerebro = bt.Cerebro()
cerebro.broker.setcash(100000)
symbol = "AAPL"
path = "/Users/me/Desktop/allgemein/Visual Studio/Stock Data/S&P500 Aktien 1H/" + symbol + ".csv"
stock_prices = pd.read_csv(path)
feed = bt.feeds.PandasData(dataname=stock_prices)
#(dataname=stock_prices)
cerebro.adddata(stock_prices)
cerebro.addstrategy(GoldenCross)
cerebro.run()
cerebro.plot()
Unnamed: 0 date close high low open
0 0 2017-01-03T15:00:00.000Z 115.450 115.815 115.400 115.600
1 1 2017-01-03T16:00:00.000Z 115.370 115.670 115.135 115.450
2 2 2017-01-03T17:00:00.000Z 115.470 115.525 115.270 115.365
3 3 2017-01-03T18:00:00.000Z 115.235 115.495 115.235 115.475
4 4 2017-01-03T19:00:00.000Z 115.435 115.445 115.160 115.235
... ... ... ... ... ... ...
stock_prices['date'] = pd.to_datetime(stock_prices['date']) #object to datetime
最佳答案
必须添加数据提要(不是 stock_prices)
feed = bt.feeds.PandasData(dataname=stock_prices)
cerebro.adddata(股票价格)
到
feed = bt.feeds.PandasData(dataname=stock_prices)
cerebro.adddata(feed)
对我来说,如果您将日期时间设置为索引并解析日期时间,这将起作用
stock_prices = pd.read_csv(path, index_col='datetime', parse_dates=True)
关于python - 反向交易者错误 : 'DataFrame' object has no attribute 'setenvironment' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62301378/
我是一名优秀的程序员,十分优秀!