def run(self): # iterate timestamp of the min level,e.g,9:30,9:35,9.40...for 5min level # timestamp represents the timestamp in kdata for timestamp in iterate_timestamps(security_type=self.security_type, exchange=self.exchanges[0], start_timestamp=self.start_timestamp, end_timestamp=self.end_timestamp, level=self.level): if not is_trading_date(security_type=self.security_type, exchange=self.exchanges[0], timestamp=timestamp): continue if self.real_time: # all selector move on to handle the coming data if self.kdata_use_begin_time: real_end_timestamp = timestamp + pd.Timedelta(seconds=self.level.to_second()) else: real_end_timestamp = timestamp waiting_seconds, _ = self.level.count_from_timestamp(real_end_timestamp, one_day_trading_minutes=get_one_day_trading_minutes( security_type=self.security_type)) # meaning the future kdata not ready yet,we could move on to check if waiting_seconds and (waiting_seconds > 0): # iterate the selector from min to max which in finished timestamp kdata for level in self.trading_level_asc: if (is_in_finished_timestamps(security_type=self.security_type, exchange=self.exchanges[0], timestamp=timestamp, level=level)): for selector in self.selectors: if selector.level == level: selector.move_on(timestamp, self.kdata_use_begin_time) # on_trading_open to setup the account if self.level == TradingLevel.LEVEL_1DAY or ( self.level != TradingLevel.LEVEL_1DAY and is_open_time(security_type=self.security_type, exchange=self.exchanges[0], timestamp=timestamp)): self.account_service.on_trading_open(timestamp) # the time always move on by min level step and we could check all level targets in the slot self.handle_targets_slot(timestamp=timestamp) for level in self.trading_level_asc: # in every cycle, all level selector do its job in its time if (is_in_finished_timestamps(security_type=self.security_type, exchange=self.exchanges[0], timestamp=timestamp, level=level)): df = self.selectors_comparator.make_decision(timestamp=timestamp, trading_level=level) if not df.empty: selected = set(df['security_id'].to_list()) else: selected = {} self.targets_slot.input_targets(level, selected) # on_trading_close to calculate date account if self.level == TradingLevel.LEVEL_1DAY or ( self.level != TradingLevel.LEVEL_1DAY and is_close_time(security_type=self.security_type, exchange=self.exchanges[0], timestamp=timestamp)): self.account_service.on_trading_close(timestamp) self.on_finish()
def run(self): # iterate timestamp of the min level for timestamp in iterate_timestamps( security_type=self.security_type, exchange=self.exchanges[0], start_timestamp=self.start_timestamp, end_timestamp=self.end_timestamp, level=self.trading_level): # on_trading_open to setup the account if self.trading_level == TradingLevel.LEVEL_1DAY or ( self.trading_level != TradingLevel.LEVEL_1DAY and is_open_time(security_type=self.security_type, exchange=self.exchanges[0], timestamp=timestamp)): self.account_service.on_trading_open(timestamp) # the time always move on by min level step and we could check all level targets in the slot self.handle_targets_slot(timestamp=timestamp) for level in self.trading_level_asc: # in every cycle, all level selector do its job in its time if (is_in_finished_timestamps(security_type=self.security_type, exchange=self.exchanges[0], timestamp=timestamp, level=level)): df = self.selectors_comparator.make_decision( timestamp=timestamp, trading_level=level) if not df.empty: selected = set(df['security_id'].to_list()) else: selected = {} self.targets_slot.input_targets(level, selected) # on_trading_close to calculate date account if self.trading_level == TradingLevel.LEVEL_1DAY or ( self.trading_level != TradingLevel.LEVEL_1DAY and is_close_time(security_type=self.security_type, exchange=self.exchanges[0], timestamp=timestamp)): self.account_service.on_trading_close(timestamp) self.on_finish()
def test_is_in_finished_timestamps(): assert is_in_finished_timestamps(entity_type='stock', exchange='sh', timestamp='1999-01-01', level=IntervalLevel.LEVEL_1DAY) assert is_in_finished_timestamps(entity_type='stock', exchange='sh', timestamp='1999-01-01 15:00', level=IntervalLevel.LEVEL_1DAY) assert not is_in_finished_timestamps(entity_type='stock', exchange='sh', timestamp='1999-01-01 14:00', level=IntervalLevel.LEVEL_1DAY) assert is_in_finished_timestamps(entity_type='stock', exchange='sh', timestamp='1999-01-01 09:31', level=IntervalLevel.LEVEL_1MIN) assert not is_in_finished_timestamps(entity_type='stock', exchange='sh', timestamp='1999-01-01 09:30', level=IntervalLevel.LEVEL_1MIN) assert not is_in_finished_timestamps(entity_type='stock', exchange='sh', timestamp='1999-01-01 08:30', level=IntervalLevel.LEVEL_1MIN) assert is_in_finished_timestamps(entity_type='stock', exchange='sh', timestamp='1999-01-01 11:30', level=IntervalLevel.LEVEL_1MIN) assert is_in_finished_timestamps(entity_type='stock', exchange='sh', timestamp='1999-01-01 11:30', level=IntervalLevel.LEVEL_5MIN) assert is_in_finished_timestamps(entity_type='stock', exchange='sh', timestamp='1999-01-01 11:30', level=IntervalLevel.LEVEL_15MIN) assert is_in_finished_timestamps(entity_type='stock', exchange='sh', timestamp='1999-01-01 11:30', level=IntervalLevel.LEVEL_30MIN) assert is_in_finished_timestamps(entity_type='stock', exchange='sh', timestamp='1999-01-01 11:30', level=IntervalLevel.LEVEL_1HOUR)
def test_is_in_finished_timestamps(): assert is_in_finished_timestamps(security_type=SecurityType.stock, exchange='sh', timestamp='1999-01-01', level=TradingLevel.LEVEL_1DAY) assert is_in_finished_timestamps(security_type=SecurityType.stock, exchange='sh', timestamp='1999-01-01 15:00', level=TradingLevel.LEVEL_1DAY) assert not is_in_finished_timestamps(security_type=SecurityType.stock, exchange='sh', timestamp='1999-01-01 14:00', level=TradingLevel.LEVEL_1DAY) assert is_in_finished_timestamps(security_type=SecurityType.stock, exchange='sh', timestamp='1999-01-01 09:31', level=TradingLevel.LEVEL_1MIN) assert not is_in_finished_timestamps(security_type=SecurityType.stock, exchange='sh', timestamp='1999-01-01 09:30', level=TradingLevel.LEVEL_1MIN) assert not is_in_finished_timestamps(security_type=SecurityType.stock, exchange='sh', timestamp='1999-01-01 08:30', level=TradingLevel.LEVEL_1MIN) assert is_in_finished_timestamps(security_type=SecurityType.stock, exchange='sh', timestamp='1999-01-01 11:30', level=TradingLevel.LEVEL_1MIN) assert is_in_finished_timestamps(security_type=SecurityType.stock, exchange='sh', timestamp='1999-01-01 11:30', level=TradingLevel.LEVEL_5MIN) assert is_in_finished_timestamps(security_type=SecurityType.stock, exchange='sh', timestamp='1999-01-01 11:30', level=TradingLevel.LEVEL_15MIN) assert is_in_finished_timestamps(security_type=SecurityType.stock, exchange='sh', timestamp='1999-01-01 11:30', level=TradingLevel.LEVEL_30MIN) assert is_in_finished_timestamps(security_type=SecurityType.stock, exchange='sh', timestamp='1999-01-01 11:30', level=TradingLevel.LEVEL_1HOUR)
def run(self): # iterate timestamp of the min level,e.g,9:30,9:35,9.40...for 5min level # timestamp represents the timestamp in kdata handled_timestamp = None for timestamp in iterate_timestamps(security_type=self.security_type, exchange=self.exchanges[0], start_timestamp=self.start_timestamp, end_timestamp=self.end_timestamp, level=self.trading_level): if self.real_time and handled_timestamp: # all selector move on to handle the coming data if self.kdata_use_begin_time: touching_timestamp = handled_timestamp + pd.Timedelta(seconds=self.trading_level.to_second()) else: touching_timestamp = handled_timestamp waiting_seconds, _ = self.trading_level.count_from_timestamp(touching_timestamp, one_day_trading_minutes=get_one_day_trading_minutes( security_type=self.security_type)) if waiting_seconds and (waiting_seconds > 10): t = waiting_seconds / 2 self.logger.info( 'level:{},handled_timestamp:{},touching_timestamp:{},current_time:{},next_ok_time:{},just sleep:{} seconds'.format( self.trading_level.value, handled_timestamp, touching_timestamp, now_pd_timestamp(), touching_timestamp + pd.Timedelta( seconds=self.trading_level.to_second()), t)) time.sleep(t) for selector in self.selectors: if (is_in_finished_timestamps(security_type=self.security_type, exchange=self.exchanges[0], timestamp=timestamp, level=selector.level)): if self.kdata_use_begin_time: to_touching_timestamp = timestamp + pd.Timedelta( seconds=selector.level.to_second()) else: to_touching_timestamp = timestamp selector.move_on(timestamp, to_touching_timestamp) # on_trading_open to setup the account if self.trading_level == TradingLevel.LEVEL_1DAY or ( self.trading_level != TradingLevel.LEVEL_1DAY and is_open_time(security_type=self.security_type, exchange=self.exchanges[0], timestamp=timestamp)): self.account_service.on_trading_open(timestamp) # the time always move on by min level step and we could check all level targets in the slot self.handle_targets_slot(timestamp=timestamp) for level in self.trading_level_asc: # in every cycle, all level selector do its job in its time if (is_in_finished_timestamps(security_type=self.security_type, exchange=self.exchanges[0], timestamp=timestamp, level=level)): df = self.selectors_comparator.make_decision(timestamp=timestamp, trading_level=level) if not df.empty: selected = set(df['security_id'].to_list()) else: selected = {} self.targets_slot.input_targets(level, selected) handled_timestamp = timestamp # on_trading_close to calculate date account if self.trading_level == TradingLevel.LEVEL_1DAY or ( self.trading_level != TradingLevel.LEVEL_1DAY and is_close_time(security_type=self.security_type, exchange=self.exchanges[0], timestamp=timestamp)): self.account_service.on_trading_close(timestamp) self.on_finish()