修正弱市止盈前减仓顺序
This commit is contained in:
@@ -8553,7 +8553,7 @@ impl Strategy for PlatformExprStrategy {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut actionable_stop_take_exit_symbols = BTreeSet::<String>::new();
|
let mut stop_take_exit_signal_symbols = BTreeSet::<String>::new();
|
||||||
if self.config.aiquant_transaction_cost
|
if self.config.aiquant_transaction_cost
|
||||||
&& self.config.rotation_enabled
|
&& self.config.rotation_enabled
|
||||||
&& trading_ratio > 0.0
|
&& trading_ratio > 0.0
|
||||||
@@ -8581,15 +8581,20 @@ impl Strategy for PlatformExprStrategy {
|
|||||||
if !stop_hit && !profit_hit {
|
if !stop_hit && !profit_hit {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if defer_execution_risk
|
let lower_limit_blocked = match self.stock_state_at_time(
|
||||||
|| self.can_sell_position_at_time(
|
|
||||||
ctx,
|
ctx,
|
||||||
execution_date,
|
projection_date,
|
||||||
&position.symbol,
|
&position.symbol,
|
||||||
Some(self.intraday_execution_start_time()),
|
Some(self.intraday_execution_start_time()),
|
||||||
)
|
) {
|
||||||
{
|
Ok(stock) => stock.lower_limit > 0.0 && stock.last <= stock.lower_limit,
|
||||||
actionable_stop_take_exit_symbols.insert(position.symbol.clone());
|
Err(BacktestError::Data(crate::data::DataSetError::MissingSnapshot {
|
||||||
|
..
|
||||||
|
})) => false,
|
||||||
|
Err(error) => return Err(error),
|
||||||
|
};
|
||||||
|
if !lower_limit_blocked {
|
||||||
|
stop_take_exit_signal_symbols.insert(position.symbol.clone());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -8620,9 +8625,6 @@ impl Strategy for PlatformExprStrategy {
|
|||||||
if pending_full_close_symbols.contains(&position.symbol) {
|
if pending_full_close_symbols.contains(&position.symbol) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if actionable_stop_take_exit_symbols.contains(&position.symbol) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
let current_value = self.projected_position_value_at_execution_price(
|
let current_value = self.projected_position_value_at_execution_price(
|
||||||
ctx,
|
ctx,
|
||||||
&projected,
|
&projected,
|
||||||
@@ -8657,6 +8659,11 @@ impl Strategy for PlatformExprStrategy {
|
|||||||
.map(|projected_position| projected_position.quantity)
|
.map(|projected_position| projected_position.quantity)
|
||||||
.unwrap_or(0);
|
.unwrap_or(0);
|
||||||
let quantity_delta = after_qty as i32 - before_qty as i32;
|
let quantity_delta = after_qty as i32 - before_qty as i32;
|
||||||
|
if stop_take_exit_signal_symbols.contains(&position.symbol)
|
||||||
|
&& quantity_delta >= 0
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
if self
|
if self
|
||||||
.config
|
.config
|
||||||
.weak_market_shrink_overweight_threshold
|
.weak_market_shrink_overweight_threshold
|
||||||
@@ -16756,6 +16763,190 @@ mod tests {
|
|||||||
)));
|
)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn platform_aiquant_sells_down_before_actionable_take_profit_exit() {
|
||||||
|
let prev_date = d(2025, 4, 28);
|
||||||
|
let date = d(2025, 4, 29);
|
||||||
|
let symbols = ["000001.SZ", "000002.SZ"];
|
||||||
|
let data = DataSet::from_components_with_actions_and_quotes(
|
||||||
|
symbols
|
||||||
|
.iter()
|
||||||
|
.map(|symbol| 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(),
|
||||||
|
})
|
||||||
|
.collect(),
|
||||||
|
symbols
|
||||||
|
.iter()
|
||||||
|
.map(|symbol| DailyMarketSnapshot {
|
||||||
|
date,
|
||||||
|
symbol: (*symbol).to_string(),
|
||||||
|
timestamp: Some("2025-04-29 10:15:00".to_string()),
|
||||||
|
day_open: 10.0,
|
||||||
|
open: 10.0,
|
||||||
|
high: 10.4,
|
||||||
|
low: 9.8,
|
||||||
|
close: 10.0,
|
||||||
|
last_price: 10.0,
|
||||||
|
bid1: 9.99,
|
||||||
|
ask1: 10.01,
|
||||||
|
prev_close: 9.8,
|
||||||
|
volume: 1_000_000,
|
||||||
|
minute_volume: 10_000,
|
||||||
|
bid1_volume: 2_000,
|
||||||
|
ask1_volume: 2_000,
|
||||||
|
trading_phase: Some("continuous".to_string()),
|
||||||
|
paused: false,
|
||||||
|
upper_limit: 10.78,
|
||||||
|
lower_limit: 8.82,
|
||||||
|
price_tick: 0.01,
|
||||||
|
})
|
||||||
|
.collect(),
|
||||||
|
symbols
|
||||||
|
.iter()
|
||||||
|
.enumerate()
|
||||||
|
.map(|(index, symbol)| DailyFactorSnapshot {
|
||||||
|
date,
|
||||||
|
symbol: (*symbol).to_string(),
|
||||||
|
market_cap_bn: 10.0 + index as f64,
|
||||||
|
free_float_cap_bn: 10.0 + index as f64,
|
||||||
|
pe_ttm: 8.0,
|
||||||
|
turnover_ratio: Some(1.0),
|
||||||
|
effective_turnover_ratio: Some(1.0),
|
||||||
|
extra_factors: BTreeMap::new(),
|
||||||
|
})
|
||||||
|
.collect(),
|
||||||
|
symbols
|
||||||
|
.iter()
|
||||||
|
.map(|symbol| CandidateEligibility {
|
||||||
|
date,
|
||||||
|
symbol: (*symbol).to_string(),
|
||||||
|
is_st: false,
|
||||||
|
is_star_st: false,
|
||||||
|
is_new_listing: false,
|
||||||
|
is_paused: false,
|
||||||
|
allow_buy: true,
|
||||||
|
allow_sell: true,
|
||||||
|
is_kcb: false,
|
||||||
|
is_one_yuan: false,
|
||||||
|
risk_level_code: None,
|
||||||
|
})
|
||||||
|
.collect(),
|
||||||
|
vec![BenchmarkSnapshot {
|
||||||
|
date,
|
||||||
|
benchmark: "000852.SH".to_string(),
|
||||||
|
open: 1000.0,
|
||||||
|
close: 1000.0,
|
||||||
|
prev_close: 1000.0,
|
||||||
|
volume: 1_000_000,
|
||||||
|
}],
|
||||||
|
Vec::new(),
|
||||||
|
symbols
|
||||||
|
.iter()
|
||||||
|
.map(|symbol| IntradayExecutionQuote {
|
||||||
|
date,
|
||||||
|
symbol: (*symbol).to_string(),
|
||||||
|
timestamp: date.and_hms_opt(10, 15, 0).expect("timestamp"),
|
||||||
|
last_price: 10.0,
|
||||||
|
bid1: 9.99,
|
||||||
|
ask1: 10.01,
|
||||||
|
bid1_volume: 2_000,
|
||||||
|
ask1_volume: 2_000,
|
||||||
|
volume_delta: 10_000,
|
||||||
|
amount_delta: 100_000.0,
|
||||||
|
trading_phase: Some("continuous".to_string()),
|
||||||
|
})
|
||||||
|
.collect(),
|
||||||
|
)
|
||||||
|
.expect("dataset");
|
||||||
|
|
||||||
|
let mut portfolio = PortfolioState::new(10_000.0);
|
||||||
|
portfolio
|
||||||
|
.position_mut("000001.SZ")
|
||||||
|
.buy(prev_date, 1_000, 8.0);
|
||||||
|
let subscriptions = BTreeSet::new();
|
||||||
|
let ctx = StrategyContext {
|
||||||
|
execution_date: date,
|
||||||
|
decision_date: date,
|
||||||
|
decision_index: 2,
|
||||||
|
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.signal_symbol = "000001.SZ".to_string();
|
||||||
|
cfg.refresh_rate = 99;
|
||||||
|
cfg.max_positions = 2;
|
||||||
|
cfg.benchmark_short_ma_days = 1;
|
||||||
|
cfg.benchmark_long_ma_days = 1;
|
||||||
|
cfg.market_cap_lower_expr = "0".to_string();
|
||||||
|
cfg.market_cap_upper_expr = "1000".to_string();
|
||||||
|
cfg.selection_limit_expr = "2".to_string();
|
||||||
|
cfg.stock_filter_expr = "close > 0".to_string();
|
||||||
|
cfg.exposure_expr = "0.5".to_string();
|
||||||
|
cfg.stop_loss_expr.clear();
|
||||||
|
cfg.take_profit_expr = "1.1".to_string();
|
||||||
|
cfg.daily_top_up_enabled = true;
|
||||||
|
cfg.aiquant_transaction_cost = true;
|
||||||
|
cfg.intraday_execution_time = Some(NaiveTime::from_hms_opt(10, 15, 0).unwrap());
|
||||||
|
let mut strategy = PlatformExprStrategy::new(cfg);
|
||||||
|
strategy.rebalance_day_counter = 2;
|
||||||
|
|
||||||
|
let decision = strategy.on_day(&ctx).expect("platform decision");
|
||||||
|
|
||||||
|
let target_adjust_index = decision
|
||||||
|
.order_intents
|
||||||
|
.iter()
|
||||||
|
.position(|intent| {
|
||||||
|
matches!(
|
||||||
|
intent,
|
||||||
|
OrderIntent::Shares {
|
||||||
|
symbol,
|
||||||
|
quantity,
|
||||||
|
reason,
|
||||||
|
} if symbol == "000001.SZ"
|
||||||
|
&& reason == "daily_position_target_adjust"
|
||||||
|
&& *quantity < 0
|
||||||
|
)
|
||||||
|
})
|
||||||
|
.expect("target sell-down before take-profit");
|
||||||
|
let take_profit_index = decision
|
||||||
|
.order_intents
|
||||||
|
.iter()
|
||||||
|
.position(|intent| {
|
||||||
|
matches!(
|
||||||
|
intent,
|
||||||
|
OrderIntent::TargetValue {
|
||||||
|
symbol,
|
||||||
|
target_value,
|
||||||
|
reason,
|
||||||
|
} if symbol == "000001.SZ"
|
||||||
|
&& *target_value == 0.0
|
||||||
|
&& reason == "take_profit_exit"
|
||||||
|
)
|
||||||
|
})
|
||||||
|
.expect("take-profit exit");
|
||||||
|
|
||||||
|
assert!(
|
||||||
|
target_adjust_index < take_profit_index,
|
||||||
|
"{:?}",
|
||||||
|
decision.order_intents
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn platform_aiquant_allows_target_adjust_when_stop_loss_sell_is_lower_limit_blocked() {
|
fn platform_aiquant_allows_target_adjust_when_stop_loss_sell_is_lower_limit_blocked() {
|
||||||
let prev_date = d(2025, 1, 10);
|
let prev_date = d(2025, 1, 10);
|
||||||
@@ -20487,7 +20678,7 @@ mod tests {
|
|||||||
let prev_date = d(2025, 2, 2);
|
let prev_date = d(2025, 2, 2);
|
||||||
let date = d(2025, 2, 3);
|
let date = d(2025, 2, 3);
|
||||||
let symbols = ["000001.SZ", "000002.SZ"];
|
let symbols = ["000001.SZ", "000002.SZ"];
|
||||||
let data = DataSet::from_components(
|
let data = DataSet::from_components_with_actions_and_quotes(
|
||||||
symbols
|
symbols
|
||||||
.iter()
|
.iter()
|
||||||
.map(|symbol| Instrument {
|
.map(|symbol| Instrument {
|
||||||
@@ -20564,6 +20755,23 @@ mod tests {
|
|||||||
prev_close: 1000.0,
|
prev_close: 1000.0,
|
||||||
volume: 1_000_000,
|
volume: 1_000_000,
|
||||||
}],
|
}],
|
||||||
|
Vec::new(),
|
||||||
|
symbols
|
||||||
|
.iter()
|
||||||
|
.map(|symbol| IntradayExecutionQuote {
|
||||||
|
date,
|
||||||
|
symbol: (*symbol).to_string(),
|
||||||
|
timestamp: date.and_hms_opt(10, 40, 0).expect("timestamp"),
|
||||||
|
last_price: 10.0,
|
||||||
|
bid1: 9.99,
|
||||||
|
ask1: 10.01,
|
||||||
|
bid1_volume: 2_000,
|
||||||
|
ask1_volume: 2_000,
|
||||||
|
volume_delta: 10_000,
|
||||||
|
amount_delta: 100_000.0,
|
||||||
|
trading_phase: Some("continuous".to_string()),
|
||||||
|
})
|
||||||
|
.collect(),
|
||||||
)
|
)
|
||||||
.expect("dataset");
|
.expect("dataset");
|
||||||
|
|
||||||
@@ -20603,6 +20811,7 @@ mod tests {
|
|||||||
cfg.take_profit_expr.clear();
|
cfg.take_profit_expr.clear();
|
||||||
cfg.daily_top_up_enabled = true;
|
cfg.daily_top_up_enabled = true;
|
||||||
cfg.aiquant_transaction_cost = true;
|
cfg.aiquant_transaction_cost = true;
|
||||||
|
cfg.intraday_execution_time = Some(NaiveTime::from_hms_opt(10, 40, 0).unwrap());
|
||||||
let mut strategy = PlatformExprStrategy::new(cfg);
|
let mut strategy = PlatformExprStrategy::new(cfg);
|
||||||
strategy.rebalance_day_counter = 2;
|
strategy.rebalance_day_counter = 2;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user