首页 > 解决方案 > Deribit 代码对我不起作用.. 有人有什么建议吗?

问题描述

我正在尝试使用 Pycharm 和 Spyder 从 Deribit 收集历史价格/数据,但我不断收到错误。我使用了以下网站的以下代码: https ://www.codearmo.com/python-tutorial/crypto-algo-trading-historical-data1

如果有人有建议的修复程序,那将是一个巨大的帮助。我对编码比较陌生。

谢谢。

import asyncio
import websockets
import json
import pandas as pd
import datetime as dt



async def call_api(msg):
   async with websockets.connect('wss://test.deribit.com/ws/api/v2') as websocket:
       await websocket.send(msg)
       while websocket.open:
           response = await websocket.recv()
           return response



def async_loop(api, message):
    return asyncio.get_event_loop().run_until_complete(api(message))


def retrieve_historic_data(start, end, instrument, timeframe):
    msg = \
        {
            "jsonrpc": "2.0",
            "id": 833,
            "method": "public/get_tradingview_chart_data",
            "params": {
                "instrument_name": instrument,
                "start_timestamp": start,
                "end_timestamp": end,
                "resolution": timeframe
            }
        }
    resp = async_loop(call_api, json.dumps(msg))

    return resp

def json_to_dataframe(json_resp):
    res = json.loads(json_resp)

    df = pd.DataFrame(res['result'])

    df['ticks'] = df.ticks / 1000
    df['timestamp'] = [dt.datetime.fromtimestamp(date) for date in df.ticks]

    return df



if __name__ == '__main__':
    start = 1554373800000
    end = 1554376800000
    instrument = "BTC-PERPETUAL"
    timeframe = '1'

    json_resp = retrieve_historic_data(start, end, instrument, timeframe)

    df = json_to_dataframe(json_resp)
    print(df.head())


Console Message:

/Users/macbookair/PycharmProjects/untitled/venv/bin/python /Users/macbookair/PycharmProjects/Deribit01/Deribit_Options_01.py
Traceback (most recent call last):
  File "/Users/macbookair/PycharmProjects/Deribit01/Deribit_Options_01.py", line 2, in <module>
    import websockets
  File "/Users/macbookair/PycharmProjects/untitled/venv/lib/python3.8/site-packages/websockets/__init__.py", line 4, in <module>
    from .client import *  # noqa
  File "/Users/macbookair/PycharmProjects/untitled/venv/lib/python3.8/site-packages/websockets/client.py", line 20, in <module>
    asyncio.get_event_loop().run_until_complete(call_api(json.dumps(msg)))
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/asyncio/base_events.py", line 616, in run_until_complete
    return future.result()
  File "/Users/macbookair/PycharmProjects/untitled/venv/lib/python3.8/site-packages/websockets/client.py", line 13, in call_api
    async with websockets.connect('wss://test.deribit.com/ws/api/v2') as websocket:
AttributeError: partially initialized module 'websockets' has no attribute 'connect' (most likely due to a circular import)

Process finished with exit code 1

标签: apisorting

解决方案


推荐阅读