Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_jq_603220_kdata():
df = quote.get_kdata(entity_id='stock_sh_603220', session=day_k_session, level=IntervalLevel.LEVEL_1DAY,
provider='joinquant')
print(df)
df = quote.get_kdata(entity_id='stock_sh_603220', session=day_1h_session, level=IntervalLevel.LEVEL_1HOUR,
provider='joinquant')
print(df)
def test_jq_1d_kdata():
df = get_kdata(entity_id='stock_sz_000338', provider='joinquant', level=IntervalLevel.LEVEL_1DAY)
se = df.loc['2019-04-19']
# make sure our fq is ok
assert round(se['open'], 2) <= 12.93
assert round(se['high'], 2) <= 13.52
assert round(se['low'], 2) <= 12.89
assert round(se['close'], 2) <= 13.33
def test_jq_1wk_kdata():
df = get_kdata(entity_id='stock_sz_000338', provider='joinquant', level=IntervalLevel.LEVEL_1WEEK)
se = df.loc['2019-04-19']
# make sure our fq is ok
assert round(se['open'], 2) <= 13.28
assert round(se['high'], 2) <= 13.90
assert round(se['low'], 2) <= 12.36
assert round(se['close'], 2) <= 13.30
def on_finish_entity(self, entity):
if self.factor != 0:
kdatas = get_kdata(provider=self.provider, entity_id=entity.id, level=self.level.value,
order=self.data_schema.timestamp.asc(),
return_type='domain',
session=self.session,
filters=[self.data_schema.timestamp < self.last_timestamp])
if kdatas:
# fill hfq data
for kdata in kdatas:
kdata.qfq_open = kdata.qfq_open * self.factor
kdata.qfq_close = kdata.qfq_close * self.factor
kdata.qfq_high = kdata.qfq_high * self.factor
kdata.qfq_low = kdata.qfq_low * self.factor
self.session.add_all(kdatas)
self.session.commit()
include_now=False)
if pd_is_not_null(df):
df['name'] = entity.name
df.rename(columns={'money': 'turnover', 'date': 'timestamp'}, inplace=True)
df['entity_id'] = entity.id
df['timestamp'] = pd.to_datetime(df['timestamp'])
df['provider'] = 'joinquant'
df['level'] = self.level.value
df['code'] = entity.code
# 判断是否需要重新计算之前保存的前复权数据
check_df = df.head(1)
check_date = check_df['timestamp'][0]
current_df = get_kdata(entity_id=entity.id, provider=self.provider, start_timestamp=check_date,
end_timestamp=check_date, limit=1, level=self.level)
if pd_is_not_null(current_df):
old = current_df.iloc[0, :]['close']
new = check_df['close'][0]
# 相同时间的close不同,表明前复权需要重新计算
if round(old, 2) != round(new, 2):
self.factor = new / old
self.last_timestamp = pd.Timestamp(check_date)
def generate_kdata_id(se):
if self.level >= IntervalLevel.LEVEL_1DAY:
return "{}_{}".format(se['entity_id'], to_time_str(se['timestamp'], fmt=TIME_FORMAT_DAY))
else:
return "{}_{}".format(se['entity_id'], to_time_str(se['timestamp'], fmt=TIME_FORMAT_ISO8601))
df['id'] = df[['entity_id', 'timestamp']].apply(generate_kdata_id, axis=1)
def __init__(self, entity_type='stock', exchanges=['sh', 'sz'], entity_ids=None, codes=None, batch_size=10,
force_update=False, sleeping_time=10, default_size=2000, real_time=True, fix_duplicate_way='add',
start_timestamp=None, end_timestamp=None,
level=IntervalLevel.LEVEL_1DAY, kdata_use_begin_time=False, close_hour=0, close_minute=0,
one_day_trading_minutes=24 * 60) -> None:
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)
self.current_factors = {}
self.latest_factors = {}
for security_item in self.entities:
kdata = get_kdata(entity_id=security_item.id, provider=self.provider,
level=self.level.value, order=Stock1dKdata.timestamp.desc(),
return_type='domain',
session=self.session)
if kdata:
self.current_factors[security_item.id] = kdata[0].factor
def on_trading_signal(self, trading_signal: TradingSignal):
self.logger.debug('trader:{} received trading signal:{}'.format(self.trader_name, trading_signal))
entity_id = trading_signal.entity_id
current_timestamp = trading_signal.the_timestamp
order_type = AccountService.trading_signal_to_order_type(trading_signal.trading_signal_type)
trading_level = trading_signal.trading_level.value
if order_type:
try:
kdata = get_kdata(provider=self.provider, entity_id=entity_id, level=trading_level,
start_timestamp=current_timestamp, end_timestamp=current_timestamp,
limit=1)
if kdata is not None and not kdata.empty:
entity_type, _, _ = decode_entity_id(kdata['entity_id'][0])
the_price = kdata['close'][0]
if the_price:
self.order(entity_id=entity_id, current_price=the_price,
current_timestamp=current_timestamp, order_pct=trading_signal.position_pct,
order_money=trading_signal.order_money,
order_type=order_type)
else:
self.logger.warning(
'ignore trading signal,wrong kdata,entity_id:{},timestamp:{},kdata:{}'.format(entity_id,
current_timestamp,
def __init__(self, entity_type='stock', exchanges=['sh', 'sz'], entity_ids=None, codes=None, batch_size=10,
force_update=False, sleeping_time=10, default_size=2000, real_time=True, fix_duplicate_way='add',
start_timestamp=None, end_timestamp=None, level=IntervalLevel.LEVEL_1DAY, kdata_use_begin_time=False,
close_hour=0, close_minute=0,
one_day_trading_minutes=24 * 60) -> None:
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)
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=Stock1dKdata.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 on_finish_entity(self, entity):
kdatas = get_kdata(entity_id=entity.id, level=IntervalLevel.LEVEL_1DAY.value,
order=Index1dKdata.timestamp.asc(),
return_type='domain', session=self.session,
filters=[Index1dKdata.cumulative_net_value.is_(None)])
if kdatas and len(kdatas) > 0:
start = kdatas[0].timestamp
end = kdatas[-1].timestamp
# 从东方财富获取基金累计净值
df = self.fetch_cumulative_net_value(entity, start, end)
if df is not None and not df.empty:
for kdata in kdatas:
if kdata.timestamp in df.index:
kdata.cumulative_net_value = df.loc[kdata.timestamp, 'LJJZ']
kdata.change_pct = df.loc[kdata.timestamp, 'JZZZL']