火币API量化交易策略<一>

96
有信仰的人
0.1 2018.12.22 17:15 字数 109

  代码只完成了核心模块,没有考虑多用户下的代码效率,没有考虑并发问题,没有做更多的交互。计划后期使用java的swing写Windows下的GUI客户端。正在研究使用机械学习实现价格走势的预判,感兴趣请联系微信 mine123045,一起讨论


  策略模型


策略模型

  核心代码

    #!/usr/bin/env python3
    #-*- coding:utf-8 -*-

    from HuobiServices import *
    import json
    import demjson
    import time
    import hues

    #自定义参数设置
    symbol = 'btcusdt'
    base_price = 300
    price_float = 200
    trade_amount = 0.1
    buy_price = base_price - price_float
    sell_price = base_price + price_float

    #买单
    def buy(symbol,amount,buy_price):
        trade_buy = send_order(amount, 'api', symbol, 'sell-market', price=buy_price)
        if trade_buy['status'] == 'ok'
            hues.log('买入成功')
            time.sleep(2)
            return trade_buy['data']
        else :
            buy(symbol,amount,buy_price)

    #卖单
    def sell(symbol,amount,sell_price):
        trade_sell = send_order(amount, 'api', symbol, 'sell-market', price=sell_price)
        if trade_sell['status'] == 'ok'
            hues.log('卖出成功')
            time.sleep(2)
            return trade_sell['data']
      else :
            sell(symbol,amount,sell_price)

    #监听订单状态,当有交易完成时,返回交易价格
    def watch(buy_id,sell_id)
        buy_info = order_matchresults(buy_id)
        sell_info = order_matchresults(sell_id)
        if buy_info['data']['status'] == 'filled' :
            hues.info('买单交易成功,撤销卖单,并重新下单')
            cancel_order(sell_id)
            temp_price = buy_info['data']['price']
            time.sleep(5)
            return temp_price
        elif sell_info['data']['status'] == 'filled' :
            hues.info('卖单交易成功,撤销买单,并重新下单')
            cancel_order(sell_id)
            temp_price = sell_info['data']['price']
            time.sleep(5)
            return temp_price
        else:
            watch(buy_id,sell_id)
    
    def main(symbol,amount,buy_id,sell_id,price_float):
        trade_price = wathc(buy_id,sell_id)
        bprice = trade_price - price_float
        sprice = trade_price + price_float
        buy_id = buy(symbol,amount,bprice)
        sell_id = sell(symbol,amount,sprice)
        time.sleep(5)
        main(symbol,amount,buy_id,sell_id)

    #首次交易
    buy_id = buy(symbol,amount,buy_price)
    sell_id = sell(symbol,amount,sell_price)

    #持续监听并自动交易
    main(symbol,amount,buy_id,sell_id)
加密货币
Web note ad 1