??代碼只完成了核心模塊,沒(méi)有考慮多用戶下的代碼效率,沒(méi)有考慮并發(fā)問(wèn)題,沒(méi)有做更多的交互。計(jì)劃后期使用java的swing寫(xiě)Windows下的GUI客戶端。正在研究使用機(jī)械學(xué)習(xí)實(shí)現(xiàn)價(jià)格走勢(shì)的預(yù)判,感興趣請(qǐng)聯(lián)系微信 mine123045,一起討論
??策略模型

策略模型
??核心代碼
#!/usr/bin/env python3
#-*- coding:utf-8 -*-
from HuobiServices import *
import json
import demjson
import time
import hues
#自定義參數(shù)設(shè)置
symbol = 'btcusdt'
base_price = 300
price_float = 200
trade_amount = 0.1
buy_price = base_price - price_float
sell_price = base_price + price_float
#買(mǎi)單
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('買(mǎi)入成功')
time.sleep(2)
return trade_buy['data']
else :
buy(symbol,amount,buy_price)
#賣(mài)單
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('賣(mài)出成功')
time.sleep(2)
return trade_sell['data']
else :
sell(symbol,amount,sell_price)
#監(jiān)聽(tīng)訂單狀態(tài),當(dāng)有交易完成時(shí),返回交易價(jià)格
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('買(mǎi)單交易成功,撤銷(xiāo)賣(mài)單,并重新下單')
cancel_order(sell_id)
temp_price = buy_info['data']['price']
time.sleep(5)
return temp_price
elif sell_info['data']['status'] == 'filled' :
hues.info('賣(mài)單交易成功,撤銷(xiāo)買(mǎi)單,并重新下單')
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)
#持續(xù)監(jiān)聽(tīng)并自動(dòng)交易
main(symbol,amount,buy_id,sell_id)