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


Posted in Python onJanuary 23, 2018

本文实例为大家分享了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实现信用卡系统(支持购物、转账、存取钱)
Jun 24 Python
Python面向对象之类的定义与继承用法示例
Jan 14 Python
Python分析彩票记录并预测中奖号码过程详解
Jul 09 Python
python使用PIL和matplotlib获取图片像素点并合并解析
Sep 10 Python
python基于socket实现的UDP及TCP通讯功能示例
Nov 01 Python
在django中自定义字段Field详解
Dec 03 Python
python实现井字棋小游戏
Mar 04 Python
keras模型保存为tensorflow的二进制模型方式
May 25 Python
利用Python实现字幕挂载(把字幕文件与视频合并)思路详解
Oct 21 Python
Pytest中skip和skipif的具体使用方法
Jun 30 Python
再谈python_tkinter弹出对话框创建
Mar 20 Python
Python何绘制带有背景色块的折线图
Apr 23 Python
python在非root权限下的安装方法
Jan 23 #Python
Python解析命令行读取参数--argparse模块使用方法
Jan 23 #Python
Python 查看文件的读写权限方法
Jan 23 #Python
Python3 中文文件读写方法
Jan 23 #Python
Python3之文件读写操作的实例讲解
Jan 23 #Python
Python实现邮件的批量发送的示例代码
Jan 23 #Python
python实现自动发送邮件发送多人、群发、多附件的示例
Jan 23 #Python
You might like
社区(php&&mysql)三
2006/10/09 PHP
discuz论坛 用户登录 后台程序代码
2008/11/27 PHP
php SQL之where语句生成器
2009/03/24 PHP
PHP中的integer类型使用分析
2010/07/27 PHP
php简单实现短网址(短链)还原的方法(测试可用)
2016/05/09 PHP
thinkPHP简单遍历数组方法分析
2016/05/16 PHP
javascript 客户端验证上传图片的大小(兼容IE和火狐)
2009/08/15 Javascript
JavaScript 解析Json字符串的性能比较分析代码
2009/12/16 Javascript
写了10年的Javascript也未必全了解的连续赋值运算
2011/03/25 Javascript
javascript操作referer详细解析
2014/03/10 Javascript
jQuery根据用户电脑是mac还是pc加载对应样式的方法
2015/06/26 Javascript
jQuery往返城市和日期查询实例讲解
2015/10/09 Javascript
无需 Flash 使用 jQuery 复制文字到剪贴板
2016/04/26 Javascript
关于javascript原型的修改与重写(覆盖)差别详解
2016/08/31 Javascript
bootstrap提示标签、提示框实现代码
2016/12/28 Javascript
Angular.js中下拉框实现渲染html的方法
2017/06/18 Javascript
基于vue-cli vue-router搭建底部导航栏移动前端项目
2018/02/28 Javascript
Vue使用mixins实现压缩图片代码
2018/03/14 Javascript
layui 表单标签的校验方法
2019/09/04 Javascript
vue实现lodop打印功能的示例
2020/11/11 Javascript
vue组件中节流函数的失效的原因和解决方法
2020/12/02 Vue.js
Python中尝试多线程编程的一个简明例子
2015/04/07 Python
python中获得当前目录和上级目录的实现方法
2017/10/12 Python
Python简单实现控制电脑的方法
2018/01/22 Python
Redis使用watch完成秒杀抢购功能的代码
2018/05/07 Python
Pandas读写CSV文件的方法示例
2019/03/27 Python
Python 内置函数globals()和locals()对比详解
2019/12/23 Python
详解如何获取localStorage最大存储大小的方法
2020/05/21 HTML / CSS
制衣厂各岗位职责
2013/12/02 职场文书
师范生的个人求职信范文
2014/01/04 职场文书
市场营销管理毕业生自荐信
2014/03/03 职场文书
师范生求职信
2014/06/14 职场文书
60条职场经典语录,总有一条能触动你的心
2019/08/21 职场文书
详解MySQL事务的隔离级别与MVCC
2021/04/22 MySQL
MySQL 发生同步延迟时Seconds_Behind_Master还为0的原因
2021/06/21 MySQL
MYSQL 的10大经典优化案例场景实战
2021/09/14 MySQL