ibapi 2.0.0

A Rust implementation of the Interactive Brokers TWS API, providing a reliable and user friendly interface for TWS and IB Gateway. Designed with a focus on simplicity and performance.
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
//! Records interactions between the Client and TWS server for mock server development
//!
//! This example demonstrates how to capture real interactions with a live TWS/Gateway
//! and save them in YAML format for building mock servers.

use core::str::FromStr;
use std::fs;
use std::path::Path;

use ibapi::accounts::{AccountSummaryResult, PositionUpdate};
use ibapi::client::blocking::Client;
use ibapi::messages::parser_registry::{MessageParserRegistry, ParsedField};
use ibapi::messages::*;
use ibapi::trace;
use serde::{Deserialize, Serialize};

/// Represents a field in a TWS message
#[derive(Debug, Serialize, Deserialize)]
struct Field {
    name: String,
    value: String,
}

/// Represents a complete interaction between client and TWS
#[derive(Debug, Serialize, Deserialize)]
struct InteractionRecord {
    /// Name of the API call (e.g., "server_time")
    name: String,
    /// The request message sent to TWS
    request: MessageRecord,
    /// The response messages received from TWS
    responses: Vec<MessageRecord>,
}

/// Represents a single message (request or response)
#[derive(Debug, Serialize, Deserialize)]
struct MessageRecord {
    /// Raw message as sent/received
    raw: String,
    /// Parsed fields with descriptions
    fields: Vec<Field>,
}

// Sanitization module
mod sanitization {
    use super::Field;

    /// Sanitize sensitive data in a field value
    pub fn sanitize_field_value(value: &str) -> String {
        // Check if this might be an account ID
        if (value.starts_with("DU") || value.starts_with("U")) && value.len() > 5 {
            return "ACCOUNT_ID".to_string();
        }

        // Check for other sensitive patterns
        if value.len() > 20 && value.chars().all(|c| c.is_alphanumeric()) {
            // Might be a token or key
            return format!("{}...", &value[..6]);
        }

        value.to_string()
    }

    /// Sanitize the raw message by replacing sensitive data
    pub fn sanitize_raw_message(raw: &str, fields: &[Field]) -> String {
        let mut sanitized = raw.to_string();

        // Look for account IDs in the raw message
        let parts: Vec<&str> = raw.split('\0').collect();
        for (i, part) in parts.iter().enumerate() {
            if (part.starts_with("DU") || part.starts_with("U")) && part.len() > 5 {
                // This looks like an account ID
                if let Some(field) = fields.get(i) {
                    if field.value == "ACCOUNT_ID" {
                        // Replace in the raw message
                        sanitized = sanitized.replace(part, "ACCOUNT_ID");
                    }
                }
            }
        }

        sanitized
    }
}

use sanitization::{sanitize_field_value, sanitize_raw_message};

// Message parsing functions
fn parse_message_parts(raw: &str) -> Vec<&str> {
    // Don't filter out empty strings in the middle - they represent empty fields
    // But remove the last empty string if present (from trailing \0)
    let mut parts: Vec<&str> = raw.split('\0').collect();
    if parts.last() == Some(&"") {
        parts.pop();
    }
    parts
}

fn parse_request_fields(raw: &str, registry: &MessageParserRegistry) -> Vec<Field> {
    let parts = parse_message_parts(raw);

    if parts.is_empty() {
        return Vec::new();
    }

    match parts.first().map(|s| OutgoingMessages::from_str(s)) {
        Some(Ok(msg_type)) => {
            let mut parsed = registry.parse_request(msg_type, &parts);

            // Sanitize account field in requests
            if matches!(msg_type, OutgoingMessages::RequestPnL) {
                for field in &mut parsed {
                    if field.name == "account" {
                        field.value = sanitize_field_value(&field.value);
                    }
                }
            }

            convert_parsed_fields(parsed)
        }
        _ => {
            let parsed = parser_registry::parse_generic_message(&parts);
            convert_parsed_fields(parsed)
        }
    }
}

fn parse_response_fields(raw: &str, registry: &MessageParserRegistry) -> Vec<Field> {
    let parts = parse_message_parts(raw);

    if parts.is_empty() {
        return Vec::new();
    }

    match parts.first().map(|s| IncomingMessages::from_str(s)) {
        Some(Ok(msg_type)) => {
            let mut parsed = registry.parse_response(msg_type, &parts);

            // Special handling for sanitization
            match msg_type {
                IncomingMessages::ManagedAccounts => {
                    // Apply sanitization to accounts field
                    for field in &mut parsed {
                        if field.name == "accounts" {
                            field.value = field
                                .value
                                .split(',')
                                .map(|acc| sanitize_field_value(acc.trim()))
                                .collect::<Vec<_>>()
                                .join(",");
                        }
                    }
                }
                IncomingMessages::Position | IncomingMessages::AccountSummary | IncomingMessages::PnL => {
                    // Sanitize account field
                    for field in &mut parsed {
                        if field.name == "account" {
                            field.value = sanitize_field_value(&field.value);
                        }
                    }
                }
                _ => {}
            }

            convert_parsed_fields(parsed)
        }
        _ => {
            let parsed = parser_registry::parse_generic_message(&parts);
            convert_parsed_fields(parsed)
        }
    }
}

// Convert ParsedField to Field
fn convert_parsed_fields(parsed: Vec<ParsedField>) -> Vec<Field> {
    parsed
        .into_iter()
        .map(|pf| Field {
            name: pf.name,
            value: pf.value,
        })
        .collect()
}

/// Interaction recorder
struct InteractionRecorder {
    registry: MessageParserRegistry,
}

impl InteractionRecorder {
    fn new() -> Self {
        Self {
            registry: MessageParserRegistry::new(),
        }
    }

    fn record_interaction<F>(&self, name: &str, operation: F) -> Result<InteractionRecord, Box<dyn std::error::Error>>
    where
        F: FnOnce() -> Result<InteractionRecord, Box<dyn std::error::Error>>,
    {
        println!("Recording {name} interaction...");

        // Clear any previous interaction
        std::thread::sleep(std::time::Duration::from_millis(100));

        // Execute the operation and get the captured record
        let record = operation()?;

        Ok(record)
    }

    fn create_request_record(&self, raw: String) -> MessageRecord {
        let fields = parse_request_fields(&raw, &self.registry);
        let sanitized_raw = sanitize_raw_message(&raw, &fields);
        MessageRecord { raw: sanitized_raw, fields }
    }

    fn create_response_record(&self, raw: String) -> MessageRecord {
        let fields = parse_response_fields(&raw, &self.registry);
        let sanitized_raw = sanitize_raw_message(&raw, &fields);
        MessageRecord { raw: sanitized_raw, fields }
    }
}

/// Header information for the recording session
#[derive(Debug, Serialize, Deserialize)]
struct RecordingHeader {
    server_version: i32,
    recorded_at: String,
}

/// YAML output wrapper for proper serialization
#[derive(Serialize)]
struct YamlOutput {
    header: RecordingHeader,
    interactions: Vec<InteractionRecord>,
}

fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Enable debug logging to activate trace recording
    env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("debug")).init();

    println!("Connecting to TWS/Gateway...");
    let client = Client::connect("127.0.0.1:4002", 100)?;
    println!("Connected successfully!");

    let recorder = InteractionRecorder::new();
    let mut interactions = Vec::new();

    // Record server_time interaction
    match recorder.record_interaction("server_time", || {
        let server_time = client.server_time()?;
        println!("Server time: {server_time}");

        // Capture interaction immediately after the call
        let interaction = trace::blocking::last_interaction().ok_or("No interaction captured - ensure debug logging is enabled")?;

        let record = InteractionRecord {
            name: "server_time".to_string(),
            request: recorder.create_request_record(interaction.request),
            responses: interaction.responses.into_iter().map(|r| recorder.create_response_record(r)).collect(),
        };

        Ok(record)
    }) {
        Ok(record) => {
            println!("\nRecorded interaction: {}", record.name);
            println!("Request: {}", record.request.raw);
            println!("Responses: {} message(s)", record.responses.len());
            interactions.push(record);
        }
        Err(e) => eprintln!("Failed to record server_time: {e}"),
    }

    // Record managed_accounts interaction
    match recorder.record_interaction("managed_accounts", || {
        let accounts = client.managed_accounts()?;
        println!("Managed accounts: {accounts:?} (will be sanitized)");

        // Capture interaction immediately
        let interaction = trace::blocking::last_interaction().ok_or("No interaction captured - ensure debug logging is enabled")?;

        let record = InteractionRecord {
            name: "managed_accounts".to_string(),
            request: recorder.create_request_record(interaction.request),
            responses: interaction.responses.into_iter().map(|r| recorder.create_response_record(r)).collect(),
        };

        Ok(record)
    }) {
        Ok(record) => {
            println!("\nRecorded interaction: {}", record.name);
            println!("Request: {}", record.request.raw);
            println!("Responses: {} message(s)", record.responses.len());
            interactions.push(record);
        }
        Err(e) => eprintln!("Failed to record managed_accounts: {e}"),
    }

    // Skip other interactions for now to debug
    // Get the first account for subsequent queries
    let accounts = client.managed_accounts().unwrap_or_default();
    let account = accounts.first().map(|s| s.as_str()).unwrap_or("DU1234567");
    println!("\nUsing account: {account} for subsequent queries");

    // Record positions interaction
    match recorder.record_interaction("positions", || {
        let mut position_count = 0;
        let positions = client.positions()?;

        // Consume positions and capture trace before drop
        println!("Starting to consume positions...");
        while let Some(position) = positions.next() {
            if let PositionUpdate::PositionEnd = position {
                break;
            }
            position_count += 1;
            println!("Got position #{position_count}");
        }
        println!("Finished consuming positions: {position_count}");

        // Capture interaction before subscription is dropped
        let interaction = trace::blocking::last_interaction().ok_or("No interaction captured - ensure debug logging is enabled")?;

        // Drop subscription before creating record to avoid cancel message
        drop(positions);

        let record = InteractionRecord {
            name: "positions".to_string(),
            request: recorder.create_request_record(interaction.request),
            responses: interaction.responses.into_iter().map(|r| recorder.create_response_record(r)).collect(),
        };

        Ok(record)
    }) {
        Ok(record) => {
            println!("\nRecorded interaction: {}", record.name);
            println!("Request: {}", record.request.raw);
            println!("Responses: {} message(s)", record.responses.len());
            interactions.push(record);
        }
        Err(e) => eprintln!("Failed to record positions: {e}"),
    }

    // Record account_summary interaction
    match recorder.record_interaction("account_summary", || {
        use ibapi::accounts::types::AccountGroup;
        let mut summary_count = 0;
        let tags = vec!["NetLiquidation", "TotalCashValue", "GrossPositionValue"];
        let group = AccountGroup("All".to_string());
        let summaries = client.account_summary(&group, &tags)?;

        // Consume all summaries
        while let Some(summary) = summaries.next() {
            if let AccountSummaryResult::End = summary {
                break;
            }
            summary_count += 1;
            println!("Got summary #{summary_count}");
        }
        println!("Finished consuming summaries: {summary_count}");

        // Capture interaction before subscription is dropped
        let interaction = trace::blocking::last_interaction().ok_or("No interaction captured - ensure debug logging is enabled")?;

        // Drop subscription before creating record
        drop(summaries);

        let record = InteractionRecord {
            name: "account_summary".to_string(),
            request: recorder.create_request_record(interaction.request),
            responses: interaction.responses.into_iter().map(|r| recorder.create_response_record(r)).collect(),
        };

        Ok(record)
    }) {
        Ok(record) => {
            println!("\nRecorded interaction: {}", record.name);
            println!("Request: {}", record.request.raw);
            println!("Responses: {} message(s)", record.responses.len());
            interactions.push(record);
        }
        Err(e) => eprintln!("Failed to record account_summary: {e}"),
    }

    // Record pnl interaction
    match recorder.record_interaction("pnl", || {
        use ibapi::accounts::types::AccountId;
        let mut pnl_updates = 0;
        let account_id = AccountId(account.to_string());
        let pnl_stream = client.pnl(&account_id, None)?;

        // Read a few PnL updates
        for update in pnl_stream.into_iter().take(3) {
            pnl_updates += 1;
            println!("Got PnL update #{pnl_updates}: {update:?}");
        }
        println!("Finished consuming PnL updates: {pnl_updates}");

        // Give it a moment to ensure trace is updated
        std::thread::sleep(std::time::Duration::from_millis(100));

        // Capture interaction before subscription is dropped
        let interaction = trace::blocking::last_interaction().ok_or("No interaction captured - ensure debug logging is enabled")?;

        println!("PnL interaction request: {}", interaction.request);
        println!("PnL interaction responses: {} messages", interaction.responses.len());

        let record = InteractionRecord {
            name: "pnl".to_string(),
            request: recorder.create_request_record(interaction.request),
            responses: interaction.responses.into_iter().map(|r| recorder.create_response_record(r)).collect(),
        };

        Ok(record)
    }) {
        Ok(record) => {
            println!("\nRecorded interaction: {}", record.name);
            println!("Request: {}", record.request.raw);
            println!("Responses: {} message(s)", record.responses.len());
            interactions.push(record);
        }
        Err(e) => eprintln!("Failed to record pnl: {e}"),
    }

    // Create header with server version
    let header = RecordingHeader {
        server_version: client.server_version(),
        recorded_at: time::OffsetDateTime::now_utc().to_string(),
    };

    // Save all interactions to YAML file using serde_yaml
    let output_path = Path::new("tws_interactions.yaml");
    let yaml_output = YamlOutput { header, interactions };

    let yaml_content = serde_yaml::to_string(&yaml_output)?;
    fs::write(output_path, &yaml_content)?;

    println!("\nSaved to {}:", output_path.display());
    println!("{yaml_content}");

    Ok(())
}