zoukankan      html  css  js  c++  java
  • 火币网现货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))
  • 相关阅读:
    configure: error: C++ preprocessor "/lib/cpp" fails sanity check See `config.log' for more details
    php7安装swoole
    Navicat 连接Mysql 8.0以上版本报错1251的详细解决方案
    1130
    linux中的 /etc/profile文件centos7
    linux安装php遇到的No package 'sqlite3' found,解决方法:
    ll: command not found
    每日一题 为了工作 2020 0325 第二十三题
    每日一题 为了工作 2020 0324 第二十二题
    每日一题 为了工作 2020 0323 第二十一题
  • 原文地址:https://www.cnblogs.com/bitquant/p/huobi-api-python.html
Copyright © 2011-2022 走看看