def get_security_schema(security_type: Union[SecurityType, str]): if SecurityType(security_type) == SecurityType.stock: return Stock if SecurityType(security_type) == SecurityType.index: return Index if SecurityType(security_type) == SecurityType.coin: return Coin
def get_kdata_schema(security_type, level=TradingLevel.LEVEL_1DAY): if SecurityType( security_type ) == SecurityType.stock and level == TradingLevel.LEVEL_1DAY: return StockDayKdata if SecurityType( security_type ) == SecurityType.index and level == TradingLevel.LEVEL_1DAY: return IndexDayKdata
def get_kdata_schema(security_type, level=TradingLevel.LEVEL_1DAY): if type(level) == str: level = TradingLevel(level) if SecurityType(security_type) == SecurityType.stock: if level == TradingLevel.LEVEL_1DAY: return StockDayKdata if level == TradingLevel.LEVEL_1HOUR: return Stock1HKdata if SecurityType( security_type ) == SecurityType.index and level == TradingLevel.LEVEL_1DAY: return IndexDayKdata
def get_trading_intervals(security_type, exchange): if type(security_type) == str: security_type = SecurityType(security_type) if security_type == SecurityType.stock and exchange in ('sh', 'sz'): return ('09:30', '11:30'), ('13:00', '15:00') if security_type == SecurityType.coin: return ('00:00', '23:59')
def is_in_finished_timestamps(security_type, exchange, timestamp, level: TradingLevel): if type(security_type) == str: security_type = SecurityType(security_type) if security_type == SecurityType.stock and exchange in ('sh', 'sz'): if to_time_str(timestamp, fmt=TIME_FORMAT_MINUTE1) in china_stock_level_map_finished_timestamps.get( level.value): return True return False
def is_trading_date(security_type, exchange, timestamp: pd.Timestamp): if type(security_type) == str: security_type = SecurityType(security_type) if type(timestamp) == str: timestamp = to_pd_timestamp(timestamp) # just ignore 00:00 # the_date = date_and_time(timestamp, '09:00') if security_type == SecurityType.stock: return (timestamp.weekday() != 5) and (timestamp.weekday() != 6) return True
def get_kdata_schema(security_type: Union[SecurityType, str], level: Union[TradingLevel, str] = TradingLevel.LEVEL_1DAY): if type(level) == str: level = TradingLevel(level) if type(security_type) == str: security_type = SecurityType(security_type) # kdata schema rule # 1)name:{SecurityType.value.capitalize()}{TradingLevel.value.upper()}Kdata schema_str = '{}{}Kdata'.format(security_type.value.capitalize(), level.value.upper()) return eval(schema_str)
def get_current_price(security_list=None, security_type=SecurityType.coin): result = {} if security_type == SecurityType.coin: if security_list: for security_id in security_list: a, exchange, code = decode_security_id(security_id) assert SecurityType(a) == security_type ccxt_exchange = CCXTAccount.get_ccxt_exchange( exchange_str=exchange) if not ccxt_exchange: raise Exception('{} not support'.format(exchange)) orderbook = ccxt_exchange.fetch_order_book(code) bid = orderbook['bids'][0][0] if len( orderbook['bids']) > 0 else None ask = orderbook['asks'][0][0] if len( orderbook['asks']) > 0 else None security_id = f'coin_{exchange}_{code}' result[security_id] = (bid, ask) return result
def is_in_finished_timestamps(security_type, exchange, timestamp, level: TradingLevel): """ :param security_type: :type security_type: zvt.domain.common.SecurityType :param exchange: :type exchange: str :param timestamp: the timestamp could be recorded in kdata of the level :type timestamp: pd.Timestamp :param level: :type level: zvt.domain.common.TradingLevel :return: :rtype: bool """ if type(security_type) == str: security_type = SecurityType(security_type) if security_type == SecurityType.stock and exchange in ('sh', 'sz'): return china_stock_finished_timestamp(timestamp, level=level) if security_type == SecurityType.coin: return coin_finished_timestamp(timestamp, level=level) return False
def decode_security_id(security_id: str): result = security_id.split('_') security_type = result[0] exchange = result[1] code = result[2] return SecurityType(security_type), exchange, code
def __init__(self, security_list: List[str] = None, security_type: Union[str, SecurityType] = SecurityType.stock, exchanges: List[str] = ['sh', 'sz'], codes: List[str] = None, start_timestamp: Union[str, pd.Timestamp] = None, end_timestamp: Union[str, pd.Timestamp] = None, provider: Union[str, Provider] = Provider.JOINQUANT, level: Union[str, TradingLevel] = TradingLevel.LEVEL_1DAY, trader_name: str = None, real_time: bool = False, kdata_use_begin_time: bool = False) -> None: if trader_name: self.trader_name = trader_name else: self.trader_name = type(self).__name__.lower() self.trading_signal_listeners = [] self.state_listeners = [] self.selectors: List[TargetSelector] = None self.security_list = security_list self.security_type = SecurityType(security_type) self.exchanges = exchanges self.codes = codes # FIXME:handle this case gracefully if self.security_list: security_type, exchange, code = decode_security_id(self.security_list[0]) if not self.security_type: self.security_type = security_type if not self.exchanges: self.exchanges = [exchange] self.provider = provider # make sure the min level selector correspond to the provider and level self.level = TradingLevel(level) self.real_time = real_time if start_timestamp and end_timestamp: self.start_timestamp = to_pd_timestamp(start_timestamp) self.end_timestamp = to_pd_timestamp(end_timestamp) else: assert False if real_time: logger.info( 'real_time mode, end_timestamp should be future,you could set it big enough for running forever') assert self.end_timestamp >= now_pd_timestamp() self.kdata_use_begin_time = kdata_use_begin_time self.account_service = SimAccountService(trader_name=self.trader_name, timestamp=self.start_timestamp, provider=self.provider, level=self.level) self.add_trading_signal_listener(self.account_service) self.init_selectors(security_list=security_list, security_type=self.security_type, exchanges=self.exchanges, codes=self.codes, start_timestamp=self.start_timestamp, end_timestamp=self.end_timestamp) self.selectors_comparator = self.init_selectors_comparator() self.trading_level_asc = list(set([TradingLevel(selector.level) for selector in self.selectors])) self.trading_level_asc.sort() self.trading_level_desc = list(self.trading_level_asc) self.trading_level_desc.reverse() self.targets_slot: TargetsSlot = TargetsSlot() self.session = get_db_session('zvt', StoreCategory.business) trader = get_trader(session=self.session, trader_name=self.trader_name, return_type='domain', limit=1) if trader: self.logger.warning("trader:{} has run before,old result would be deleted".format(self.trader_name)) self.session.query(business.Trader).filter(business.Trader.trader_name == self.trader_name).delete() self.session.commit() self.on_start()
def __init__(self, security_list: List[str] = None, security_type: Union[str, SecurityType] = SecurityType.stock, exchanges: List[str] = ['sh', 'sz'], codes: List[str] = None, start_timestamp: Union[str, pd.Timestamp] = None, end_timestamp: Union[str, pd.Timestamp] = None, provider: Union[str, Provider] = Provider.JOINQUANT, level: Union[str, TradingLevel] = TradingLevel.LEVEL_1DAY, trader_name: str = None, real_time: bool = False, kdata_use_begin_time: bool = False) -> None: if trader_name: self.trader_name = trader_name else: self.trader_name = type(self).__name__.lower() self.trading_signal_listeners = [] self.state_listeners = [] self.selectors: List[TargetSelector] = None self.security_list = security_list self.security_type = SecurityType(security_type) self.exchanges = exchanges self.codes = codes self.provider = provider # make sure the min level selector correspond to the provider and level self.level = TradingLevel(level) self.real_time = real_time if start_timestamp and end_timestamp: self.start_timestamp = to_pd_timestamp(start_timestamp) self.end_timestamp = to_pd_timestamp(end_timestamp) else: assert False if real_time: logger.info( 'real_time mode, end_timestamp should be future,you could set it big enough for running forever' ) assert self.end_timestamp >= now_pd_timestamp() self.kdata_use_begin_time = kdata_use_begin_time self.account_service = SimAccountService( trader_name=self.trader_name, timestamp=self.start_timestamp, provider=self.provider, level=self.level) self.add_trading_signal_listener(self.account_service) self.init_selectors(security_list=security_list, security_type=self.security_type, exchanges=self.exchanges, codes=self.codes, start_timestamp=self.start_timestamp, end_timestamp=self.end_timestamp) self.selectors_comparator = self.init_selectors_comparator() self.trading_level_asc = list( set([TradingLevel(selector.level) for selector in self.selectors])) self.trading_level_asc.sort() self.trading_level_desc = list(self.trading_level_asc) self.trading_level_desc.reverse() self.targets_slot: TargetsSlot = TargetsSlot()
def get_security_schema(security_type): if SecurityType(security_type) == SecurityType.stock: return Stock if SecurityType(security_type) == SecurityType.index: return Index
def get_kdata_schema(security_type): if SecurityType(security_type) == SecurityType.stock: return StockKdata if SecurityType(security_type) == SecurityType.index: return IndexKdata