127 lines
4.1 KiB
Python
127 lines
4.1 KiB
Python
|
|
#!/usr/bin/env python
|
||
|
|
# -*- coding: utf-8 -*-
|
||
|
|
|
||
|
|
"""
|
||
|
|
OKX marketdata属性测试脚本
|
||
|
|
用于验证通过marketdata属性获取价格数据
|
||
|
|
"""
|
||
|
|
|
||
|
|
import sys
|
||
|
|
from pprint import pprint
|
||
|
|
|
||
|
|
# 导入必要的库
|
||
|
|
try:
|
||
|
|
from okx import OkxRestClient
|
||
|
|
print("成功导入 okx.OkxRestClient")
|
||
|
|
except ImportError:
|
||
|
|
print("错误: 无法导入OKX SDK, 请安装: pip install okx-sdk")
|
||
|
|
sys.exit(1)
|
||
|
|
|
||
|
|
def test_marketdata():
|
||
|
|
"""测试通过marketdata属性获取价格"""
|
||
|
|
print("\n=== 测试 marketdata.get_ticker 方法 ===")
|
||
|
|
|
||
|
|
# 初始化客户端 (不使用API密钥)
|
||
|
|
client = OkxRestClient()
|
||
|
|
|
||
|
|
# 检查是否有marketdata属性
|
||
|
|
if not hasattr(client, 'marketdata'):
|
||
|
|
print("❌ 错误: client对象没有marketdata属性")
|
||
|
|
print(f"可用属性: {', '.join([a for a in dir(client) if not a.startswith('_')])}")
|
||
|
|
return
|
||
|
|
|
||
|
|
print("✅ 找到marketdata属性")
|
||
|
|
|
||
|
|
# 检查marketdata对象是否有get_ticker方法
|
||
|
|
marketdata = client.marketdata
|
||
|
|
if not hasattr(marketdata, 'get_ticker'):
|
||
|
|
print("❌ 错误: marketdata对象没有get_ticker方法")
|
||
|
|
print(f"可用方法: {', '.join([a for a in dir(marketdata) if not a.startswith('_')])}")
|
||
|
|
return
|
||
|
|
|
||
|
|
print("✅ 找到get_ticker方法")
|
||
|
|
|
||
|
|
# 测试获取ETH-USDT价格
|
||
|
|
try:
|
||
|
|
pair = "ETH-USDT"
|
||
|
|
print(f"\n获取 {pair} 价格...")
|
||
|
|
response = client.marketdata.get_ticker(instId=pair)
|
||
|
|
|
||
|
|
print(f"响应状态码: {response.get('code')}")
|
||
|
|
if response and response.get('code') == '0' and response.get('data'):
|
||
|
|
print("✅ 成功获取价格数据!")
|
||
|
|
|
||
|
|
ticker_data = response['data'][0]
|
||
|
|
price = float(ticker_data['last'])
|
||
|
|
|
||
|
|
print(f"\n{pair} 当前价格: {price} USDT")
|
||
|
|
print(f"24小时最高价: {ticker_data.get('high24h', 'N/A')}")
|
||
|
|
print(f"24小时最低价: {ticker_data.get('low24h', 'N/A')}")
|
||
|
|
print(f"24小时涨跌幅: {ticker_data.get('pctChange', 'N/A')}%") # 使用get方法避免KeyError
|
||
|
|
|
||
|
|
print("\n完整响应数据:")
|
||
|
|
pprint(response)
|
||
|
|
else:
|
||
|
|
print("❌ 请求成功但返回错误:")
|
||
|
|
pprint(response)
|
||
|
|
except Exception as e:
|
||
|
|
print(f"❌ 请求过程中出错: {e}")
|
||
|
|
|
||
|
|
def test_with_api_keys():
|
||
|
|
"""测试使用API密钥的情况"""
|
||
|
|
print("\n=== 测试使用API密钥 ===")
|
||
|
|
|
||
|
|
# 填写您的API密钥信息
|
||
|
|
api_key = input("请输入API密钥 (或直接回车跳过): ").strip()
|
||
|
|
if not api_key:
|
||
|
|
print("跳过API密钥测试")
|
||
|
|
return
|
||
|
|
|
||
|
|
secret_key = input("请输入Secret Key: ").strip()
|
||
|
|
passphrase = input("请输入Passphrase: ").strip()
|
||
|
|
|
||
|
|
# 初始化带API密钥的客户端
|
||
|
|
client = OkxRestClient(api_key, secret_key, passphrase)
|
||
|
|
|
||
|
|
# 测试获取ETH-USDT价格
|
||
|
|
try:
|
||
|
|
pair = "ETH-USDT"
|
||
|
|
print(f"\n使用API密钥获取 {pair} 价格...")
|
||
|
|
response = client.marketdata.get_ticker(instId=pair)
|
||
|
|
|
||
|
|
if response and response.get('code') == '0' and response.get('data'):
|
||
|
|
print("✅ 使用API密钥成功获取价格!")
|
||
|
|
price = float(response['data'][0]['last'])
|
||
|
|
print(f"{pair} 当前价格: {price} USDT")
|
||
|
|
else:
|
||
|
|
print("❌ 使用API密钥请求失败:")
|
||
|
|
pprint(response)
|
||
|
|
except Exception as e:
|
||
|
|
print(f"❌ 使用API密钥请求过程中出错: {e}")
|
||
|
|
|
||
|
|
def main():
|
||
|
|
"""主函数"""
|
||
|
|
print("OKX Marketdata属性测试")
|
||
|
|
print("-" * 50)
|
||
|
|
|
||
|
|
# 检查SDK版本
|
||
|
|
try:
|
||
|
|
import pkg_resources
|
||
|
|
print(f"SDK版本: {pkg_resources.get_distribution('okx-sdk').version}")
|
||
|
|
except Exception as e:
|
||
|
|
print(f"无法获取SDK版本: {e}")
|
||
|
|
|
||
|
|
# 测试不带API密钥
|
||
|
|
test_marketdata()
|
||
|
|
|
||
|
|
# 询问是否测试API密钥
|
||
|
|
print("\n要测试使用API密钥吗?")
|
||
|
|
choice = input("输入 'y' 进行测试, 或任意键跳过: ")
|
||
|
|
|
||
|
|
if choice.lower() == 'y':
|
||
|
|
test_with_api_keys()
|
||
|
|
|
||
|
|
print("\n测试完成!")
|
||
|
|
|
||
|
|
if __name__ == "__main__":
|
||
|
|
main()
|