简体   繁体   English

如何使用 ccxt 为 backtrader 创建 pandasData

[英]How to created pandasData for backtrader with ccxt

I am trying to back test a strategy using backtrader.我正在尝试使用 backtrader 对策略进行回测。 Most of the examples I have seen only are using a csv file.我看到的大多数示例仅使用 csv 文件。 I was wondering if it is possible to just get data from an exchange and turn it into pandas dataframe and then use backtrader?我想知道是否可以从交易所获取数据并将其转换为 pandas dataframe 然后使用反向交易者? When I run it I get an error AttributeError: 'numpy.int64' object has no attribute 'lower' which is refers to pandafeed.py当我运行它时,我得到一个错误AttributeError: 'numpy.int64' object has no attribute 'lower'这是指pandafeed.py

import ccxt,os
from dotenv import load_dotenv
import backtrader

class Trader:
    def __init__(self) -> None:
        load_dotenv()
        self.connect()

    """ Creates Binance client """
    def connect(self):
        self.exchange = ccxt.binance({
            'apiKey': os.getenv('BINANCE_API_KEY'),
            'secret': os.getenv('BINANCE_API_SECRET')
        })

klines = Trader().exchange.fetch_ohlcv(symbol=trading_pair,timeframe=interval)
dataFrame = pd.DataFrame(klines)
dataFrame[0] = [datetime.fromtimestamp(t/1000) for t in dataFrame[0]]
data = backtrader.feeds.PandasData(dataname=dataFrame)

cerebro = backtrader.Cerebro()
cerebro.broker.set_cash(10000)
cerebro.adddata(data)
cerebro.run()

If I use a column name and change my code to the below如果我使用列名并将代码更改为以下

colums = ['datetime', 'open','high', 'low', 'close', 'volume']
dataFrame = pd.DataFrame(klines, columns=colums)
dataFrame["datetime"] = [datetime.fromtimestamp(t/1000) for t in dataFrame["datetime"]]
data = backtrader.feeds.PandasData(dataname=dataFrame)

I get this error我收到这个错误

AttributeError: 'int' object has no attribute 'to_pydatetime' AttributeError: 'int' object 没有属性 'to_pydatetime'

pandafeed.py 熊猫饲料.py

My question is: how do I turn a list into something which I can use to run backtrader?我的问题是:如何将列表变成可以用来运行反向交易者的东西? thank you.谢谢你。

PS an example data structure return by klines will be like PS由klines返回的示例数据结构将类似于

[ [1621152000000, 49375.28, 49795.89, 48656.0, 49014.99, 10956.006583], [1621166400000, 49014.99, 49249.06, 47566.01, 47727.26, 14166.961995], [1621180800000, 47727.26, 48097.59, 44444.44, 45549.26, 36819.653456], [1621195200000, 45553.24, 46480.0, 43825.39, 46431.5, 28724.055984], [1621209600000, 46426.83, 46686.0, 42777.0, 42915.46, 28171.858447], [1621224000000, 42915.46, 45400.0, 42196.97, 45149.18, 40557.45817], [1621238400000, 45143.28, 45800.0, 44291.84, 45731.39, 23851.50751], [1621252800000, 45733.55, 45791.04, 43156.0, 43362.75, 23137.989315], [1621267200000, 43357.0, 44400.0, 42001.0, 44197.73, 30883.162039], [1621281600000, 44197.73, 44939.2, 42500.0, 43538.04, 20055.197255], [1621296000000, 43538.02, 45281.34, 43150.79, 44779.83, 19252.919453], [1621310400000, 44774.78, 45799.29, 44738.26, 45172.7, 17218.430549], [1621324800000, 45172.69, 45420.0, 44607.08, 45225.71, 8427.020047] ] [ [1621152000000, 49375.28, 49795.89, 48656.0, 49014.99, 10956.006583], [1621166400000, 49014.99, 49249.06, 47566.01, 47727.26, 14166.961995], [1621180800000, 47727.26, 48097.59, 44444.44, 45549.26, 36819.653456], [1621195200000, 45553.24, 46480.0, 43825.39, 46431.5, 28724.055984], [1621209600000, 46426.83, 46686.0, 42777.0, 42915.46, 28171.858447], [1621224000000, 42915.46, 45400.0, 42196.97, 45149.18, 40557.45817], [1621238400000, 45143.28, 45800.0, 44291.84, 45731.39, 23851.50751], [ 1621252800000, 45733.55, 45791.04, 43156.0, 43362.75, 23137.989315], [1621267200000, 43357.0, 44400.0, 42001.0, 44197.73, 30883.162039], [1621281600000, 44197.73, 44939.2, 42500.0, 43538.04, 20055.197255], [1621296000000, 43538.02, 45281.34, 43150.79, 44779.83,19252.919453],[1621310400000,44774.78,45799.29,44738.26,45172.7,17218.430549],[1621324800000

I think backtrader is trying to read headers where there are none.我认为反向交易者正在尝试读取没有标题的标题。 Try telling PandasData there are no headers.尝试告诉 PandasData 没有标题。 See the docs查看文档

data = backtrader.feeds.PandasData(dataname=dataFrame, header=None)

I got the same issue, and after debug it i found it append because backtrader expect your dataframe to be indexed by the datetime (to make it simple).我遇到了同样的问题,在调试后我发现它 append 因为反向交易者希望您的 dataframe 被日期时间索引(为了简单起见)。 Then to solve it, just add to your code:然后要解决它,只需添加到您的代码中:

...
dataFrame = dataFrame.set_index('datetime')
...

In my case I also had also to change the type of the 'datetime' field and I did by:在我的情况下,我还必须更改“日期时间”字段的类型,我这样做了:

...
dataFrame["datetime"] = dataFrame["datetime"].values.astype(dtype='datetime64[ms]')
dataFrame = dataFrame.set_index('datetime')
...

I hope that will help you, even if you asked that question 2 months ago.我希望这会对你有所帮助,即使你在 2 个月前问过这个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM