我是反向交易者的新手,我有一个大问题。我想开始我的策略(只是一个简单的 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
现在我想用我的 run.py 脚本运行这个策略。在此脚本中,代码如下所示:
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()
现在,Visual Studio 编译器返回了一个名为“AttributeError: 'DataFrame' object has no attribute 'setenvironment'”的错误。
我不知道有什么问题。可能问题出在我的 csv 数据中。我的日期列如下所示:
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
但这也改变不了问题..
有人对我有什么好的建议吗?
最好的问候克里斯蒂安