首页 > 解决方案 > Python 错误:预期的 str、字节或 os.PathLike 对象,而不是 _io.TextIOWrapper

问题描述

我认为错误来自加载的 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

有人可以告诉我错误来自哪里吗?

非常感谢

标签: python

解决方案


推荐阅读