|
| 1 | +# Advanced Orders |
| 2 | + |
| 3 | +This guide covers the advanced order types available in FLOX, including conditional orders, OCO, and execution flags. |
| 4 | + |
| 5 | +## Order Types Overview |
| 6 | + |
| 7 | +| Type | Use Case | |
| 8 | +|------|----------| |
| 9 | +| `STOP_MARKET` | Stop loss, breakout entry | |
| 10 | +| `STOP_LIMIT` | Stop with price control | |
| 11 | +| `TAKE_PROFIT_MARKET` | Lock in profits | |
| 12 | +| `TAKE_PROFIT_LIMIT` | Lock in profits with price control | |
| 13 | +| `TRAILING_STOP` | Dynamic stop that follows price | |
| 14 | +| OCO | One-Cancels-Other for breakouts | |
| 15 | + |
| 16 | +## Using Signal Factory Methods |
| 17 | + |
| 18 | +### Stop Orders |
| 19 | + |
| 20 | +```cpp |
| 21 | +// Stop market - triggers market order when price crosses trigger |
| 22 | +emitStopMarket(symbol, Side::SELL, Price::fromDouble(95.0), qty); |
| 23 | + |
| 24 | +// Stop limit - triggers limit order when price crosses trigger |
| 25 | +emitStopLimit(symbol, Side::SELL, |
| 26 | + Price::fromDouble(95.0), // trigger |
| 27 | + Price::fromDouble(94.5), // limit price |
| 28 | + qty); |
| 29 | +``` |
| 30 | +
|
| 31 | +**Stop trigger logic:** |
| 32 | +- SELL stop: triggers when price <= triggerPrice (falling) |
| 33 | +- BUY stop: triggers when price >= triggerPrice (rising) |
| 34 | +
|
| 35 | +### Take Profit Orders |
| 36 | +
|
| 37 | +```cpp |
| 38 | +// Take profit market - exits position at profit target |
| 39 | +emitTakeProfitMarket(symbol, Side::SELL, Price::fromDouble(110.0), qty); |
| 40 | +
|
| 41 | +// Take profit limit |
| 42 | +emitTakeProfitLimit(symbol, Side::SELL, |
| 43 | + Price::fromDouble(110.0), // trigger |
| 44 | + Price::fromDouble(109.5), // limit price |
| 45 | + qty); |
| 46 | +``` |
| 47 | + |
| 48 | +**Take profit trigger logic:** |
| 49 | +- SELL TP: triggers when price >= triggerPrice (rising, lock profit on long) |
| 50 | +- BUY TP: triggers when price <= triggerPrice (falling, lock profit on short) |
| 51 | + |
| 52 | +### Trailing Stop |
| 53 | + |
| 54 | +```cpp |
| 55 | +// Fixed offset trailing stop (follows price by 5.0) |
| 56 | +emitTrailingStop(symbol, Side::SELL, Price::fromDouble(5.0), qty); |
| 57 | + |
| 58 | +// Percentage trailing stop (follows price by 2%) |
| 59 | +emitTrailingStopPercent(symbol, Side::SELL, 200, qty); // 200 bps = 2% |
| 60 | +``` |
| 61 | +
|
| 62 | +**Trailing stop behavior:** |
| 63 | +- SELL trailing: trigger follows price UP (never down) |
| 64 | +- When price drops to trigger -> order executes |
| 65 | +
|
| 66 | +## Time-In-Force Options |
| 67 | +
|
| 68 | +```cpp |
| 69 | +// IOC (Immediate-Or-Cancel) - fill immediately or cancel |
| 70 | +emitLimitBuy(symbol, price, qty, TimeInForce::IOC); |
| 71 | +
|
| 72 | +// FOK (Fill-Or-Kill) - fill completely or reject |
| 73 | +emitLimitBuy(symbol, price, qty, TimeInForce::FOK); |
| 74 | +
|
| 75 | +// POST_ONLY - maker only, reject if would take |
| 76 | +emitLimitBuy(symbol, price, qty, TimeInForce::POST_ONLY); |
| 77 | +``` |
| 78 | + |
| 79 | +## Execution Flags |
| 80 | + |
| 81 | +### Using Signal Modifiers |
| 82 | + |
| 83 | +```cpp |
| 84 | +auto signal = Signal::limitBuy(symbol, price, qty, orderId) |
| 85 | + .withTimeInForce(TimeInForce::IOC) |
| 86 | + .withReduceOnly() |
| 87 | + .withPostOnly(); |
| 88 | +emit(signal); |
| 89 | +``` |
| 90 | +
|
| 91 | +### Close Position |
| 92 | +
|
| 93 | +```cpp |
| 94 | +// Close entire position with reduce-only market order |
| 95 | +emitClosePosition(symbol); |
| 96 | +``` |
| 97 | + |
| 98 | +## Checking Exchange Capabilities |
| 99 | + |
| 100 | +Before using advanced features, check if they're supported: |
| 101 | + |
| 102 | +```cpp |
| 103 | +void MyStrategy::onStart() { |
| 104 | + auto caps = engine().executor().capabilities(); |
| 105 | + |
| 106 | + _useTrailingStop = caps.supports(OrderType::TRAILING_STOP); |
| 107 | + _useOCO = caps.supportsOCO; |
| 108 | + |
| 109 | + if (!_useTrailingStop) { |
| 110 | + log().warn("Trailing stop not supported, using manual logic"); |
| 111 | + } |
| 112 | +} |
| 113 | +``` |
| 114 | + |
| 115 | +## Example: Manual TP/SL Management |
| 116 | + |
| 117 | +Since strategies have full control over order lifecycle, you can implement custom TP/SL logic: |
| 118 | + |
| 119 | +```cpp |
| 120 | +void onOrderFilled(const Order& entryOrder) { |
| 121 | + if (entryOrder.id == _entryOrderId) { |
| 122 | + Price entryPrice = entryOrder.price; |
| 123 | + |
| 124 | + // Place TP |
| 125 | + _tpOrderId = emitTakeProfitMarket( |
| 126 | + entryOrder.symbol, Side::SELL, |
| 127 | + Price::fromDouble(entryPrice.toDouble() * 1.06), // 6% profit |
| 128 | + entryOrder.quantity); |
| 129 | + |
| 130 | + // Place SL |
| 131 | + _slOrderId = emitStopMarket( |
| 132 | + entryOrder.symbol, Side::SELL, |
| 133 | + Price::fromDouble(entryPrice.toDouble() * 0.98), // 2% stop |
| 134 | + entryOrder.quantity); |
| 135 | + } |
| 136 | + |
| 137 | + // When TP fills, cancel SL |
| 138 | + if (entryOrder.id == _tpOrderId) { |
| 139 | + emitCancel(_slOrderId); |
| 140 | + } |
| 141 | + |
| 142 | + // When SL fills, cancel TP |
| 143 | + if (entryOrder.id == _slOrderId) { |
| 144 | + emitCancel(_tpOrderId); |
| 145 | + } |
| 146 | +} |
| 147 | +``` |
| 148 | +
|
| 149 | +## Example: Trailing Stop for Profit Protection |
| 150 | +
|
| 151 | +```cpp |
| 152 | +void onOrderFilled(const Order& order) { |
| 153 | + if (order.side == Side::BUY) { |
| 154 | + // Place trailing stop to protect profits |
| 155 | + emitTrailingStopPercent(order.symbol, Side::SELL, 300, order.quantity); |
| 156 | + } |
| 157 | +} |
| 158 | +``` |
| 159 | + |
| 160 | +## See Also |
| 161 | + |
| 162 | +* [Order Types](../reference/api/common.md#ordertype) |
| 163 | +* [Time-In-Force](../reference/api/common.md#timeinforce) |
| 164 | +* [Order Structure](../reference/api/execution/order.md) |
| 165 | +* [Exchange Capabilities](../reference/api/execution/exchange_capabilities.md) |
0 commit comments