trading-ig 0.1.0

Async Rust client for the IG Markets REST and Lightstreamer streaming APIs
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
//! Internal subscription registry.
//!
//! Each call to `subscribe_*` on [`crate::streaming::StreamingClient`] produces a
//! numbered subscription that is registered here.  When an incoming frame
//! arrives with a matching item index the registry deserialises the raw field
//! state and forwards the typed event to the appropriate channel.
//!
//! When a subscriber drops its [`tokio::sync::mpsc::Receiver`] the channel
//! becomes closed.  The next time the reader task tries to send on a closed
//! sender, `send` returns `Err` and we remove the entry from the registry.

use std::collections::HashMap;
use std::sync::{Arc, Mutex};

use tokio::sync::mpsc;
use tracing::debug;

use crate::streaming::events::{
    AccountUpdate, CandleScale, ChartCandleUpdate, ChartTickUpdate, MarketUpdate, TradeUpdate,
};
use crate::streaming::protocol::{FieldValue, merge_fields};

// ---------------------------------------------------------------------------
// Subscription kind
// ---------------------------------------------------------------------------

/// Internal enum describing what kind of data a subscription carries.
pub(crate) enum SubscriptionKind {
    Market {
        epic: String,
        tx: mpsc::Sender<MarketUpdate>,
    },
    ChartTick {
        epic: String,
        tx: mpsc::Sender<ChartTickUpdate>,
    },
    ChartCandle {
        epic: String,
        scale: CandleScale,
        tx: mpsc::Sender<ChartCandleUpdate>,
    },
    Account {
        account_id: String,
        tx: mpsc::Sender<AccountUpdate>,
    },
    Trade {
        account_id: String,
        tx: mpsc::Sender<TradeUpdate>,
    },
}

impl std::fmt::Debug for SubscriptionKind {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Market { epic, .. } => write!(f, "Market({epic})"),
            Self::ChartTick { epic, .. } => write!(f, "ChartTick({epic})"),
            Self::ChartCandle { epic, scale, .. } => write!(f, "ChartCandle({epic}/{scale})"),
            Self::Account { account_id, .. } => write!(f, "Account({account_id})"),
            Self::Trade { account_id, .. } => write!(f, "Trade({account_id})"),
        }
    }
}

// ---------------------------------------------------------------------------
// Registry entry
// ---------------------------------------------------------------------------

pub(crate) struct Entry {
    pub(crate) kind: SubscriptionKind,
    /// Running field state — one slot per field declared at subscription time.
    pub(crate) field_state: Vec<Option<String>>,
    /// Lightstreamer 1-based item number assigned by the server.
    pub(crate) item_index: usize,
}

impl std::fmt::Debug for Entry {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "Entry {{ item_index: {}, kind: {:?} }}",
            self.item_index, self.kind
        )
    }
}

// ---------------------------------------------------------------------------
// Registry
// ---------------------------------------------------------------------------

/// Shared subscription registry.
///
/// Cheap to clone — backed by `Arc<Mutex<..>>`.
#[derive(Debug, Clone)]
pub(crate) struct Registry {
    inner: Arc<Mutex<Inner>>,
}

#[derive(Debug)]
struct Inner {
    /// Map from Lightstreamer item index → subscription entry.
    by_index: HashMap<usize, Entry>,
    /// Monotonically increasing counter used to assign item indices.
    next_index: usize,
}

impl Registry {
    pub(crate) fn new() -> Self {
        Self {
            inner: Arc::new(Mutex::new(Inner {
                by_index: HashMap::new(),
                next_index: 1,
            })),
        }
    }

    /// Register a new subscription and return the assigned item index.
    pub(crate) fn register(&self, kind: SubscriptionKind) -> usize {
        let mut inner = self.inner.lock().expect("registry lock");
        let idx = inner.next_index;
        inner.next_index += 1;
        let field_len = kind_field_count(&kind);
        inner.by_index.insert(
            idx,
            Entry {
                kind,
                field_state: vec![None; field_len],
                item_index: idx,
            },
        );
        idx
    }

    /// Apply an incoming update frame to the matching subscription.
    ///
    /// Returns `true` if the subscription is still live, `false` if the
    /// sender was closed (caller should remove the entry).
    pub(crate) fn apply_update(&self, item_index: usize, fields: &[FieldValue]) -> bool {
        let mut inner = self.inner.lock().expect("registry lock");
        let Some(entry) = inner.by_index.get_mut(&item_index) else {
            return true; // unknown index — ignore
        };
        merge_fields(&mut entry.field_state, fields);
        let state = entry.field_state.clone();
        let alive = dispatch(&entry.kind, &state);
        if !alive {
            debug!(item_index, "subscriber dropped — removing subscription");
        }
        alive
    }

    /// Remove a dead subscription from the registry.
    pub(crate) fn remove(&self, item_index: usize) {
        self.inner
            .lock()
            .expect("registry lock")
            .by_index
            .remove(&item_index);
    }

    /// Snapshot all entries for re-subscription after a reconnect.
    ///
    /// Returns `(item_index, item_name, fields_csv, mode)` tuples.
    pub(crate) fn snapshot_for_resubscribe(&self) -> Vec<(usize, String, String, &'static str)> {
        self.inner
            .lock()
            .expect("registry lock")
            .by_index
            .values()
            .map(|e| {
                let (name, fields, mode) = kind_wire_params(&e.kind);
                (e.item_index, name, fields, mode)
            })
            .collect()
    }
}

/// Send a typed event to the appropriate channel.  Returns `false` when the
/// channel is closed (receiver dropped).
fn dispatch(kind: &SubscriptionKind, state: &[Option<String>]) -> bool {
    match kind {
        SubscriptionKind::Market { epic, tx } => {
            let update = MarketUpdate::from_raw(epic, state);
            tx.try_send(update).is_ok() || !tx.is_closed()
        }
        SubscriptionKind::ChartTick { epic, tx } => {
            let update = ChartTickUpdate::from_raw(epic, state);
            tx.try_send(update).is_ok() || !tx.is_closed()
        }
        SubscriptionKind::ChartCandle { epic, scale, tx } => {
            let update = ChartCandleUpdate::from_raw(epic, *scale, state);
            tx.try_send(update).is_ok() || !tx.is_closed()
        }
        SubscriptionKind::Account { account_id, tx } => {
            let update = AccountUpdate::from_raw(account_id, state);
            tx.try_send(update).is_ok() || !tx.is_closed()
        }
        SubscriptionKind::Trade { account_id, tx } => {
            let update = TradeUpdate::from_raw(account_id, state);
            tx.try_send(update).is_ok() || !tx.is_closed()
        }
    }
}

/// How many fields does this subscription kind declare?
fn kind_field_count(kind: &SubscriptionKind) -> usize {
    use crate::streaming::events::{
        ACCOUNT_FIELDS, CHART_CANDLE_FIELDS, CHART_TICK_FIELDS, MARKET_FIELDS, TRADE_FIELDS,
    };
    match kind {
        SubscriptionKind::Market { .. } => MARKET_FIELDS.len(),
        SubscriptionKind::ChartTick { .. } => CHART_TICK_FIELDS.len(),
        SubscriptionKind::ChartCandle { .. } => CHART_CANDLE_FIELDS.len(),
        SubscriptionKind::Account { .. } => ACCOUNT_FIELDS.len(),
        SubscriptionKind::Trade { .. } => TRADE_FIELDS.len(),
    }
}

/// Return `(item_name, fields_csv, mode_str)` for the wire protocol.
fn kind_wire_params(kind: &SubscriptionKind) -> (String, String, &'static str) {
    use crate::streaming::events::{
        ACCOUNT_FIELDS, CHART_CANDLE_FIELDS, CHART_TICK_FIELDS, MARKET_FIELDS, TRADE_FIELDS,
    };
    match kind {
        SubscriptionKind::Market { epic, .. } => {
            (format!("MARKET:{epic}"), MARKET_FIELDS.join(" "), "MERGE")
        }
        SubscriptionKind::ChartTick { epic, .. } => (
            format!("CHART:{epic}:TICK"),
            CHART_TICK_FIELDS.join(" "),
            "DISTINCT",
        ),
        SubscriptionKind::ChartCandle { epic, scale, .. } => (
            format!("CHART:{epic}:{}", scale.as_str()),
            CHART_CANDLE_FIELDS.join(" "),
            "MERGE",
        ),
        SubscriptionKind::Account { account_id, .. } => (
            format!("ACCOUNT:{account_id}"),
            ACCOUNT_FIELDS.join(" "),
            "MERGE",
        ),
        SubscriptionKind::Trade { account_id, .. } => (
            format!("TRADE:{account_id}"),
            TRADE_FIELDS.join(" "),
            "DISTINCT",
        ),
    }
}

// ---------------------------------------------------------------------------
// Unit tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;
    use crate::streaming::protocol::FieldValue;
    use tokio::sync::mpsc;

    fn make_market_entry(
        epic: &str,
    ) -> (
        Registry,
        mpsc::Receiver<crate::streaming::events::MarketUpdate>,
    ) {
        let registry = Registry::new();
        let (tx, rx) = mpsc::channel(32);
        registry.register(SubscriptionKind::Market {
            epic: epic.to_owned(),
            tx,
        });
        (registry, rx)
    }

    #[tokio::test]
    async fn registry_assigns_sequential_indices() {
        let registry = Registry::new();
        let (tx1, _rx1) = mpsc::channel::<crate::streaming::events::MarketUpdate>(1);
        let (tx2, _rx2) = mpsc::channel::<crate::streaming::events::MarketUpdate>(1);
        let idx1 = registry.register(SubscriptionKind::Market {
            epic: "A".into(),
            tx: tx1,
        });
        let idx2 = registry.register(SubscriptionKind::Market {
            epic: "B".into(),
            tx: tx2,
        });
        assert_eq!(idx1, 1);
        assert_eq!(idx2, 2);
    }

    #[tokio::test]
    async fn apply_update_dispatches_to_correct_channel() {
        let (registry, mut rx) = make_market_entry("EPIC1");
        let fields = vec![FieldValue::Value("1.0".into())];
        let alive = registry.apply_update(1, &fields);
        assert!(alive);
        assert!(rx.try_recv().is_ok());
    }

    #[tokio::test]
    async fn apply_update_for_unknown_index_is_ignored() {
        let (registry, mut rx) = make_market_entry("EPIC1");
        let fields = vec![FieldValue::Value("1.0".into())];
        // Item index 99 does not exist.
        let alive = registry.apply_update(99, &fields);
        assert!(alive, "unknown index should not report dead");
        assert!(rx.try_recv().is_err(), "no message should be dispatched");
    }

    #[tokio::test]
    async fn apply_update_detects_closed_channel() {
        let registry = Registry::new();
        let (tx, rx) = mpsc::channel::<crate::streaming::events::MarketUpdate>(1);
        registry.register(SubscriptionKind::Market {
            epic: "EPIC".into(),
            tx,
        });
        drop(rx); // close the receiver

        let fields = vec![FieldValue::Value("1.0".into())];
        let alive = registry.apply_update(1, &fields);
        assert!(!alive, "should detect closed receiver");
    }

    #[tokio::test]
    async fn merge_is_applied_across_updates() {
        let registry = Registry::new();
        let (tx, mut rx) = mpsc::channel(32);
        let idx = registry.register(SubscriptionKind::Market {
            epic: "E".into(),
            tx,
        });

        // First update: set bid.
        registry.apply_update(idx, &[FieldValue::Value("1.0".into())]);
        let u1 = rx.try_recv().unwrap();
        assert_eq!(u1.bid, Some(1.0));

        // Second update: bid unchanged, offer set.
        registry.apply_update(
            idx,
            &[FieldValue::Unchanged, FieldValue::Value("1.1".into())],
        );
        let u2 = rx.try_recv().unwrap();
        // Bid should be preserved from state.
        assert_eq!(u2.bid, Some(1.0));
        assert_eq!(u2.offer, Some(1.1));
    }

    #[tokio::test]
    async fn remove_clears_registry_entry() {
        let registry = Registry::new();
        let (tx, _rx) = mpsc::channel::<crate::streaming::events::MarketUpdate>(1);
        let idx = registry.register(SubscriptionKind::Market {
            epic: "E".into(),
            tx,
        });

        registry.remove(idx);

        // After removal, apply_update should be a no-op (returns true — unknown index).
        let fields = vec![FieldValue::Value("1.0".into())];
        let alive = registry.apply_update(idx, &fields);
        assert!(alive, "removed entry treated as unknown (ignored)");
    }

    #[tokio::test]
    async fn snapshot_for_resubscribe_returns_all_entries() {
        let registry = Registry::new();
        let (tx1, _rx1) = mpsc::channel::<crate::streaming::events::MarketUpdate>(1);
        let (tx2, _rx2) = mpsc::channel::<crate::streaming::events::AccountUpdate>(1);
        registry.register(SubscriptionKind::Market {
            epic: "IX.D.FTSE".into(),
            tx: tx1,
        });
        registry.register(SubscriptionKind::Account {
            account_id: "ABC123".into(),
            tx: tx2,
        });

        let subs = registry.snapshot_for_resubscribe();
        assert_eq!(subs.len(), 2);

        let names: Vec<&str> = subs.iter().map(|(_, name, _, _)| name.as_str()).collect();
        assert!(names.contains(&"MARKET:IX.D.FTSE"));
        assert!(names.contains(&"ACCOUNT:ABC123"));
    }

    #[test]
    fn kind_wire_params_correct() {
        let (name, fields, mode) = kind_wire_params(&SubscriptionKind::Market {
            epic: "A".into(),
            tx: tokio::sync::mpsc::channel(1).0,
        });
        assert_eq!(name, "MARKET:A");
        assert!(fields.contains("BID"));
        assert_eq!(mode, "MERGE");

        let (name, _, mode) = kind_wire_params(&SubscriptionKind::ChartTick {
            epic: "B".into(),
            tx: tokio::sync::mpsc::channel(1).0,
        });
        assert_eq!(name, "CHART:B:TICK");
        assert_eq!(mode, "DISTINCT");

        let (name, _, mode) = kind_wire_params(&SubscriptionKind::ChartCandle {
            epic: "C".into(),
            scale: crate::streaming::events::CandleScale::Hour,
            tx: tokio::sync::mpsc::channel(1).0,
        });
        assert_eq!(name, "CHART:C:HOUR");
        assert_eq!(mode, "MERGE");

        let (name, _, mode) = kind_wire_params(&SubscriptionKind::Trade {
            account_id: "D".into(),
            tx: tokio::sync::mpsc::channel(1).0,
        });
        assert_eq!(name, "TRADE:D");
        assert_eq!(mode, "DISTINCT");
    }
}