修正数字止损边界口径

This commit is contained in:
boris
2026-07-08 05:06:55 +08:00
parent a30face86a
commit 185ed49fe2
+111 -3
View File
@@ -7964,12 +7964,12 @@ impl PlatformExprStrategy {
boolean boolean
} else if let Some(multiplier) = stop_result.clone().try_cast::<f64>() { } else if let Some(multiplier) = stop_result.clone().try_cast::<f64>() {
if multiplier > 0.0 && multiplier < 0.5 { if multiplier > 0.0 && multiplier < 0.5 {
holding_return <= -multiplier holding_return < -multiplier
} else { } else {
current_price <= stop_take_base_price * multiplier current_price < stop_take_base_price * multiplier
} }
} else if let Some(multiplier) = stop_result.try_cast::<i64>() { } else if let Some(multiplier) = stop_result.try_cast::<i64>() {
current_price <= stop_take_base_price * multiplier as f64 current_price < stop_take_base_price * multiplier as f64
} else { } else {
false false
} }
@@ -13667,6 +13667,114 @@ mod tests {
assert_eq!(evaluate(9.1, "0.08", ""), (true, false)); assert_eq!(evaluate(9.1, "0.08", ""), (true, false));
} }
#[test]
fn platform_numeric_stop_loss_is_strictly_below_multiplier() {
let prev_date = d(2025, 3, 13);
let date = d(2025, 3, 14);
let symbol = "600561.SH";
let evaluate = |last_price: f64| {
let data = DataSet::from_components(
vec![Instrument {
symbol: symbol.to_string(),
name: symbol.to_string(),
board: "SH".to_string(),
round_lot: 100,
listed_at: Some(d(2020, 1, 1)),
delisted_at: None,
status: "active".to_string(),
}],
vec![DailyMarketSnapshot {
date,
symbol: symbol.to_string(),
timestamp: Some("2025-03-14 10:15:00".to_string()),
day_open: last_price,
open: last_price,
high: last_price,
low: last_price,
close: last_price,
last_price,
bid1: last_price,
ask1: last_price,
prev_close: 100.0,
volume: 1_000_000,
minute_volume: 1_000,
bid1_volume: 1_000,
ask1_volume: 1_000,
trading_phase: Some("continuous".to_string()),
paused: false,
upper_limit: 110.0,
lower_limit: 90.0,
price_tick: 0.01,
}],
vec![DailyFactorSnapshot {
date,
symbol: symbol.to_string(),
market_cap_bn: 3.2,
free_float_cap_bn: 2.1,
pe_ttm: 8.0,
turnover_ratio: Some(3.0),
effective_turnover_ratio: Some(3.0),
extra_factors: BTreeMap::new(),
}],
vec![CandidateEligibility {
date,
symbol: symbol.to_string(),
is_st: false,
is_star_st: false,
is_new_listing: false,
is_paused: false,
allow_buy: false,
allow_sell: true,
is_kcb: false,
is_one_yuan: false,
risk_level_code: None,
}],
vec![BenchmarkSnapshot {
date,
benchmark: "000852.SH".to_string(),
open: 1000.0,
close: 1002.0,
prev_close: 998.0,
volume: 1_000_000,
}],
)
.expect("dataset");
let mut portfolio = PortfolioState::new(1_000_000.0);
portfolio.position_mut(symbol).buy(prev_date, 10_000, 100.0);
let subscriptions = BTreeSet::new();
let ctx = StrategyContext {
execution_date: date,
decision_date: date,
decision_index: 40,
data: &data,
portfolio: &portfolio,
futures_account: None,
open_orders: &[],
dynamic_universe: None,
subscriptions: &subscriptions,
process_events: &[],
active_process_event: None,
active_datetime: None,
order_events: &[],
fills: &[],
};
let mut cfg = PlatformExprStrategyConfig::microcap_rotation();
cfg.rotation_enabled = false;
cfg.signal_symbol = symbol.to_string();
cfg.stop_loss_expr = "0.92".to_string();
cfg.take_profit_expr.clear();
let strategy = PlatformExprStrategy::new(cfg);
let day = strategy.day_state(&ctx, date).expect("day state");
strategy
.stop_take_action(&ctx, date, date, &day, symbol)
.expect("stop take action")
.0
};
assert!(!evaluate(92.0));
assert!(evaluate(91.99));
}
#[test] #[test]
fn platform_stock_state_uses_current_day_limit_status_for_buy_scale() { fn platform_stock_state_uses_current_day_limit_status_for_buy_scale() {
let date = d(2025, 3, 14); let date = d(2025, 3, 14);