Backtrader error: 'DataFrame' object has no attribute 'setenvironment'

我只是一个虾纸丫 提交于 2021-01-07 01:39:54

问题


I am new to backtrader, and I have a big problem. I want to start my strategy (just a simple GoldenCross strategy). This GoldenCross.py Script looks like this:

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

Now I want to run the strategy with my run.py script. In this script the code looks like this:

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()

Now the visual studio compiler gives me back an error called: "AttributeError: 'DataFrame' object has no attribute 'setenvironment'".

I don't know what's the problem. Maby the problem is in my csv data.. My Date column looks like this:

     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
...          ...                       ...      ...      ...      ...      ...

But I already tried to convert this date to date time with using :

stock_prices['date'] = pd.to_datetime(stock_prices['date']) #object to datetime

But this doesn't change the problem either..

Does anyone have a good tip for me?

best regards Christian


回答1:


must adddata feed (not stock_prices) feed = bt.feeds.PandasData(dataname=stock_prices) cerebro.adddata(stock_prices)

to

feed = bt.feeds.PandasData(dataname=stock_prices)
cerebro.adddata(feed)

for me this works if you set datetime as index and parse datetime

stock_prices = pd.read_csv(path, index_col='datetime', parse_dates=True)



回答2:


Start with removing self from self.bt.whatever... bt is not a member of self here. Additionally you've not called the correct crossover indicator. The name should be camel cased. Try:

class GoldenCross(bt.Strategy):
    params = (("fast", 50),
                ("slow", 200),
                ("order percentage", 0.95),
                ("ticker", "AAPL"))

    def __init__(self):
        self.fast_moving_average = bt.indicators.SmoothedMovingAverage(
           self.data.close,
           period=self.p.fast,
           plotname="50 day moving average")
        self.slow_moving_average = bt.indicators.SmoothedMovingAverage(
           self.data.close,
           period=self.p.slow,
           plotname="200 day moving average")
        self.crossover = bt.indicators.CrossOver(self.fast_moving_average, self.slow_moving_average) 


来源:https://stackoverflow.com/questions/62301378/backtrader-error-dataframe-object-has-no-attribute-setenvironment

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!