qnloft-stock/utils/stock_web_api.py

68 lines
2.1 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from utils.comm import *
def stock_web_api_method_decorator(func):
def wrapper(*args, **kwargs):
return func(*args, **kwargs)
return wrapper
@stock_web_api_method_decorator
def stock_web_api_money_flow(start_date=None, end_date=None):
# 沪深港通资金流向
df = xcsc_pro.moneyflow_hsgt(start_date=start_date, end_date=end_date)[::-1]
if df[df["trade_date"] == end_date].empty:
# df 如果里面不包含当天日期的数据,则使用备用接口
ak_df = ak.stock_hsgt_fund_flow_summary_em()
northward = ak_df[ak_df['资金方向'] == '北向']
hgt = round(northward[northward['板块'] == '沪股通']['成交净买额'].sum(), 2) * 100
sgt = round(northward[northward['板块'] == '深股通']['成交净买额'].sum(), 2) * 100
north_money = round(hgt + sgt, 2)
substitute_df = pd.DataFrame(
[{
"trade_date": northward["交易日"].iloc[0].strftime('%Y%m%d'),
"hgt": hgt,
"sgt": sgt,
"north_money": north_money,
}],
index=[0],
)
result = pd.concat([df, substitute_df], ignore_index=True)
return result
if df is not None and not df.empty:
return df
raise Exception("沪深港通资金流向数据拉取失败!")
@stock_web_api_method_decorator
def stock_web_api_industry_summary(): # 同花顺 - 行业板块数据
df = ak.stock_board_industry_summary_ths()
if df is not None and not df.empty:
return df
raise Exception("同花顺 - 行业板块数据 数据拉取失败!")
@stock_web_api_method_decorator
def stock_web_api_concept_name(): # 同花顺 - 概念板块数据
df = ak.stock_board_concept_name_em()
if df is not None and not df.empty:
return df
raise Exception("同花顺 - 概念板块数据 数据拉取失败!")
def execute_stock_web_api_method(func, *args, **kwargs):
for _ in range(5):
try:
return func(*args, **kwargs)
except Exception as e:
print(f"{func}】 -- 拉取出现问题 【{e}】, 开始进行重试!")
time.sleep(1)
else:
print(f"{func}】 5次出现错误请关注")
return None
if __name__ == '__main__':
print(execute_stock_web_api_method(func=stock_web_api_money_flow, start_date='20231123', end_date='20231123'))