最近工作很忙,炒币的时间越来越少,就想着是不是写自动交易的程序,按自己的思路,自动监控数字币的价格,发现机会就自动交易。
在几个交易所查了一下API文档,发现龙网的文档最全,还给出了python接口代码,于是就决定先用龙网的api做一个试试。
代码还在写,先分享给大家看一下,提供一点思路:
不知道头条是否支持源代码格式,先试试吧!
# -*- 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")
閱讀更多 區塊鏈傳奇 的文章