nt-execution 1.0.0

Order execution and broker integration for Neural Trader - supports Alpaca, Interactive Brokers, and more
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
// IBKR Integration Tests
//
// These tests require a running TWS/Gateway instance in paper trading mode
// Run with: cargo test --test ibkr_integration_tests -- --ignored

use nt_execution::ibkr_broker::*;
use nt_execution::*;
use rust_decimal::Decimal;
use std::time::Duration;

#[tokio::test]
#[ignore] // Requires TWS/Gateway running
async fn test_ibkr_connection() {
    let _config = IBKRConfig {
        host: "127.0.0.1".to_string(),
        port: 7497,
        paper_trading: true,
        ..Default::default()
    };

    let broker = IBKRBroker::new(config);
    let result = broker.connect().await;

    assert!(result.is_ok(), "Failed to connect to IBKR: {:?}", result.err());
}

#[tokio::test]
#[ignore]
async fn test_get_account() {
    let _config = IBKRConfig::default();
    let broker = IBKRBroker::new(config);
    broker.connect().await.unwrap();

    let account = broker.get_account().await;
    assert!(account.is_ok());

    let account = account.unwrap();
    assert!(!account.account_id.is_empty());
    assert!(account.buying_power > Decimal::ZERO);
}

#[tokio::test]
#[ignore]
async fn test_place_market_order() {
    let _config = IBKRConfig::default();
    let broker = IBKRBroker::new(config);
    broker.connect().await.unwrap();

    let order = OrderRequest {
        symbol: Symbol::new("AAPL").unwrap(),
        quantity: 1,
        side: OrderSide::Buy,
        order_type: OrderType::Market,
        time_in_force: TimeInForce::Day,
        limit_price: None,
        stop_price: None,
        extended_hours: false,
        client_order_id: None,
    };

    let response = broker.place_order(order).await;
    assert!(response.is_ok(), "Order placement failed: {:?}", response.err());

    let response = response.unwrap();
    assert!(!response.order_id.is_empty());
    assert_eq!(response.status, OrderStatus::Accepted);
}

#[tokio::test]
#[ignore]
async fn test_place_limit_order() {
    let _config = IBKRConfig::default();
    let broker = IBKRBroker::new(config);
    broker.connect().await.unwrap();

    let order = OrderRequest {
        symbol: Symbol::new("AAPL").unwrap(),
        quantity: 1,
        side: OrderSide::Buy,
        order_type: OrderType::Limit,
        time_in_force: TimeInForce::Day,
        limit_price: Some(Decimal::from(150)),
        stop_price: None,
        extended_hours: false,
        client_order_id: None,
    };

    let response = broker.place_order(order).await;
    assert!(response.is_ok());
}

#[tokio::test]
#[ignore]
async fn test_bracket_order() {
    let _config = IBKRConfig::default();
    let broker = IBKRBroker::new(config);
    broker.connect().await.unwrap();

    let bracket = BracketOrder {
        entry: OrderRequest {
            symbol: Symbol::new("AAPL").unwrap(),
            quantity: 1,
            side: OrderSide::Buy,
            order_type: OrderType::Limit,
            time_in_force: TimeInForce::Day,
            limit_price: Some(Decimal::from(150)),
            stop_price: None,
            extended_hours: false,
            client_order_id: None,
        },
        stop_loss: OrderRequest {
            symbol: Symbol::new("AAPL").unwrap(),
            quantity: 1,
            side: OrderSide::Sell,
            order_type: OrderType::StopLoss,
            time_in_force: TimeInForce::GTC,
            limit_price: None,
            stop_price: Some(Decimal::from(145)),
            extended_hours: false,
            client_order_id: None,
        },
        take_profit: OrderRequest {
            symbol: Symbol::new("AAPL").unwrap(),
            quantity: 1,
            side: OrderSide::Sell,
            order_type: OrderType::Limit,
            time_in_force: TimeInForce::GTC,
            limit_price: Some(Decimal::from(160)),
            stop_price: None,
            extended_hours: false,
            client_order_id: None,
        },
    };

    let responses = broker.place_bracket_order(bracket).await;
    assert!(responses.is_ok(), "Bracket order failed: {:?}", responses.err());

    let responses = responses.unwrap();
    assert!(!responses.is_empty());
}

#[tokio::test]
#[ignore]
async fn test_trailing_stop_percentage() {
    let _config = IBKRConfig::default();
    let broker = IBKRBroker::new(config);
    broker.connect().await.unwrap();

    let response = broker
        .place_trailing_stop(
            "AAPL",
            1,
            OrderSide::Sell,
            TrailingStop::Percentage(5.0),
        )
        .await;

    assert!(response.is_ok(), "Trailing stop failed: {:?}", response.err());
}

#[tokio::test]
#[ignore]
async fn test_trailing_stop_dollar() {
    let _config = IBKRConfig::default();
    let broker = IBKRBroker::new(config);
    broker.connect().await.unwrap();

    let response = broker
        .place_trailing_stop(
            "AAPL",
            1,
            OrderSide::Sell,
            TrailingStop::Dollar(Decimal::from(10)),
        )
        .await;

    assert!(response.is_ok());
}

#[tokio::test]
#[ignore]
async fn test_vwap_order() {
    let _config = IBKRConfig {
        algo_orders: true,
        ..Default::default()
    };
    let broker = IBKRBroker::new(config);
    broker.connect().await.unwrap();

    let response = broker
        .place_algo_order(
            "AAPL",
            100,
            OrderSide::Buy,
            AlgoStrategy::VWAP {
                start_time: "09:30:00".to_string(),
                end_time: "16:00:00".to_string(),
            },
        )
        .await;

    assert!(response.is_ok(), "VWAP order failed: {:?}", response.err());
}

#[tokio::test]
#[ignore]
async fn test_twap_order() {
    let _config = IBKRConfig {
        algo_orders: true,
        ..Default::default()
    };
    let broker = IBKRBroker::new(config);
    broker.connect().await.unwrap();

    let response = broker
        .place_algo_order(
            "AAPL",
            100,
            OrderSide::Buy,
            AlgoStrategy::TWAP {
                start_time: "09:30:00".to_string(),
                end_time: "16:00:00".to_string(),
            },
        )
        .await;

    assert!(response.is_ok());
}

#[tokio::test]
#[ignore]
async fn test_option_chain() {
    let _config = IBKRConfig {
        options_enabled: true,
        ..Default::default()
    };
    let broker = IBKRBroker::new(config);
    broker.connect().await.unwrap();

    let chain = broker.get_option_chain("AAPL").await;
    assert!(chain.is_ok(), "Option chain fetch failed: {:?}", chain.err());

    let chain = chain.unwrap();
    assert!(!chain.is_empty(), "Option chain is empty");
}

#[tokio::test]
#[ignore]
async fn test_option_greeks() {
    let _config = IBKRConfig {
        options_enabled: true,
        ..Default::default()
    };
    let broker = IBKRBroker::new(config);
    broker.connect().await.unwrap();

    let contract = OptionContract {
        underlying: "AAPL".to_string(),
        strike: Decimal::from(150),
        expiry: "20250117".to_string(),
        right: OptionRight::Call,
        multiplier: 100,
    };

    let greeks = broker.get_option_greeks(&contract).await;
    assert!(greeks.is_ok(), "Greeks calculation failed: {:?}", greeks.err());

    let greeks = greeks.unwrap();
    assert!(greeks.delta >= -1.0 && greeks.delta <= 1.0);
}

#[tokio::test]
#[ignore]
async fn test_place_option_order() {
    let _config = IBKRConfig {
        options_enabled: true,
        ..Default::default()
    };
    let broker = IBKRBroker::new(config);
    broker.connect().await.unwrap();

    let contract = OptionContract {
        underlying: "AAPL".to_string(),
        strike: Decimal::from(150),
        expiry: "20250117".to_string(),
        right: OptionRight::Call,
        multiplier: 100,
    };

    let response = broker
        .place_option_order(contract, 1, OrderSide::Buy, Some(Decimal::from(5)))
        .await;

    assert!(response.is_ok(), "Option order failed: {:?}", response.err());
}

#[tokio::test]
#[ignore]
async fn test_market_data_streaming() {
    let _config = IBKRConfig {
        streaming: true,
        ..Default::default()
    };
    let broker = IBKRBroker::new(config);
    broker.connect().await.unwrap();

    let symbols = vec!["AAPL".to_string(), "MSFT".to_string()];
    let result = broker.start_streaming(symbols).await;

    assert!(result.is_ok(), "Streaming start failed: {:?}", result.err());

    // Get receiver and wait for data
    if let Some(mut rx) = broker.market_data_stream() {
        tokio::select! {
            tick = rx.recv() => {
                assert!(tick.is_ok(), "Failed to receive tick data");
            }
            _ = tokio::time::sleep(Duration::from_secs(5)) => {
                // Timeout is acceptable for this test
            }
        }
    }
}

#[tokio::test]
#[ignore]
async fn test_level2_depth() {
    let _config = IBKRConfig {
        level2_depth: true,
        ..Default::default()
    };
    let broker = IBKRBroker::new(config);
    broker.connect().await.unwrap();

    let symbols = vec!["AAPL".to_string()];
    let result = broker.start_depth_streaming(symbols).await;

    assert!(result.is_ok(), "Depth streaming start failed: {:?}", result.err());
}

#[tokio::test]
#[ignore]
async fn test_historical_data() {
    let _config = IBKRConfig::default();
    let broker = IBKRBroker::new(config);
    broker.connect().await.unwrap();

    let bars = broker
        .get_historical_data("AAPL", "1d", "1min")
        .await;

    assert!(bars.is_ok(), "Historical data fetch failed: {:?}", bars.err());

    let bars = bars.unwrap();
    assert!(!bars.is_empty(), "No historical data returned");
}

#[tokio::test]
#[ignore]
async fn test_pre_trade_risk_check() {
    let _config = IBKRConfig::default();
    let broker = IBKRBroker::new(config);
    broker.connect().await.unwrap();

    let order = OrderRequest {
        symbol: Symbol::new("AAPL").unwrap(),
        quantity: 1,
        side: OrderSide::Buy,
        order_type: OrderType::Market,
        time_in_force: TimeInForce::Day,
        limit_price: None,
        stop_price: None,
        extended_hours: false,
        client_order_id: None,
    };

    let check = broker.pre_trade_risk_check(&order).await;
    assert!(check.is_ok(), "Risk check failed: {:?}", check.err());

    let check = check.unwrap();
    assert!(check.margin_required >= Decimal::ZERO);
}

#[tokio::test]
#[ignore]
async fn test_buying_power_calculation() {
    let _config = IBKRConfig::default();
    let broker = IBKRBroker::new(config);
    broker.connect().await.unwrap();

    let bp_stocks = broker.calculate_buying_power("STK").await;
    assert!(bp_stocks.is_ok());
    assert!(bp_stocks.unwrap() > Decimal::ZERO);

    let bp_options = broker.calculate_buying_power("OPT").await;
    assert!(bp_options.is_ok());
    assert!(bp_options.unwrap() > Decimal::ZERO);
}

#[tokio::test]
#[ignore]
async fn test_pattern_day_trader_check() {
    let _config = IBKRConfig::default();
    let broker = IBKRBroker::new(config);
    broker.connect().await.unwrap();

    let is_pdt = broker.is_pattern_day_trader().await;
    assert!(is_pdt.is_ok());
}

#[tokio::test]
#[ignore]
async fn test_get_positions() {
    let _config = IBKRConfig::default();
    let broker = IBKRBroker::new(config);
    broker.connect().await.unwrap();

    let positions = broker.get_positions().await;
    assert!(positions.is_ok(), "Get positions failed: {:?}", positions.err());
}

#[tokio::test]
#[ignore]
async fn test_cancel_order() {
    let _config = IBKRConfig::default();
    let broker = IBKRBroker::new(config);
    broker.connect().await.unwrap();

    // Place an order first
    let order = OrderRequest {
        symbol: Symbol::new("AAPL").unwrap(),
        quantity: 1,
        side: OrderSide::Buy,
        order_type: OrderType::Limit,
        time_in_force: TimeInForce::Day,
        limit_price: Some(Decimal::from(1)), // Very low price, won't fill
        stop_price: None,
        extended_hours: false,
        client_order_id: None,
    };

    let response = broker.place_order(order).await.unwrap();

    // Wait a bit for order to be accepted
    tokio::time::sleep(Duration::from_millis(500)).await;

    // Cancel the order
    let cancel_result = broker.cancel_order(&response.order_id).await;
    assert!(cancel_result.is_ok(), "Cancel order failed: {:?}", cancel_result.err());
}

#[tokio::test]
#[ignore]
async fn test_list_orders() {
    let _config = IBKRConfig::default();
    let broker = IBKRBroker::new(config);
    broker.connect().await.unwrap();

    let orders = broker.list_orders(OrderFilter::All).await;
    assert!(orders.is_ok(), "List orders failed: {:?}", orders.err());
}

#[tokio::test]
#[ignore]
async fn test_health_check() {
    let _config = IBKRConfig::default();
    let broker = IBKRBroker::new(config);

    // Before connection
    let health = broker.health_check().await.unwrap();
    assert_eq!(health, HealthStatus::Unhealthy);

    // After connection
    broker.connect().await.unwrap();
    let health = broker.health_check().await.unwrap();
    assert_eq!(health, HealthStatus::Healthy);
}

// Stress tests
#[tokio::test]
#[ignore]
async fn test_concurrent_orders() {
    let _config = IBKRConfig::default();
    let broker = IBKRBroker::new(config);
    broker.connect().await.unwrap();

    let mut handles = vec![];

    for i in 0..10 {
        let broker_clone = broker.clone();
        let handle = tokio::spawn(async move {
            let order = OrderRequest {
                symbol: Symbol::new("AAPL").unwrap(),
                quantity: 1,
                side: OrderSide::Buy,
                order_type: OrderType::Limit,
                time_in_force: TimeInForce::Day,
                limit_price: Some(Decimal::from(100 + i)),
                stop_price: None,
                extended_hours: false,
                client_order_id: None,
            };

            broker_clone.place_order(order).await
        });
        handles.push(handle);
    }

    let results = futures::future::join_all(handles).await;
    let successes = results.iter().filter(|r| r.is_ok()).count();

    assert!(successes >= 8, "Too many concurrent order failures: {}/{}", successes, results.len());
}

#[tokio::test]
#[ignore]
async fn test_rate_limiting() {
    let _config = IBKRConfig::default();
    let broker = IBKRBroker::new(config);
    broker.connect().await.unwrap();

    // Try to exceed rate limit (50 req/s)
    let start = std::time::Instant::now();

    for _ in 0..100 {
        let _ = broker.get_account().await;
    }

    let elapsed = start.elapsed();

    // Should take at least 2 seconds due to rate limiting
    assert!(elapsed.as_secs() >= 2, "Rate limiting not working: {:?}", elapsed);
}