python做量化投资系列之比特币初始配置

 更新时间:2018年01月23日 16:34:53   作者:云金杞   我要评论

这篇文章主要为大家详细介绍了python做量化投资系列之比特币初始配置,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了python比特币初始配置的具体代码,供大家参考,具体内容如下

# -*- 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. 
""" 
#用于访问OKCOIN 现货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='+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) 
 
  #获取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_RESOURCE,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,DEPTH_RESOURCE,params)  
 
  #获取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) 
   
  #获取用户现货账户信息 
  def userinfo(self): 
    USERINFO_RESOURCE = "/api/v1/userinfo.do" 
    params ={} 
    params['api_key'] = self.__apikey 
    params['sign'] = buildMySign(params,self.__secretkey) 
    return httpPost(self.__url,USERINFO_RESOURCE,params) 
 
  #现货交易 
  def trade(self,symbol,tradeType,price='',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 httpPost(self.__url,TRADE_RESOURCE,params) 
 
  #现货批量下单 
  def batchTrade(self,symbol,tradeType,orders_data): 
    BATCH_TRADE_RESOURCE = "/api/v1/batch_trade.do" 
    params = { 
      'api_key':self.__apikey, 
      'symbol':symbol, 
      'type':tradeType, 
      'orders_data':orders_data 
    } 
    params['sign'] = buildMySign(params,self.__secretkey) 
    return httpPost(self.__url,BATCH_TRADE_RESOURCE,params) 
 
  #现货取消订单 
  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) 
 
  #现货获得历史订单信息 
  def orderHistory(self,symbol,status,currentPage,pageLength): 
      ORDER_HISTORY_RESOURCE = "/api/v1/order_history.do" 
      params = { 
       'api_key':self.__apikey, 
       'symbol':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/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) 
    return httpGet(self.__url, kline_resourse, temp_params) 
######################################################## 
#!/usr/bin/python 
# -*- coding: utf-8 -*- 
#用于访问OKCOIN 期货REST API 
import urllib 
 
class OKCoinFuture: 
 
  def __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 contractType: 
      params += '&contract_type=' + contractType if params else 'contract_type=' +symbol 
    return httpGet(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 += '&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 params else 'size=' + size 
    return httpGet(self.__url,FUTURE_DEPTH_RESOURCE,params) 
 
  #OKCoin期货交易记录信息 
  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: 
      params += '&contract_type=' + contractType if params else 'contract_type=' +symbol 
    return httpGet(self.__url,FUTURE_TRADES_RESOURCE,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(self.__url,EXCHANGE_RATE,'') 
 
  #获取预估交割价 
  def future_estimated_price(self,symbol): 
    FUTURE_ESTIMATED_PRICE = "/api/v1/future_estimated_price.do" 
    params='' 
    if symbol: 
      params = 'symbol=' +symbol 
    return httpGet(self.__url,FUTURE_ESTIMATED_PRICE,params) 
 
  #期货全仓账户信息 
  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) 
 
  #期货下单 
  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,orders_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(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) 
 
  #期货获取订单信息 
  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/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.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) 
######################## 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • Python实现统计代码行的方法分析

    Python实现统计代码行的方法分析

    这篇文章主要介绍了Python实现统计代码行的方法,结合实例形式分析了Python针对代码行数的计算实现步骤与操作技巧,需要的朋友可以参考下
    2017-07-07
  • python实现下载指定网址所有图片的方法

    python实现下载指定网址所有图片的方法

    这篇文章主要介绍了python实现下载指定网址所有图片的方法,涉及Python针对页面的读取、遍历及文件操作的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-08-08
  • Python 遍历子文件和所有子文件夹的代码实例

    Python 遍历子文件和所有子文件夹的代码实例

    本篇文章主要介绍了Python 遍历子文件和所有子文件夹的代码实例,具有一定的参考价值,感兴趣的小伙伴们可以参考一下。
    2016-12-12
  • Pyramid Mako模板引入helper对象的步骤方法

    Pyramid Mako模板引入helper对象的步骤方法

    ylons中的mako模板,默认会引入一个helper对象,我们可以在里面扩展方法,应对在模板输出时候会常用的操作,那么在Pyramid中如何默认引入同样的辅助类到模板中
    2013-11-11
  • python2.7到3.x迁移指南

    python2.7到3.x迁移指南

    由于PYTHON2.7即将停止支持,小编给大家分享了一篇关python2.7到3.x迁移指南内容,希望对各位有用。
    2018-02-02
  • Python requests发送post请求的一些疑点

    Python requests发送post请求的一些疑点

    在Python爬虫中,使用requests发送请求,访问指定网站,是常见的做法,这篇文章主要介绍了Python requests发送post请求的一些疑点,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-05-05
  • Python缩进和冒号详解

    Python缩进和冒号详解

    下面小编就为大家带来一篇Python缩进和冒号详解。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2016-06-06
  • 详谈Python基础之内置函数和递归

    详谈Python基础之内置函数和递归

    下面小编就为大家带来一篇Python基础之内置函数和递归。小编觉得挺不错的。现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-06-06
  • python之从文件读取数据到list的实例讲解

    python之从文件读取数据到list的实例讲解

    下面小编就为大家分享一篇python之从文件读取数据到list的实例讲解,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-04-04
  • 学习python 之编写简单乘法运算题

    学习python 之编写简单乘法运算题

    这篇文章主要介绍了学习python 第一季 编写简单乘法运算题,需要的朋友可以参考下
    2016-02-02

最新评论