??代碼只完成了核心模塊,沒有考慮多用戶下的代碼效率,沒有考慮并發問題,沒有做更多的交互。計劃后期使用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)