簡體   English   中英

ValueError:日期超出了 backtrader 中發生的月份的范圍

[英]ValueError: day is out of range for month occuring in backtrader

請幫助解決此錯誤,這是我將自己的 csv 數據提供給 backtrader 時遇到的錯誤。

數據樣本如下

csv數據樣本

我是 python 和 python 社區的新手

錯誤:

C:\\Python\\python.exe C:\\Users\\harif\\PycharmProjects\\untitled\\venv\\learning\\dataframe1.py Traceback(最近一次調用):文件“C:\\Users\\harif\\PycharmProjects\\untitled\\venv\\learning \\dataframe1.py", line 32, in fromdate=datetime(2, 10, 2016), ValueError: day is out of range for month

進程以退出代碼 1 結束

import backtrader as bt


# Create a subclass of Strategy to define the indicators and logic

class SmaCross(bt.Strategy):
    # list of parameters which are configurable for the strategy
    params = dict(
        pfast=10,  # period for the fast moving average
        pslow=30  # period for the slow moving average
    )

    def __init__(self):
        sma1 = bt.ind.SMA(period=self.p.pfast)  # fast moving average
        sma2 = bt.ind.SMA(period=self.p.pslow)  # slow moving average
        self.crossover = bt.ind.CrossOver(sma1, sma2)  # crossover signal

    def next(self):
        if not self.position:  # not in the market
            if self.crossover > 0:  # if fast crosses slow to the upside
                self.buy()  # enter long

        elif self.crossover < 0:  # in the market & cross to the downside
            self.close()  # close long position


cerebro = bt.Cerebro()  # create a "Cerebro" engine instance

# Create a data feed
data = bt.feeds.GenericCSVData(dataname=r'C:\Hard_Drive\Tick_Data\tickdata_2\sp.csv',
                               fromdate=datetime(2, 10, 2016),
                               todate=datetime(1, 10, 2019))
print(data)
# cerebro.adddata(data)  # Add the data feed

# cerebro.addstrategy(SmaCross)  # Add the trading strategy
# cerebro.run()  # run it all
# cerebro.plot()  # and plot it with a single command

日期時間對象按此順序接受值。

datetime(year, month, day[, hour[, minute[, second[, microsecond[,tzinfo]]]]]

有 2 種方法可以解決此問題。

  1. 您可以按正確的順序寫入值。 例如: datetime(2016, 10, 2)
  2. 您可以指定哪個值是什么。 例如: datetime(day=2, month=10, year=2016),

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM