Python错误:应为str、bytes或os.PathLike对象,而不是_io.TextIOWrapper

2024-06-28 14:30:37 发布

您现在位置:Python中文网/ 问答频道 /正文

我认为错误来自加载的csv文件

我使用的代码是

data = pd.read_csv(r'C:\Users\CCHHR\Advanced-Trading-Analysis-Data.csv')

def TrendStrategyRun1(nfastSMA, nslowSMA, chart):
    instruments = ['SPY']

    feed = csvfeed.GenericBarFeed(bar.Frequency.DAY)
    feed.addBarsFromCSV(
        instruments[0],
        data,
        skipMalformedBars=True
    )

    trendStrategy1 = TrendStrategy(
        feed,
        instrument[0],
        nfastSMA,
        nslowSMA
    )

    trendStrategy1.getBroker().setCommission(
        broker.backtesting.FixedPerTrade(6)
    )

    retAnalyzer = ret.Returns(maxLen=2518)
    trendStrategy1.attachAnalyzer(retAnalyzer)

    plt = plotter.StrategyPlotter(
        trendStrategy1,
        plotPortfolio=False
    )

    plt.getInstrumentSubplot('SPY').addDataSeries(
        'Fast SMA',
        trendStrategy1.getfastSMA()
    )

    plt.getInstrumentSubplot('SPY').addDataSeries(
        'Slow SMA',
        trendStrategy1.getslowSMA()
    )

    trendStrategy1.run()

    datesReturns = retAnalyzer.getReturns().getDateTimes()[:]
    dailyReturns = retAnalyzer.getReturns()[:]
    dailyReturns = pd.DteFrame(dailyReturns).set_index(
        pd.DatetimeIndex(datesReturns)
    )

    if chart == True:
        plt.plot(
            fromDateTime=dt.datetime(2016, 1, 1),
            toDateTime=dt.datetime(2016, 12, 31)
        )

    return dailyReturns


TrendStrategyRun1(5, 20, True)

我得到了下面的错误信息

expected str, bytes or os.PathLike object, not _io.TextIOWrapper

有人能告诉我错误来自哪里吗

非常感谢


Tags: csvtruedatafeed错误chartpltpd