def request(self, url=None, method='get', param=None, path_fields=None): security_item = param['security_item'] start_timestamp = param['start_timestamp'] end_timestamp = param['end_timestamp'] # 不复权 df = get_price( to_jq_security_id(security_item), start_date=to_time_str(start_timestamp), end_date=end_timestamp, frequency=param['jq_level'], fields=['open', 'close', 'low', 'high', 'volume', 'money'], skip_paused=True, fq=None) df.index.name = 'timestamp' df.reset_index(inplace=True) df['name'] = security_item.name df.rename(columns={'money': 'turnover'}, inplace=True) df['timestamp'] = pd.to_datetime(df['timestamp']) df['provider'] = Provider.JOINQUANT.value df['level'] = param['level'] # remove the unfinished kdata if is_in_trading(security_type='stock', exchange='sh', timestamp=df.iloc[-1, :]['timestamp']): df = df.iloc[:-1, :] return df.to_dict(orient='records')
def on_finish(self, security_item): kdatas = get_kdata( provider=self.provider, security_id=security_item.id, level=self.level.value, order=self.data_schema.timestamp.asc(), return_type='domain', session=self.session, filters=[ self.data_schema.hfq_close.is_(None), self.data_schema.timestamp >= to_pd_timestamp('2005-01-01') ]) if kdatas: start = kdatas[0].timestamp end = kdatas[-1].timestamp # get hfq from joinquant df = get_price(to_jq_security_id(security_item), start_date=to_time_str(start), end_date=now_time_str(), frequency='daily', fields=['factor', 'open', 'close', 'low', 'high'], skip_paused=True, fq='post') if df is not None and not df.empty: # fill hfq data for kdata in kdatas: time_str = to_time_str(kdata.timestamp) if time_str in df.index: kdata.hfq_open = df.loc[time_str, 'open'] kdata.hfq_close = df.loc[time_str, 'close'] kdata.hfq_high = df.loc[time_str, 'high'] kdata.hfq_low = df.loc[time_str, 'low'] kdata.factor = df.loc[time_str, 'factor'] self.session.commit() latest_factor = df.factor[-1] # factor not change yet, no need to reset the qfq past if latest_factor == self.current_factors.get(security_item.id): sql = 'UPDATE {} SET qfq_close=hfq_close/{},qfq_high=hfq_high/{}, qfq_open= hfq_open/{}, qfq_low= hfq_low/{} where ' \ 'security_id=\'{}\' and level=\'{}\' and (qfq_close isnull or qfq_high isnull or qfq_low isnull or qfq_open isnull)'.format( self.data_schema.__table__, latest_factor, latest_factor, latest_factor, latest_factor, security_item.id, self.level.value) else: sql = 'UPDATE {} SET qfq_close=hfq_close/{},qfq_high=hfq_high/{}, qfq_open= hfq_open/{}, qfq_low= hfq_low/{} where ' \ 'security_id=\'{}\' and level=\'{}\''.format(self.data_schema.__table__, latest_factor, latest_factor, latest_factor, latest_factor, security_item.id, self.level.value) self.logger.info(sql) self.session.execute(sql) self.session.commit() # TODO:use netease provider to get turnover_rate self.logger.info('use netease provider to get turnover_rate')
def request(self, url=None, method='get', param=None, path_fields=None): security_item = param['security_item'] start_timestamp = param['start_timestamp'] # 不复权 df = get_price( to_jq_security_id(security_item), start_date=to_time_str(start_timestamp), end_date=now_time_str(), frequency='daily', fields=['open', 'close', 'low', 'high', 'volume', 'money'], skip_paused=True, fq=None) df.index.name = 'timestamp' df.reset_index(inplace=True) df['name'] = security_item.name df.rename(columns={'money': 'turnover'}, inplace=True) df['timestamp'] = pd.to_datetime(df['timestamp']) df['provider'] = Provider.JOINQUANT.value df['level'] = param['level'] return df.to_dict(orient='records')