# 双平台对冲 练习用 求交流求指点

Author: perseus, Date: 2017-09-02 15:14:53
Tags: Python Hedge

Python 双平台对冲策略

import time
import numpy as np

def reject_outliers(arr, m=2):
final_list = [x for x in arr if (x > np.mean(arr) - 2 * np.std(arr))]
final_list = [x for x in final_list if (x < np.mean(arr) + 2 * np.std(arr))]
return final_list

def main():
'''SetErrorFilter("canceled")'''

LogReset()
LogProfitReset()

global sellTrue
sellTrue = 0
initStocks = 0.0
initBalance = 0.0
accountsCache = []
depthCache=[]
names = []
sellPrice=0
dealtransactioned = False

for e in exchanges:
names.append(e.GetName())
account = _C(e.GetAccount)
initStocks += account.Stocks
initBalance += account.Balance
Log("Switch", e.GetLabel(), "To", e.IO("websocket"))

Log("Total Currency:", _N(initBalance), "Total Asset", _N(initStocks), 'Python:', __import__('sys').version)
while (True):
LogStatus(_D())
if not accountsCache:
accountsCache = [_C(e.GetAccount) for e in exchanges]
Sleep(LoopInterval)
depthA = exchanges[0].GetDepth()
depthCache.append(depthA)
if not depthA:
continue
depthB = exchanges[1].GetDepth()
depthCache.append(depthB)
if not depthB:
continue

diffA = _N(depthA.Bids[0].Price - depthB.Asks[0].Price, 3)
diffB = _N(depthB.Bids[0].Price - depthA.Asks[0].Price, 3)
LogStatus("A平台币",exchanges[0].GetAccount().Stocks,"A平台钱",exchanges[0].GetAccount().Balance,"B平台币",exchanges[1].GetAccount().Stocks,"B平台钱",exchanges[1].GetAccount().Balance)

if diffA > 0:

Log('buy from B sell at A')
Log("A平台币",exchanges[0].GetAccount().Stocks,"A平台钱",exchanges[0].GetAccount().Balance,"B平台币",exchanges[1].GetAccount().Stocks,"B平台钱",exchanges[1].GetAccount().Balance)
canceller = 0

while True:

if (exchanges[1].GetOrder(buyorder).Status == 2) and (exchanges[0].GetOrder(sellorder).Status == 2):
Log('deal transactioned')
sellPrice = exchanges[0].GetOrder(sellorder).Price
dealtransactioned = True
break
elif ((exchanges[1].GetOrder(buyorder).Status == 1) or (exchanges[0].GetOrder(sellorder).Status == 1)):
sellPrice = exchanges[0].GetOrder(sellorder).Price
canceller += 1
dealtransactioned = True
Sleep(200)
elif ((exchanges[1].GetOrder(buyorder).Status == 0) and (exchanges[0].GetOrder(sellorder).Status == 0)):
Sleep(200)
canceller += 1
if canceller == 5:
exchanges[0].CancelOrder(sellorder)
Log('deal cancelled')
break

elif diffB > 0:

canceller = 0
Log('buy from A sell at B')

canceller =0

while True:

if (exchanges[0].GetOrder(buyorder).Status == 2) and (exchanges[1].GetOrder(sellorder).Status == 2):
Log('deal transactioned')
sellPrice=exchanges[1].GetOrder(sellorder).Price
dealtransactioned = True
break
elif ((exchanges[0].GetOrder(buyorder).Status == 1) or (exchanges[1].GetOrder(sellorder).Status == 1)):

sellPrice=exchanges[1].GetOrder(sellorder).Price
canceller += 1
dealtransactioned = True
Sleep(200)
elif ((exchanges[0].GetOrder(buyorder).Status == 0) and (exchanges[1].GetOrder(sellorder).Status == 0)):
Sleep(200)
canceller += 1
if canceller == 5:
exchanges[1].CancelOrder(sellorder)
Log('deal cancelled')
break

'''balance part'''
for i in [0,1]:
if dealtransactioned:
if(exchanges[i].GetAccount().Stocks > (initStocks/2)* q and exchanges[i].GetAccount().Balance < (initBalance/2)*q):
sellwait = 1
loopbreaker = 0
while (sellwait):

sellwait = 0

break
else:
Sleep(1000)
Log('sellwait')
loopbreaker += 1
if loopbreaker == 600:
'''break'''

sellTrue = 1

while(sellTrue):
dealprice = depthCache[i].Bids[0].Price
Log('insufficient money, sell some')
idealamount = initBalance/8
availamount = exchanges[i].GetAccount().Stocks/2
dealamount = min(idealamount,availamount)
balancesell = exchanges[i].Sell(depthCache[i].Bids[0].Price, dealamount)
Sleep(200)
while( exchanges[i].GetOrder(balancesell).Status not in [1,2] ):
exchanges[i].CancelOrder(balancesell)
dealprice -= 1
balancesell = exchanges[i].Sell(dealprice,dealamount)
Sleep(200)
Log("Sell Balance finished")
sellTrue=0

if(exchanges[i].GetAccount().Balance > (initBalance/2)*q and exchanges[i].GetAccount().Stocks < (initStocks/2)*q):
loopbreaker = 0

break
else:
Sleep(1000)
loopbreaker +=1
if loopbreaker == 600:
'''break
break'''

idealamount = initStocks/8
availamount = exchanges[i].GetAccount().Balance/dealprice/2
dealamount = min(idealamount,availamount)
Sleep(200)

if (dealprice < sellPrice):
dealprice += 1
Sleep(200)
'''sellPrice=[]'''
break
else:
continue
break
else:
break

Related

More

quant_lxd 如何加你为好友？

leocpy 加点注释吧，一起学习。

DrLiu 能否加上注释，方便学习和交流

perseus 请不要将此策略用于实盘！我是程序新手，仅供交流学习用，求指教！

perseus 好的！我会慢慢加注释的，谢谢你的关注，一起交流一起进步