forked from qmhedging/poboquant
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path账户风控止损 Account Risk Control Excample Cut Loss Liquidation
154 lines (111 loc) · 7.06 KB
/
账户风控止损 Account Risk Control Excample Cut Loss Liquidation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# coding:utf-8
#!/usr/bin/env python
from PoboAPI import *
import datetime
#python实现期货账户风控,如果有问题 可加群 726895887 咨询
#开始时间,用于初始化一些参数
def OnStart(context) :
print "I\'m starting..."
accountname="sm113053" #这里填自己设置好的(期货)账户名
#设定一个全局变量品种
g.code = GetMainContract('DCE','m',20) #订阅一个品种 驱动 onQuote
#订阅实时数据,用于驱动OnQuote事件
SubscribeQuote(g.code)
#订阅K线数据,用于驱动OnBar事件
#SubscribeBar(g.code, BarType.Day)
#登录交易账号,需在主页用户管理中设置账号,并把证券测试替换成您的账户名称
context.myacc = None
if context.accounts.has_key(accountname) :
print "登录期货交易账号:"+str(accountname)
if context.accounts[accountname].Login() :
context.myacc = context.accounts[accountname]
def CutPrice(price,percent,buyorsell): #生成止损平仓价格 (平仓价,浮动比例,多空标志)
print "开始计算止损价格-------------------------------------------------"
if int(buyorsell)==0:#多头,卖平止损
cutprice=round(price*(1-percent),-1) #下调价格 确保卖平
if int(buyorsell)==1:#空头,买平止损
cutprice=round(price*(1+percent),-1) #上调价格 确保买平
return float(cutprice)
def CutLoss(pos,context): #账户止损函数,实现完全清仓
print "开始止损-------------------------------------------------------------------"
for i in pos:
contractname=i.contract
print "持仓合约: "+str(contractname)
contractExchange=str(contractname).split(".", 1)[1]#读取交易所名称,即"m1901.DCE"的后半段
print "合约交易所:"+str(contractExchange) +str(len(contractExchange))
NowPriceSet=GetQuote(contractname) #取合约 最新价格
NowPrice=NowPriceSet.now
print "合约最新价格:" + str(NowPrice)
openprice=i.openavgprice
print "开仓均价: "+str(openprice)
availablevolume=i.availvolume
print "持仓数量: "+str(availablevolume)
BuySellFlag=i.bstype.BuySellFlag
print "多空方向:"+str(BuySellFlag)
TodayFlag=i.bstype.TodayFlag
print "今仓昨仓:"+str(TodayFlag)# true 今仓 False 昨仓
#posnum=int(i.availvolume)
print "reach-------------------cuting conditions--------------------"
if str(contractExchange)=="SHFE" and NowPrice<>0.0: #上期所 for SHFE
print "上期所合约"
if str(TodayFlag)=="True": #持有上期所今仓
if int(BuySellFlag)==0: # 持有多头
TradePrice=CutPrice(NowPrice, 0.02, BuySellFlag) # 生成平仓价格 (平仓价,浮动比例,多空标志)
print "cut price is "+str(TradePrice)
context.myacc.InsertOrder(contractname, BSType.SellCloseToday, TradePrice, availablevolume) #上期所 多头 卖平今
if int(BuySellFlag)==1: # 持有空头
TradePrice=CutPrice(NowPrice, 0.02, BuySellFlag) # 生成平仓价格 (平仓价,浮动比例,多空标志)
print "cut price is "+str(TradePrice)
context.myacc.InsertOrder(contractname, BSType.BuyCloseToday, TradePrice, availablevolume) #上期所 空头 买平今
if str(TodayFlag)=="False": #持有上期所昨仓
if int(BuySellFlag)==0: # 持有多头
TradePrice=CutPrice(NowPrice, 0.02, BuySellFlag) # 生成平仓价格 (平仓价,浮动比例,多空标志)
print "cut price is "+str(TradePrice)
context.myacc.InsertOrder(contractname, BSType.SellClose, TradePrice, availablevolume) #上期所 多头 卖平昨
if int(BuySellFlag)==1: # 持有空头
TradePrice=CutPrice(NowPrice, 0.02, BuySellFlag) # 生成平仓价格 (平仓价,浮动比例,多空标志)
print "cut price is "+str(TradePrice)
context.myacc.InsertOrder(contractname, BSType.BuyClose, TradePrice, availablevolume) #上期所 多头 买平昨
if str(contractExchange)<>"SHFE" and NowPrice<>0.0: #非上期所品种
print "非上期所合约"
if int(BuySellFlag)==0: # 持有多头
TradePrice=CutPrice(NowPrice, 0.02, BuySellFlag) # 生成平仓价格 (平仓价,浮动比例,多空标志)
print "cut price is "+str(TradePrice)
context.myacc.InsertOrder(contractname, BSType.SellClose, TradePrice, availablevolume) #非上期所 多头 卖平
if int(BuySellFlag)==1: # 持有空头
TradePrice=CutPrice(NowPrice, 0.02, BuySellFlag) # 生成平仓价格 (平仓价,浮动比例,多空标志)
print "cut price is "+str(TradePrice)
context.myacc.InsertOrder(contractname, BSType.BuyClose, TradePrice, availablevolume) #非上期所 空头 买平
return
#实时行情事件,当有新行情出现时调用该事件
def OnQuote(context, code) :
#过滤掉不需要的行情通知
accountname="sm113053" #这里填自己设置好的(期货)账户名
StartBalance=1000000.0 #填写账户期初金额
print "monitoring account "+str(accountname)
CutBalance= StartBalance*0.99 #填写清盘线金额
print "the code is "+str(code)
if code != g.code :
code = GetMainContract('DCE','m',20) #订阅一个品种 驱动 onQuote
# return
#获取最新行情
dyndata = GetQuote(code)
if dyndata :
#.now指最新价,详细属性见API文档
now1 = dyndata.now
#打印最新价
#log.info("最新价: " + str(dyndata.now))
balset = context.accounts[accountname].AccountBalance #获取账户权益
bal=balset.AssetsBalance
print "balance is "+str(bal)
if bal<=CutBalance : #判断是否止损,即权益触及规定止损权益
print "Balance to cut loss is "+str(bal)
option = PBObj()
pos = context.accounts[accountname].GetPositions(option)#查询持仓
print "with positions "+str(len(pos))
if len(pos)>0:
CutLoss(pos,context)
def OnOrderChange(context, AccountName, order) :
#打印委托信息,id是编号,volume是数量,详细见API文档
print "委托编号: " + order.id + " 账号名称: " + AccountName
print "Vol: " + str(order.volume) + " Price: " + str(order.price)