use ibapi::orders::conditions::TriggerMethod;
use ibapi::orders::{
order_builder, Action, ExecutionCondition, MarginCondition, OrderCondition, PercentChangeCondition, PriceCondition, TimeCondition,
VolumeCondition,
};
fn main() {
println!("=== Conditional Orders Example ===\n");
println!("1. PRICE CONDITION");
println!(" Scenario: Buy MSFT when price exceeds $350");
let price_condition = PriceCondition::builder(
272093, "SMART", )
.greater_than(350.0) .trigger_method(TriggerMethod::Last) .build();
let mut order = order_builder::market_order(Action::Buy, 100.0);
order.conditions = vec![OrderCondition::Price(price_condition)];
println!(" Order: Buy 100 shares when MSFT last price > $350.00");
println!(" Condition: Price > 350.0, method=Last, exchange=SMART\n");
println!("2. TIME CONDITION");
println!(" Scenario: Submit order after 2:30 PM ET");
let time_condition = TimeCondition::builder().greater_than("20251230 14:30:00 US/Eastern").build();
let mut order = order_builder::limit_order(Action::Sell, 50.0, 155.0);
order.conditions = vec![OrderCondition::Time(time_condition)];
println!(" Order: Sell 50 shares at limit $155.00");
println!(" Condition: Activate after 2:30 PM ET on Dec 30, 2025\n");
println!("3. MARGIN CONDITION");
println!(" Scenario: Close position if margin cushion drops below 30%");
let margin_condition = MarginCondition::builder()
.less_than(30) .build();
let mut order = order_builder::market_order(Action::Sell, 200.0);
order.conditions = vec![OrderCondition::Margin(margin_condition)];
order.tif = ibapi::orders::TimeInForce::GoodTilCanceled;
println!(" Order: Sell 200 shares at market");
println!(" Condition: Margin cushion < 30%");
println!(" Purpose: Risk management / margin call protection\n");
println!("4. EXECUTION CONDITION");
println!(" Scenario: Buy protective puts after buying stock");
let execution_condition = ExecutionCondition::builder(
"TSLA", "STK", "SMART", )
.build();
let mut order = order_builder::limit_order(Action::Buy, 1.0, 5.50);
order.conditions = vec![OrderCondition::Execution(execution_condition)];
println!(" Order: Buy 1 put option contract at $5.50");
println!(" Condition: Triggers when any TSLA stock order executes");
println!(" Purpose: Automatic hedge/protection\n");
println!("5. VOLUME CONDITION");
println!(" Scenario: Trade after volume indicates sufficient liquidity");
let volume_condition = VolumeCondition::builder(
76792991, "SMART", )
.greater_than(50_000_000) .build();
let mut order = order_builder::limit_order(Action::Buy, 100.0, 245.0);
order.conditions = vec![OrderCondition::Volume(volume_condition)];
println!(" Order: Buy 100 shares at limit $245.00");
println!(" Condition: TSLA volume > 50,000,000 shares");
println!(" Purpose: Ensure liquidity before trading\n");
println!("6. PERCENT CHANGE CONDITION");
println!(" Scenario: Momentum trade when SPY moves significantly");
let percent_change_condition = PercentChangeCondition::builder(
756733, "SMART", )
.greater_than(2.0) .build();
let mut order = order_builder::limit_order(Action::Buy, 50.0, 452.0);
order.conditions = vec![OrderCondition::PercentChange(percent_change_condition)];
println!(" Order: Buy 50 shares at limit $452.00");
println!(" Condition: SPY price change > 2% from open");
println!(" Purpose: Momentum/breakout trading\n");
println!("7. COMPLEX SCENARIO - AND CONDITIONS");
println!(" Scenario: Trade only when multiple conditions align");
let price_cond = PriceCondition::builder(265598, "SMART")
.greater_than(150.0)
.conjunction(true) .build();
let volume_cond = VolumeCondition::builder(265598, "SMART")
.greater_than(80_000_000)
.conjunction(true) .build();
let time_cond = TimeCondition::builder()
.greater_than("20251230 10:00:00 US/Eastern")
.conjunction(true) .build();
let mut order = order_builder::limit_order(Action::Buy, 100.0, 151.0);
order.conditions = vec![
OrderCondition::Price(price_cond),
OrderCondition::Volume(volume_cond),
OrderCondition::Time(time_cond),
];
order.conditions_ignore_rth = false;
println!(" Order: Buy 100 shares at limit $151.00");
println!(" Conditions (ALL must be true):");
println!(" - AAPL price > $150.00");
println!(" - AAPL volume > 80,000,000");
println!(" - Time after 10:00 AM ET");
println!(" Purpose: High-confidence trade setup\n");
println!("8. COMPLEX SCENARIO - OR CONDITIONS");
println!(" Scenario: Close position if any risk threshold is breached");
let margin_cond = MarginCondition::builder()
.less_than(25)
.conjunction(false) .build();
let price_cond = PriceCondition::builder(265598, "SMART")
.less_than(140.0)
.conjunction(false) .build();
let time_cond = TimeCondition::builder()
.greater_than("20251230 15:55:00 US/Eastern")
.conjunction(false) .build();
let mut order = order_builder::market_order(Action::Sell, 100.0);
order.conditions = vec![
OrderCondition::Margin(margin_cond),
OrderCondition::Price(price_cond),
OrderCondition::Time(time_cond),
];
order.conditions_cancel_order = false; order.tif = ibapi::orders::TimeInForce::GoodTilCanceled;
println!(" Order: Sell 100 shares at market");
println!(" Conditions (ANY can trigger):");
println!(" - Margin cushion < 25%");
println!(" - AAPL price < $140.00 (stop loss)");
println!(" - After 3:55 PM ET (end of day exit)");
println!(" Purpose: Risk management with multiple exit triggers\n");
println!("9. CANCEL ORDER ON CONDITION");
println!(" Scenario: Cancel order if not filled by specific time");
let time_cond = TimeCondition::builder().greater_than("20251230 15:30:00 US/Eastern").build();
let mut order = order_builder::limit_order(Action::Buy, 100.0, 149.0);
order.conditions = vec![OrderCondition::Time(time_cond)];
order.conditions_cancel_order = true; order.tif = ibapi::orders::TimeInForce::Day;
println!(" Order: Buy 100 shares at limit $149.00");
println!(" Condition: Cancel after 3:30 PM ET if not filled");
println!(" Purpose: Time-limited opportunity\n");
println!("10. REAL-WORLD STRATEGY - PAIRS TRADING");
println!(" Scenario: Trade spread when correlation breaks");
let leader_condition = PercentChangeCondition::builder(756733, "SMART")
.greater_than(1.5)
.conjunction(true)
.build();
let laggard_condition = PercentChangeCondition::builder(265598, "SMART").less_than(0.5).conjunction(true).build();
let mut order = order_builder::limit_order(Action::Buy, 100.0, 150.0);
order.conditions = vec![
OrderCondition::PercentChange(leader_condition),
OrderCondition::PercentChange(laggard_condition),
];
println!(" Order: Buy 100 shares of AAPL at limit $150.00");
println!(" Conditions:");
println!(" - SPY (leader) up > 1.5% from open");
println!(" - AAPL (laggard) up < 0.5% from open");
println!(" Strategy: Buy laggard expecting it to catch up to market\n");
println!("=== End of Examples ===");
println!();
println!("Key Points:");
println!("- Each condition has a trigger direction (above/below, after/before)");
println!("- Conditions can be chained with AND (conjunction=true) or OR (conjunction=false)");
println!("- Use conditions_cancel_order to cancel instead of activate");
println!("- Use conditions_ignore_rth to include after-hours monitoring");
println!("- Contract IDs must be obtained via contract_details() API");
println!();
println!("To actually place these orders:");
println!("1. Connect to TWS/Gateway: client.connect(...)");
println!("2. Get valid contract IDs: client.contract_details(...)");
println!("3. Get order ID: client.next_order_id()");
println!("4. Place order: client.place_order(order_id, &contract, &order)");
}