![迴歸技術第二篇:數字幣自動交易系統的python程序設計](http://p2.ttnews.xyz/loading.gif)
最近工作很忙,炒幣的時間越來越少,就想著是不是寫自動交易的程序,按自己的思路,自動監控數字幣的價格,發現機會就自動交易。
在幾個交易所查了一下API文檔,發現龍網的文檔最全,還給出了python接口代碼,於是就決定先用龍網的api做一個試試。
![迴歸技術第二篇:數字幣自動交易系統的python程序設計](http://p2.ttnews.xyz/loading.gif)
代碼還在寫,先分享給大家看一下,提供一點思路:
不知道頭條是否支持源代碼格式,先試試吧!
# -*- coding: utf-8 -*-
from dragonex import DragonExV1
import time
import os
#自動定時任務
from apscheduler.schedulers.background import BackgroundScheduler
ACCESS_KEY = '在龍網上申請,這個要保密'
SECRET_KEY = '在龍網上申請,這個要保密'
HOST = 'https://openapi.dragonex.im'
COIN_ID=104 #DT交易對
Price_Avg=0
Price_CurBuy=0
Price_CurSell=0
Price_CurBuyVol=0
Price_CurSellVol=0
#當前作動
P_ACTION='START'
P_Order_ID=0
P_Wait_Time=0
def getUSDT():
#獲取我的所有幣資產
r = dragonex.get_user_own_coins()
#print(type(r.data),r.data)
for i in r.data:
#print('代碼:', i['coin_id'],'幣種:', i['code'],'餘額:', i['volume'])
if i['code']=='usdt':
#print(i)
usdtcoin=i
print ('USDT餘額:',usdtcoin['volume'],usdtcoin['frozen'])
return float(usdtcoin['volume'])-float(usdtcoin['frozen'])
def getCurPrice():
# 獲取指定ID的買盤數據
r = dragonex.get_market_buy(COIN_ID)
#print(type(r.data), r.data)
BuyVolSum = 0
Price_CurBuy = float(r.data[0]['price'])
Price_CurBuyVol = float(r.data[0]['volume'])
for i in r.data:
#print('代碼:', i['price'], i['volume'])
BuyVolSum+=float(i['volume'])
print('+買盤數據', Price_CurBuy, Price_CurBuyVol,BuyVolSum)
# 獲取指定ID的賣盤數據
r = dragonex.get_market_sell(COIN_ID)
#print(type(r.data), r.data)
SellVolSum = 0
Price_CurSell = float(r.data[0]['price'])
Price_CurSellVol = float(r.data[0]['volume'])
for i in r.data:
#print('代碼:', i['price'], i['volume'])
SellVolSum+= float(i['volume'])
print('-賣盤數據', Price_CurSell, Price_CurSellVol,SellVolSum,'\n',time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())))
#自動交易分析
global P_ACTION
if (P_ACTION=='START'):
P_ACTION='WAIT'
curUSDT = getUSDT()
elif (P_ACTION=='WAIT'):
if (Price_CurBuyVol < (Price_CurSellVol * 1)):
print('提交買單')
curUSDT = getUSDT()
buy_price=4.3
buy_vol=float('%.2f' % (curUSDT/buy_price))
P_ACTION = 'POSTBUY'
print('交易價格',buy_price,buy_vol,curUSDT,P_ACTION)
r = dragonex.add_order_buy(COIN_ID, buy_price, buy_vol)
print(type(r.data), r.data)
myorder = r.data
if len(myorder) > 1:
print('交易編號:', myorder['order_id'])
P_ACTION = 'WAITBUY'
P_Order_ID= myorder['order_id']
else:
print('當前買單量是賣單的三倍以上,開始買入', Price_CurBuy, Price_CurBuyVol, Price_CurSellVol)
print(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())),'當前動作',P_ACTION)
'''
if (Price_CurBuyVol
u=getUSDT()
if u<5.0:
r = dragonex.get_user_order_history(COIN_ID,2,0,10,1)
print(type(r.data), r.data)
myorder_history = r.data
myorder_list = myorder_history['list']
for i in myorder_list:
pass
print('需撤單的訂單編號',i['order_id'])
r=dragonex.cancel_order(COIN_ID, i['order_id'])
print(type(r.data), r.data)
else:
postBuy(4.3,0.3)
print('當前買單量是賣單的三倍以上,開始買入',Price_CurBuy,Price_CurBuyVol,Price_CurSellVol)
else:
print('USDT',Price_CurBuy,Price_CurSell, Price_CurBuyVol,Price_CurSellVol)
'''
def postBuy(buy_price,buy_vol):
#獲取指定ID的提交買單
print('提交買單')
r=dragonex.add_order_buy(COIN_ID,buy_price,buy_vol)
print(type(r.data), r.data)
myorder=r.data
if len(myorder)>1:
print('交易編號:',myorder['order_id'])
else:
print('買單提交不成功',len(myorder))
#dragonex.cancel_order(104, 1532315844219386001)
r=dragonex.get_user_order_history(COIN_ID)
print(type(r.data), r.data)
myorder_history=r.data
print(type(myorder_history))
print(myorder_history['list'])
print(myorder_history.keys())
myorder_list=myorder_history['list']
print(type(myorder_list),myorder_list[0])
myorder_get=myorder_list[0]
print(type(myorder_get),myorder_get['order_id'])
dragonex.cancel_order(COIN_ID, myorder_get['order_id'])
if __name__ == '__main__':
dragonex = DragonExV1(access_key=ACCESS_KEY, secret_key=SECRET_KEY, host=HOST)
dragonex.ensure_token_enable(False)
#獲取我的所有幣資產
r = dragonex.get_user_own_coins()
print(type(r.data),r.data)
for i in r.data:
print('代碼:', i['coin_id'],'幣種:', i['code'],'餘額:', i['volume'])
if i['code']=='usdt':
#print(i)
usdtcoin=i
print ('USDT餘額:',usdtcoin['volume'])
#獲取指定ID的實時交易數據
r=dragonex.get_market_real(COIN_ID)
print(type(r.data), r.data)
dtcoin=r.data[0]
print(dtcoin['close_price'])
print('當前價格',dtcoin['close_price'],'24小時最高',dtcoin['max_price'],'24小時最低',dtcoin['min_price'],'價格變動量',dtcoin['price_change'],'價格變動比例',dtcoin['price_change_rate'],'24小時金額',dtcoin['total_amount'],'24小時成交數量',dtcoin['total_volume'])
Price_Avg=(float(dtcoin['max_price'])+float(dtcoin['min_price']))/2
#print('24小時平均價:',float('%0.4f' % Price_Avg),Price_Avg)
scheduler = BackgroundScheduler()
scheduler.add_job(getCurPrice, 'interval', seconds=3)
scheduler.start()
print('wait index and list...\n')
input("Press the enter key to exit.\n")
閱讀更多 區塊鏈傳奇 的文章