实现FIDC配置化风控与交易成本

This commit is contained in:
boris
2026-07-02 07:16:47 +08:00
parent 754fc91376
commit 7db0e8da1d
17 changed files with 2689 additions and 249 deletions
+205 -4
View File
@@ -4,6 +4,7 @@ use chrono::NaiveDate;
use serde::Serialize;
use crate::data::{BenchmarkSnapshot, DataSet, EligibleUniverseSnapshot};
use crate::risk_control::{ChinaAShareRiskControl, FidcRiskControlConfig, FidcRiskDecisionAudit};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum BandRegime {
@@ -39,6 +40,7 @@ pub struct SelectionDiagnostics {
pub missing_market_cap_symbols: Vec<String>,
pub selected_symbols: Vec<String>,
pub rejection_examples: Vec<String>,
pub risk_decisions: Vec<FidcRiskDecisionAudit>,
}
pub struct SelectionContext<'a> {
@@ -47,20 +49,61 @@ pub struct SelectionContext<'a> {
pub reference_level: f64,
pub data: &'a DataSet,
pub dynamic_universe: Option<&'a BTreeSet<String>>,
pub risk_config: Option<&'a FidcRiskControlConfig>,
}
impl SelectionContext<'_> {
fn eligible_universe(&self) -> Vec<EligibleUniverseSnapshot> {
let eligible = self.data.eligible_universe_on(self.decision_date);
let eligible = match self.risk_config {
Some(risk_config) => self
.data
.eligible_universe_on_with_risk_config(self.decision_date, risk_config),
None => self.data.eligible_universe_on(self.decision_date).to_vec(),
};
match self.dynamic_universe {
Some(symbols) if !symbols.is_empty() => eligible
.iter()
.into_iter()
.filter(|row| symbols.contains(&row.symbol))
.cloned()
.collect(),
_ => eligible.to_vec(),
_ => eligible,
}
}
fn selection_risk_decisions(&self) -> Vec<FidcRiskDecisionAudit> {
let default_risk_config;
let risk_config = match self.risk_config {
Some(value) => value,
None => {
default_risk_config = FidcRiskControlConfig::default();
&default_risk_config
}
};
let mut decisions = Vec::new();
for factor in self.data.factor_snapshots_on(self.decision_date) {
if self
.dynamic_universe
.is_some_and(|symbols| !symbols.is_empty() && !symbols.contains(&factor.symbol))
{
continue;
}
let Some(candidate) = self.data.candidate(self.decision_date, &factor.symbol) else {
continue;
};
let Some(market) = self.data.market(self.decision_date, &factor.symbol) else {
continue;
};
if let Some(decision) = ChinaAShareRiskControl::selection_rejection_decision_with_config(
self.decision_date,
candidate,
market,
self.data.instrument(&factor.symbol),
risk_config,
) {
decisions.push(decision);
}
}
decisions
}
}
pub trait UniverseSelector {
@@ -166,9 +209,23 @@ impl UniverseSelector for DynamicMarketCapBandSelector {
missing_market_cap_symbols: Vec::new(),
selected_symbols: Vec::new(),
rejection_examples: Vec::new(),
risk_decisions: Vec::new(),
};
diagnostics.factor_total = ctx.data.factor_snapshots_on(ctx.decision_date).len();
diagnostics.risk_decisions = ctx.selection_risk_decisions();
diagnostics.not_eligible_count = diagnostics.risk_decisions.len();
diagnostics.paused_count = diagnostics
.risk_decisions
.iter()
.filter(|decision| decision.rule_code == "paused")
.count();
diagnostics.rejection_examples = diagnostics
.risk_decisions
.iter()
.take(8)
.map(|decision| format!("{} rejected by {}", decision.symbol, decision.rule_code))
.collect();
let eligible = ctx.eligible_universe();
diagnostics.market_cap_missing_count =
diagnostics.factor_total.saturating_sub(eligible.len());
@@ -221,3 +278,147 @@ fn to_universe_candidate(
band_high,
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::data::{
BenchmarkSnapshot, CandidateEligibility, DailyFactorSnapshot, DailyMarketSnapshot, DataSet,
};
use crate::instrument::Instrument;
fn d() -> NaiveDate {
NaiveDate::from_ymd_opt(2025, 1, 2).unwrap()
}
fn instrument(symbol: &str) -> Instrument {
Instrument {
symbol: symbol.to_string(),
name: symbol.to_string(),
board: symbol.rsplit('.').next().unwrap_or("").to_string(),
round_lot: 100,
listed_at: Some(NaiveDate::from_ymd_opt(2020, 1, 1).unwrap()),
delisted_at: None,
status: "active".to_string(),
}
}
fn market(symbol: &str, price: f64) -> DailyMarketSnapshot {
DailyMarketSnapshot {
date: d(),
symbol: symbol.to_string(),
timestamp: Some("2025-01-02 10:00:00".to_string()),
day_open: price,
open: price,
high: price,
low: price,
close: price,
last_price: price,
bid1: price,
ask1: price,
prev_close: price,
volume: 1_000_000,
minute_volume: 10_000,
bid1_volume: 10_000,
ask1_volume: 10_000,
trading_phase: Some("continuous".to_string()),
paused: false,
upper_limit: price * 1.1,
lower_limit: price * 0.9,
price_tick: 0.01,
}
}
fn factor(symbol: &str, market_cap_bn: f64) -> DailyFactorSnapshot {
DailyFactorSnapshot {
date: d(),
symbol: symbol.to_string(),
market_cap_bn,
free_float_cap_bn: market_cap_bn,
pe_ttm: 10.0,
turnover_ratio: Some(0.01),
effective_turnover_ratio: Some(0.01),
extra_factors: Default::default(),
}
}
fn candidate(symbol: &str, is_st: bool, is_kcb: bool) -> CandidateEligibility {
CandidateEligibility {
date: d(),
symbol: symbol.to_string(),
is_st,
is_new_listing: false,
is_paused: false,
allow_buy: true,
allow_sell: true,
is_kcb,
is_one_yuan: false,
risk_level_code: None,
}
}
fn benchmark() -> BenchmarkSnapshot {
BenchmarkSnapshot {
date: d(),
benchmark: "000852.SH".to_string(),
open: 2000.0,
close: 2000.0,
prev_close: 1990.0,
volume: 1_000_000,
}
}
#[test]
fn selector_records_structured_selection_risk_decisions() {
let data = DataSet::from_components(
vec![
instrument("000001.SZ"),
instrument("688001.SH"),
instrument("000002.SZ"),
],
vec![
market("000001.SZ", 10.0),
market("688001.SH", 10.0),
market("000002.SZ", 10.0),
],
vec![
factor("000001.SZ", 8.0),
factor("688001.SH", 9.0),
factor("000002.SZ", 10.0),
],
vec![
candidate("000001.SZ", true, false),
candidate("688001.SH", false, true),
candidate("000002.SZ", false, false),
],
vec![benchmark()],
)
.unwrap();
let selector = DynamicMarketCapBandSelector::new(2000.0, 7.0, 10.0, 0.0, 10, 0.0, 0.0, 0.0);
let (_selected, diagnostics) = selector.select_with_diagnostics(&SelectionContext {
decision_date: d(),
benchmark: &benchmark(),
reference_level: 2000.0,
data: &data,
dynamic_universe: None,
risk_config: Some(&FidcRiskControlConfig::default()),
});
let rules = diagnostics
.risk_decisions
.iter()
.map(|decision| decision.rule_code.as_str())
.collect::<BTreeSet<_>>();
assert!(rules.contains("st"), "{:?}", diagnostics.risk_decisions);
assert!(rules.contains("kcb"), "{:?}", diagnostics.risk_decisions);
assert_eq!(
diagnostics.not_eligible_count,
diagnostics.risk_decisions.len()
);
assert!(
diagnostics.risk_decisions[0]
.diagnostic_line()
.starts_with("risk_decision=")
);
}
}