Refine jq microcap execution alignment
This commit is contained in:
@@ -634,15 +634,40 @@ impl JqMicroCapStrategy {
|
||||
if !sizing_price.is_finite() || sizing_price <= 0.0 {
|
||||
return 0;
|
||||
}
|
||||
let requested_qty = self.round_lot_quantity(
|
||||
let snapshot_requested_qty = self.round_lot_quantity(
|
||||
((projected.cash().min(order_value)) / sizing_price).floor() as u32,
|
||||
round_lot,
|
||||
);
|
||||
if requested_qty == 0 {
|
||||
let projected_execution_price = self.projected_execution_price(market, OrderSide::Buy);
|
||||
let mut projected_fill = self.projected_select_execution_fill(
|
||||
ctx,
|
||||
date,
|
||||
symbol,
|
||||
OrderSide::Buy,
|
||||
u32::MAX,
|
||||
round_lot,
|
||||
Some(projected.cash()),
|
||||
Some(order_value + 400.0),
|
||||
execution_state,
|
||||
);
|
||||
let mut quantity = snapshot_requested_qty;
|
||||
while quantity > 0 {
|
||||
let gross_amount = projected_execution_price * quantity as f64;
|
||||
let cash_out = gross_amount + self.buy_commission(gross_amount);
|
||||
if gross_amount <= order_value + 400.0
|
||||
&& cash_out <= projected.cash() + 1e-6
|
||||
{
|
||||
break;
|
||||
}
|
||||
quantity = quantity.saturating_sub(round_lot);
|
||||
}
|
||||
if quantity == 0 {
|
||||
return 0;
|
||||
}
|
||||
let execution_price = self.projected_execution_price(market, OrderSide::Buy);
|
||||
let mut quantity = requested_qty;
|
||||
let execution_price = projected_fill
|
||||
.as_ref()
|
||||
.map(|fill| fill.price)
|
||||
.unwrap_or(projected_execution_price);
|
||||
while quantity > 0 {
|
||||
let gross_amount = execution_price * quantity as f64;
|
||||
let cash_out = gross_amount + self.buy_commission(gross_amount);
|
||||
@@ -695,11 +720,25 @@ impl JqMicroCapStrategy {
|
||||
return None;
|
||||
}
|
||||
let market = ctx.data.market(date, symbol)?;
|
||||
let fill = ProjectedExecutionFill {
|
||||
price: self.projected_execution_price(market, OrderSide::Sell),
|
||||
quantity,
|
||||
next_cursor: date.and_time(self.intraday_execution_start_time()) + Duration::seconds(1),
|
||||
};
|
||||
let round_lot = self.projected_round_lot(ctx, symbol);
|
||||
let fill = self
|
||||
.projected_select_execution_fill(
|
||||
ctx,
|
||||
date,
|
||||
symbol,
|
||||
OrderSide::Sell,
|
||||
quantity,
|
||||
round_lot,
|
||||
None,
|
||||
None,
|
||||
execution_state,
|
||||
)
|
||||
.unwrap_or(ProjectedExecutionFill {
|
||||
price: self.projected_execution_price(market, OrderSide::Sell),
|
||||
quantity,
|
||||
next_cursor: date.and_time(self.intraday_execution_start_time())
|
||||
+ Duration::seconds(1),
|
||||
});
|
||||
let gross_amount = fill.price * fill.quantity as f64;
|
||||
let net_cash = gross_amount - self.sell_cost(gross_amount);
|
||||
projected
|
||||
@@ -770,14 +809,8 @@ impl JqMicroCapStrategy {
|
||||
symbol: &str,
|
||||
execution_state: &ProjectedExecutionState,
|
||||
) -> Option<NaiveDateTime> {
|
||||
execution_state
|
||||
.execution_cursors
|
||||
.get(symbol)
|
||||
.copied()
|
||||
.into_iter()
|
||||
.chain(execution_state.global_execution_cursor)
|
||||
.chain(Some(date.and_time(self.intraday_execution_start_time())))
|
||||
.max()
|
||||
let _ = (symbol, execution_state);
|
||||
Some(date.and_time(self.intraday_execution_start_time()))
|
||||
}
|
||||
|
||||
fn projected_select_execution_fill(
|
||||
@@ -917,13 +950,8 @@ impl JqMicroCapStrategy {
|
||||
}
|
||||
|
||||
fn uses_serial_execution_cursor(&self, reason: &str) -> bool {
|
||||
matches!(
|
||||
reason,
|
||||
"stop_loss_exit"
|
||||
| "take_profit_exit"
|
||||
| "replacement_after_stop_loss_exit"
|
||||
| "replacement_after_take_profit_exit"
|
||||
)
|
||||
let _ = reason;
|
||||
false
|
||||
}
|
||||
|
||||
fn trading_ratio(
|
||||
|
||||
Reference in New Issue
Block a user