優化
優化觸發條件:
之前的觸發條件有問題,導致遲遲不能觸發,優化后觸發條件如下:
dydx_take = 0.0002apex_make = 0.0005?float(b_first_price_apex)-float(s_first_price_dydx) > float(b_first_price_apex)*apex_make+float(s_first_price_dydx)*dydx_take
優化風險控制:
每次異常退出再重啟杠桿都會從0開始計算,這導致杠桿越拉越高,故而采用全局文件的方式存儲arbitrage_count
place_order_all.py
# 其他導入和函數定義...?# 讀取存儲 arbitrage_count 的文件,如果存在則讀取值try:with open('arbitrage_count.txt', 'r') as file:arbitrage_count = int(file.read())except FileNotFoundError:arbitrage_count = 0?async def arbitrage():global arbitrage_count# 其他代碼...while True:# 計算價差和交易邏輯# ...?# 在合適的地方,更新 arbitrage_count 的值# 比如:arbitrage_count += 1 ?# 或者根據你的邏輯修改 arbitrage_count# 在適當的時候將 arbitrage_count 的值寫入文件,以便下次讀取with open('arbitrage_count.txt', 'w') as file:file.write(str(arbitrage_count))# 等待 1 秒await asyncio.sleep(1)?# 其他代碼...?# 運行異步函數asyncio.run(arbitrage())
?
run.py
# 其他代碼...?if __name__ == "__main__":choice = input("請輸入要運行的文件(1-btc,2-eth,3-link,4-ltc,5-avax,6-atom,7-doge,8-bch,9-matic,10-sol,11-all ):")with open('arbitrage_count.txt', 'w') as file:file.write(str(0))while True:program = run_program(choice)if program:while program.poll() is None:time.sleep(5)print("程序已終止,重新啟動中...")time.sleep(1)
apex買賣價設置
要盡可能大和盡可能小,但不能太大,不然會觸發報錯:
{’code’: 3, ‘msg’: ‘If order is filled, your account may be liquidated.’, ‘key’: ’ORDER_POSSIBLE_LEAD_TO_ACCOUNT_LIQUIDATED’}
dydx參數設置
post-only一定要設置成false,改正后買單的price可設置成賣一價,賣單的price可設置成買一價,保證立刻成交!
否則極其容易訂單發出去就被取消!
time 設置為currentTime+1000比較好,大約15分鐘。
結果分析
時間:
大概10s達到杠桿上限
倉位對比:
apex:
dydx:
收益為:0.05764U
初始資金為:
apex:100U
dydx:100U
按此頻率,如果持續交易,則每日收益為498U
后續需要加上自動平倉的機制,以及風險控制模塊需要優化,同時多幣種套利可以算最優解,大體套利模塊已經搭好,本系列至此也就完結了。