需要安装ccxt
以下代码在jupyter中运行
需要去app中获取自己的apiKey、secret、password,才能查到自己的个人持仓情况
根据自己的代理设置proxies
标签:python,okx,查询,df,imr,res,import,datetime,columns From: https://www.cnblogs.com/youmuchen/p/18615787import ccxt import pandas as pd import time from IPython.display import clear_output import traceback from datetime import datetime exchange = ccxt.okex({ 'apiKey': 'xxx', 'secret': 'xxx', "password":"xxx", 'timeout': 30000, 'enableRateLimit': True, "proxies":{ "http":"http://127.0.0.1:10809", "https":"http://127.0.0.1:10809", } }) # 设置中文列名 cn_columns = { 'instId': '名称', 'entryPrice': '入场价格', 'markPx': '标记价格', 'liqPx': '清算价格', 'unrealizedPnl': '收益', 'percentage': '收益率' } all_df = [] while 1: try: res = exchange.fetchBalance() df = pd.DataFrame([{**item["info"],**item} for item in exchange.fetchPositions()]) if df.shape[0]: imr = df["imr"].apply(lambda x:"%.2f%%"% (float(x) / res["total"]["USDT"] * 100) ) df = df[cn_columns.keys()].rename(columns=cn_columns) df['时间'] = datetime.now().strftime('%H:%M:%S') df["当前金额"] = res["total"]["USDT"] * 7.2755 df["清算价格"] = df["清算价格"].astype(float).round(2) df["收益率"] = df["收益率"].apply(lambda x: "%.2f%%"%float(x)) df["仓位占比"] = imr all_df = [df] + all_df all_df = all_df[:10] clear_output(wait=True) display(pd.concat(all_df)) else: clear_output(wait=True) display( res["total"]["USDT"] * 7.2755) # break except: traceback.print_exc() break运行结果: