增加实盘支持仓位查询

This commit is contained in:
msincenselee 2016-10-19 21:16:10 +08:00
parent 7aa918f4f8
commit d45fe79d94
3 changed files with 62 additions and 8 deletions

View File

@ -321,7 +321,7 @@ class CtaEngine(object):
dt = datetime.now() dt = datetime.now()
if (ctaTick.datetime - dt).seconds > 10: if (ctaTick.datetime - dt).seconds > 10:
today = dt.strftime('%Y%m%d') today = dt.strftime('%Y%m%d')
if today == tick.date: if today != tick.date:
ctaTick.datetime = dt ctaTick.datetime = dt
else: else:
ctaTick.datetime = datetime.strptime(' '.join([tick.date, tick.time]), '%Y%m%d %H:%M:%S.%f') ctaTick.datetime = datetime.strptime(' '.join([tick.date, tick.time]), '%Y%m%d %H:%M:%S.%f')
@ -633,12 +633,20 @@ class CtaEngine(object):
self.writeCtaLog(u'策略实例不存在:' + name) self.writeCtaLog(u'策略实例不存在:' + name)
return None return None
#---------------------------------------------------------------------- # ----------------------------------------------------------------------
def putStrategyEvent(self, name): def putStrategyEvent(self, name):
"""触发策略状态变化事件通常用于通知GUI更新""" """触发策略状态变化事件通常用于通知GUI更新"""
event = Event(EVENT_CTA_STRATEGY+name) event = Event(EVENT_CTA_STRATEGY+name)
self.eventEngine.put(event) self.eventEngine.put(event)
# ----------------------------------------------------------------------
def getAccountInfo(self):
"""获取账号的实时权益、可用资金、仓位比例
Added by Incenselee
暂不支持多接口同时运行哦
"""
return self.mainEngine.getAccountInfo()
######################################################################## ########################################################################

View File

@ -180,7 +180,7 @@ class CtaLineBar(object):
self.lineRsiTop = [] # 记录RSI的最高峰只保留 inputRsiLen个 self.lineRsiTop = [] # 记录RSI的最高峰只保留 inputRsiLen个
self.lineRsiButtom = [] # 记录RSI的最低谷只保留 inputRsiLen个 self.lineRsiButtom = [] # 记录RSI的最低谷只保留 inputRsiLen个
self.lastRsiTopButtom = None # 最近的一个波峰/波谷 self.lastRsiTopButtom = {} # 最近的一个波峰/波谷
# K线的CMI计算数据 # K线的CMI计算数据
self.inputCmiLen = EMPTY_INT self.inputCmiLen = EMPTY_INT
@ -199,6 +199,9 @@ class CtaLineBar(object):
self.lineK = [] # K为快速指标 self.lineK = [] # K为快速指标
self.lineD = [] # D为慢速指标 self.lineD = [] # D为慢速指标
self.lineJ = [] # self.lineJ = [] #
self.lineKdjTop = [] # 记录KDJ最高峰只保留 inputKdjLen个
self.lineKdjButtom = [] # 记录KDJ的最低谷只保留 inputKdjLen个
self.lastKdjTopButtom = {} # 最近的一个波峰/波谷
# K线的MACD计算数据 # K线的MACD计算数据
self.inputMacdFastPeriodLen = EMPTY_INT self.inputMacdFastPeriodLen = EMPTY_INT
@ -282,6 +285,7 @@ class CtaLineBar(object):
self.__recoundAvgVol() self.__recoundAvgVol()
self.__recountRsi() self.__recountRsi()
self.__recountCmi() self.__recountCmi()
self.__recountKdj()
self.__recountBoll() self.__recountBoll()
self.__recountMacd() self.__recountMacd()
@ -337,6 +341,9 @@ class CtaLineBar(object):
if self.inputRsi2Len > 0 and len(self.lineRsi2) > 0: if self.inputRsi2Len > 0 and len(self.lineRsi2) > 0:
msg = msg + u',Rsi({0}):{1}'.format(self.inputRsi2Len, self.lineRsi2[-1]) msg = msg + u',Rsi({0}):{1}'.format(self.inputRsi2Len, self.lineRsi2[-1])
if self.inputKdjLen > 0 and len(self.lineK) > 0:
msg = msg + u',KDJ({0}):{1},{2},{3}'.format(self.inputKdjLen, self.lineK[-1],self.lineD[-1],self.lineJ[-1])
if self.inputBollLen > 0 and len(self.lineUpperBand)>0: if self.inputBollLen > 0 and len(self.lineUpperBand)>0:
msg = msg + u',Boll({0}):u:{1},m:{2},l:{3}'.\ msg = msg + u',Boll({0}):u:{1},m:{2},l:{3}'.\
format(self.inputBollLen, round(self.lineUpperBand[-1], 2), format(self.inputBollLen, round(self.lineUpperBand[-1], 2),
@ -926,8 +933,10 @@ class CtaLineBar(object):
# 3、inputRsi1Len(包含当前周期)的相对强弱 # 3、inputRsi1Len(包含当前周期)的相对强弱
if self.mode == self.TICK_MODE: if self.mode == self.TICK_MODE:
listClose=[x.close for x in self.lineBar[-self.inputRsi1Len - 2:-1]] listClose=[x.close for x in self.lineBar[-self.inputRsi1Len - 2:-1]]
idx = 2
else: else:
listClose=[x.close for x in self.lineBar[-self.inputRsi1Len-1:]] listClose=[x.close for x in self.lineBar[-self.inputRsi1Len-1:]]
idx = 1
barRsi = ta.RSI(numpy.array(listClose, dtype=float), self.inputRsi1Len)[-1] barRsi = ta.RSI(numpy.array(listClose, dtype=float), self.inputRsi1Len)[-1]
barRsi = round(float(barRsi), 3) barRsi = round(float(barRsi), 3)
@ -945,7 +954,7 @@ class CtaLineBar(object):
t={} t={}
t["Type"] = u'T' t["Type"] = u'T'
t["RSI"] = self.lineRsi1[-2] t["RSI"] = self.lineRsi1[-2]
t["Close"] = self.lineBar[-2].close t["Close"] = self.lineBar[-1-idx].close
if len(self.lineRsiTop) > self.inputRsi1Len: if len(self.lineRsiTop) > self.inputRsi1Len:
@ -960,7 +969,7 @@ class CtaLineBar(object):
b={} b={}
b["Type"] = u'B' b["Type"] = u'B'
b["RSI"] = self.lineRsi1[-2] b["RSI"] = self.lineRsi1[-2]
b["Close"] = self.lineBar[-2].close b["Close"] = self.lineBar[-1-idx].close
if len(self.lineRsiButtom) > self.inputRsi1Len: if len(self.lineRsiButtom) > self.inputRsi1Len:
del self.lineRsiButtom[0] del self.lineRsiButtom[0]
@ -1092,8 +1101,10 @@ class CtaLineBar(object):
if self.mode == self.TICK_MODE: if self.mode == self.TICK_MODE:
listClose =[x.close for x in self.lineBar[-self.inputKdjLen-1:-1]] listClose =[x.close for x in self.lineBar[-self.inputKdjLen-1:-1]]
idx = 2
else: else:
listClose =[x.close for x in self.lineBar[-self.inputKdjLen:]] listClose =[x.close for x in self.lineBar[-self.inputKdjLen:]]
idx = 1
hhv = max(listClose) hhv = max(listClose)
llv = min(listClose) llv = min(listClose)
@ -1108,7 +1119,10 @@ class CtaLineBar(object):
else: else:
lastD = 0 lastD = 0
rsv= ( self.lineBar[-1].close - llv)/(hhv - llv) * 100 if hhv == llv:
rsv = 50
else:
rsv= (self.lineBar[-1].close - llv)/(hhv - llv) * 100
k = 2*lastK/3 + rsv/3 k = 2*lastK/3 + rsv/3
if k < 0: k = 0 if k < 0: k = 0
@ -1128,10 +1142,41 @@ class CtaLineBar(object):
del self.lineD[0] del self.lineD[0]
self.lineD.append(d) self.lineD.append(d)
if len(self.lineJ) > self.inputKdjLen * 8: l = len(self.lineJ)
if l > self.inputKdjLen * 8:
del self.lineJ[0] del self.lineJ[0]
self.lineJ.append(j) self.lineJ.append(j)
#增加KDJ的J谷顶和波底
if l > 3:
# 峰
if self.lineJ[-1] < self.lineJ[-2] and self.lineJ[-3] <= self.lineJ[-2]:
t={}
t["Type"] = u'T'
t["J"] = self.lineJ[-2]
t["Close"] = self.lineBar[-1-idx].close
if len(self.lineKdjTop) > self.inputKdjLen:
del self.lineKdjTop[0]
self.lineKdjTop.append( t )
self.lastKdjTopButtom = self.lineKdjTop[-1]
# 谷
elif self.lineJ[-1] > self.lineJ[-2] and self.lineJ[-3] >= self.lineJ[-2]:
b={}
b["Type"] = u'B'
b["J"] = self.lineJ[-2]
b["Close"] = self.lineBar[-1-idx].close
if len(self.lineKdjButtom) > self.inputKdjLen:
del self.lineKdjButtom[0]
self.lineKdjButtom.append(b)
self.lastKdjTopButtom = self.lineKdjButtom[-1]
def __recountMacd(self): def __recountMacd(self):
""" """
Macd计算方法 Macd计算方法

View File

@ -25,6 +25,7 @@ class CtaPosition:
self.posList = [] self.posList = []
self.avgPrice = EMPTY_FLOAT self.avgPrice = EMPTY_FLOAT
def avaliablePos2Add(self): def avaliablePos2Add(self):