Skip to content

Commit

Permalink
fix logic bug of QA_util_get_next_trade_date and QA_util_get_pre_trad…
Browse files Browse the repository at this point in the history
…e_date
  • Loading branch information
huchen committed Dec 27, 2020
1 parent 59dab73 commit 2fe7681
Show file tree
Hide file tree
Showing 3 changed files with 7,804 additions and 7,764 deletions.
35 changes: 35 additions & 0 deletions QUANTAXIS/QAFactor/fetcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,41 @@ def _get_crosssection_financial(report_date, report_type, sheet_type, fields, wa
trial_count=0)


# FIXME: Add Fetch Get Method of Daily Basic
def QA_fetch_get_daily_basic(
code: Union[str, List, Tuple] = None,
trade_date: Union[str, pd.Timestamp, datetime.datetime] = None,
fields: Union[str, List, Tuple] = None,
wait_seconds: int=61,
max_trials: int=3
) -> pd.DataFrame:
"""
从网络获取市场指定交易日重要基本面指标,用于选股分析和报表展示
Args:
code(Union[str, List, Tuple], optional): 指定股票代码,默认为 None,即对应交易日的全市场股票
trade_date(Union[str, pd.Timestamp, datetime.datetime], optional): 指定交易日期, 默认为 None, 即距离当前
日期最近的交易日
fields(Union[str, List, Tuple], optional): 默认为 None,如果指定为某一单个 str,默认返回 DataFrame 包括
交易日等附加信息
wait_seconds (int, optional): 查询超时时间, 默认为 61.
max_trial (int, optional): 查询最大尝试次数, 默认为 3.
Returns:
pd.DataFrame: 指定交易日指定范围指定标的的每日基本面指标信息
"""
def _fetch_get_daily_basic(code, trade_date, fields, trial_count):
nonlocal pro, max_trials
if trial_count >= max_trial:
raise ValueError("[ERROR]\tEXCEED MAX TRIAL!")
if not fields:
qry = {}
else:
qry = {}

pro = get_pro()


def QA_fetch_crosssection_financial(
report_date: Union[str, datetime.datetime, pd.Timestamp],
report_type: Union[int, str] = 1,
Expand Down
63 changes: 43 additions & 20 deletions QUANTAXIS/QAFactor/localize.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,13 @@ def _ts_update_all(report_date, report_type, sheet_type, wait_seconds, max_trial
for item in QA_util_to_json_from_pandas(df):
report_label = None
if item["report_date"].endswith("0331"):
report_label = '1'
report_label = "1"
elif item["report_date"].endswith("0630"):
report_label = '2'
report_label = "2"
elif item["report_date"].endswith("0930"):
report_label = '3'
report_label = "3"
elif item["report_date"].endswith("1231"):
report_label = '4'
report_label = "4"
item["report_label"] = report_label
coll.update_one(
{
Expand Down Expand Up @@ -167,12 +167,11 @@ def QA_ts_update_inc(wait_seconds: int = 61, max_trial=3) -> pd.DataFrame:
# 使用最新的合并报表作为基准
# 当前数据库已经包含了这个代码的数据, 继续增量更新
# 加入这个判断的原因是因为如果股票是刚上市的 数据库会没有数据 所以会有负索引问题出现
type_count = coll.count_documents(
filter={"report_type": report_type})
type_count = coll.count_documents(filter={"report_type": report_type})
if type_count > 0:
# 接着上次获取的日期继续更新
ref = coll.find({"report_type": report_type})
cursor_date = ref[type_count-1]["report_date"]
cursor_date = ref[type_count - 1]["report_date"]
cursor_year = pd.Timestamp(cursor_date).year
report_dates = None
if report_type == "1" or report_type == "2": # 往前回溯一期
Expand All @@ -186,7 +185,7 @@ def QA_ts_update_inc(wait_seconds: int = 61, max_trial=3) -> pd.DataFrame:
]
)
report_dates = fmt_report_date[
fmt_report_date.index(cursor_date) - 1:
fmt_report_date.index(cursor_date) - 1 :
]
else: # 往前回溯三年
fmt_report_date = sorted(
Expand Down Expand Up @@ -222,13 +221,13 @@ def QA_ts_update_inc(wait_seconds: int = 61, max_trial=3) -> pd.DataFrame:
for item in js:
report_label = None
if item["report_date"].endswith("0331"):
report_label = '1'
report_label = "1"
elif item["report_date"].endswith("0630"):
report_label = '2'
report_label = "2"
elif item["report_date"].endswith("0930"):
report_label = '3'
report_label = "3"
elif item["report_date"].endswith("1231"):
report_label = '4'
report_label = "4"
item["report_label"] = report_label
coll.update_one(
{
Expand All @@ -250,6 +249,7 @@ def QA_ts_update_inc(wait_seconds: int = 61, max_trial=3) -> pd.DataFrame:
upsert=True,
)
else:

def _func(x):
if x[-4:] == "0331":
return "1"
Expand All @@ -259,6 +259,7 @@ def _func(x):
return "3"
else:
return "4"

df_2 = df_2[df_1.columns]
df_1_1 = df_1.loc[
(df_1.report_date == report_date)
Expand All @@ -285,10 +286,12 @@ def _func(x):
QA_util_date_stamp
)
df_1_1["report_date_stamp"] = df_1_1.report_date.apply(
QA_util_date_stamp)
QA_util_date_stamp
)
# TODO: add appendix
df_1_1["report_label"] = df_1_1["report_date"].apply(
_func)
_func
)
js = QA_util_to_json_from_pandas(df_1_1)
coll.insert_many(js)
df_1_2 = df_1.loc[
Expand Down Expand Up @@ -316,9 +319,11 @@ def _func(x):
QA_util_date_stamp
)
df_1_2["report_date_stamp"] = df_1_2.report_date.apply(
QA_util_date_stamp)
QA_util_date_stamp
)
df_1_2["report_label"] = df_1_2["report_date"].apply(
_func)
_func
)
js = QA_util_to_json_from_pandas(df_1_2)
coll.insert_many(js)
else:
Expand All @@ -333,8 +338,7 @@ def QA_ts_update_stock_basic():
"""
coll = DATABASE.stock_basic
coll.create_index(
[("code", ASCENDING), ("status", ASCENDING),
("list_date_stamp", ASCENDING)],
[("code", ASCENDING), ("status", ASCENDING), ("list_date_stamp", ASCENDING)],
unique=True,
)

Expand Down Expand Up @@ -468,8 +472,13 @@ def QA_ts_update_industry(
coll = DATABASE.industry
try:
coll.create_index(
[("code", ASCENDING), ("level", ASCENDING), ("src", ASCENDING),
("in_date_stamp", DESCENDING), ("out_date_stamp", DESCENDING)],
[
("code", ASCENDING),
("level", ASCENDING),
("src", ASCENDING),
("in_date_stamp", DESCENDING),
("out_date_stamp", DESCENDING),
],
unique=False,
)
except:
Expand All @@ -492,6 +501,20 @@ def QA_ts_update_industry(
)


def QA_ts_update_daily_basic():
"""
更新每日全市场重要基本面指标,用于选股分析和报表展示
"""

def _ts_update_daily_basic(trade_date, wait_seconds, trial_count):
nonlocal pro, max_trial
if trial_count > max_trial:
raise ValueError("[ERROR]\tEXCEED MAX TRIALS!")

pro = get_pro()
df = pro.daily_basic()


if __name__ == "__main__":
# QA_ts_update_all()
# QA_ts_update_inc()
Expand Down
Loading

0 comments on commit 2fe7681

Please sign in to comment.