Skip to content

Commit

Permalink
【Feature】补充 JQData 从网易获取换手率及涨跌幅
Browse files Browse the repository at this point in the history
Former-commit-id: 3f010fb
  • Loading branch information
buginux committed Jul 4, 2019
1 parent 07553f1 commit 7a5bf18
Showing 1 changed file with 25 additions and 3 deletions.
28 changes: 25 additions & 3 deletions zvt/recorders/joinquant/quotes/jq_china_stock__kdata_recorder.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import logging
from datetime import timedelta

import io
import pandas as pd
import requests
from jqdatasdk import auth, get_price, logout

from zvt.api.common import generate_kdata_id, to_jq_security_id, get_kdata_schema, to_jq_trading_level
Expand All @@ -13,7 +15,7 @@
from zvt.recorders.recorder import TimeSeriesFetchingStyle, FixedCycleDataRecorder, ApiWrapper
from zvt.settings import JQ_ACCOUNT, JQ_PASSWD, SAMPLE_STOCK_CODES
from zvt.utils.time_utils import to_time_str, now_time_str, to_pd_timestamp, now_pd_timestamp
from zvt.utils.utils import init_process_log
from zvt.utils.utils import init_process_log, read_csv

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -142,8 +144,28 @@ def on_finish(self, security_item):
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')
# use netease provider to get turnover_rate
query_url = 'http://quotes.money.163.com/service/chddata.html?code={}{}&start={}&end={}&fields=PCHG;TURNOVER'

if security_item.exchange == 'sh':
exchange_flag = 0
else:
exchange_flag = 1

url = query_url.format(exchange_flag, security_item.code, to_time_str(start), to_time_str(end))
response = requests.get(url=url)

df = read_csv(io.BytesIO(response.content), encoding='GB2312', na_values='None')
df['日期'] = pd.to_datetime(df['日期'])
df.set_index('日期', drop=True, inplace=True)

if df is not None and not df.empty:
# fill turnover_rate, pct_change
for kdata in kdatas:
if kdata.timestamp in df.index:
kdata.turnover_rate = df.loc[kdata.timestamp, '换手率']
kdata.change_pct = df.loc[kdata.timestamp, '涨跌幅']
self.session.commit()

def on_stop(self):
super().on_stop()
Expand Down

0 comments on commit 7a5bf18

Please sign in to comment.