火币 期货 现货 API [Python3版]

一、Util.py,基础类,包括参数配置、签名,HTTP 请求方法,发送信息到API

#coding=utf-8
import hashlib
import time
import urllib
import urllib.parse
import urllib.request  

#在此输入您的Key
ACCESS_KEY = ""
SECRET_KEY = ""

HUOBI_SERVICE_API="https://api.huobi.com/apiv3"
ACCOUNT_INFO = "get_account_info"
GET_ORDERS = "get_orders"
ORDER_INFO = "order_info"
BUY = "buy"
BUY_MARKET = "buy_market"
CANCEL_ORDER = "cancel_order"
NEW_DEAL_ORDERS = "get_new_deal_orders"
ORDER_ID_BY_TRADE_ID = "get_order_id_by_trade_id"
SELL = "sell"
SELL_MARKET = "sell_market"

'''
发送信息到api
'''
def send2api(pParams, extra):
    pParams['access_key'] = ACCESS_KEY
    pParams['created'] = int(time.time())
    pParams['sign'] = createSign(pParams)
    if(extra) :
        for k in extra:
            v = extra.get(k)
            if(v != None):
                pParams[k] = v
        #pParams.update(extra)
    tResult = httpRequest(HUOBI_SERVICE_API, pParams)
    return tResult

'''
生成签名
'''
def createSign(params):
    params['secret_key'] = SECRET_KEY;
    params = sorted(params.items(), key=lambda d:d[0], reverse=False)
    message = urllib.parse.urlencode(params)
    message=message.encode(encoding='UTF8')
    m = hashlib.md5()
    m.update(message)
    m.digest()
    sig=m.hexdigest()
    return sig

'''
request
'''
def httpRequest(url, params):
    postdata = urllib.parse.urlencode(params)
    postdata = postdata.encode('utf-8')

    fp = urllib.request.urlopen(url, postdata)
    if fp.status != 200 :
        return None
    else:
        mybytes = fp.read()
        mystr = mybytes.decode("utf8")
        fp.close()
        return mystr

二、HuobiService.py,火币现货API

#coding=utf-8
from Util import *

'''
获取账号详情
'''
def getAccountInfo(method):
    params = {"method":method}
    extra = {}
    res = send2api(params, extra)
    return res
'''
获取所有正在进行的委托
'''
def getOrders(coinType,method):
    params = {"method":method}
    params['coin_type'] = coinType
    extra = {}
    res = send2api(params, extra)
    return res
'''
获取订单详情
@param coinType
@param id
'''
def getOrderInfo(coinType,id,method):
    params = {"method":method}
    params['coin_type'] = coinType
    params['id'] = id
    extra = {}
    res = send2api(params, extra)
    return res

'''
限价买入
@param coinType
@param price
@param amount
@param tradePassword
@param tradeid
@param method
'''
def buy(coinType,price,amount,tradePassword,tradeid,method):
    params = {"method":method}
    params['coin_type'] = coinType
    params['price'] = price
    params['amount'] = amount
    extra = {}
    extra['trade_password'] = tradePassword
    extra['trade_id'] = tradeid
    res = send2api(params, extra)
    return res

'''
限价卖出
@param coinType
@param price
@param amount
@param tradePassword
@param tradeid
'''
def sell(coinType,price,amount,tradePassword,tradeid,method):
    params = {"method":method}
    params['coin_type'] = coinType
    params['price'] = price
    params['amount'] = amount
    extra = {}
    extra['trade_password'] = tradePassword
    extra['trade_id'] = tradeid
    res = send2api(params, extra)
    return res

'''
市价买
@param coinType
@param amount
@param tradePassword
@param tradeid
'''

def buyMarket(coinType,amount,tradePassword,tradeid,method):
    params = {"method":method}
    params['coin_type'] = coinType
    params['amount'] = amount
    extra = {}
    extra['trade_password'] = tradePassword
    extra['trade_id'] = tradeid
    res = send2api(params, extra)
    return res

'''
市价卖出
@param coinType
@param amount
@param tradePassword
@param tradeid
'''
def sellMarket(coinType,amount,tradePassword,tradeid,method):
    params = {"method":method}
    params['coin_type'] = coinType
    params['amount'] = amount
    extra = {}
    extra['trade_password'] = tradePassword
    extra['trade_id'] = tradeid
    res = send2api(params, extra)
    return res

'''
查询个人最新10条成交订单
@param coinType
'''
def getNewDealOrders(coinType,method):
    params = {"method":method}
    params['coin_type'] = coinType
    extra = {}
    res = send2api(params, extra)
    return res
'''
根据trade_id查询oder_id
@param coinType
@param tradeid
'''
def getOrderIdByTradeId(coinType,tradeid,method):
    params = {"method":method}
    params['coin_type'] = coinType
    params['trade_id'] = tradeid
    extra = {}
    res = send2api(params, extra)
    return res

'''
撤销订单
@param coinType
@param id
'''

def cancelOrder(coinType,id,method):
    params = {"method":method}
    params['coin_type'] = coinType
    params['id'] = id
    extra = {}
    res = send2api(params, extra)
    return res

三、HuobiMain.py,客户端调用方法

#coding=utf-8

'''
本程序在 Python 3.3.0 环境下测试成功
使用方法:python HuobiMain.py
'''

from Util import *
import HuobiService

if __name__ == "__main__":
    print ("获取账号详情")
    print (HuobiService.getAccountInfo(ACCOUNT_INFO))
    print ("获取所有正在进行的委托")
    print (HuobiService.getOrders(1,GET_ORDERS))
    print ("获取订单详情")
    print (HuobiService.getOrderInfo(1,68278313,ORDER_INFO))
    print ("限价买入")
    ","0.01",None,None,BUY))
    print ("限价卖出")
    ","0.2",None,None,SELL))
    print ("市价买入")
    ",None,None,BUY_MARKET))
    print ("市价卖出")
    print (HuobiService.sellMarket(2,"1.3452",None,None,SELL_MARKET))
    print ("查询个人最新10条成交订单")
    print (HuobiService.getNewDealOrders(1,NEW_DEAL_ORDERS))
    print ("根据trade_id查询order_id")
    print (HuobiService.getOrderIdByTradeId(1,274424,ORDER_ID_BY_TRADE_ID))
    print ("取消订单接口")
    print (HuobiService.cancelOrder(1,68278313,CANCEL_ORDER))

下载地址

http://files.cnblogs.com/files/fangbei/huobi-api-python3.zip

火币网现货API[Python3版]的更多相关文章

  1. OKCoin期货现货API[Python3版]

    OKCoin 期货 现货 API [Python版] 一.HttpMD5Util.py,基础类,包括MD5签名,HTTP Post及HTTP Get方法 #!/usr/bin/python # -*- ...

  2. 聚币网API[Python3版]

    代码 #!/usr/bin/env python # -*- coding:utf-8 -*- import hashlib import requests import time import ur ...

  3. 聚币网API[Python2版]

    聚币 现货 API [Python2版] 一.utils.py,基础类,包括HTTP 请求.签名等 # -*- coding: utf-8 -*- import hashlib import hmac ...

  4. RESTful API终极版序列化封装

    urls: from django.conf.urls import url from app01 import views urlpatterns = [ # url(r"comment/ ...

  5. 百度地图 JavaScript API 极速版 开发体会

    前段时间百度地图API推出了 JavaScript API 极速版 1.0 简单看了一下,从产品定位来说真是挺好. 把开发人员细分成普通web开发人员和移动web开发人员.正好用到了手机地图这块决定尝 ...

  6. 阿里大鱼短信接口(Python3版)

    近期由于须要用到短信接口,选型的的结果是用阿里大鱼的短信服务,然而淘宝开放平台(TOP)的SDK已经非常多年没有更新了.不支持python3.自己动手改了半天,还是不太正常,索性不用它,自己写一个算了 ...

  7. 百度翻译新API C#版在 winform,Asp.Net的小程序

    3月的下午,在C#群里日常装逼(聊天), 一兄弟说百度翻译有没有winfrom上用的Demo,问了一天  嫌烦了  我就干脆自己写个: PS 百度上的部分代码害死人啊  api地址都换的不成样了, 还 ...

  8. jquery easyui 1.4.1 API( CHM版)

    ChangeLog Bug The combogrid has different height than other combo components. fixed. datagrid: The r ...

  9. jQuery EasyUI 1.3.4 API CHM版下载

    网盘下载

随机推荐

  1. hibernate(六)一对一映射

    关系映射是指对象之间的关系,并不是指数据库的关系,关系映射是解决当对象处于以下关系之一时,数据库表该如何映射的问题 (一)一对一单向外键关联 1.注解方式配置 创建一个Husband类和Wife类 H ...

  2. PHP获取MySql新增记录ID值的方法

    今天发现用mysql_insert_id()获取到的新增记录的id不正确, 虽然发现源代码的事务写的有问题,但是最根本的原因是,我插入数据的id类型是bigint型 获取MySql新增记录ID值的方法 ...

  3. Django~Models1

    不用数据库不用Models 省却 --------------- startapp urls +链接 view以下 ---------------- 添加Model 在project/settings ...

  4. 第 一 百 天上课 PHP TP框架 数据库修改和删除

    修改的三种方式 //造数组的方式修改 public function xiugai1() { $db=D('yonghu'); $attr=array ( 'zhanghao'=>001, // ...

  5. UVa10047 The Monocycle

    UVa10047 The Monocycle 链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=19491 (以上摘自htt ...

  6. 【转】iOS申请发布证书-图文详解

    摘要 发布产品到App Store所需证书,2013年5月26日测试 IOS 发布证书 distribution 打包程序 真机调试证书 本文讲述发布证书的申请,申请真机调试证书请参考:http:// ...

  7. Linux显示内存统计最大和最小的详情

    Linux显示内存统计最大和最小的详情 youhaidong@youhaidong-ThinkPad-Edge-E545:~$ free -l total used free shared buffe ...

  8. 【webpack系列】从零搭建 webpack4+react 脚手架(二)

    html文件如何也同步到dist目录?bundle.js文件修改了,万一被浏览器缓存了怎么办?如何为导出的文件加md5?如何把js引用自动添加到html?非业务代码和业务代码如何分开打包?如何搭建开发 ...

  9. java的几种模式以及如何实现的

    创建Bean实例的方式: 1) 通过构造器(有参或无参) 方式: <bean id="" class=""/> 2) 通过静态工厂方法 方式: &l ...

  10. WinSDK(菜单笔记)