update UI width parameter and add stock info fetching methods

This commit is contained in:
Your Name 2025-10-09 19:44:51 +08:00
parent c8d80ff6a7
commit 1099885804
5 changed files with 90 additions and 8 deletions

View File

@ -381,7 +381,7 @@ def render_backtest_review() -> None:
title="风险事件分布",
)
agg_fig.update_layout(height=320, margin=dict(l=10, r=10, t=40, b=20))
st.plotly_chart(agg_fig, use_container_width=True)
st.plotly_chart(agg_fig, width="stretch")
except Exception: # noqa: BLE001
LOGGER.debug("绘制风险事件分布失败", extra=LOG_EXTRA)
except Exception: # noqa: BLE001

View File

@ -274,7 +274,7 @@ def render_factor_calculation() -> None:
if df_data:
df = pd.DataFrame(df_data)
st.dataframe(df.head(100), use_container_width=True) # 只显示前100条
st.dataframe(df.head(100), width="stretch") # 只显示前100条
st.info(f"{len(df_data)} 条因子记录显示前100条")
else:
st.info("没有找到因子计算结果")

View File

@ -194,10 +194,10 @@ def render_market_visualization() -> None:
]
)
fig.update_layout(title=f"{ts_code} K线图", xaxis_title="日期", yaxis_title="价格")
st.plotly_chart(fig, use_container_width=True)
st.plotly_chart(fig, width="stretch")
fig_vol = px.bar(df, x="trade_date", y="vol", title="成交量")
st.plotly_chart(fig_vol, use_container_width=True)
st.plotly_chart(fig_vol, width="stretch")
df_ma = df.copy()
df_ma["MA5"] = df_ma["close"].rolling(window=5).mean()
@ -205,6 +205,6 @@ def render_market_visualization() -> None:
df_ma["MA60"] = df_ma["close"].rolling(window=60).mean()
fig_ma = px.line(df_ma, x="trade_date", y=["close", "MA5", "MA20", "MA60"], title="均线对比")
st.plotly_chart(fig_ma, use_container_width=True)
st.plotly_chart(fig_ma, width="stretch")
st.dataframe(df, hide_index=True, width='stretch')

View File

@ -208,7 +208,7 @@ def render_stock_evaluation() -> None:
st.dataframe(
result_df,
hide_index=True,
use_container_width=True
width="stretch"
)
# 绘制IC均值分布
@ -237,7 +237,7 @@ def render_stock_evaluation() -> None:
st.dataframe(
score_df,
hide_index=True,
use_container_width=True
width="stretch"
)
# 添加入池功能
@ -260,7 +260,12 @@ def _calculate_stock_scores(
# 标准化权重
weights = np.array(factor_weights)
weights = weights / np.sum(np.abs(weights))
abs_sum = np.sum(np.abs(weights))
if abs_sum > 0: # 避免除以零
weights = weights / abs_sum
else:
# 如果所有权重都是零,则使用均匀分布
weights = np.ones_like(weights) / len(weights)
# 获取所有股票的因子值
stocks = universe or broker.get_all_stocks(eval_date.strftime("%Y%m%d"))

View File

@ -1397,6 +1397,83 @@ class DataBroker:
self._coverage_cache.clear()
LOGGER.info("数据覆盖缓存已清除", extra=LOG_EXTRA)
def get_stock_info(self, ts_code: str, trade_date: str = None) -> Optional[Dict[str, Any]]:
"""获取股票基本信息。
Args:
ts_code: 股票代码
trade_date: 交易日期默认为最新日期
Returns:
Dict: 股票基本信息包含名称行业等
"""
if not trade_date:
# 如果没有提供交易日期,使用当前日期
trade_date = datetime.now().strftime("%Y%m%d")
try:
# 获取股票基本信息
info = self.fetch_latest(
ts_code=ts_code,
trade_date=trade_date,
fields=["stock_basic.name", "stock_basic.industry"]
)
if not info:
return None
# 添加股票代码
result = {"ts_code": ts_code}
result.update(info)
return result
except Exception as exc:
LOGGER.debug(
"获取股票信息失败 ts_code=%s err=%s",
ts_code,
exc,
extra=LOG_EXTRA
)
return None
def fetch_latest_factor(self, ts_code: str, factor: str, eval_date: date) -> Optional[float]:
"""获取指定股票的最新因子值。
Args:
ts_code: 股票代码
factor: 因子名称
eval_date: 评估日期
Returns:
float: 因子值如果获取失败则返回None
"""
trade_date = eval_date.strftime("%Y%m%d")
try:
# 构建因子字段名称
factor_field = f"factors.{factor}"
# 获取因子值
result = self.fetch_latest(
ts_code=ts_code,
trade_date=trade_date,
fields=[factor_field]
)
if not result or factor_field not in result:
return None
return result[factor_field]
except Exception as exc:
LOGGER.debug(
"获取因子值失败 ts_code=%s factor=%s err=%s",
ts_code,
factor,
exc,
extra=LOG_EXTRA
)
return None
def get_data_coverage(self, start_date: str, end_date: str) -> Dict:
"""获取指定日期范围内的数据覆盖情况。