Go to file
oker 17cafca909 调整描述 2022-10-21 18:07:53 +08:00
example 调整描述 2022-10-21 18:07:53 +08:00
okx first commit 2022-10-12 10:53:50 +08:00
test 调整外层文件夹 2022-10-12 11:31:08 +08:00
.DS_Store first commit 2022-10-12 10:53:50 +08:00
.gitignore add README 2022-10-12 11:23:11 +08:00
README.md add README 2022-10-12 11:21:18 +08:00
http2_example.py first commit 2022-10-12 10:53:50 +08:00
websocket_example.py first commit 2022-10-12 10:53:50 +08:00

README.md

[TOC]

如何使用?

python版本3.6 - 3.8

WebSocketAPI建议websockets库版本为6.0

第一步下载SDK安装相关所需库

1.1 下载python SDK

  • 将SDK目录Clone或者Download到本地,选择使用okx-python-sdk-api-v5即可

1.2 安装所需库

pip install requests
pip install websockets==6.0

第二步:配置个人信息

2.1 如果还未有API点击前往官网进行申请

2.2 将各项信息在example.pyRestAPIwebsocket_example.pyWebSocketAPI中填写

api_key = ""
secret_key = ""
passphrase = ""

第三步:调用接口

  • RestAPI

    • 运行example.py

    • 解开相应方法的注释传参调用各接口即可

  • WebSocketAPI

    • 运行websocket_example.py

    • 根据公共频道/私有频道/交易选择对应url、对应启动方法,传入相应参数即可

    # WebSocket公共频道
    url = "wss://ws.okx.com:8443/ws/v5/public?brokerId=9999"
    
    # WebSocket私有频道
    url = "wss://ws.okx.com:8443/ws/v5/private?brokerId=9999"
    
    # 公共频道 不需要登录行情持仓总量K线标记价格深度资金费率等
    loop.run_until_complete(subscribe_without_login(url, channels))
    
    # 私有频道 需要登录(账户,持仓,订单等)
    loop.run_until_complete(subscribe(url, api_key, passphrase, seceret_key, channels))
    
    # 交易(下单,撤单,改单等)
    loop.run_until_complete(trade(url, api_key, passphrase, seceret_key, trade_param))
    

附言:

  • 如果对API尚不了解建议参考OKx官方API文档

  • 使用RestAPI的用户可以通过设置example.py文件下的flag参数来选择接入实盘或者模拟盘

  • 使用WebSocketAPI的用户可以通过解开websocket_example.py文件下对应的url注释来选择接入实盘或者模拟盘

  • RestAPI 提供了http2的请求方式,可参考http2_example.py

  • 若使用WebSocketAPI遇到问题建议参考相关链接

    • asynciowebsockets文档/github

      https://docs.python.org/3/library/asyncio-dev.html
      https://websockets.readthedocs.io/en/stable/intro.html
      https://github.com/aaugustin/websockets
      
    • 关于code=1006

      https://github.com/Rapptz/discord.py/issues/1996
      https://github.com/aaugustin/websockets/issues/587
      

How to use ?

python version3.6 - 3.8

WebSocketAPI websockets package advise version 6.0

Step 1: Download the SDK and install the necessary libraries

1.1 Download python SDK

  • Clone or Download the SDK directory to your local directory,choose to use okx-python-sdk-api-v5

1.2 Install the necessary libraries

pip install requests
pip install websockets==6.0

Step 2: Configure Personal Information

2.1 If you have no APIClick here to the official websit to apply for the API

2.2 Fill out all necessary informatiuon in example.pyRestAPI and websocket_example.pyWebSocketAPI

api_key = ""
secret_key = ""
passphrase = ""

Step 3: Call API

  • RestAPI
    • Run example.py
    • Uncomment the corresponding method and then pass the arguments and call the interfaces
  • WebSocketAPI
    • Run websocket_example.py
    • According to the public channel/private channel/trade, select the corresponding url, the corresponding start method, and pass in the corresponding parameters
# WebSocket public channel
url = "wss://ws.okx.com:8443/ws/v5/public?brokerId=9999"

# WebSocket private channel
url = "wss://ws.okx.com:8443/ws/v5/private?brokerId=9999"
# Not necessary for public channel to login Instrument, Tickers, Index, Mark price, Order Book, Funding rate, etc
loop.run_until_complete(subscribe_without_login(url, channels))

# necessary for private channel to loginAccount,Positions, Order, etc
loop.run_until_complete(subscribe(url, api_key, passphrase, seceret_key, channels))

# tradePlace Order, Cancel Order, Amend Order, etc
loop.run_until_complete(trade(url, api_key, passphrase, seceret_key, trade_param))

P.S.

  • If you know little about API, advise consulting the offical API document

  • User with RestAPI can configure parameter flag in example.py in to choose to access to real trading or demo trading

  • User with WebSocketAPI can ucomment the corresponding url to choose to access to real trading or demo trading

  • Rest API support request by http2, you can refer to http2_example

  • If you face any questions when using WebSocketAPI,you can consult related link

    • asynciowebsockets document/github

      https://docs.python.org/3/library/asyncio-dev.html
      https://websockets.readthedocs.io/en/stable/intro.html
      https://github.com/aaugustin/websockets
      
    • About code=1006

      https://github.com/Rapptz/discord.py/issues/1996
      https://github.com/aaugustin/websockets/issues/587