def __init__(self, security_list=None, security_type=SecurityType.stock, exchanges=['sh', 'sz'], codes=None, the_timestamp=None, start_timestamp=None, end_timestamp=None, columns=[], filters=None, provider='netease', level=TradingLevel.LEVEL_1DAY, indicators=['ma', 'macd'], indicators_param=[{ 'window': 5 }, { 'slow': 26, 'fast': 12, 'n': 9 }], valid_window=26) -> None: self.indicators = indicators self.indicators_param = indicators_param self.data_schema = get_kdata_schema(security_type, level=level) self.valid_window = valid_window super().__init__(self.data_schema, security_list, security_type, exchanges, codes, the_timestamp, start_timestamp, end_timestamp, False, None, columns, filters, provider, level, None)
def __init__(self, exchanges=['binance'], entity_ids=None, codes=None, batch_size=10, force_update=False, sleeping_time=10, default_size=2000, one_shot=False, fix_duplicate_way='add', start_timestamp=None, end_timestamp=None, contain_unfinished_data=False, level=IntervalLevel.LEVEL_1DAY, kdata_use_begin_time=True, close_hour=0, close_minute=0, one_day_trading_minutes=24 * 60) -> None: self.data_schema = get_kdata_schema(entity_type='coin', level=level) self.ccxt_trading_level = to_ccxt_trading_level(level) super().__init__('coin', exchanges, entity_ids, codes, batch_size, force_update, sleeping_time, default_size, one_shot, fix_duplicate_way, start_timestamp, end_timestamp, contain_unfinished_data, level, kdata_use_begin_time, close_hour, close_minute, one_day_trading_minutes)
def __init__(self, exchanges=['binance'], entity_ids=None, codes=None, batch_size=10, force_update=True, sleeping_time=10, default_size=2000, real_time=True, fix_duplicate_way='ignore', start_timestamp=None, end_timestamp=None, kdata_use_begin_time=False, close_hour=None, close_minute=None, one_day_trading_minutes=24 * 60) -> None: self.data_schema = get_kdata_schema(entity_type='coin', level=IntervalLevel.LEVEL_TICK) super().__init__('coin', exchanges, entity_ids, codes, batch_size, force_update, sleeping_time, default_size, real_time, fix_duplicate_way, start_timestamp, end_timestamp, close_hour, close_minute, IntervalLevel.LEVEL_TICK, kdata_use_begin_time, one_day_trading_minutes)
def get_kdata(entity_id=None, level=IntervalLevel.LEVEL_1DAY.value, provider='joinquant', columns=None, return_type='df', start_timestamp=None, end_timestamp=None, filters=None, session=None, order=None, limit=None, index='timestamp'): entity_type, exchange, code = decode_entity_id(entity_id) data_schema: Mixin = get_kdata_schema(entity_type, level=level) return data_schema.query_data(entity_id=entity_id, level=level, provider=provider, columns=columns, return_type=return_type, start_timestamp=start_timestamp, end_timestamp=end_timestamp, filters=filters, session=session, order=order, limit=limit, index=index)
def __init__(self, entity_type='stock', exchanges=['sh', 'sz'], entity_ids=None, codes=None, batch_size=10, force_update=False, sleeping_time=5, default_size=2000, one_shot=False, fix_duplicate_way='add', start_timestamp=None, end_timestamp=None, contain_unfinished_data=False, level=IntervalLevel.LEVEL_1DAY, kdata_use_begin_time=False, close_hour=15, close_minute=0, one_day_trading_minutes=4 * 60) -> None: self.data_schema = get_kdata_schema(entity_type=entity_type, level=level) self.jq_trading_level = to_jq_trading_level(level) super().__init__(entity_type, exchanges, entity_ids, codes, batch_size, force_update, sleeping_time, default_size, one_shot, fix_duplicate_way, start_timestamp, end_timestamp, contain_unfinished_data, level, kdata_use_begin_time, close_hour, close_minute, one_day_trading_minutes) self.current_factors = {} for security_item in self.entities: kdata = get_kdata(entity_id=security_item.id, provider=self.provider, level=self.level.value, order=self.data_schema.timestamp.desc(), limit=1, return_type='domain', session=self.session) if kdata: self.current_factors[security_item.id] = kdata[0].factor self.logger.info('{} latest factor:{}'.format(security_item.id, kdata[0].factor)) auth(JQ_ACCOUNT, JQ_PASSWD)
def __init__(self, entity_ids: List[str] = None, entity_type: str = 'stock', exchanges: List[str] = ['sh', 'sz'], codes: List[str] = None, the_timestamp: Union[str, pd.Timestamp] = None, start_timestamp: Union[str, pd.Timestamp] = None, end_timestamp: Union[str, pd.Timestamp] = None, columns: List = None, filters: List = None, order: object = None, limit: int = None, provider: str = 'joinquant', level: IntervalLevel = IntervalLevel.LEVEL_1DAY, category_field: str = 'entity_id', time_field: str = 'timestamp', trip_timestamp: bool = True, auto_load: bool = True, # child added arguments fq='qfq', indicators=['ma', 'macd'], indicators_param=[{'window': 5}, {'slow': 26, 'fast': 12, 'n': 9}], valid_window=26 ) -> None: self.fq = fq self.indicators = indicators self.indicators_param = indicators_param self.data_schema = get_kdata_schema(entity_type, level=level) self.valid_window = valid_window self.indicator_cols = set() super().__init__(self.data_schema, entity_ids, entity_type, exchanges, codes, the_timestamp, start_timestamp, end_timestamp, columns, filters, order, limit, provider, level, category_field, time_field, trip_timestamp, auto_load, keep_all_timestamp=False, fill_method=None, effective_number=None)
def __init__(self, entity_ids=None, codes=None, batch_size=10, force_update=True, sleeping_time=10, default_size=2000, real_time=False, fix_duplicate_way='ignore', start_timestamp=None, end_timestamp=None, level=IntervalLevel.LEVEL_1WEEK, kdata_use_begin_time=False, close_hour=15, close_minute=0, one_day_trading_minutes=4 * 60) -> None: # 周线以上级别 assert level >= IntervalLevel.LEVEL_1WEEK self.data_schema = get_kdata_schema(entity_type='stock', level=level) self.jq_trading_level = to_jq_trading_level(level) super().__init__('stock', ['sh', 'sz'], entity_ids, codes, batch_size, force_update, sleeping_time, default_size, real_time, fix_duplicate_way, start_timestamp, end_timestamp, level, kdata_use_begin_time, close_hour, close_minute, one_day_trading_minutes) self.factor = 0 self.last_timestamp = None auth(JQ_ACCOUNT, JQ_PASSWD)
def __init__(self, security_type=SecurityType.coin, exchanges=['binance'], codes=None, batch_size=10, force_update=False, sleeping_time=5, fetching_style=TimeSeriesFetchingStyle.end_size, default_size=2000, contain_unfinished_data=False, level=TradingLevel.LEVEL_1DAY, one_shot=False, start_timestamp=None) -> None: self.data_schema = get_kdata_schema(security_type=security_type, level=level) self.ccxt_trading_level = to_ccxt_trading_level(level) self.start_timestamp = to_pd_timestamp(start_timestamp) super().__init__(security_type, exchanges, codes, batch_size, force_update, sleeping_time, fetching_style, default_size, contain_unfinished_data, level, one_shot, kdata_use_begin_time=True)
def __init__(self, entity_type='index', exchanges=None, entity_ids=None, codes=None, batch_size=10, force_update=False, sleeping_time=10, default_size=10000, real_time=True, fix_duplicate_way='add', start_timestamp=None, end_timestamp=None, level=IntervalLevel.LEVEL_1WEEK, kdata_use_begin_time=False, close_hour=0, close_minute=0, one_day_trading_minutes=24 * 60) -> None: self.data_schema = get_kdata_schema(entity_type=entity_type, level=level) super().__init__(entity_type, exchanges, entity_ids, codes, batch_size, force_update, sleeping_time, default_size, real_time, fix_duplicate_way, start_timestamp, end_timestamp, close_hour, close_minute, level, kdata_use_begin_time, one_day_trading_minutes)
def __init__(self) -> None: if self.start_timestamp: self.start_timestamp = to_pd_timestamp(self.start_timestamp) self.start_timestamp = self.trading_level.floor_timestamp(self.start_timestamp) self.current_timestamp = self.start_timestamp else: self.start_timestamp = now_pd_timestamp() if self.end_timestamp: self.end_timestamp = to_pd_timestamp(self.end_timestamp) self.security_type, self.exchange, self.code = decode_security_id(self.security_id) self.kdata_schema = get_kdata_schema(self.security_type) # init history data for model in self.models: datas = \ get_kdata(self.security_id, level=model.trading_level, end_timestamp=self.start_timestamp, order=self.kdata_schema.timestamp.desc(), limit=model.history_size) if datas: model.init_history_data(datas) if not datas: self.logger.warning( "to {}, {} no history data ".format(self.start_timestamp, self.security_id)) elif len(datas) < self.history_data_size: self.logger.warning( "to {}, {} history data size:{}".format(self.start_timestamp, self.security_id, len(datas)))
def get_kdata(security_id, level=TradingLevel.LEVEL_1DAY.value, provider='eastmoney', columns=None, return_type='df', start_timestamp=None, end_timestamp=None, filters=None, session=None, order=None, limit=None): security_type, exchange, code = decode_security_id(security_id) data_schema = get_kdata_schema(security_type, level=level) return get_data(data_schema=data_schema, security_id=security_id, level=level, provider=provider, columns=columns, return_type=return_type, start_timestamp=start_timestamp, end_timestamp=end_timestamp, filters=filters, session=session, order=order, limit=limit)
def __init__(self, security_list: List[str] = None, security_type: Union[str, SecurityType] = SecurityType.stock, exchanges: List[str] = ['sh', 'sz'], codes: List[str] = None, the_timestamp: Union[str, pd.Timestamp] = None, start_timestamp: Union[str, pd.Timestamp] = None, end_timestamp: Union[str, pd.Timestamp] = None, columns: List = None, filters: List = None, provider: Union[str, Provider] = 'joinquant', level: TradingLevel = TradingLevel.LEVEL_1DAY, real_time: bool = False, refresh_interval: int = 10, category_field: str = 'security_id', # child added arguments indicators=['ma', 'macd'], indicators_param=[{'window': 5}, {'slow': 26, 'fast': 12, 'n': 9}], valid_window=26 ) -> None: self.indicators = indicators self.indicators_param = indicators_param self.data_schema = get_kdata_schema(security_type, level=level) self.valid_window = valid_window self.indicator_cols = set() super().__init__(self.data_schema, security_list, security_type, exchanges, codes, the_timestamp, start_timestamp, end_timestamp, columns, filters, provider, level, real_time, refresh_interval, category_field, keep_all_timestamp=False, fill_method=None, effective_number=None)
def __init__(self, security_type=SecurityType.stock, exchanges=['sh', 'sz'], codes=None, the_timestamp=None, window=None, window_func='mean', start_timestamp=None, end_timestamp=None, columns=[StockDayKdata.close], filters=None, provider='eastmoney') -> None: self.data_schema = get_kdata_schema(security_type) super().__init__(security_type, exchanges, codes, the_timestamp, window, window_func, start_timestamp, end_timestamp, columns, filters, provider)
def on_trading_close(self, timestamp): self.logger.info('on_trading_close:{}'.format(timestamp)) self.latest_account['value'] = 0 self.latest_account['all_value'] = 0 for position in self.latest_account['positions']: # use qfq for stock entity_type, _, _ = decode_entity_id(position['entity_id']) data_schema = get_kdata_schema(entity_type, level=self.level) kdata = get_kdata(provider=self.provider, level=self.level, entity_id=position['entity_id'], order=data_schema.timestamp.desc(), end_timestamp=timestamp, limit=1) # use qfq for stock if entity_type == 'stock': closing_price = kdata['qfq_close'][0] else: closing_price = kdata['close'][0] position['available_long'] = position['long_amount'] position['available_short'] = position['short_amount'] if closing_price: if (position['long_amount'] is not None) and position['long_amount'] > 0: position['value'] = position['long_amount'] * closing_price self.latest_account['value'] += position['value'] elif (position['short_amount'] is not None) and position['short_amount'] > 0: position['value'] = 2 * (position['short_amount'] * position['average_short_price']) position[ 'value'] -= position['short_amount'] * closing_price self.latest_account['value'] += position['value'] else: self.logger.warning( 'could not refresh close value for position:{},timestamp:{}' .format(position['entity_id'], timestamp)) # remove the empty position self.latest_account['positions'] = [ position for position in self.latest_account['positions'] if position['long_amount'] > 0 or position['short_amount'] > 0 ] self.latest_account['all_value'] = self.latest_account[ 'value'] + self.latest_account['cash'] self.latest_account['closing'] = True self.latest_account['timestamp'] = to_pd_timestamp(timestamp) self.logger.info('on_trading_close:{},latest_account:{}'.format( timestamp, self.latest_account)) self.persist_account(timestamp)
def get_close_column(security_id): security_type, _, _ = decode_security_id(security_id) data_schema = get_kdata_schema(security_type) if security_type == SecurityType.stock: columns = [ data_schema.security_id, data_schema.timestamp, data_schema.qfq_close ] else: columns = [ data_schema.security_id, data_schema.timestamp, data_schema.close ] return columns
def __init__(self, entity_ids=None, codes=None, batch_size=10, force_update=True, sleeping_time=10, default_size=2000, real_time=False, fix_duplicate_way='ignore', start_timestamp=None, end_timestamp=None, level=IntervalLevel.LEVEL_1DAY, kdata_use_begin_time=False, close_hour=15, close_minute=0, one_day_trading_minutes=4 * 60) -> None: # 周线以上级别请使用jq_stock_bar_recorder assert level <= IntervalLevel.LEVEL_1DAY self.data_schema = get_kdata_schema(entity_type='stock', level=level) self.jq_trading_level = to_jq_trading_level(level) super().__init__('stock', ['sh', 'sz'], entity_ids, codes, batch_size, force_update, sleeping_time, default_size, real_time, fix_duplicate_way, start_timestamp, end_timestamp, close_hour, close_minute, level, kdata_use_begin_time, one_day_trading_minutes) # 读取已经保存的最新factor,更新时有变化才需要重新计算前复权价格 self.current_factors = {} for security_item in self.entities: kdata = get_kdata(entity_id=security_item.id, provider=self.provider, level=self.level.value, order=self.data_schema.timestamp.desc(), limit=1, return_type='domain', session=self.session) if kdata: self.current_factors[security_item.id] = kdata[0].factor self.logger.info('{} latest factor:{}'.format( security_item.id, kdata[0].factor)) auth(JQ_ACCOUNT, JQ_PASSWD)
def __init__(self, entity_schema: EntityMixin = Stock, provider: str = None, entity_provider: str = None, entity_ids: List[str] = None, exchanges: List[str] = None, codes: List[str] = None, the_timestamp: Union[str, pd.Timestamp] = None, start_timestamp: Union[str, pd.Timestamp] = None, end_timestamp: Union[str, pd.Timestamp] = None, columns: List = [ 'id', 'entity_id', 'timestamp', 'level', 'open', 'close', 'high', 'low' ], filters: List = None, order: object = None, limit: int = None, level: Union[str, IntervalLevel] = IntervalLevel.LEVEL_1DAY, category_field: str = 'entity_id', time_field: str = 'timestamp', computing_window: int = None, keep_all_timestamp: bool = False, fill_method: str = 'ffill', effective_number: int = None, transformer: Transformer = MacdTransformer(), accumulator: Accumulator = None, persist_factor: bool = False, dry_run: bool = False) -> None: self.data_schema = get_kdata_schema(entity_schema.__name__, level=level) if transformer: self.indicator_cols = transformer.indicator_cols super().__init__(self.data_schema, entity_schema, provider, entity_provider, entity_ids, exchanges, codes, the_timestamp, start_timestamp, end_timestamp, columns, filters, order, limit, level, category_field, time_field, computing_window, keep_all_timestamp, fill_method, effective_number, transformer, accumulator, persist_factor, dry_run)
def __init__(self, security_type=SecurityType.stock, exchanges=['sh', 'sz'], codes=None, batch_size=10, force_update=False, sleeping_time=5, fetching_style=TimeSeriesFetchingStyle.end_size, default_size=2000, contain_unfinished_data=False, level=TradingLevel.LEVEL_1DAY, one_shot=True, start_timestamp=None) -> None: self.data_schema = get_kdata_schema(security_type=security_type, level=level) self.jq_trading_level = to_jq_trading_level(level) self.start_timestamp = to_pd_timestamp(start_timestamp) super().__init__(security_type, exchanges, codes, batch_size, force_update, sleeping_time, fetching_style, default_size, contain_unfinished_data, level, one_shot) self.current_factors = {} for security_item in self.securities: kdata = get_kdata(security_id=security_item.id, provider=self.provider, level=self.level.value, order=self.data_schema.timestamp.desc(), limit=1, return_type='domain', session=self.session) if kdata: self.current_factors[security_item.id] = kdata[0].factor self.logger.info('{} latest factor:{}'.format( security_item.id, kdata[0].factor)) auth(JQ_ACCOUNT, JQ_PASSWD)
def __init__(self, security_type=SecurityType.stock, exchanges=['sh', 'sz'], codes=None, the_timestamp=None, window=None, window_func='mean', start_timestamp=None, end_timestamp=None, keep_all_timestamp=False, fill_method='ffill', columns=[StockDayKdata.qfq_close], filters=None, provider='netease', short_window=5, long_window=10) -> None: self.data_schema = get_kdata_schema(security_type) self.short_window = short_window self.long_window = long_window super().__init__(security_type, exchanges, codes, the_timestamp, window, window_func, start_timestamp, end_timestamp, keep_all_timestamp, fill_method, columns, filters, provider)
def __init__( self, security_type=SecurityType.stock, exchanges=['sh', 'sz'], codes=None, the_timestamp=None, start_timestamp=None, end_timestamp=None, keep_all_timestamp=False, fill_method='ffill', filters=None, provider='netease', level=TradingLevel.LEVEL_1DAY, indicators=['ma', 'macd'], indicators_param=[{ 'window': 5 }, { 'slow': 26, 'fast': 12, 'n': 9 }] ) -> None: """ this base class is for init the kdata,you could calculate technical factor from it :param security_type: :type security_type: :param exchanges: :type exchanges: :param codes: :type codes: :param the_timestamp: :type the_timestamp: :param start_timestamp: :type start_timestamp: :param end_timestamp: :type end_timestamp: :param keep_all_timestamp: :type keep_all_timestamp: :param fill_method: :type fill_method: :param filters: :type filters: :param provider: :type provider: :param level: :type level: :param indicators: the technical factors need to calculate :type indicators: """ self.indicators = indicators self.indicators_param = indicators_param self.data_schema = get_kdata_schema(security_type, level=level) super().__init__(security_type, exchanges, codes, the_timestamp, None, None, start_timestamp, end_timestamp, keep_all_timestamp, fill_method, None, filters, provider, level=level)