OKEX API v1 SDK基于 Python 实现

本文深入解析OKEx交易所API的使用方法,包括市场数据获取、交易操作、账户信息查询等核心功能,并提供了详细的代码示例,帮助开发者快速上手。

 

#!/usr/bin/python
# -*- coding: utf-8 -*-
# Note: it is okex v1. okex v3 api has many bugs, for example kline don't support 1hour when set start or end 
# https://www.cnblogs.com/fangbei/p/okex-api-v1.html
import time,datetime import json import math import hashlib,hmac import numpy as np import websocket import sys from base.exchange import EXCHANGE class OKEX(EXCHANGE): _api_url = "https://www.okex.com" _ws_url = "wss://real.okex.com:10441/websocket" ''' 初始化 def __init__(self, apikey="", secret=""): self.__apikey = apikey self.__secret = secret ''' # 货币对列表 def markets(self): url = self._api_url + '/v2/spot/markets/products' result = self.http_request(url) list = [] for item in result["data"]: if item["online"] == 1: temp = item["symbol"].split("_") list.append({ 'symbol': item["symbol"], 'base': temp[0].upper(), 'quote': temp[1].upper()}) return list # 精度 def precisions(self): url = self._api_url + '/api/spot/v3/instruments' result = self.http_request(url) dict = {} for item in result: symbol = item["base_currency"] + "_" + item["quote_currency"] filter_dict = {} filter_dict['price'] = abs(round(math.log(float(item["tick_size"]), 10))) filter_dict['quantity'] = abs(round(math.log(float(item["size_increment"]), 10))) dict[symbol] = filter_dict return dict # 获取行情 def ticker(self, symbol = 'btc_usdt'): url = self._api_url + '/api/v1/ticker.do?symbol=%s'%(self._x_symbol(symbol)) result = self.http_request(url) dict = { 'bid': float(result["ticker"]["buy"]), 'ask': float(result["ticker"]["sell"]), 'last': float(result["ticker"]["last"])} return dict # 获取市场深度 def depth(self, symbol = 'btc_usdt'): url = self._api_url + '/api/v1/depth.do?symbol=%s'%(self._x_symbol(symbol)) depth = self.http_request(url) depth['asks'].sort(key=lambda x:x[0], reverse=False) depth['bids'].sort(key=lambda x:x[0], reverse=True) return depth #标准格式,不用转换 # # 获取交易信息(60条) def trades(self, symbol = 'btc_usdt', limit = 60): url = self._api_url + '/api/v1/trades.do?symbol=%s'%(self._x_symbol(symbol)) # print(url) result = self.http_request(url) list = [] for item in result: list.append({ 'id': int(item["tid"]), '
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

比特量化

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值