火币网现货API[Python3版]

火币 期货 现货 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 ("限价买入")
    print (HuobiService.buy(1,"1","0.01",None,None,BUY))
    print ("限价卖出")
    print (HuobiService.sell(2,"100","0.2",None,None,SELL))
    print ("市价买入")
    print (HuobiService.buyMarket(2,"30",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))
时间: 2024-10-12 20:25:27

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

聚币网API[Python2版]

聚币 现货 API [Python3版] 一.utils.py,基础类,包括HTTP 请求.签名等 # -*- coding: utf-8 -*- import hashlib import hmac import time import urllib import urllib2 import json from datetime import datetime from uuid import UUID from objutil import dict_obj # import reques

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

前段时间百度地图API推出了 JavaScript API 极速版 1.0 简单看了一下,从产品定位来说真是挺好. 把开发者细分成普通web开发者和移动web开发者.正好用到了手机地图这块决定尝试一下.先看一下百度地图官方对它的定义.     本套百度地图API是专为手机浏览器提供的API,您可使用该套API,在手机页面中展示地图.标注位置.检索poi.查询线路等. 特点: 较同时兼容PC和手机浏览器的JavaScript API大众版而言,该版更适配移动设备,体积更小,加载地图速度更快,更省流

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

前段时间百度地图API推出了 JavaScript API 极速版 1.0 简单看了一下,从产品定位来说真是挺好. 把开发人员细分成普通web开发人员和移动web开发人员.正好用到了手机地图这块决定尝试一下.先看一下百度地图官方对它的定义. 尊重劳动成功请保留原文地址:http://blog.csdn.net/lbsnews/article/details/29368879     本套百度地图API是专为手机浏览器提供的API,您可使用该套API,在手机页面中展示地图.标注位置.检索poi.查

Java EE 7 教程 第一部分 简介 第1章 概述 第1.8节 Java平台中的Java EE 7 API, 标准版7

原文:http://docs.oracle.com/javaee/7/tutorial/doc/overview008.htm 翻译:石卓林 [email protected] 1.8 Java EE 7 APIs in the Java Platform, Standard Edition 7 Several APIs that are required by the Java EE 7 platform are included in the Java Platform, Standard

RESTful API终极版序列化封装

urls: from django.conf.urls import url from app01 import views urlpatterns = [ # url(r"comment/$",views.Comment.as_view()), url(r"comment/(?P<pk>\d+)",views.CommentDetailView.as_view()), url(r"article/$",views.Article.a

[ASP.net WebForm/Google] Google Map标记点(Marker)更改图示、加入对话框/API 3版

[ASP.net WebForm/Google] Google Map标记点(Marker)更改图示icon.加入对话框/API 3版 延续上一篇文章:[ASP.net WebForm/Google] 在Google Map上放置多个标记地点(Marker)/API 3版 这次要想办法把Marker更改icon (顺道一提,我一直以为使用Google Map API的话,网站一定要对外开放,目前发现只要本机电脑网络有通 有引用 就可以玩了) 另外,为了配合项目需求,程序有稍做重构且追加DB数据

微信支付 获取RSA加密公钥API JAVA版

近做微信支付  企业付款到银行卡,其中收款方银行卡号.收款方用户名需要获取RSA加密公钥API进行加密:本文以windows为例: 微信开发文档地址:https://pay.weixin.qq.com/wiki/doc/api/tools/mch_pay.php?chapter=24_7&index=4 openssl下载地址:http://slproweb.com/products/Win32OpenSSL.html 获取接口: 首先要通过接口获取PKCS#1格式的公钥,再转为PKCS#8格式

jquery easyui 1.4.1 API( CHM版)

ChangeLog Bug The combogrid has different height than other combo components. fixed. datagrid: The row element loses some class style value after calling 'updateRow' method. fixed. menubutton: Calling 'enable' method on a disabled button can not work

在qqwry.dat里查询IP地址归属地,Python3版

for Python 3.0+ 类似代码网上也有,这个的特点就是: 1.有两套实现供选择.有一个查找速度更快,但占用更多内存. 2.看着干净,仅import array和bisect这两个Python自带的模块. 用法:q = QQwry()q.load_file('qqwry.dat')q.lookup('8.8.8.8')详细用法见qqwry.py文件里面的说明 github上的项目:https://github.com/animalize/qqwry-python3 源码: # codin