api - 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
解决方案
推荐阅读
- geom-bar - 如何订购离散的 x 轴?
- android - IF 条件为真,但程序执行 else
- php - 尽管在本地服务器上工作,但在托管服务器上上传 Laravel 图像不起作用
- jquery - 如何强制在 laravel 中对 AJAX 请求返回 HTTP 响应
- c - 等距瓷砖拾取/选择算法
- javascript - 我可以暂停函数的执行,直到在节点中接收到某个值?
- vb.net - 如何让我的应用程序在启动时检查更新然后下载?
- c# - Url.Page 创建错误的 url
- javascript - 如何从 Javascript 中的查询 Firebase 中获取密钥
- c# - .Net 标准的自定义 System.Web.HttpContext