大陆使用okx接口

简介

背景

我使用的是 MacBook,用 clash 走代理,clash 的config 文件如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# cat ~/.config/clash/config.yaml

#---------------------------------------------------#
## 配置文件需要放置在 $HOME/.config/clash/*.yaml

## 这份文件是clashX的基础配置文件,请尽量新建配置文件进行修改。
## !!!只有这份文件的端口设置会随ClashX启动生效

## 如果您不知道如何操作,请参阅 官方Github文档 https://github.com/Dreamacro/clash/blob/dev/README.md
#---------------------------------------------------#

# (HTTP and SOCKS5 in one port) 原来是7890
mixed-port: 7890
socks-port: 7891 # 启用SOCKS5代理,这个是我自己加的。
# RESTful API for clash
external-controller: 127.0.0.1:9090
allow-lan: false
mode: rule
log-level: warning

proxies:

proxy-groups:

rules:
- DOMAIN-SUFFIX,google.com,DIRECT
- DOMAIN-KEYWORD,google,DIRECT
- DOMAIN,google.com,DIRECT
- DOMAIN-SUFFIX,ad.com,REJECT
- GEOIP,CN,DIRECT
- MATCH,DIRECT

在终端 使用代理命令(本终端生效)

1
2
3
4
5
6
7
8
9
export https_proxy=http://127.0.0.1:7890 
export http_proxy=http://127.0.0.1:7890
export all_proxy=socks5://127.0.0.1:7891

# 依次是 https 走 http://127.0.0.1:7890 代理 ;http 也走 http://127.0.0.1:7890 代理。
# 全局代理 走 socks5://127.0.0.1:7891

lsof -i :7891 # 检查端口监听状态*
lsof -i :7890

读API

看见 okx 的文档中写着

1
2
# 获取交易产品基础信息
GET /api/v5/account/instruments?instType=SPOT

截屏2025-05-27 22.32.51

写脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# cd ~/chainApiUse 
# vim getInstruments.py

import requests

url = "https://www.okx.com/api/v5/public/instruments"
params = {
"instType": "SPOT"
}

try:
response = requests.get(url, params=params, timeout=10)
response.raise_for_status()
print(response.json())
except requests.exceptions.RequestException as e:
print("请求失败:", e)

运行
python getInstruments.py > output.json

从output.json 中可见

截屏2025-05-27 22.46.39

我们修改一下 脚本,让看起来 更加好理解一点

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# vim okx_spot_to_csv.py

import requests
import csv

url = "https://www.okx.com/api/v5/public/instruments"
params = {
"instType": "SPOT"
}

try:
response = requests.get(url, params=params, timeout=10)
response.raise_for_status()
data = response.json().get("data", [])

# 写入 CSV 文件
with open("okx_spot.csv", "w", newline="", encoding="utf-8") as f:
writer = csv.writer(f)
# 写表头
writer.writerow(["交易对", "基础币种", "报价币种", "最小交易量", "精度", "状态", "上线时间戳"])
# 写每一行数据
for item in data:
writer.writerow([
item["instId"],
item["baseCcy"],
item["quoteCcy"],
item["minSz"],
item["tickSz"],
item["state"],
item["listTime"]
])

print("数据已写入 okx_spot.csv")

except requests.exceptions.RequestException as e:
print("请求失败:", e)

运行

okx_spot.csv 中可见

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
SHIB-USDT,SHIB,USDT,100000,0.000000001,live,1620446728000
SKL-USDT,SKL,USDT,100,0.00001,live,1617007316000
SLERF-USDT,SLERF,USDT,10,0.00001,live,1721721600000
SLP-USDT,SLP,USDT,100,0.000001,live,1629354310000
SNT-USDT,SNT,USDT,100,0.00001,live,1611907686000
SNX-USDT,SNX,USDT,1,0.0001,live,1611907686000
SONIC-USDT,SONIC,USDT,10,0.00001,live,1736247600000
SPURS-USDT,SPURS,USDT,1,0.0001,live,1698314400000
SSV-USDT,SSV,USDT,0.1,0.001,live,1677664800000
STETH-USDT,STETH,USDT,0.001,0.01,live,1689674400000
STORJ-USDT,STORJ,USDT,10,0.0001,live,1611907686000
STRK-USDT,STRK,USDT,1,0.0001,live,1708432800000
STX-USDT,STX,USDT,1,0.0001,live,1614853497000
SUI-USDT,SUI,USDT,1,0.0001,live,1683114000000
SUSHI-USDT,SUSHI,USDT,1,0.0001,live,1611907686000
SWEAT-USDT,SWEAT,USDT,100,0.000001,live,1663051473000
SWFTC-USDT,SWFTC,USDT,100,0.000001,live,1611907686000
T-USDT,T,USDT,100,0.00001,live,1648699657000
THETA-USDT,THETA,USDT,1,0.0001,live,1611907686000
TIA-USDT,TIA,USDT,0.1,0.001,live,1698775200000
TNSR-USDT,TNSR,USDT,1,0.0001,live,1712589300000
TRA-USDT,TRA,USDT,1,0.0001,live,1617963219000
TRB-USDT,TRB,USDT,0.01,0.01,live,1611907686000
TRX-USDT,TRX,USDT,1,0.00001,live,1611907686000
TURBO-USDT,TURBO,USDT,1000,0.000001,live,1684742400000
ULTI-USDT,ULTI,USDT,100,0.000001,live,1717668000000
UMA-USDT,UMA,USDT,1,0.001,live,1611907686000
UNI-USDT,UNI,USDT,0.1,0.001,live,1611907686000
USDC-USDT,USDC,USDT,1,0.0001,live,1611907686000
USTC-USDT,USTC,USDT,100,0.00001,live,1653637616000
UXLINK-USDT,UXLINK,USDT,10,0.0001,live,1721289600000
VELO-USDT,VELO,USDT,100,0.000001,live,1615192253000
VELODROME-USDT,VELODROME,USDT,10,0.00001,live,1679040000000
VENOM-USDT,VENOM,USDT,10,0.00001,live,1711353600000
VINE-USDT,VINE,USDT,10,0.00001,live,1737882000000
VRA-USDT,VRA,USDT,100,0.000001,live,1619591733000
W-USDT,W,USDT,10,0.00001,live,1712145600000
WAXP-USDT,WAXP,USDT,100,0.00001,live,1644555324000
WBTC-USDT,WBTC,USDT,0.0001,0.1,live,1611907686000
WCT-USDT,WCT,USDT,10,0.0001,live,1744711200000
WIF-USDT,WIF,USDT,1,0.0001,live,1713171600000
WIN-USDT,WIN,USDT,10000,0.00000001,live,1638426346000
WLD-USDT,WLD,USDT,1,0.0001,live,1690190400000
WOO-USDT,WOO,USDT,10,0.00001,live,1639549699000
X-USDT,X,USDT,10000,0.00000001,live,1729767600000
XAUT-USDT,XAUT,USDT,0.001,0.1,live,1658200770000
XCH-USDT,XCH,USDT,0.1,0.01,live,1620050929000
XLM-USDT,XLM,USDT,10,0.00001,live,1611907686000
XNO-USDT,XNO,USDT,1,0.0001,live,1654683315000
XTZ-USDT,XTZ,USDT,1,0.0001,live,1611907686000
YFI-USDT,YFI,USDT,0.0005,1,live,1611907686000
YGG-USDT,YGG,USDT,10,0.0001,live,1627468177000
ZBCN-USDT,ZBCN,USDT,1000,0.0000001,live,1713254400000
ZENT-USDT,ZENT,USDT,100,0.000001,live,1714975200000
ZERO-USDT,ZERO,USDT,10000,0.00000001,live,1714982400000
ZETA-USDT,ZETA,USDT,1,0.0001,live,1706756400000
ZEUS-USDT,ZEUS,USDT,10,0.0001,live,1712239200000
ZIL-USDT,ZIL,USDT,100,0.00001,live,1611907686000
ZK-USDT,ZK,USDT,10,0.00001,live,1718697600000
ZKJ-USDT,ZKJ,USDT,1,0.0001,live,1718607600000
ZRO-USDT,ZRO,USDT,1,0.001,live,1718884800000
ZRX-USDT,ZRX,USDT,10,0.0001,live,1611907686000
BTC-USDC,BTC,USDC,0.00001,0.1,live,1611907686000
ETH-USDC,ETH,USDC,0.0001,0.01,live,1611907686000
OKB-USDC,OKB,USDC,0.1,0.01,live,1611907686000
SOL-USDC,SOL,USDC,0.01,0.01,live,1653285156000
TON-USDC,TON,USDC,0.1,0.001,live,1656387593000
DOGE-USDC,DOGE,USDC,10,0.00001,live,1653285260000
XRP-USDC,XRP,USDC,1,0.0001,live,1611907686000
PEPE-USDC,PEPE,USDC,100000,0.000000001,live,1715670000000
1INCH-USDC,1INCH,USDC,10,0.0001,live,1655787020000
AAVE-USDC,AAVE,USDC,0.01,0.01,live,1654583929000
ADA-USDC,ADA,USDC,10,0.0001,live,1653286080000
AEVO-USDC,AEVO,USDC,1,0.00001,live,1728896400000
AGLD-USDC,AGLD,USDC,1,0.0001,live,1658815263000
ALGO-USDC,ALGO,USDC,10,0.0001,live,1655119932000
APE-USDC,APE,USDC,1,0.0001,live,1653285819000
APT-USDC,APT,USDC,0.1,0.001,live,1666086889000
ARB-USDC,ARB,USDC,1,0.0001,live,1716188400000
ARKM-USDC,ARKM,USDC,1,0.0001,live,1731922500000
ATH-USDC,ATH,USDC,100,0.00001,live,1728896700000
ATOM-USDC,ATOM,USDC,0.1,0.001,live,1653974613000
AVAX-USDC,AVAX,USDC,0.1,0.001,live,1653285982000
BCH-USDC,BCH,USDC,0.01,0.1,live,1611907686000
BLUR-USDC,BLUR,USDC,10,0.00001,live,1676356333000
BONK-USDC,BONK,USDC,100000,0.000000001,live,1730190000000
CATI-USDC,CATI,USDC,10,0.00001,live,1728897000000
CHZ-USDC,CHZ,USDC,10,0.00001,live,1655785198000
COMP-USDC,COMP,USDC,0.1,0.01,live,1656386558000
CRO-USDC,CRO,USDC,10,0.00001,live,1655784892000
CRV-USDC,CRV,USDC,1,0.0001,live,1653974689000
DAI-USDC,DAI,USDC,1,0.0001,live,1722235500000
DOGS-USDC,DOGS,USDC,1000,0.0000001,live,1726812300000
DOT-USDC,DOT,USDC,0.1,0.001,live,1653285472000
DYDX-USDC,DYDX,USDC,1,0.0001,live,1655785076000
EIGEN-USDC,EIGEN,USDC,1,0.0001,live,1729155600000
ENS-USDC,ENS,USDC,0.1,0.001,live,1720666800000
ETC-USDC,ETC,USDC,0.1,0.01,live,1660022233000
ETHW-USDC,ETHW,USDC,1,0.001,live,1663296038000
ETHFI-USDC,ETHFI,USDC,1,0.0001,live,1728897300000
FIL-USDC,FIL,USDC,0.1,0.001,live,1653285381000
FLR-USDC,FLR,USDC,100,0.00001,live,1673256144000
GALA-USDC,GALA,USDC,100,0.00001,live,1654584967000
GRT-USDC,GRT,USDC,10,0.00001,live,1655120006000
HBAR-USDC,HBAR,USDC,10,0.00001,live,1660022454000
ICP-USDC,ICP,USDC,0.1,0.001,live,1654584395000
JTO-USDC,JTO,USDC,1,0.001,live,1715331600000
JUP-USDC,JUP,USDC,10,0.0001,live,1728897600000
LDO-USDC,LDO,USDC,1,0.0001,live,1720667100000
LINK-USDC,LINK,USDC,0.1,0.001,live,1654583999000
LRC-USDC,LRC,USDC,10,0.00001,live,1654584131000
LTC-USDC,LTC,USDC,0.01,0.01,live,1611907686000
LUNC-USDC,LUNC,USDC,10000,0.00000001,live,1653637053000
LUNA-USDC,LUNA,USDC,10,0.0001,live,1653732118000
MERL-USDC,MERL,USDC,10,0.00001,live,1717405200000
MKR-USDC,MKR,USDC,0.001,0.1,live,1658815781000
MOODENG-USDC,MOODENG,USDC,10,0.00001,live,1731918900000
MORPHO-USDC,MORPHO,USDC,1,0.0001,live,1732786500000
MOVE-USDC,MOVE,USDC,10,0.00001,live,1733754600000
NEAR-USDC,NEAR,USDC,1,0.001,live,1653285574000
NEIRO-USDC,NEIRO,USDC,1000,0.0000001,live,1730189700000
NOT-USDC,NOT,USDC,100,0.000001,live,1726812000000
OKT-USDC,OKT,USDC,0.1,0.001,live,1656387780000
ONDO-USDC,ONDO,USDC,1,0.0001,live,1724915100000
OP-USDC,OP,USDC,1,0.0001,live,1654009129000
PNUT-USDC,PNUT,USDC,1,0.0001,live,1731918600000
PYTH-USDC,PYTH,USDC,10,0.0001,live,1720667400000
RDNT-USDC,RDNT,USDC,10,0.00001,live,1679385600000
SAND-USDC,SAND,USDC,10,0.0001,live,1653974760000
SHIB-USDC,SHIB,USDC,100000,0.000000001,live,1653286153000
STRK-USDC,STRK,USDC,1,0.0001,live,1712811000000
STX-USDC,STX,USDC,1,0.0001,live,1660022629000
SUI-USDC,SUI,USDC,1,0.0001,live,1715332200000
TIA-USDC,TIA,USDC,0.1,0.001,live,1720668000000
TRX-USDC,TRX,USDC,10,0.00001,live,1611907686000
TURBO-USDC,TURBO,USDC,1000,0.000001,live,1729156500000
UNI-USDC,UNI,USDC,0.1,0.001,live,1653974977000
USDT-USDC,USDT,USDC,1,0.0001,live,1675305962000
WBTC-USDC,WBTC,USDC,0.0001,0.1,live,1723100100000
WIF-USDC,WIF,USDC,1,0.0001,live,1715332800000
WLD-USDC,WLD,USDC,1,0.0001,live,1717146000000
XLM-USDC,XLM,USDC,10,0.00001,live,1655785301000
YGG-USDC,YGG,USDC,10,0.0001,live,1658814984000
ZENT-USDC,ZENT,USDC,100,0.000001,live,1719910500000
ZETA-USDC,ZETA,USDC,1,0.0001,live,1728897900000
ZEUS-USDC,ZEUS,USDC,10,0.0001,live,1732785600000
ZK-USDC,ZK,USDC,10,0.00001,live,1722675000000
ZRO-USDC,ZRO,USDC,1,0.001,live,1724914800000
ETH-BTC,ETH,BTC,0.0001,0.00001,live,1611907686000
OKB-BTC,OKB,BTC,0.1,0.0000001,live,1611907686000
SOL-BTC,SOL,BTC,0.001,0.0000001,live,1611907686000
BCH-BTC,BCH,BTC,0.01,0.000001,live,1611907686000
BSV-BTC,BSV,BTC,0.1,0.0000001,live,1611907686000
LTC-BTC,LTC,BTC,0.01,0.0000001,live,1611907686000
STX-BTC,STX,BTC,1,0.000000001,live,1614853497000
WBTC-BTC,WBTC,BTC,0.0001,0.0001,live,1611907686000
SOL-ETH,SOL,ETH,0.001,0.00001,live,1611907686000
BETH-ETH,BETH,ETH,0.001,0.0001,live,1611907686000
STETH-ETH,STETH,ETH,0.001,0.0001,live,1689674400000

截屏2025-05-27 22.53.26