Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
94 commits
Select commit Hold shift + click to select a range
3313cb2
Update WsPrivateTest.py
okxapi Dec 15, 2022
3b79325
Update WsPublicTest.py
okxapi Dec 15, 2022
2830a6e
Update WsPrivateTest.py
okxapi Dec 15, 2022
ed9d820
Update WsClientFactory.py
okxapi Dec 15, 2022
7122e14
Update WsConnectManager.py
okxapi Dec 15, 2022
48373d9
Update WsPrivate.py
okxapi Dec 15, 2022
59f2b56
Update WsPublic.py
okxapi Dec 15, 2022
0fbcb4f
Update README.md
okxapi Dec 16, 2022
41b1950
Update README.md
okxapi Dec 16, 2022
24efd1f
enriched README.md
Feb 15, 2023
ccfc453
modified readme
okxapi Feb 16, 2023
7fdd902
modified readme
okxapi Feb 16, 2023
e472ac4
modified readme
okxapi Feb 16, 2023
93ef09f
modified readme
okxapi Feb 16, 2023
617081b
update README
PerryZhu Feb 16, 2023
48fd15e
update requirements.txt
PerryZhu Feb 16, 2023
3df5503
update version
PerryZhu Feb 16, 2023
ee870fe
add setup.py
PerryZhu Feb 16, 2023
19dc436
Update trade_derivatives_en.ipynb
okxapi Mar 9, 2023
6867bfe
Update trade_derivatives_en.ipynb
okxapi Mar 9, 2023
a0a946b
Update get_started_en.ipynb
okxapi Mar 9, 2023
48d29f1
Update trade_derivatives_en.ipynb
okxapi Mar 9, 2023
cdb0a21
Update get_started_en.ipynb
okxapi Mar 9, 2023
317fb09
Update get_started_en.ipynb
okxapi Mar 9, 2023
709947b
Update get_started_en.ipynb
okxapi Mar 9, 2023
5b9acb6
httpx client not varify and no timeout
Mar 16, 2023
e4b66a4
Merge pull request #3 from Hearty07/master
okxapi Mar 16, 2023
7f9a76d
fix request path of server timestamp
Mar 17, 2023
317ccc9
retry while http request failed
Mar 20, 2023
1c8b51a
show traceback msg while exception
Mar 20, 2023
3084294
re connect while reaching the max request times
Mar 20, 2023
b47f519
retry in 1 second but not raise exception
Mar 21, 2023
b414a60
Merge pull request #8 from Hearty07/master
okxapi Mar 23, 2023
e3fa532
update version
PerryZhu Mar 23, 2023
bb36351
Update get_started_en.ipynb
okxapi Mar 27, 2023
522c7b6
Update trade_derivatives_en.ipynb
okxapi Mar 27, 2023
7b19463
Update get_started_en.ipynb
okxapi Mar 27, 2023
f5e0b6d
Update get_started_en.ipynb
okxapi Mar 28, 2023
f6338c1
Update trade_derivatives_en.ipynb
okxapi Mar 28, 2023
806924e
update dependence and version
PerryZhu Mar 28, 2023
c9e9de6
Merge remote-tracking branch 'origin/master'
PerryZhu Mar 28, 2023
4594cda
delete retry logic
PerryZhu Mar 28, 2023
c8496e5
fixed typo
okxapi Mar 28, 2023
cf81854
Add the wdId parameter to the get_withdrawal_history interface.
Sharp-Lee Apr 9, 2023
8adde22
Merge pull request #14 from Sharp-Lee/master
okxapi Apr 11, 2023
0afee92
update version
PerryZhu Apr 12, 2023
fcd7f52
delete unnecessary log
PerryZhu May 11, 2023
ceadad7
jzh
Jun 16, 2023
9f95fbb
update
Jun 19, 2023
ed77fed
upgrade
Jun 19, 2023
42ea0c3
upgrade
Jun 19, 2023
55f44a3
Delete .DS_Store
okxapi Jun 19, 2023
872d924
Delete test/__pycache__ directory
okxapi Jun 19, 2023
8bd3c5a
Delete .DS_Store
okxapi Jun 19, 2023
f989505
delete unusefull doc
PerryZhu Jun 19, 2023
ec58e88
Delete okx/websocket/__pycache__ directory
okxapi Jun 19, 2023
3137cfe
Merge branch 'master' of github.com:okxapi/python-okx
PerryZhu Jun 19, 2023
a09940e
add git ignore
PerryZhu Jun 19, 2023
9266813
add git ignore
PerryZhu Jun 19, 2023
b136361
add spread
Aug 14, 2023
37782e0
Update README.md
okxapi Oct 8, 2023
1e7bdfa
update version
Oct 9, 2023
7cf0ca5
fix
Oct 18, 2023
12e983d
fix: throws errors when we use the URLs for the demo trading environment
007live Dec 26, 2023
cc2d1f7
fix: client parameter exception
007live Dec 27, 2023
9006434
Update getting ts from _get_timestamp() (#33)
kkathena Jan 8, 2024
7fd4543
fix: Python install python-okx --upgrade error && Error in file name
007live Jan 8, 2024
71504d3
Merge remote-tracking branch 'origin/master'
007live Jan 8, 2024
db0bbb5
Update WsPrivateAsyncTest.py
okxapi Jan 18, 2024
068c63d
feat: upgrade for proxy
007live Jan 25, 2024
422ee7e
feat:Support multiply trigger TP
007live Jan 28, 2024
7d43159
feat: supported position builder
007live Mar 7, 2024
d95d1ae
update Funding non_tradable_assets
Nort007 May 23, 2023
c4e7a98
Rename ORDERS_ALGO_OENDING to ORDERS_ALGO_PENDING
WoLeo-Z Aug 25, 2023
a10a7f1
Rename ORDERS_ALGO_OENDING to ORDERS_ALGO_PENDING
WoLeo-Z Aug 25, 2023
e2e7b4f
updated typo in constant and reference
dalton-cole Nov 15, 2023
26956b9
fix: #55 incorrect timestamp format in 'OK-ACCESS-TIMESTAMP' header w…
grzhan Mar 17, 2024
0df4058
remove use_server_time param
007live May 9, 2024
ef7e4f3
Merge remote-tracking branch 'origin/master'
007live May 9, 2024
2a796ab
add new api and extend new apram
007live May 31, 2024
f983c5b
supported new features, fixed loan & earn
007live Aug 7, 2024
7420e2a
fix get_max_loan
Oct 17, 2024
6cd392d
upgrade
Dec 9, 2024
9313397
upgrade
Dec 26, 2024
1711d62
upgrade
Mar 31, 2025
a3e7a8b
upgrade
Mar 31, 2025
472a5ed
upgrade
Apr 29, 2025
564208c
upgrade
May 12, 2025
53f5582
Update README.md
okxapi May 29, 2025
6d4bcf7
upgrade
Jul 22, 2025
c5d834d
add sol-staking and update endpoint params
alex-okx Jul 28, 2025
e0be0b1
Initialize websocket in constructor to avoid AttributeError
BlazeStorm001 Jun 4, 2025
a5af471
fix bug, func amend_algo_order lost params
Worldworld Oct 31, 2024
dcd479b
add begin and end params
oprudnikov Aug 28, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file removed .DS_Store
Binary file not shown.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
build/
dist/
python_okx.egg-info/
*.DS_Store
**/__pycache__
**/**/__pycache__

### STS ###
.apt_generated
Expand Down Expand Up @@ -29,4 +32,4 @@ build/

### VS Code ###
.vscode/
setup.py
id_rsa*
154 changes: 36 additions & 118 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,143 +1,64 @@
[TOC]
### Overview
This is an unofficial Python wrapper for the [OKX exchange v5 API](https://www.okx.com/okx-api)

### 如何使用?
If you came here looking to purchase cryptocurrencies from the OKX exchange, please go [here](https://www.okx.com/).

`python版本:>=3.9
#### Source code
https://github.com/okxapi/python-okx
#### API trading tutorials
- Spot trading: https://www.okx.com/help/how-can-i-do-spot-trading-with-the-jupyter-notebook
- Derivative trading: https://www.okx.com/help/how-can-i-do-derivatives-trading-with-the-jupyter-notebook

`WebSocketAPI:autobahn.twisted>=22.10.0`
Make sure you update often and check the [Changelog](https://www.okx.com/docs-v5/log_en/) for new features and bug fixes.

#### 第一步:下载SDK,安装相关所需库
### Features
- Implementation of all Rest API endpoints.
- Private and Public Websocket implementation
- Testnet support
- Websocket handling with reconnection and multiplexed connections

1.1 下载`python SDK`

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

1.2 安装所需库

```python
pip install requests
pip install autobahn\[twisted\]
pip install pyOpenSSL
```

#### 第二步:配置个人信息

2.1 如果还未有API,可[点击](https://www.okx.com/account/users/myApi)前往官网进行申请

```python
api_key = ""
secret_key = ""
passphrase = ""
```

#### 第三步:调用接口

* RestAPI

* 运行`example.py`

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

* WebSocketAPI
* 参考Test文件夹下`WsPrivate`和`WsPublic`文件示例;
* 根据`公共频道`/`私有频道`选择对应`url`(如果是私有频道需要设置登陆信息),传入相应参数即可。

```python
# WebSocket公共频道
url = "wss://ws.okx.com:8443/ws/v5/public"
# WebSocket私有频道
url = "wss://ws.okx.com:8443/ws/v5/private"
```

```python
# 公共频道 不需要登录(行情,持仓总量,K线,标记价格,深度,资金费率等)
参考 WsPublicTest.py

# 私有频道 需要登录(账户,持仓,订单等)
参考 WsPrivateTest.py
```

附言:

* 如果对API尚不了解,建议参考`OKX`官方[API文档](https://www.okx.com/docs-v5/zh/)

* 使用RestAPI的用户可以通过参考Test文件夹下的示例,设置正确的参数即可

* 使用WebSocketAPI的用户可以通过参考Test文件夹下的`WsPublicTest.py`和`WsPrivateTest.py`,设置正确的参数即可

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

* `asyncio`、`websockets`文档/`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 ?
### Quick start
#### Prerequisites

`python version:>=3.9`

`WebSocketAPI: autobahn.twisted>=22.10.0`

#### Step 1: Download the SDK and install the necessary libraries

1.1 Download python SDK
`WebSocketAPI: websockets package advise version 6.0`

- `Clone` or `Download` the SDK directory to your local directory,choose to use `okx-python-sdk-api-v5`
#### Step 1: register an account on OKX and apply for an API key
- Register for an account: https://www.okx.com/account/register
- Apply for an API key: https://www.okx.com/account/users/myApi

1.2 Install the necessary libraries
#### Step 2: install python-okx

```python
pip install requests
pip install autobahn\[twisted\]
pip install pyOpenSSL
pip install python-okx
```

#### Step 2: Configure Personal Information

2.1 If you have no API,[Click here](https://www.okx.com/account/users/myApi) to the official websit to apply for the API

2.2 Fill out all necessary informatiuon in `example.py(RestAPI)` and `websocket_example.py(WebSocketAPI)`
#### Step 3: Run examples

- Fill in API credentials in the corresponding examples
```python
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
- For spot trading: run example/get_started_en.ipynb
- For derivative trading: run example/trade_derivatives_en.ipynb
- Tweak the value of the parameter `flag` (live trading: 0, demo trading: 1
) to switch between live and demo trading environment
- WebSocketAPI
- Open `websocket_example.py`
- According to the `public channel`/`private channel`, select the corresponding `url`, the corresponding start method, and pass in the corresponding parameters

```python
# 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"
```
- Run test/WsPrivateTest.py for private websocket channels
- Run test/WsPublicTest.py for public websocket channels
- Use different URLs for different environment
- Live trading URLs: https://www.okx.com/docs-v5/en/#overview-production-trading-services
- Demo trading URLs: https://www.okx.com/docs-v5/en/#overview-demo-trading-services

P.S.
Note

- If you know little about API, advise consulting the offical [API document](https://www.okx.com/docs-v5/en/)
- To learn more about OKX API, visit official [OKX API documentation](https://www.okx.com/docs-v5/en/)

- 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
- If you face any questions when using `WebSocketAPI`,you can consult the following links

- `asyncio`、`websockets` document/`github`:

Expand All @@ -153,6 +74,3 @@ P.S.
https://github.com/Rapptz/discord.py/issues/1996
https://github.com/aaugustin/websockets/issues/587
```



Loading