yatws 0.1.3

Yet Another TWS (Interactive Brokers TWS API) Implementation
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
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
// yatws/test_data_sub.rs
use anyhow::{Context, Result};
use log::{debug, error, info, warn};
use std::time::Duration;
use chrono::Utc;
use yatws::{
  IBKRError,
  IBKRClient,
  contract::{Contract, WhatToShow, BarSize, SecType},
  data::{MarketDataType, DurationUnit, TickByTickRequestType},
  data_subscription::{
    HistoricalDataEvent,
    MarketDataIterator,
    MarketDataSubscription,
    TickDataEvent,
    TickByTickEvent,
    MarketDepthEvent
  },
};

pub(super) fn subscribe_market_data_impl(client: &IBKRClient, is_live: bool) -> Result<()> {
  info!("--- Testing Subscribe Market Data (TickDataSubscription) ---");
  let data_mgr = client.data_market();
  let contract = Contract::stock("GOOG");

  info!("Building TickDataSubscription for {}...", contract.symbol);
  let subscription = data_mgr.subscribe_market_data(&contract)
    .with_snapshot(false) // Streaming
    .with_market_data_type(MarketDataType::Delayed)
    .submit()
    .context("Failed to submit TickDataSubscription")?;

  info!("Subscription submitted with ReqID: {}. Iterating events...", subscription.request_id());

  let mut event_count = 0;
  let max_events_or_duration = if is_live { 10 } else { 2 }; // Fewer events for replay, or rely on timeout
  let iteration_timeout = if is_live { Duration::from_secs(2) } else { Duration::from_millis(100) };
  let total_wait_duration = if is_live { Duration::from_secs(15) } else { Duration::from_secs(1) };
  let start_time = std::time::Instant::now();

  let mut iter = subscription.events(); // Default timeout is None (blocking)

  while start_time.elapsed() < total_wait_duration && event_count < max_events_or_duration {
    match iter.try_next(iteration_timeout) { // Use try_next with a timeout
      Some(event) => {
        info!("Received TickDataEvent: {:?}", event);
        event_count += 1;
        if let TickDataEvent::Error(e) = event {
          error!("Error event received in subscription: {:?}", e);
          subscription.cancel().ok(); // Best effort cancel
          return Err(e.into());
        }
      }
      None => { // Timeout
        if !is_live && subscription.is_completed() && !subscription.has_error() {
          info!("Subscription completed in replay with no error and no more events.");
          break;
        }
        if subscription.has_error() {
          let err = subscription.get_error().unwrap_or_else(|| IBKRError::InternalError("Unknown error in subscription".to_string()));
          error!("Subscription has error: {:?}", err);
          subscription.cancel().ok();
          return Err(err.into());
        }
        if subscription.is_completed() {
          info!("Subscription completed with no more events.");
          break;
        }
        debug!("No event in last {:?}, continuing iteration...", iteration_timeout);
      }
    }
  }

  info!("Finished iterating events. Total events received: {}. Cancelling subscription...", event_count);
  subscription.cancel().context("Failed to cancel TickDataSubscription")?;
  info!("TickDataSubscription cancelled.");

  if event_count == 0 && is_live {
    warn!("Received 0 events for GOOG. This might be okay if market is closed or no data ticks during the window.");
  }
  Ok(())
}

pub(super) fn subscribe_historical_keep_up_to_date_impl(client: &IBKRClient, _is_live: bool) -> Result<()> {
  info!("--- Testing Subscribe Historical Data with keep_up_to_date ---");
  let symbol = "GOOG";
  let seconds_ago = 180;

  let contract = Contract::stock(symbol);
  let bars_event = client
    .data_market()
    .subscribe_historical_data(
      &contract,
      yatws::data::DurationUnit::Second(seconds_ago),
      yatws::contract::BarSize::FifteenSeconds,
      yatws::contract::WhatToShow::Trades,
    )
    .with_use_rth(true)
    .with_keep_up_to_date(true)
    .with_market_data_type(MarketDataType::RealTime)
    .submit()?;

  {
    let mut completed_bars = false;
    let mut bars = bars_event.events();
    // The udpates will come at 5 second intervals. We are asking for 15 seconds bars.
    // So, if we ask for 16 bars, we will get:
    //  - a complete signal to show that we are done with the past
    //  - at most 3 updates for the current bar, and
    //  - at least 12 updates in the future.
    // This is only true if the stock actually trades in all bars, otherwise the bar may not
    // close. There is no marker for a complete bar, you have to figure it out when the time changes.
    const NUM_BAR_UPDATES: usize = 16;
    const MIN_NUM_FUTURE_BAR_UPDATES: usize = 12;
    let mut bar_updates = 0;
    let mut future_bar_updates = 0;
    let mut num_complete_signals = 0;
    let now = Utc::now();
    while bar_updates < NUM_BAR_UPDATES {
      match bars.next() {
        Some(evt) => match evt {
          HistoricalDataEvent::Bar(bar) => {
            assert!(!completed_bars, "Got a historical bar after complete");
            log::info!("Got history bar: {:?}", bar);
          }
          HistoricalDataEvent::UpdateBar(bar) => {
            assert!(completed_bars);
            log::info!("Future bar: {:?}", bar);
            bar_updates += 1;
            if bar.time < now {
              // We are completing the first bar.
              assert!(num_complete_signals == 1, "Bar time in the past but we have received a Complete: bar.time = {}", bar.time);
            } else {
              future_bar_updates += 1;
            }
          }
          HistoricalDataEvent::Complete { .. } => {
            if completed_bars {
              log::error!("Something is wrong: multiple Complete signals");
            }
            completed_bars = true;
            num_complete_signals += 1;
            log::info!("History complete, further updates will be current bars");
          }
          HistoricalDataEvent::Error(e) => {
            log::info!("Error: {}", e);
          }
        },
        None => {
          log::info!("Ignoring timeout");
        }
      }
    }

    let mut err = String::new();
    if future_bar_updates < MIN_NUM_FUTURE_BAR_UPDATES {
      err.push_str(&format!("Not enough future updates: expected {}, got: {}",
                            MIN_NUM_FUTURE_BAR_UPDATES, future_bar_updates));
    }
    if num_complete_signals > 1 {
      if !err.is_empty() {
        err.push_str("\n");
      }
      err.push_str(&format!("Too many Completed events: {}", num_complete_signals));
    }
    if !err.is_empty() {
      return Err(anyhow::anyhow!(err));
    }
  }

  Ok(())
}

pub(super) fn subscribe_historical_combined_impl(client: &IBKRClient, is_live: bool) -> Result<()> {
  info!("--- Testing Combined Historical Data Subscriptions ---");
  let data_mgr = client.data_market();

  let contract1 = Contract::stock("IBM");
  let contract2 = Contract::stock("CSCO");

  let duration = DurationUnit::Day(1);
  let bar_size = BarSize::FiveMinutes;
  let what_to_show = WhatToShow::Trades;

  info!("Building HistoricalDataSubscription for {}...", contract1.symbol);
  let sub1 = data_mgr.subscribe_historical_data(&contract1, duration, bar_size, what_to_show)
    .with_market_data_type(MarketDataType::Delayed)
    .submit()
    .context(format!("Failed to submit HistoricalDataSubscription for {}", contract1.symbol))?;

  info!("Building HistoricalDataSubscription for {}...", contract2.symbol);
  let sub2 = data_mgr.subscribe_historical_data(&contract2, duration, bar_size, what_to_show)
    .with_market_data_type(MarketDataType::Delayed)
    .submit()
    .context(format!("Failed to submit HistoricalDataSubscription for {}", contract2.symbol))?;

  info!("Combining subscriptions for ReqID {} ({}) and ReqID {} ({})...",
        sub1.request_id(), contract1.symbol, sub2.request_id(), contract2.symbol);

  let mut multi_iter = data_mgr.combine_subscriptions::<HistoricalDataEvent>()
    .add(&sub1, sub1.events())
    .add(&sub2, sub2.events())
    .build();
  // .with_timeout(Duration::from_secs(1)); // Timeout for each try_next in the multi-iterator

  let mut event_count1 = 0;
  let mut event_count2 = 0;
  let mut completed1 = false;
  let mut completed2 = false;

  let iteration_timeout = if is_live { Duration::from_secs(5) } else { Duration::from_millis(200) }; // Timeout for each underlying iterator poll
  let total_wait_duration = if is_live { Duration::from_secs(30) } else { Duration::from_secs(2) };
  let start_time = std::time::Instant::now();

  info!("Iterating combined historical events (Total timeout: {:?}, Per-iterator poll: {:?})...", total_wait_duration, iteration_timeout);

  // The multi_iter.next() will internally use try_next on underlying iterators.
  // We need a loop that respects the total_wait_duration.
  // The default .next() on multi_iter can block if underlying iterators block.
  // Using try_next on multi_iter is better for controlled waiting.
  while start_time.elapsed() < total_wait_duration && !(completed1 && completed2) {
    match multi_iter.try_next(iteration_timeout) { // Poll underlying iterators with a timeout
      Some(tagged_event) => {
        info!("Combined Event: ReqID={}, Contract={}, Event={:?}",
              tagged_event.req_id, tagged_event.contract.symbol, tagged_event.event);
        if tagged_event.req_id == sub1.request_id() {
          event_count1 += 1;
          if let HistoricalDataEvent::Complete { .. } = tagged_event.event {
            completed1 = true;
            info!("Subscription for {} (ReqID {}) completed.", contract1.symbol, sub1.request_id());
          }
          if let HistoricalDataEvent::Error(e) = tagged_event.event {
            error!("Error from {} (ReqID {}): {:?}", contract1.symbol, sub1.request_id(), e);
            completed1 = true; // Treat error as completion for this sub
          }
        } else if tagged_event.req_id == sub2.request_id() {
          event_count2 += 1;
          if let HistoricalDataEvent::Complete { .. } = tagged_event.event {
            completed2 = true;
            info!("Subscription for {} (ReqID {}) completed.", contract2.symbol, sub2.request_id());
          }
          if let HistoricalDataEvent::Error(e) = tagged_event.event {
            error!("Error from {} (ReqID {}): {:?}", contract2.symbol, sub2.request_id(), e);
            completed2 = true; // Treat error as completion for this sub
          }
        }
      }
      None => { // Timeout from multi_iter.try_next
        debug!("No event from combined iterator in last poll. Checking completion status...");
        // Check if underlying subscriptions are done, even if no 'Complete' event was caught by this loop iteration
        if !completed1 && sub1.is_completed() {
          info!("Subscription for {} (ReqID {}) detected as completed (externally or error).", contract1.symbol, sub1.request_id());
          completed1 = true;
        }
        if !completed2 && sub2.is_completed() {
          info!("Subscription for {} (ReqID {}) detected as completed (externally or error).", contract2.symbol, sub2.request_id());
          completed2 = true;
        }
        if completed1 && completed2 {
          info!("Both subscriptions reported completion. Exiting loop.");
          break;
        }
      }
    }
  }


  info!("Finished iterating combined events.");
  info!("  {} (ReqID {}): {} events, Completed={}", contract1.symbol, sub1.request_id(), event_count1, completed1 || sub1.is_completed());
  info!("  {} (ReqID {}): {} events, Completed={}", contract2.symbol, sub2.request_id(), event_count2, completed2 || sub2.is_completed());

  // Explicitly cancel/drop subscriptions (though Drop should handle it)
  // sub1.cancel().ok();
  // sub2.cancel().ok();
  // multi_iter.cancel_all().ok(); // This relies on individual subscription drops

  if (event_count1 == 0 || event_count2 == 0) && is_live {
    warn!("Received 0 events for one or both contracts. This might be okay depending on market data availability.");
  }
  if (!completed1 && is_live && !sub1.has_error()) || (!completed2 && is_live && !sub2.has_error()) {
    warn!("One or both subscriptions did not explicitly complete within the test window.");
  }

  Ok(())
}

pub(super) fn subscribe_real_time_bars_impl(client: &IBKRClient, is_live: bool) -> Result<()> {
  info!("--- Testing Subscribe Real Time Bars ---");
  let data_mgr = client.data_market();
  let contract = Contract::stock("AAPL");
  let what_to_show = WhatToShow::Trades;

  info!("Building RealTimeBarSubscription for {}...", contract.symbol);
  let subscription = data_mgr.subscribe_real_time_bars(&contract, what_to_show)
    .with_use_rth(true)
    .submit()
    .context("Failed to submit RealTimeBarSubscription")?;

  info!("Subscription submitted with ReqID: {}. Iterating events...", subscription.request_id());

  let mut event_count = 0;
  let max_events = if is_live { 3 } else { 1 }; // 5-second bars, so fewer events needed
  let iteration_timeout = if is_live { Duration::from_secs(6) } else { Duration::from_millis(100) };
  let total_wait_duration = if is_live { Duration::from_secs(20) } else { Duration::from_secs(1) };
  let start_time = std::time::Instant::now();

  let mut iter = subscription.events();

  while start_time.elapsed() < total_wait_duration && event_count < max_events {
    match iter.try_next(iteration_timeout) {
      Some(bar) => {
        info!("Received Bar: Time={}, O={}, H={}, L={}, C={}, V={}",
              bar.time.format("%H:%M:%S"), bar.open, bar.high, bar.low, bar.close, bar.volume);
        event_count += 1;
      }
      None => {
        if subscription.is_completed() {
          info!("Subscription completed with no more events.");
          break;
        }
        if subscription.has_error() {
          let err = subscription.get_error().unwrap_or_else(||
                                                            IBKRError::InternalError("Unknown error in subscription".to_string()));
          error!("Subscription has error: {:?}", err);
          subscription.cancel().ok();
          return Err(err.into());
        }
        debug!("No event in last {:?}, continuing iteration...", iteration_timeout);
      }
    }
  }

  info!("Finished iterating events. Total events received: {}. Cancelling subscription...", event_count);
  subscription.cancel().context("Failed to cancel RealTimeBarSubscription")?;
  info!("RealTimeBarSubscription cancelled.");

  if event_count == 0 && is_live {
    warn!("Received 0 real-time bars for AAPL. This might be okay if market is closed.");
  }
  Ok(())
}

// 2. Test for subscribe_tick_by_tick
pub(super) fn subscribe_tick_by_tick_impl(client: &IBKRClient, is_live: bool) -> Result<()> {
  info!("--- Testing Subscribe Tick By Tick ---");
  let data_mgr = client.data_market();
  let contract = Contract::stock_with_exchange("MSFT", "SMART", "USD");
  let tick_type = TickByTickRequestType::BidAsk;

  info!("Building TickByTickSubscription for {}...", contract.symbol);
  let subscription = data_mgr.subscribe_tick_by_tick(&contract, tick_type)
    .with_number_of_ticks(0) // Streaming
    .with_market_data_type(MarketDataType::Delayed)
    .submit()
    .context("Failed to submit TickByTickSubscription")?;

  info!("Subscription submitted with ReqID: {}. Iterating events...", subscription.request_id());

  let mut event_count = 0;
  let max_events = if is_live { 10 } else { 2 };
  let iteration_timeout = if is_live { Duration::from_secs(2) } else { Duration::from_millis(100) };
  let total_wait_duration = if is_live { Duration::from_secs(15) } else { Duration::from_secs(1) };
  let start_time = std::time::Instant::now();

  let mut iter = subscription.events();

  while start_time.elapsed() < total_wait_duration && event_count < max_events {
    match iter.try_next(iteration_timeout) {
      Some(event) => {
        info!("Received TickByTickEvent: {:?}", event);
        event_count += 1;
        if let TickByTickEvent::Error(e) = event {
          error!("Error event received in subscription: {:?}", e);
          subscription.cancel().ok();
          return Err(e.into());
        }
      }
      None => {
        if subscription.is_completed() {
          info!("Subscription completed with no more events.");
          break;
        }
        if subscription.has_error() {
          let err = subscription.get_error().unwrap_or_else(||
                                                            IBKRError::InternalError("Unknown error in subscription".to_string()));
          error!("Subscription has error: {:?}", err);
          subscription.cancel().ok();
          return Err(err.into());
        }
        debug!("No event in last {:?}, continuing iteration...", iteration_timeout);
      }
    }
  }

  info!("Finished iterating events. Total events received: {}. Cancelling subscription...", event_count);
  subscription.cancel().context("Failed to cancel TickByTickSubscription")?;
  info!("TickByTickSubscription cancelled.");

  if event_count == 0 && is_live {
    warn!("Received 0 tick-by-tick events for MSFT. This might be okay if market is closed.");
  }
  Ok(())
}

// 3. Test for subscribe_market_depth
pub(super) fn subscribe_market_depth_impl(client: &IBKRClient, is_live: bool) -> Result<()> {
  info!("--- Testing Subscribe Market Depth ---");
  let data_mgr = client.data_market();
  // FX market depth is free.
  let contract = Contract {
    symbol: "EUR".to_string(),
    exchange: "IDEALPRO".to_string(),
    sec_type: SecType::Forex,
    currency: "GBP".to_string(),
    ..Default::default()
  };
  let num_rows = 5;

  info!("Building MarketDepthSubscription for {}...", contract.symbol);
  let subscription = data_mgr.subscribe_market_depth(&contract, num_rows)
    .with_smart_depth(false)
    .submit()
    .context("Failed to submit MarketDepthSubscription")?;

  info!("Subscription submitted with ReqID: {}. Iterating events...", subscription.request_id());

  let mut event_count = 0;
  let max_events = if is_live { 10 } else { 2 };
  let iteration_timeout = if is_live { Duration::from_secs(2) } else { Duration::from_millis(100) };
  let total_wait_duration = if is_live { Duration::from_secs(15) } else { Duration::from_secs(1) };
  let start_time = std::time::Instant::now();

  let mut iter = subscription.events();

  while start_time.elapsed() < total_wait_duration && event_count < max_events {
    match iter.try_next(iteration_timeout) {
      Some(event) => {
        info!("Received MarketDepthEvent: {:?}", event);
        event_count += 1;
        if let MarketDepthEvent::Error(e) = event {
          error!("Error event received in subscription: {:?}", e);
          subscription.cancel().ok();
          return Err(e.into());
        }
      }
      None => {
        if subscription.is_completed() {
          info!("Subscription completed with no more events.");
          break;
        }
        if subscription.has_error() {
          let err = subscription.get_error().unwrap_or_else(||
                                                            IBKRError::InternalError("Unknown error in subscription".to_string()));
          error!("Subscription has error: {:?}", err);
          subscription.cancel().ok();
          return Err(err.into());
        }
        debug!("No event in last {:?}, continuing iteration...", iteration_timeout);
      }
    }
  }

  info!("Finished iterating events. Total events received: {}. Cancelling subscription...", event_count);
  subscription.cancel().context("Failed to cancel MarketDepthSubscription")?;
  info!("MarketDepthSubscription cancelled.");

  if event_count == 0 && is_live {
    warn!("Received 0 market depth events for IBM. This might be okay if market is closed.");
  }
  Ok(())
}

pub(super) fn multi_subscription_mixed_impl(client: &IBKRClient, is_live: bool) -> Result<()> {
  info!("--- Testing Multi-Subscription for Same Event Types ---");
  let data_mgr = client.data_market();

  // Create two market data subscriptions for different symbols
  info!("Creating market data subscriptions for AAPL and MSFT...");
  let aapl_sub = data_mgr.subscribe_market_data(&Contract::stock("AAPL"))
    .with_market_data_type(MarketDataType::Delayed)
    .submit()
    .context("Failed to submit AAPL market data subscription")?;

  let msft_sub = data_mgr.subscribe_market_data(&Contract::stock("MSFT"))
    .with_market_data_type(MarketDataType::Delayed)
    .submit()
    .context("Failed to submit MSFT market data subscription")?;

  info!("Setting up multi-subscription iterator...");

  // Create the multi-subscription iterator, properly adding both subscriptions
  let mut multi_iter = data_mgr.combine_subscriptions::<TickDataEvent>()
    .add(&aapl_sub, aapl_sub.events())
    .add(&msft_sub, msft_sub.events())
    .build()
    .with_timeout(Duration::from_millis(100));

  info!("Successfully created multi-subscription with both market data subscriptions");
  info!("Consuming events from the combined iterator...");

  let mut event_count = 0;
  let mut aapl_events = 0;
  let mut msft_events = 0;
  let max_events = if is_live { 10 } else { 4 };
  let wait_duration = if is_live { Duration::from_secs(15) } else { Duration::from_secs(1) };
  let start_time = std::time::Instant::now();

  while start_time.elapsed() < wait_duration && event_count < max_events {
    // Use the multi-iterator to get events from either subscription
    match multi_iter.try_next(Duration::from_millis(200)) {
      Some(tagged_event) => {
        info!("Got event from symbol {} (ReqID: {}): {:?}",
              tagged_event.contract.symbol,
              tagged_event.req_id,
              tagged_event.event);

        event_count += 1;

        // Track which symbol we got the event for
        if tagged_event.req_id == aapl_sub.request_id() {
          aapl_events += 1;
        } else if tagged_event.req_id == msft_sub.request_id() {
          msft_events += 1;
        }
      }
      None => {
        // No event available, check if both subscriptions are completed
        if aapl_sub.is_completed() && msft_sub.is_completed() {
          info!("Both subscriptions are completed. Exiting event loop.");
          break;
        }
        debug!("No event available in latest poll. Continuing...");
      }
    }
  }

  info!("Cancelling subscriptions...");
  aapl_sub.cancel().ok();
  msft_sub.cancel().ok();

  info!("Multi-subscription test completed:");
  info!("  Total Events: {}", event_count);
  info!("  AAPL Events: {}", aapl_events);
  info!("  MSFT Events: {}", msft_events);

  if event_count == 0 && is_live {
    warn!("Received 0 events from both subscriptions. This might indicate market is closed.");
  }

  Ok(())
}