修正AiQuant止盈止损成本基准
This commit is contained in:
@@ -6231,11 +6231,19 @@ impl PlatformExprStrategy {
|
|||||||
{
|
{
|
||||||
return Ok((false, false));
|
return Ok((false, false));
|
||||||
}
|
}
|
||||||
let avg_price = position
|
let entry_avg_price = position
|
||||||
.average_entry_price()
|
.average_entry_price()
|
||||||
.filter(|value| value.is_finite() && *value > 0.0)
|
.filter(|value| value.is_finite() && *value > 0.0)
|
||||||
.unwrap_or(position.average_cost);
|
.unwrap_or(position.average_cost);
|
||||||
if position.quantity == 0 || avg_price <= 0.0 {
|
let stop_take_base_price = if self.config.aiquant_transaction_cost
|
||||||
|
&& position.average_cost.is_finite()
|
||||||
|
&& position.average_cost > 0.0
|
||||||
|
{
|
||||||
|
position.average_cost
|
||||||
|
} else {
|
||||||
|
entry_avg_price
|
||||||
|
};
|
||||||
|
if position.quantity == 0 || stop_take_base_price <= 0.0 {
|
||||||
return Ok((false, false));
|
return Ok((false, false));
|
||||||
}
|
}
|
||||||
let stock = match self.stock_state_at_time(
|
let stock = match self.stock_state_at_time(
|
||||||
@@ -6251,8 +6259,8 @@ impl PlatformExprStrategy {
|
|||||||
Err(error) => return Err(error),
|
Err(error) => return Err(error),
|
||||||
};
|
};
|
||||||
let current_price = stock.last;
|
let current_price = stock.last;
|
||||||
let holding_return = if avg_price > 0.0 {
|
let holding_return = if stop_take_base_price > 0.0 {
|
||||||
current_price / avg_price - 1.0
|
current_price / stop_take_base_price - 1.0
|
||||||
} else {
|
} else {
|
||||||
0.0
|
0.0
|
||||||
};
|
};
|
||||||
@@ -6265,7 +6273,7 @@ impl PlatformExprStrategy {
|
|||||||
let position_state = PositionExpressionState {
|
let position_state = PositionExpressionState {
|
||||||
order_book_id: position.symbol.clone(),
|
order_book_id: position.symbol.clone(),
|
||||||
avg_cost: position.average_cost,
|
avg_cost: position.average_cost,
|
||||||
avg_price,
|
avg_price: entry_avg_price,
|
||||||
current_price,
|
current_price,
|
||||||
prev_close: stock.prev_close,
|
prev_close: stock.prev_close,
|
||||||
holding_return,
|
holding_return,
|
||||||
@@ -6308,10 +6316,10 @@ impl PlatformExprStrategy {
|
|||||||
if multiplier > 0.0 && multiplier < 0.5 {
|
if multiplier > 0.0 && multiplier < 0.5 {
|
||||||
holding_return <= -multiplier
|
holding_return <= -multiplier
|
||||||
} else {
|
} else {
|
||||||
current_price <= avg_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 <= avg_price * multiplier as f64
|
current_price <= stop_take_base_price * multiplier as f64
|
||||||
} else {
|
} else {
|
||||||
false
|
false
|
||||||
}
|
}
|
||||||
@@ -6332,10 +6340,10 @@ impl PlatformExprStrategy {
|
|||||||
if multiplier <= 1.0 {
|
if multiplier <= 1.0 {
|
||||||
holding_return > multiplier
|
holding_return > multiplier
|
||||||
} else {
|
} else {
|
||||||
current_price / avg_price > multiplier
|
current_price / stop_take_base_price > multiplier
|
||||||
}
|
}
|
||||||
} else if let Some(multiplier) = take_result.try_cast::<i64>() {
|
} else if let Some(multiplier) = take_result.try_cast::<i64>() {
|
||||||
current_price / avg_price > multiplier as f64
|
current_price / stop_take_base_price > multiplier as f64
|
||||||
} else {
|
} else {
|
||||||
false
|
false
|
||||||
}
|
}
|
||||||
@@ -8854,6 +8862,140 @@ mod tests {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn platform_aiquant_stop_loss_uses_fee_cost_basis_avg_cost() {
|
||||||
|
let prev_date = d(2025, 3, 13);
|
||||||
|
let date = d(2025, 3, 14);
|
||||||
|
let symbol = "301076.SZ";
|
||||||
|
let data = DataSet::from_components_with_actions_and_quotes(
|
||||||
|
vec![Instrument {
|
||||||
|
symbol: symbol.to_string(),
|
||||||
|
name: symbol.to_string(),
|
||||||
|
board: "SZ".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:18:00".to_string()),
|
||||||
|
day_open: 9.30,
|
||||||
|
open: 9.30,
|
||||||
|
high: 9.35,
|
||||||
|
low: 9.18,
|
||||||
|
close: 9.20,
|
||||||
|
last_price: 9.20,
|
||||||
|
bid1: 9.20,
|
||||||
|
ask1: 9.21,
|
||||||
|
prev_close: 10.00,
|
||||||
|
volume: 1_000_000,
|
||||||
|
tick_volume: 1_000,
|
||||||
|
bid1_volume: 1_000,
|
||||||
|
ask1_volume: 1_000,
|
||||||
|
trading_phase: Some("continuous".to_string()),
|
||||||
|
paused: false,
|
||||||
|
upper_limit: 11.00,
|
||||||
|
lower_limit: 9.00,
|
||||||
|
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_new_listing: false,
|
||||||
|
is_paused: false,
|
||||||
|
allow_buy: false,
|
||||||
|
allow_sell: true,
|
||||||
|
is_kcb: false,
|
||||||
|
is_one_yuan: false,
|
||||||
|
}],
|
||||||
|
vec![BenchmarkSnapshot {
|
||||||
|
date,
|
||||||
|
benchmark: "000852.SH".to_string(),
|
||||||
|
open: 1000.0,
|
||||||
|
close: 1002.0,
|
||||||
|
prev_close: 998.0,
|
||||||
|
volume: 1_000_000,
|
||||||
|
}],
|
||||||
|
Vec::new(),
|
||||||
|
vec![IntradayExecutionQuote {
|
||||||
|
date,
|
||||||
|
symbol: symbol.to_string(),
|
||||||
|
timestamp: date.and_hms_opt(10, 18, 0).expect("timestamp"),
|
||||||
|
last_price: 9.205,
|
||||||
|
bid1: 9.20,
|
||||||
|
ask1: 9.21,
|
||||||
|
bid1_volume: 1_000,
|
||||||
|
ask1_volume: 1_000,
|
||||||
|
volume_delta: 1_000,
|
||||||
|
amount_delta: 9_205.0,
|
||||||
|
trading_phase: Some("continuous".to_string()),
|
||||||
|
}],
|
||||||
|
)
|
||||||
|
.expect("dataset");
|
||||||
|
let mut portfolio = PortfolioState::new(1_000_000.0);
|
||||||
|
portfolio.position_mut(symbol).buy(prev_date, 100, 10.0);
|
||||||
|
portfolio.position_mut(symbol).record_buy_trade_cost(100, 1.0);
|
||||||
|
let position = portfolio.position(symbol).expect("position");
|
||||||
|
assert!((position.average_entry_price().unwrap() - 10.0).abs() < 1e-12);
|
||||||
|
assert!((position.average_cost - 10.01).abs() < 1e-12);
|
||||||
|
assert!(9.205 > position.average_entry_price().unwrap() * 0.92);
|
||||||
|
assert!(9.205 <= position.average_cost * 0.92);
|
||||||
|
|
||||||
|
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.aiquant_transaction_cost = true;
|
||||||
|
cfg.intraday_execution_time = Some(NaiveTime::from_hms_opt(10, 18, 0).expect("time"));
|
||||||
|
cfg.signal_symbol = symbol.to_string();
|
||||||
|
cfg.stop_loss_expr = "0.92".to_string();
|
||||||
|
cfg.take_profit_expr.clear();
|
||||||
|
let mut strategy = PlatformExprStrategy::new(cfg);
|
||||||
|
|
||||||
|
let decision = strategy.on_day(&ctx).expect("platform decision");
|
||||||
|
|
||||||
|
assert!(
|
||||||
|
decision.order_intents.iter().any(|intent| matches!(
|
||||||
|
intent,
|
||||||
|
OrderIntent::TargetValue {
|
||||||
|
symbol: intent_symbol,
|
||||||
|
target_value,
|
||||||
|
reason,
|
||||||
|
} if intent_symbol == symbol && *target_value == 0.0 && reason == "stop_loss_exit"
|
||||||
|
)),
|
||||||
|
"{:?}",
|
||||||
|
decision.order_intents
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn platform_stop_take_fraction_values_are_return_thresholds() {
|
fn platform_stop_take_fraction_values_are_return_thresholds() {
|
||||||
let prev_date = d(2025, 3, 13);
|
let prev_date = d(2025, 3, 13);
|
||||||
|
|||||||
Reference in New Issue
Block a user