Python implements the initial configuration of bitcoin in the quantitative investment series, and python bit
This example shares the code for the initial configuration of python bitcoin for your reference. The details are as follows:
#-*-Coding: UTF-8-*-"Created on Fri Jan 13 10:36:19 2017 @ author: yunjinqi E-mail: yunjinqi@qq.com Differentiate yourself in the world from anyone else. "" # used to access 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 + = ke Y + '=' + str (params [key]) + '& 'data = sign + 'secret _ key =' + secretKey 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) response = conn. getresponse () data = response. read (). decode ('utf8') return json. loads (data) def httpPost (url, resource, 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. _ secretkey )# Obtain the OKCOIN spot price information def ticker (self, symbol = ''): TICKER_RESOURCE ="/api/v1/ticker. do "params ='' if symbol: params = 'symbol = % (symbol) s' % {'symbol ': symbol} return httpGet (self. _ url, TICKER_RESOURCE, params) # obtain the depth of the OKCOIN spot market def depth (self, symbol = ''): DEPTH_RESOURCE ="/api/v1/depth. do "params ='' if symbol: params = 'symbol = % (symbol) s' % {'symbol ': symbol} return httpGet (self. _ url, DEPTH_RESOURCE, param S) # obtain the historical transaction information of OKCOIN def trades (self, symbol = ''): TRADES_RESOURCE ="/api/v1/trades. do "params ='' if symbol: params = 'symbol = % (symbol) s' % {'symbol ': symbol} return httpGet (self. _ url, TRADES_RESOURCE, params) # obtain the user's spot account information def userinfo (self): USERINFO_RESOURCE = "/api/v1/userinfo. do "params = {} params ['api _ key'] = self. _ apikey params ['sign'] = buildMySign (params, self. _ secretkey) return httpPost (sel F. _ url, USERINFO_RESOURCE, params) # spot transaction def trade (self, symbol, tradeType, price = '', amount = ''): TRADE_RESOURCE = "/api/v1/trade. do "params = {'api _ key': self. _ apikey, 'sympost': symbol, 'type': tradeType} if price: params ['price'] = price if amount: params ['amount '] = amount params ['sign'] = buildMySign (params, self. _ secretkey) return httpPost (self. _ url, TRADE_RESOURCE, params) # spot batch order def batchTrade (s Elf, symbol, tradeType, orders_data): BATCH_TRADE_RESOURCE = "/api/v1/batch_trade.do" params = {'api _ key': self. _ apikey, 'sympost': symbol, 'type': tradeType, 'Orders _ data': orders_data} params ['sign'] = buildMySign (params, self. _ secretkey) return httpPost (self. _ url, BATCH_TRADE_RESOURCE, params) # cancel order def cancelOrder (self, symbol, orderId): CANCEL_ORDER_RESOURCE = "/api/v1/cancel_order.do" params = {' Api_key ': self. _ apikey, 'symbol': symbol, 'order _ id': orderId} params ['sign'] = buildMySign (params, 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. _ secretkey) 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. _ apikey, 'symbol': symbol, 'order _ id': orderId, 'type': tradeType} params ['sign'] = buildMySign (params, self. _ secretkey) return httpPost (self. _ url, ORDERS_INFO_RESOURCE, params) # obtain historical order information from the stock def orderHistory (self, symbol, st Atus, currentPage, pageLength): ORDER_HISTORY_RESOURCE = "/api/v1/order_history.do" params = {'api _ key': self. _ apikey, 'sympost': symbol, 'status': status, 'current _ page': currentPage, 'page _ length ': pageLength} params ['sign'] = buildMySign (params, self. _ secretkey) return httpPost (self. _ url, ORDER_HISTORY_RESOURCE, params) def getKline (self, duration, size, since): kline_resourse = "https://www.okcoin.cn/ Pi/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) return httpGet (self. _ url, kline_resourse, temp_params) ######################################## #################! /Usr/bin/python #-*-coding: UTF-8-*-# used to access the OKCOIN futures rest api import urllib class OKCoinFuture: def _ init _ (self, url, apikey, secretkey): self. _ url = url self. _ apikey = apikey self. _ secretkey = secretkey # def future_ticker (self, symbol, contractType): FUTURE_TICKER_RESOURCE = "/api/v1/future_ticker.do" params = ''if symbol: params + = '& symbol =' + symbol if params else 'symbol = '+ symb Ol if contractType: params + = '& contract_type =' + contractType if params else 'contract _ type =' + symbol return httpGet (self. _ url, FUTURE_TICKER_RESOURCE, params) # Deep information of the OKCoin futures market def future_depth (self, symbol, contractType, size ): FUTURE_DEPTH_RESOURCE = "/api/v1/future_depth.do" params = ''if symbol: params + = '& symbol =' + symbol if params else 'symbol = '+ symbol if contractType: params + = '& contract_t Ype = '+ contractType if params else' contract _ type = '+ symbol if size: params + = '& size =' + size if params else 'size = '+ size return httpGet (self. _ url, FUTURE_DEPTH_RESOURCE, params) # def future_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: pa Rams + = '& contract_type =' + contractType if params else 'contract _ type =' + symbol return httpGet (self. _ url, FUTURE_TRADES_RESOURCE, params) # OKCoin Futures index 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) # obtain the dollar Renminbi exchange rate def exchange_rate (self): EXCHANGE_RATE = "/api/v1/exchange_rate.do" retu Rn httpGet (self. _ url, EXCHANGE_RATE, '') # obtain the estimated delivery price def future_estimated_price (self, symbol): FUTURE_ESTIMATED_PRICE ="/api/v1/else "params ='' if symbol: params = 'symbol = '+ symbol return httpGet (self. _ url, FUTURE_ESTIMATED_PRICE, params) # Full futures Warehouse account information def future_userinfo (self): FUTURE_USERINFO = "/api/v1/future_userinfo.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, params) # futures order 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, 'ver _ rate': leverRate} if price: params ['price'] = price params ['sign'] = buildMySign (params, self. _ secretkey) return httpPost (self. _ url, FUTURE_TRADE, params) # Future batch order def future_batchTrade (self, symbol, contractType, orders_data, leverRate): FUTURE_BATCH_TR ADE = "/api/v1/future_batch_trade.do? "Params = {'api _ key': self. _ apikey, 'symbol': symbol, 'Contract _ type': contractType, 'Orders _ data': orders_data, 'ver _ rate ': leverRate} params ['sign'] = buildMySign (params, 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, self. _ secretkey) return httpPost (self. _ url, FUTURE_CANCEL, params) # obtain order information for Futures def future_orderinfo (self, symbol, 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 _ length': pageLength} params ['sign'] = buildMySign (params, self. _ secretkey) return httpPost (self. _ url, FUTURE_ORDERINFO, params) # def future_userinfo_4fix (self): FUTURE_INFO_4FIX = "/api/v1/future_userinfo_4fix.do? "Params = {'api _ key': self. _ apikey} params ['sign'] = buildMySign (params, self. _ secretkey) return httpPost (self. _ url, FUTURE_INFO_4FIX, params) # def future_position_4fix (self, symbol, contractType, type1): FUTURE_POSITION_4FIX = "/api/v1/snapshot? "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.okcoin.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. _ url, kline_resourse, params) # temp_params = urllib. parse. urlencode (params) # print (temp_params) # return httpGet (self. _ url, kline_resourse, temp_params )########################
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.