pycharm Deribit代码对我不起作用,有人有什么建议吗?

nvbavucw  于 8个月前  发布在  PyCharm
关注(0)|答案(1)|浏览(108)

我试图使用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
efzxgjgh

efzxgjgh1#

我不知道上面的代码有什么问题。但是这里有一个API/网站,你可以在那里获得deribit历史数据。事实上,您可以使用它获得数百个加密货币交易所的历史记录。
基本URL https://api.tokeninsight.com
历史数据终点:api/v1/history/exchanges/{id}
因此,对于deribit,URL将是

curl --request GET \
     --url https://api.tokeninsight.com/api/v1/history/exchanges/deribit \
     --header 'TI_API_KEY: replace here with your own key' \
     --header 'accept: application/json'

在这里应用您自己的密钥(免费):https://tokeninsight-api.readme.io/reference/get-your-api-key-here
那么你就可以得到这样的历史交易量

{
  "status": {
    "code": 0,
    "message": "",
    "timestamp": 1692008153771
  },
  "data": {
    "exchange_name": "Deribit",
    "exid": "deribit",
    "centralized": true,
    "link": "https://tokeninsight.com/en/exchanges/deribit",
    "history_data": [
      {
        "vol_spot_24h": null,
        "vol_derivatives_24h": 100305190,
        "total_volume": 100305190,
        "open_interest": 1315586292,
        "timestamp": 1691971200000
      },
      {
        "vol_spot_24h": null,
        "vol_derivatives_24h": 64857082,
        "total_volume": 64857082,
        "open_interest": 1315120963,
        "timestamp": 1691884800000
      },
      {
        "vol_spot_24h": null,
        "vol_derivatives_24h": 226452034,
        "total_volume": 226452034,
        "open_interest": 1315710576,
        "timestamp": 1691798400000
      },
      {
        "vol_spot_24h": null,
        "vol_derivatives_24h": 232268514,
        "total_volume": 232268514,
        "open_interest": 1334486963,
        "timestamp": 1691712000000
      },
      {
        "vol_spot_24h": null,
        "vol_derivatives_24h": 326144084,
        "total_volume": 326144084,
        "open_interest": 1317615562,
        "timestamp": 1684281600000
      }
    ]
  }
}

相关问题