コード例 #1
0
    def __init__(self, stra_barupdate=None, stra_onorder=None):
        '''初始所有变量'''
        self.stra_uppdate = stra_barupdate
        self.stra_onorder = stra_onorder
        '''每bar只执行一次交易'''
        self.SingleOrderOneBar = False
        '''每bar只执行一 次交易'''
        '''K线序列'''
        self.Bars = []
        '''K线序列'''
        '''合约'''
        self.Instrument = ''
        '''合约'''
        '''合约信息'''
        self.InstrumentInfo = InstrumentField()
        '''合约信息'''
        '''周期'''
        self.Interval = 1
        '''周期'''
        '''周期类型'''
        self.IntervalType = IntervalType.Minute
        '''周期类型'''
        '''分笔数据
        Tick.Instrument用来判断是否有实盘数据'''
        self.Tick = Tick()
        '''分笔数据
        Tick.Instrument用来判断是否有实盘数据'''
        '''买卖信号'''
        self.Orders = []
        '''买卖信号'''
        '''指标字典
        策略使用的指标保存在此字典中
        以便管理程序显示和处理'''
        self.IndexDict = {}
        '''指标字典
        策略使用的指标保存在此字典中
        以便管理程序显示和处理'''
        '''时间'''
        self.D = []
        '''时间'''
        '''最高价'''
        self.H = []
        '''最高价'''
        '''最低价'''
        self.L = []
        '''最低价'''
        '''开盘价'''
        self.O = []
        '''开盘价'''
        '''收盘价'''
        self.C = []
        '''收盘价'''
        '''交易量'''
        self.V = []
        '''交易量'''
        '''持仓量'''
        self.I = []
        '''持仓量'''

        self._lastOrder = OrderItem()
コード例 #2
0
    def q_Tick(self, q: CtpQuote, tick: Tick):
        """"""
        # 对tick时间进行修正处理
        ut = tick.UpdateTime[0:6] + '00'
        mins_dict = self.trade_time[tick.Instrument]
        # 由下面的 updatetime[-2:0] != '00' 处理
        if ut not in mins_dict['Mins']:
            # 开盘/收盘
            if ut in mins_dict['Opens']:
                tick.UpdateTime = (datetime.strptime(ut, '%H:%M:%S') +
                                   timedelta(minutes=1)).strftime('%H:%M:%S')
            elif ut in mins_dict['Ends']:
                # 重新登录会收到上一节的最后tick
                tick_dt = datetime.strptime(
                    '{} {}'.format(datetime.now().strftime('%Y%m%d'),
                                   tick.UpdateTime), '%Y%m%d %H:%M:%S')
                now_dt = datetime.now()
                diff_snd = 0
                if tick_dt > now_dt:
                    diff_snd = (tick_dt - now_dt).seconds
                else:
                    diff_snd = (now_dt - tick_dt).seconds
                if diff_snd > 30:
                    return
                tick.UpdateTime = (datetime.strptime(ut, '%H:%M:%S') +
                                   timedelta(seconds=-1)).strftime('%H:%M:%S')
            else:
                return
        # 首tick不处理(新开盘时会收到之前的旧数据)
        if tick.Instrument not in self.received_instrument:
            self.received_instrument.append(tick.Instrument)
            return

        actionday = self.TradingDay
        if tick.UpdateTime[0:2] > '20':
            actionday = self.Actionday
        elif tick.UpdateTime[0:2] < '04':
            actionday = self.Actionday1

        ut = actionday[0:4] + '-' + actionday[4:6] + '-' + actionday[
            6:] + ' ' + tick.UpdateTime
        tick.UpdateTime = ut
        for stra in self.stra_instances:
            for data in stra.Datas:
                if data.Instrument == tick.Instrument:
                    data.on_tick(tick, self.TradingDay)
        self.tick_time = ut
コード例 #3
0
ファイル: a_t_p.py プロジェクト: windstart/hf_at_py
    def q_Tick(self, q: CtpQuote, tick: Tick):
        """"""
        # print(tick)
        # self.fix_tick(tick)
        actionday = self.TradingDay
        if tick.UpdateTime[0:2] > '20':
            actionday = self.Actionday
        elif tick.UpdateTime[0:2] < '04':
            actionday = self.Actionday1

        ut = actionday[0:4] + '-' + actionday[4:6] + '-' + actionday[
            6:] + ' ' + tick.UpdateTime
        tick.UpdateTime = ut
        for stra in self.stra_instances:
            for data in stra.Datas:
                if data.Instrument == tick.Instrument:
                    data.on_tick(tick, self.TradingDay)
        self.tick_time = ut
コード例 #4
0
ファイル: atp.py プロジェクト: sunli212/hf_at_py
 def read_ticks(self, stra: Strategy, tradingday: str) -> []:
     """读取tick数据
     返回 list[Tick]"""
     ticks: list = []
     if self.cfg.engine_postgres is not None:
         conn = self.cfg.engine_postgres.raw_connection()
         cursor = conn.cursor()
         sql = "select count(1) from pg_tables where schemaname='future_tick' and tablename='{}'".format(
             tradingday)
         try:
             cursor.execute(sql)
             if cursor.fetchone()[0] == 0:
                 return []
             for data in stra.Datas:
                 sql = 'select "Actionday", "AskPrice", "AskVolume", "BidPrice", "BidVolume", "Instrument", "LastPrice", "OpenInterest", "UpdateMillisec", "UpdateTime", "Volume" from future_tick."{}" where "Instrument" = \'{}\''.format(
                     tradingday, data.Instrument)
                 cursor.execute(sql)
                 rows = cursor.fetchall()
                 for d in rows:
                     tick = Tick()
                     tick.Instrument = data.Instrument
                     tick.AskPrice = d[1]
                     tick.AskVolume = d[2]
                     tick.BidPrice = d[3]
                     tick.BidVolume = d[4]
                     tick.LastPrice = d[6]
                     tick.OpenInterest = d[7]
                     tick.UpdateMillisec = d[8]
                     tick.UpdateTime = d[0][0:4] + '-' + d[0][
                         4:6] + '-' + d[0][6:] + ' ' + d[9]
                     tick.Volume = d[10]
                     ticks.append(tick)
         finally:
             conn.close()
     ticks.sort(key=lambda t: t.UpdateTime)
     return ticks
コード例 #5
0
ファイル: quote.py プロジェクト: weiyuzhu/hf_ctp_py_proxy
    def _OnRtnDepthMarketData(
            self, pDepthMarketData: CThostFtdcDepthMarketDataField):
        """"""
        tick: Tick = None
        # 这个逻辑交由应用端处理更合理 ==> 第一个tick不送给客户端(以处理隔夜早盘时收到夜盘的数据的问题)
        inst = pDepthMarketData.getInstrumentID()
        if inst not in self.inst_tick:
            tick = Tick()
            self.inst_tick[inst] = tick
        else:
            tick = self.inst_tick[inst]

        tick.AskPrice = pDepthMarketData.getAskPrice1()
        tick.AskVolume = pDepthMarketData.getAskVolume1()
        tick.AveragePrice = pDepthMarketData.getAveragePrice()
        tick.BidPrice = pDepthMarketData.getBidPrice1()
        tick.BidVolume = pDepthMarketData.getBidVolume1()
        tick.Instrument = pDepthMarketData.getInstrumentID()
        tick.LastPrice = pDepthMarketData.getLastPrice()
        tick.OpenInterest = pDepthMarketData.getOpenInterest()
        tick.Volume = pDepthMarketData.getVolume()

        # 用tradingday替代Actionday不可取
        # day = pDepthMarketData.getTradingDay()
        # str = day + ' ' + pDepthMarketData.getUpdateTime()
        # if day is None or day == ' ':
        #     str = time.strftime('%Y%m%d %H:%M:%S', time.localtime())
        # tick.UpdateTime = str  # time.strptime(str, '%Y%m%d %H:%M:%S')

        tick.UpdateTime = pDepthMarketData.getUpdateTime()
        tick.UpdateMillisec = pDepthMarketData.getUpdateMillisec()

        # 用线程会导入多数据入库时报错
        # _thread.start_new_thread(self.OnTick, (self, tick))
        self.OnTick(self, tick)