以下代码通过易盛的极星量化版实现,其他平台不可用。
def risk(contractId1,percent):
if A_BuyPosition(contractNo=contractId1)>0 and Q_Last()<A_BuyAvgPrice()*(1-percent):
avloid=DeleteAllOrders(contractNo=contractId1)
LogInfo('risk')
if avloid!=None:
DeleteAllOrders(contractNo=contractId1)
else:
mysendorder.closelong(A_BuyPosition(),Q_BidPrice(),ExchangeName())
if A_SellPosition(contractNo=contractId1)>0 and Q_Last()>A_SellAvgPrice()*(1+percent):
avloid=DeleteAllOrders(contractNo=contractId1)
LogInfo('risk')
if avloid!=None:
DeleteAllOrders(contractNo=contractId1)
else:
mysendorder.closeshort(A_SellPosition(),Q_AskPrice(),ExchangeName())