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

??代碼只完成了核心模塊,沒有考慮多用戶下的代碼效率,沒有考慮并發問題,沒有做更多的交互。計劃后期使用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)
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • 關于Mongodb的全面總結 MongoDB的內部構造《MongoDB The Definitive Guide》...
    中v中閱讀 32,012評論 2 89
  • 今天中午看了復仇者聯盟 無限戰爭,漫威十年力作,電影里出現了各路英雄,在這里再次給各路英雄打call啦! 第一次接...
    Nancy0611閱讀 344評論 0 0
  • 我這周非常的開心,。因為我每天都有事情可干。這周我又學了架子鼓新曲子 ,now divde我還把桌子分了類 ...
    雷子赫閱讀 144評論 0 1
  • 這世界多簡單—— 我喜歡你。 我很喜歡你。 和我在一起。 怎么辦,我還是喜歡你。 四行情詩,就湊夠了許多許多年里牽...
    張大大的小嘴巴閱讀 616評論 0 0