def XMINUTE(m1, sfunc, _ts=None): ''' 1分钟以上周期 sfunc为确认周期结束的点 ''' if len(m1.sclose) == 0: return NullXMinute if not _ts.initialized: _ts.initialized = True _ts.sopen = [] _ts.sclose = [] _ts.shigh = [] _ts.slow = [] _ts.svol = [] _ts.sholding = [] _ts.iorder = [] _ts.xmin = [] #开盘分钟 _ts.cur = BaseObject( vopen=0, vclose=0, vhigh=0, vlow=99999999, xmin=0, svol=0, holding=0, iorder=0, ) _ts.ilast = 0 _ts.modified = False #上周期完成标志 scur = _ts.cur for i in range(_ts.ilast, len(m1.sclose)): morder = m1.iorder[i] if scur.vopen == 0: scur.vopen = m1.sopen[i] scur.xmin = m1.min1[i] scur.vclose = m1.sclose[i] scur.svol += m1.svol[i] scur.holding = m1.sholding[i] if m1.shigh[i] > scur.vhigh: scur.vhigh = m1.shigh[i] if m1.slow[i] < scur.vlow: scur.vlow = m1.slow[i] _ts.modified = False if sfunc(morder): #切换 _ts.sopen.append(scur.vopen) _ts.sclose.append(scur.vclose) _ts.shigh.append(scur.vhigh) _ts.slow.append(scur.vlow) _ts.svol.append(scur.svol) _ts.sholding.append(scur.holding) _ts.iorder.append(scur.iorder) _ts.xmin.append(scur.xmin) scur.vopen = 0 scur.vclose = 0 scur.vhigh = 0 scur.vlow = 99999999 scur.svol = 0 scur.xmin = 0 scur.holding = 0 scur.iorder += 1 _ts.modified = True _ts.ilast = len(m1.sclose) return _ts
def MINUTE(ticks, pre_min1=None, t2order=t2order_if, _ts=None): ''' 分钟切分 这个实现的最大问题是未处理最后一分钟的收尾 但这种场景仅用于保存数据, 可以在使用MINUTE之后, 由特别的语句去判断最后一个tick,并收尾最后一分钟 pre_min1为默认时,只能用当日分钟 反之延续之前的分钟 ''' if len(ticks) == 0: return NullMinute if not _ts.initialized: _ts.initialized = True if pre_min1 == None: #不接续 _ts.sopen = [] _ts.sclose = [] _ts.shigh = [] _ts.slow = [] _ts.svol = [] _ts.sholding = [] _ts.iorder = [] _ts.min1 = [] else: _ts.sopen = pre_min1.sopen _ts.sclose = pre_min1.sclose _ts.shigh = pre_min1.shigh _ts.slow = pre_min1.slow _ts.svol = pre_min1.svol _ts.sholding = pre_min1.sholding _ts.iorder = pre_min1.iorder _ts.min1 = pre_min1.min1 _ts.cur = BaseObject( vopen=ticks[0].price, vclose=ticks[0].price, vhigh=ticks[0].price, vlow=ticks[0].price, open_dvol=ticks[0].dvolume, #存在初始误差 close_dvol=ticks[0].dvolume, holding=ticks[0].holding, min1=ticks[0].min1, #当日第一min1 iorder=t2order[ticks[0].min1] ) #这里对dvol的处理,使得中断恢复也必须从当日最开始开始,否则所有前述成交量被归结到第一tick _ts.ilast = 0 _ts.modified = False #上周期完成标志 scur = _ts.cur for i in range(_ts.ilast, len(ticks)): tcur = ticks[i] #if tcur.min1 != scur.min1: #切换 if tcur.min1 > scur.min1: #切换, 避免ticks数据错误引发分钟序列紊乱,如20120905:958:59:500插入在20120905:959:00:00之后,虽然非常罕见,但会导致分钟序列出现958->959->958->959...这个情况 _ts.sopen.append(scur.vopen) _ts.sclose.append(scur.vclose) _ts.shigh.append(scur.vhigh) _ts.slow.append(scur.vlow) _ts.svol.append(scur.close_dvol - scur.open_dvol) _ts.sholding.append(scur.holding) _ts.min1.append(scur.min1) _ts.iorder.append(scur.iorder) scur.vopen = scur.vclose = scur.vhigh = scur.vlow = tcur.price scur.open_dvol = scur.close_dvol scur.close_dvol = tcur.dvolume scur.dvol = tcur.dvolume scur.holding = tcur.holding scur.min1 = tcur.min1 scur.iorder = t2order[tcur.min1] _ts.modified = True else: #未切换 scur.vclose = tcur.price scur.close_dvol = tcur.dvolume scur.holding = tcur.holding #print scur.min1,'close:',scur.vclose if tcur.price > scur.vhigh: scur.vhigh = tcur.price elif tcur.price < scur.vlow: scur.vlow = tcur.price _ts.modified = False _ts.ilast = len(ticks) return _ts
def MINUTE(dates, times, prices, dvols, holdings, pre_min1=None, _ts=None): ''' 分钟切分, 参数不同 _ts用于暂存。同时可用于接续历史数据 如果pre_min1不为空,调用者需保证ticks[0].min1 > pre_min1.stime ''' if len(prices) == 0: return NullMinute if not _ts.initialized: _ts.initialized = True if pre_min1 == None: #不接续 _ts.sopen = [] _ts.sclose = [] _ts.shigh = [] _ts.slow = [] _ts.svol = [] _ts.sholding = [] _ts.sdate = [] _ts.stime = [] else: _ts.sopen = pre_min1.sopen _ts.sclose = pre_min1.sclose _ts.shigh = pre_min1.shigh _ts.slow = pre_min1.slow _ts.svol = pre_min1.svol _ts.sholding = pre_min1.sholding _ts.sdate = pre_min1.sdate _ts.stime = pre_min1.stime _ts.cur = BaseObject( vopen=prices[0], vclose=prices[0], vhigh=prices[0], vlow=prices[0], open_dvol=dvols[0], #存在初始误差 close_dvol=dvols[0], holding=holdings[0], xtime=time2min(times[0]), #启动后第一min1 xdate=dates[0], ) #这里对dvol的处理,使得中断恢复也必须从当日最开始开始,否则所有前述成交量被归结到第一tick _ts.ilast = 0 _ts.modified = False #上周期完成标志 _ts.modified = False #上周期完成标志 scur = _ts.cur for i in range(_ts.ilast, len(prices)): tmin1 = time2min(times[i]) if tmin1 > scur.xtime or (tmin1 == 0 and scur.xtime > 0) or ( dates[i] > scur.xdate and scur.xtime > 0): #tcur.min1 = 0, 分钟切换用,要求其它字段均为0 if scur.xtime > 0: #前一分钟不是切换标志 _ts.sopen.append(scur.vopen) _ts.sclose.append(scur.vclose) _ts.shigh.append(scur.vhigh) _ts.slow.append(scur.vlow) _ts.svol.append(scur.close_dvol - scur.open_dvol) _ts.sholding.append(scur.holding) _ts.sdate.append(scur.xdate) _ts.stime.append(scur.xtime) scur.vopen = scur.vclose = scur.vhigh = scur.vlow = prices[i] scur.open_dvol = scur.close_dvol scur.close_dvol = dvols[i] scur.dvol = dvols[i] scur.holding = holdings[i] scur.xdate = dates[i] scur.xtime = tmin1 _ts.modified = True else: #未切换 scur.vclose = prices[i] scur.close_dvol = dvols[i] scur.holding = holdings[i] #print tmin1,'close:',scur.vclose if prices[i] > scur.vhigh: scur.vhigh = prices[i] elif prices[i] < scur.vlow: scur.vlow = prices[i] _ts.ilast = len(prices) return _ts
#print 'initialize REF' for i in range(len(_ts.ref), offset if offset <= len(source) else len(source)): _ts.ref.append(source[0]) for i in range(len(_ts.ref), len(source)): _ts.ref.append(source[i - offset]) return _ts.ref NullMinute = BaseObject(sopen=[], sclose=[], shigh=[], slow=[], svol=[], iorder=[], sholding=[]) @indicator def MINUTE(ticks, pre_min1=None, t2order=t2order_if, _ts=None): ''' 分钟切分 这个实现的最大问题是未处理最后一分钟的收尾 但这种场景仅用于保存数据, 可以在使用MINUTE之后, 由特别的语句去判断最后一个tick,并收尾最后一分钟 pre_min1为默认时,只能用当日分钟 反之延续之前的分钟 '''
_ts.ref.append(source[0]) for i in range(len(_ts.ref), len(source)): _ts.ref.append(source[i - offset]) return _ts.ref ####分钟切换 time2min = lambda t: t // 100000 NullMinute = BaseObject(sopen=[], sclose=[], shigh=[], slow=[], svol=[], sholding=[], sdate=[], stime=[], modified=False) @indicator def MINUTE_1(ticks, pre_min1=None, _ts=None): ''' 分钟切分, 以ticks为参数 _ts用于暂存。同时可用于接续历史数据 如果pre_min1不为空,调用者需保证ticks[0].min1 > pre_min1.stime ''' if len(ticks) == 0: