def test_is_open_close_time(): timestamps = iterate_timestamps(security_type=SecurityType.coin, exchange='binance', level=TradingLevel.LEVEL_1MIN, start_timestamp='2019-05-01', end_timestamp='2019-05-01') assert is_open_time(security_type=SecurityType.coin, exchange='binance', timestamp=timestamps[0]) assert is_close_time(security_type=SecurityType.coin, exchange='binance', timestamp=timestamps[-1]) timestamps = iterate_timestamps(security_type=SecurityType.coin, exchange='binance', level=TradingLevel.LEVEL_5MIN, start_timestamp='2019-05-01', end_timestamp='2019-05-01') assert is_open_time(security_type=SecurityType.coin, exchange='binance', timestamp=timestamps[0]) assert is_close_time(security_type=SecurityType.coin, exchange='binance', timestamp=timestamps[-1]) timestamps = iterate_timestamps(security_type=SecurityType.coin, exchange='binance', level=TradingLevel.LEVEL_5MIN, start_timestamp='2019-05-01', end_timestamp='2019-05-02') open = [] close = [] for timestamp in timestamps: if is_open_time(security_type=SecurityType.coin, exchange='binance', timestamp=timestamp): open.append(timestamp) if is_close_time(security_type=SecurityType.coin, exchange='binance', timestamp=timestamp): close.append(timestamp) assert len(open) == 3 assert len(close) == 3
def test_iterate_stock_timestamps(): timestamps = iterate_timestamps(entity_type='stock', exchange='sh', level=IntervalLevel.LEVEL_1HOUR, start_timestamp='2019-05-01', end_timestamp='2019-05-02') assert is_same_time(timestamps[0], '2019-05-01 09:30:00') assert is_same_time(timestamps[-1], '2019-05-02 15:00:00') timestamps = iterate_timestamps(entity_type='stock', exchange='sh', level=IntervalLevel.LEVEL_5MIN, start_timestamp='2019-05-01', end_timestamp='2019-05-02') assert is_same_time(timestamps[0], '2019-05-01 09:30:00') assert is_same_time(timestamps[-1], '2019-05-02 15:00:00') timestamps = iterate_timestamps(entity_type='stock', exchange='sh', level=IntervalLevel.LEVEL_1HOUR, start_timestamp='2019-05-01', end_timestamp='2019-05-02', contain_all_timestamp=False) assert is_same_time(timestamps[0], '2019-05-01 10:30:00') assert is_same_time(timestamps[-1], '2019-05-02 15:00:00') timestamps = iterate_timestamps(entity_type='stock', exchange='sh', level=IntervalLevel.LEVEL_5MIN, start_timestamp='2019-05-01', end_timestamp='2019-05-02', contain_all_timestamp=False) assert is_same_time(timestamps[0], '2019-05-01 09:35:00') assert is_same_time(timestamps[-1], '2019-05-02 15:00:00')
def test_iterate_coin_timestamps(): timestamps = iterate_timestamps(security_type=SecurityType.coin, exchange='binance', level=TradingLevel.LEVEL_1HOUR, start_timestamp='2019-05-01', end_timestamp='2019-05-02') assert is_same_time(timestamps[0], '2019-05-01 00:00:00') assert is_same_time(timestamps[-1], '2019-05-03 00:00:00') timestamps = iterate_timestamps(security_type=SecurityType.coin, exchange='binance', level=TradingLevel.LEVEL_5MIN, start_timestamp='2019-05-01', end_timestamp='2019-05-02') assert is_same_time(timestamps[0], '2019-05-01 00:00:00') assert is_same_time(timestamps[-1], '2019-05-03 00:00:00')
def test_china_stock_reader(): data_reader = DataReader(codes=['002572', '000338'], data_schema=Stock1DKdata, provider='joinquant', start_timestamp='2019-01-01', end_timestamp='2019-06-10') categories = data_reader.get_categories() df = data_reader.get_data_df() assert 'stock_sz_002572' in categories assert 'stock_sz_000338' in categories assert ('stock_sz_002572', '2019-01-02') in df.index assert ('stock_sz_000338', '2019-01-02') in df.index assert ('stock_sz_002572', '2019-06-10') in df.index assert ('stock_sz_000338', '2019-06-10') in df.index for timestamp in iterate_timestamps(security_type=SecurityType.stock, exchange='sz', level=TradingLevel.LEVEL_1DAY, start_timestamp='2019-06-11', end_timestamp='2019-06-14'): data_reader.move_on(to_timestamp=timestamp, timeout=0) df = data_reader.get_data_df() assert ('stock_sz_002572', timestamp) in df.index assert ('stock_sz_000338', to_time_str(timestamp)) in df.index
def test_china_stock_reader(): data_reader = DataReader(codes=['002572', '000338'], data_schema=Stock1dKdata, entity_schema=Stock, start_timestamp='2019-01-01', end_timestamp='2019-06-10', entity_provider='eastmoney') categories = data_reader.data_df.index.levels[0].to_list() df = data_reader.data_df assert 'stock_sz_002572' in categories assert 'stock_sz_000338' in categories assert ('stock_sz_002572', '2019-01-02') in df.index assert ('stock_sz_000338', '2019-01-02') in df.index assert ('stock_sz_002572', '2019-06-10') in df.index assert ('stock_sz_000338', '2019-06-10') in df.index for timestamp in iterate_timestamps(entity_type='stock', exchange='sz', level=IntervalLevel.LEVEL_1DAY, start_timestamp='2019-06-11', end_timestamp='2019-06-14'): data_reader.move_on(to_timestamp=timestamp) df = data_reader.data_df assert ('stock_sz_002572', timestamp) in df.index assert ('stock_sz_000338', to_time_str(timestamp)) in df.index
def test_iterate_coin_timestamps(): timestamps = iterate_timestamps(entity_type='coin', exchange='binance', level=IntervalLevel.LEVEL_1HOUR, start_timestamp='2019-05-01', end_timestamp='2019-05-02') assert is_same_time(timestamps[0], '2019-05-01 00:00:00') assert is_same_time(timestamps[-1], '2019-05-03 00:00:00') timestamps = iterate_timestamps(entity_type='coin', exchange='binance', level=IntervalLevel.LEVEL_5MIN, start_timestamp='2019-05-01', end_timestamp='2019-05-02') assert is_same_time(timestamps[0], '2019-05-01 00:00:00') assert is_same_time(timestamps[-1], '2019-05-03 00:00:00')
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 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()