Skip to content

Commit

Permalink
analyse新增个股权重分析
Browse files Browse the repository at this point in the history
  • Loading branch information
Zhou-JiaJun committed Jun 9, 2023
1 parent 5fbbe9d commit f2b8dfb
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
19 changes: 19 additions & 0 deletions rqalpha/mod/rqalpha_mod_sys_analyser/mod.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,7 @@ def _to_position_record(self, date, order_book_id, long, short):
for direction_prefix, pos in direction_pos_iter:
data[direction_prefix + "_pnl"] = self._safe_convert(getattr(pos, "pnl", None))
data[direction_prefix + "_margin"] = self._safe_convert(pos.margin)
data[direction_prefix + "_market_value"] = self._safe_convert(pos.market_value)
data[direction_prefix + "_quantity"] = self._safe_convert(pos.quantity)
data[direction_prefix + "_avg_open_price"] = self._safe_convert(getattr(pos, "avg_price", None))
return data
Expand Down Expand Up @@ -489,6 +490,24 @@ def tear_down(self, code, exception=None):
df = df.set_index("date").sort_index()
result_dict["{}_positions".format(account_name)] = df

# 个股权重
df_list = []
need_cols = ["order_book_id", "market_value"]
for table_name in ["stock_positions", "future_positions"]:
if table_name not in result_dict:
continue
table = result_dict[table_name]
for field in ["market_value", "LONG_market_value", "SHORT_market_value"]:
if field not in table.columns:
continue
df_list.append(table.rename(columns={field: "market_value"})[need_cols])
if len(df_list) > 0:
positions_weight_df = pd.concat(df_list).dropna()
positions_weight_df["total_value"] = positions_weight_df.groupby(by="date")["market_value"].sum()
positions_weight_df["weight"] = positions_weight_df["market_value"] / positions_weight_df["total_value"]
positions_weight_df = positions_weight_df.groupby(by="date")["weight"].describe()
positions_weight_df = positions_weight_df.reindex(total_portfolios.index).fillna(value=0)
result_dict["positions_weight"] = positions_weight_df
result_dict["yearly_risk_free_rates"] = dict(_get_yearly_risk_free_rates(data_proxy, start_date, end_date))

if self._mod_config.output_file:
Expand Down
2 changes: 1 addition & 1 deletion rqalpha/mod/rqalpha_mod_sys_analyser/report/report.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ def generate_report(result_dict, output_path):
}, output_path)

for name in ["portfolio", "stock_account", "future_account",
"stock_positions", "future_positions", "trades"]:
"stock_positions", "future_positions", "trades", "positions_weight"]:
try:
df = result_dict[name]
except KeyError:
Expand Down

0 comments on commit f2b8dfb

Please sign in to comment.