#-*-Coding:utf-8-*-"" "Created on Fri the 10:36:19 of the @author: 2017 Yunjinqi e-mail:yunjinqi@qq.com
yourself in the world from anyone else. "" #用于访问OKCOIN spot rest API ############################################################################## Import Http.client Import urllib Import JSON import hashlib import time Def buildmysign (params,secretkey): sign = "for Key in Sorted (Params.keys ()): sign + = key + ' = ' + str (Params[key]) + ' & ' data = sign+ ' secret_key= ' +SECRETK EY return Hashlib.md5 (Data.encode ("UTF8")). Hexdigest (). Upper () def httpget (url,resource,params= '): conn = http. Client. Httpsconnection (URL, timeout=10) conn.request ("Get", Resource + '? ' + params) #print (resource + '? ' + params) r Esponse = Conn.getresponse () data = Response.read (). Decode (' UTF8 ') return json.loads (data) def httppost (Url,resou
Rce,params): headers = {"Content-type": "Application/x-www-form-urlencoded"}conn = http.client.HTTPSConnection (URL, timeout=10) temp_params = Urllib.parse.urlencode (params) #print ("https:/ /"+url+resource+"? +str (Temp_params)) conn.request ("POST", resource,temp_params,headers) response = Conn.getresponse () data = Response.read (). Decode (' Utf-8 ') params.clear () Conn.close () return Data ###################################
########################################## import Urllib class Okcoinspot:def __init__ (Self,url,apikey,secretkey): Self.__url = URL Self.__apikey = Apikey Self.__secretkey = secretkey print (self.__secretk
EY) #获取OKCOIN现货行情信息 def ticker (Self,symbol = '): Ticker_resource = "/api/v1/ticker.do" params= " If Symbol:params = ' symbol=% (symbol) s '%{' symbol ': symbol} return HttpGet (Self.__url,ticker_r
Esource,params) #获取OKCOIN现货市场深度信息 def depth (Self,symbol = '): Depth_resource = "/api/v1/depth.do" ParAms= ' If Symbol:params = ' symbol=% (symbol) s '%{' symbol ': symbol} return HttpGet (Self.__url,de Pth_resource,params) #获取OKCOIN现货历史交易信息 def trades (self,symbol = '): Trades_resource = "/API/V1/TRADES.D O "params= ' if Symbol:params = ' symbol=% (symbol) s '%{' symbol ': symbol} return HttpGet (self.__url,trades_resource,params) #获取用户现货账户信息 def userinfo (self): Userinfo_resource = "/api/v1/user Info.do "params ={} params[' api_key '] = Self.__apikey params[' sign '] = Buildmysign (params,self.__s Ecretkey) return HttpPost (self.__url,userinfo_resource,params) #现货交易 def trade (self,symbol,tradetype,pric
E= ', amount= '): Trade_resource = "/api/v1/trade.do" params = {' Api_key ': Self.__apikey,
' symbol ': symbol, ' type ': Tradetype} if price:params[' price ' = Price
If Amount: params[' amount '] = Amount params[' sign '] = buildmysign (params,self.__secretkey) return HTTP Post (self.__url,trade_resource,params) #现货批量下单 def batchtrade (self,symbol,tradetype,orders_data): BATCH_T Rade_resource = "/api/v1/batch_trade.do" params = {' Api_key ': self.__apikey, ' symbol ': SYMB OL, ' type ': Tradetype, ' Orders_data ': orders_data} params[' sign ' = buildmysign (par Ams,self.__secretkey) return HttpPost (self.__url,batch_trade_resource,params) #现货取消订单 def cancelorder (sel F,symbol,orderid): Cancel_order_resource = "/api/v1/cancel_order.do" params = {' Api_key ': sel F.__apikey, ' symbol ': symbol, ' order_id ': orderId} params[' sign ' = Buildmysign (P Arams,self.__secretkey) return HttpPost (self.__url,cancel_order_resource,params) #现货订单信息查询 def orderinfo (
Self,symbol,orderid): Order_info_resource = "/api/v1/order_info.do" params = {' Api_key ': Self.__apikey, ' symbol ': symbol, ' order_id ': orderId} params[' sign ' = Buildmysign (Params,self.__secretke Y) return HttpPost (self.__url,order_info_resource,params) #现货批量订单信息查询 def ordersinfo (Self,symbol,orderid , tradetype): Orders_info_resource = "/api/v1/orders_info.do" params = {' Api_key ': Self.__ap Ikey, ' symbol ': symbol, ' order_id ': orderId, ' type ': tradetype} par ams[' sign ' = Buildmysign (Params,self.__secretkey) return HttpPost (self.__url,orders_info_resource,params) # Spot Get History Order Information def orderhistory (self,symbol,status,currentpage,pagelength): Order_history_resource = "/api/v1/o
Rder_history.do "params = {' Api_key ': self.__apikey, ' symbol ': symbol,
' Status ': status, ' Current_page ': currentpage, ' Page_length ':p agelength} params[' sign '] = BUILDM Ysign (Params,self.__secretkey) return HttpPost (Self.__url,order_history_resource,params) def getKline (sel f,duration,size,since): Kline_resourse = "https://www.okcoin.cn/api/v1/kline.do" params = {# '
Api_key ': self.__apikey, ' symbol ': ' btc_cny ', ' type ': Duration, ' size ': size, ' Since ': since} temp_params = Urllib.parse.urlencode (params) #print (temp_params) Retu RN HttpGet (Self.__url, Kline_resourse, Temp_params) ############################################################## ############### #!/usr/bin/python #-*-Coding:utf-8-*-#用于访问OKCOIN futures Rest API import urllib class Okcoinfuture:d EF __init__ (self,url,apikey,secretkey): Self.__url = URL Self.__apikey = Apikey Self.__secretkey =
Secretkey #OKCOIN期货行情信息 def future_ticker (self,symbol,contracttype): Future_ticker_resource = "/api/v1/future_ticker.do" params = ' if symbol:params + = ' &symbol= ' + symbol if params else ' symbol= ' +symbol if contract Type:params + = ' &contract_type= ' + contracttype if params else ' contract_type= ' +symbol return HT
Tpget (self.__url,future_ticker_resource,params) #OKCoin期货市场深度信息 def future_depth (self,symbol,contracttype,size): Future_depth_resource = "/api/v1/future_depth.do" params = ' if symbol:params + = ' & Amp;symbol= ' + symbol if params else ' symbol= ' +symbol if contracttype:params = ' &contract_type= ' + contracttype if params else ' contract_type= ' +symbol if size:params + = ' &size= ' + size if par AMS Else ' size= ' + size return HttpGet (self.__url,future_depth_resource,params) #OKCoin期货交易记录信息 def futur E_trades (Self,symbol,conTracttype): Future_trades_resource = "/api/v1/future_trades.do" params = ' if symbol: params + = ' &symbol= ' + symbol if params else ' symbol= ' +symbol if contracttype:params = ' &c Ontract_type= ' + contracttype if params else ' contract_type= ' +symbol return HttpGet (Self.__url,future_trades_reso
Urce,params) #OKCoin期货指数 def future_index (self,symbol): Future_index = "/api/v1/future_index.do"
Params= ' If symbol:params = ' symbol= ' +symbol return HttpGet (self.__url,future_index,params) #获取美元人民币汇率 def exchange_rate (self): exchange_rate = "/api/v1/exchange_rate.do" Return HttpGet (SE Lf.__url,exchange_rate, ') #获取预估交割价 def future_estimated_price (self,symbol): Future_estimated_price = "/A Pi/v1/future_estimated_price.do "params= ' if Symbol:params = ' symbol= ' +symbol retur N HttpGet (Self.__url,futUre_estimated_price,params) #期货全仓账户信息 def future_userinfo (self): Future_userinfo = "/api/v1/future_userin
Fo.do? "
Params ={} params[' api_key '] = Self.__apikey params[' sign '] = buildmysign (Params,self.__secretkey)
Return HttpPost (self.__url,future_userinfo,params) #期货全仓持仓信息 def future_position (self,symbol,contracttype):
Future_position = "/api/v1/future_position.do?"
params = {' Api_key ': self.__apikey, ' symbol ': symbol, ' contract_type ': contracttype } params[' sign ' = Buildmysign (Params,self.__secretkey) return HttpPost (Self.__url,future_position,par
AMS) #期货下单 def future_trade (self,symbol,contracttype,price= ', amount= ', tradetype= ', matchprice= ', leverrate= '):
Future_trade = "/api/v1/future_trade.do?"
params = {' Api_key ': self.__apikey, ' symbol ': symbol, ' contract_type ': Contracttype, ' AMount ': Amount, ' type ': Tradetype, ' match_price ': Matchprice, ' lever_rate ': leverrate
If price:params[' price '] = Price params[' sign ' = Buildmysign (Params,self.__secretkey) Return HttpPost (self.__url,future_trade,params) #期货批量下单 def future_batchtrade (self,symbol,contracttype,or
Ders_data,leverrate): Future_batch_trade = "/api/v1/future_batch_trade.do?"
params = {' Api_key ': self.__apikey, ' symbol ': symbol, ' contract_type ': Contracttype, ' Orders_data ': orders_data, ' lever_rate ': leverrate} params[' sign '] = Buildmysign (para Ms,self.__secretkey) return HttpPost (self.__url,future_batch_trade,params) #期货取消订单 def future_cancel (self
, Symbol,contracttype,orderid): Future_cancel = "/api/v1/future_cancel.do?"
params = {' Api_key ': self.__apikey, ' symbol ': symbol, ' Contract_type ': contracttype, ' order_id ': orderId} params[' sign '] = Buildmysign (Params,sel F.__secretkey) return HttpPost (self.__url,future_cancel,params) #期货获取订单信息 def future_orderinfo (Self,symbo
L,contracttype,orderid,status,currentpage,pagelength): Future_orderinfo = "/api/v1/future_order_info.do?"
params = {' Api_key ': self.__apikey, ' symbol ': symbol, ' contract_type ': Contracttype, ' order_id ': orderId, ' status ': status, ' Current_page ': currentpage, ' page_lengt H ':p agelength} params[' sign '] = buildmysign (Params,self.__secretkey) return HttpPost (self.__url,f Uture_orderinfo,params) #期货逐仓账户信息 def future_userinfo_4fix (self): Future_info_4fix = "/api/v1/future_user
Info_4fix.do? " params = {' Api_key ': self.__apikey} params[' sign ' = Buildmysign (Params,self.__secretkey) return HttpPost (s ElF.__url,future_info_4fix,params) #期货逐仓持仓信息 def future_position_4fix (self,symbol,contracttype,type1): FUTU
Re_position_4fix = "/api/v1/future_position_4fix.do?"
params = {' Api_key ': self.__apikey, ' symbol ': symbol, ' contract_type ': Contracttype, ' Type ': type1} params[' sign ' = Buildmysign (Params,self.__secretkey) return HttpPost (self . __url,future_position_4fix,params) def getkline (self,duration,size,since): Kline_resourse = "HTTPS://WWW.OKC
Oin.com/api/v1/future_kline.do? "
params = {# ' Api_key ': self.__apikey, ' symbol ': ' BTC_USD ', ' type ': Duration, ' Contract_type ': ' Quarter ', ' size ': size, ' since ': since} temp_params = Urllib. Parse.urlencode (params) return HttpGet (Self.__url, Kline_resourse, Temp_params) #return HttpPost (Self.__ur L,kline_resourse,params) #temp_params = Urllib.parse.urlencode (params) #print (temp_params) # return HttpGet (Self.__url, Kline_resourse, temp_p Arams) ########################