forked from qmhedging/poboquant
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path持仓排名策略 Open Interests Ranking Following
104 lines (84 loc) · 3.32 KB
/
持仓排名策略 Open Interests Ranking Following
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
#!/usr/bin/env python
# coding:utf-8
from PoboAPI import *
import datetime
import numpy as np
#用poboquant python实现,在poboquant上运行,如果有问题 可加群 726895887 咨询
#开始时间,用于初始化一些参数
def OnStart(context) :
print "I\'m starting..."
#登录交易账号,需在主页用户管理中设置账号,并把期货测试替换成您的账户名称
context.myacc = None
if context.accounts.has_key("回测期货") :
print "登录交易账号[回测期货]"
if context.accounts["回测期货"].Login() :
context.myacc = context.accounts["回测期货"]
def OnMarketQuotationInitialEx(context, exchange,daynight):
if exchange != "CZCE":#'DCE':
return
#获取主力合约
g.code = GetMainContract('CZCE', 'TA',20)
#订阅K线数据,用于驱动OnBar事件
SubscribeBar(g.code, BarType.Day)
#实时行情事件,当有新行情出现时调用该事件Ex
def GetTradingDayLeft(code):
import re
m=re.match(r'[a-zA-Z]+[0-9]+\.+[A-Z]',code)
print "checking m----------------------------"
print "m "+str(m)
print "checking code "+str(code)
if m:
info=GetContractInfo(code)
T=info['最后交易日']
exchange=GetExchangeByCode(code)
t=GetCurrentTradingDate(exchange)
return (T-t).days
else:
info=GetContractInfo(code)
T=info['行权到期日']
exchange=GetExchangeByCode(code)
t=GetCurrentTradingDate(exchange)
return (T-t).days
def OnBar(context,code,bartype) :
#过滤掉不需要的行情通知
sig=0
if code != g.code:
return
dyndata = GetQuote(g.code)
currentday=GetCurrentTradingDate('DCE')
rankdata = GetContractRanking(0,g.code,1,currentday,currentday,1)
#print rankdata[0]
#计算均线
n = 0
while n<len(rankdata):
for oo in rankdata[n].keys():
print str(oo) +':' + str(rankdata[n][oo])
if rankdata[n]['会员']=="永安期货": #银河期货 中信期货 海通期货 whatever
print "sig=1"
sig=1
n +=1
if sig==1:
QuickInsertOrder(context.myacc,g.code,'buy','open',dyndata.now,1)
if sig==0:
QuickInsertOrder(context.myacc,g.code,'sell','close',dyndata.now,1)
option = PBObj()
option.buysellflag = '0'
pos = context.accounts["回测期货"].GetPositions(option)
print "持仓检查 "+str(len(pos))
if len(pos)>0:
for i in pos:
daysleft =GetTradingDayLeft(i.contract)
if daysleft<10:
print "平仓到期合约 "+str(i.contract)
dyndatapos = GetQuote(i.contract)
QuickInsertOrder(context.myacc,i.contract,'sell','close',dyndatapos.now,i.availvolume)
UnsubscribeBar(i.contract, BarType.Day)
print '接近到期平仓----------------'
# if len(MA2)<2:
# return
# #ma1上穿ma2时买入螺纹主力1手
# elif MA1[-1] >= MA2[-1] and MA1[-2]<MA2[-2]:
# QuickInsertOrder(context.myacc,g.code,'buy','open',dyndata.now,1)
# #ma1下穿ma2时卖出平仓
# elif MA1[-1] <= MA2[-1] and MA1[-2]>MA2[-2]:
# QuickInsertOrder(context.myacc,g.code,'sell','close',dyndata.now,1)