#!/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"]), '

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

被折叠的 条评论
为什么被折叠?



