trading-ig 0.1.5

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
//! Request and response types for the `dealing/positions` domain.

use chrono::NaiveDateTime;
use serde::{Deserialize, Serialize};

// DealConfirmation and DealStatus are re-exported from dealing::positions via dealing::common.
use crate::models::common::{
    DealId, DealReference, Direction, Epic, MarketSnapshot, OrderType, TimeInForce,
};

// MarketSnapshot is defined in models::common (unified across all domains).

// ---------------------------------------------------------------------------
// V1 position
// ---------------------------------------------------------------------------

/// Raw position sub-object as returned by the v1 list endpoint.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
struct PositionSubV1 {
    contract_size: f64,
    controlled_risk: bool,
    created_date: String, // v1 format: "YYYY:MM:DD-HH:MM:SS"
    deal_id: DealId,
    deal_reference: DealReference,
    direction: Direction,
    level: f64,
    limit_level: Option<f64>,
    size: f64,
    stop_level: Option<f64>,
    trailing_step: Option<f64>,
    trailing_stop_distance: Option<f64>,
    currency: Option<String>,
    limited_risk_premium: Option<serde_json::Value>,
}

/// A position entry as returned by `GET /positions` v1.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PositionV1 {
    pub deal_id: DealId,
    pub deal_reference: DealReference,
    pub direction: Direction,
    pub size: f64,
    pub level: f64,
    pub limit_level: Option<f64>,
    pub stop_level: Option<f64>,
    pub controlled_risk: bool,
    pub contract_size: f64,
    /// Created-date as an opaque string (v1 format `YYYY:MM:DD-HH:MM:SS`).
    pub created_date: String,
    pub trailing_step: Option<f64>,
    pub trailing_stop_distance: Option<f64>,
    pub market: MarketSnapshot,
}

/// Wire envelope for a single v1 list entry `{ "position": {…}, "market": {…} }`.
#[derive(Debug, Deserialize)]
struct PositionEntryV1 {
    position: PositionSubV1,
    market: MarketSnapshot,
}

impl From<PositionEntryV1> for PositionV1 {
    fn from(e: PositionEntryV1) -> Self {
        Self {
            deal_id: e.position.deal_id,
            deal_reference: e.position.deal_reference,
            direction: e.position.direction,
            size: e.position.size,
            level: e.position.level,
            limit_level: e.position.limit_level,
            stop_level: e.position.stop_level,
            controlled_risk: e.position.controlled_risk,
            contract_size: e.position.contract_size,
            created_date: e.position.created_date,
            trailing_step: e.position.trailing_step,
            trailing_stop_distance: e.position.trailing_stop_distance,
            market: e.market,
        }
    }
}

/// Wire envelope for `GET /positions` v1: `{ "positions": [ … ] }`.
#[derive(Debug, Deserialize)]
pub(super) struct PositionsEnvelopeV1 {
    positions: Vec<PositionEntryV1>,
}

impl PositionsEnvelopeV1 {
    pub(super) fn into_vec(self) -> Vec<PositionV1> {
        self.positions.into_iter().map(PositionV1::from).collect()
    }
}

// ---------------------------------------------------------------------------
// V2 position
// ---------------------------------------------------------------------------

/// Raw position sub-object as returned by the v2 list/get endpoints.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
struct PositionSubV2 {
    contract_size: f64,
    controlled_risk: bool,
    created_date: String, // v2 format: "YYYY/MM/DD HH:MM:SS:mmm"
    #[serde(rename = "createdDateUTC")]
    created_date_utc: Option<String>,
    deal_id: DealId,
    deal_reference: DealReference,
    direction: Direction,
    level: f64,
    limit_level: Option<f64>,
    size: f64,
    stop_level: Option<f64>,
    trailing_step: Option<f64>,
    trailing_stop_distance: Option<f64>,
    currency: Option<String>,
    limited_risk_premium: Option<serde_json::Value>,
}

/// A position entry as returned by `GET /positions` v2 or `GET /positions/{dealId}`.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PositionV2 {
    pub deal_id: DealId,
    pub deal_reference: DealReference,
    pub direction: Direction,
    pub size: f64,
    pub level: f64,
    pub limit_level: Option<f64>,
    pub stop_level: Option<f64>,
    pub controlled_risk: bool,
    pub contract_size: f64,
    /// Created-date as an opaque string (v2 format `YYYY/MM/DD HH:MM:SS`).
    pub created_date: String,
    pub created_date_utc: Option<NaiveDateTime>,
    pub trailing_step: Option<f64>,
    pub trailing_stop_distance: Option<f64>,
    pub market: MarketSnapshot,
}

/// Wire envelope for a single v2 entry `{ "position": {…}, "market": {…} }`.
#[derive(Debug, Deserialize)]
struct PositionEntryV2 {
    position: PositionSubV2,
    market: MarketSnapshot,
}

fn parse_optional_utc(s: &str) -> Option<NaiveDateTime> {
    NaiveDateTime::parse_from_str(s, "%Y-%m-%dT%H:%M:%S")
        .or_else(|_| NaiveDateTime::parse_from_str(s, "%Y-%m-%dT%H:%M:%S%.f"))
        .ok()
}

impl From<PositionEntryV2> for PositionV2 {
    fn from(e: PositionEntryV2) -> Self {
        let created_date_utc = e
            .position
            .created_date_utc
            .as_deref()
            .and_then(parse_optional_utc);
        Self {
            deal_id: e.position.deal_id,
            deal_reference: e.position.deal_reference,
            direction: e.position.direction,
            size: e.position.size,
            level: e.position.level,
            limit_level: e.position.limit_level,
            stop_level: e.position.stop_level,
            controlled_risk: e.position.controlled_risk,
            contract_size: e.position.contract_size,
            created_date: e.position.created_date,
            created_date_utc,
            trailing_step: e.position.trailing_step,
            trailing_stop_distance: e.position.trailing_stop_distance,
            market: e.market,
        }
    }
}

/// Wire envelope for `GET /positions` v2: `{ "positions": [ … ] }`.
#[derive(Debug, Deserialize)]
pub(super) struct PositionsEnvelopeV2 {
    positions: Vec<PositionEntryV2>,
}

impl PositionsEnvelopeV2 {
    pub(super) fn into_vec(self) -> Vec<PositionV2> {
        self.positions.into_iter().map(PositionV2::from).collect()
    }
}

// DealStatus and DealConfirmation are defined in dealing::common.
// They are re-exported from positions::mod for backwards compatibility.

// ---------------------------------------------------------------------------
// Update position request
// ---------------------------------------------------------------------------

/// Request body for `PUT /positions/otc/{dealId}` (v2).
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct UpdatePositionRequest {
    /// Whether the stop is guaranteed (mandatory field).
    pub guaranteed_stop: bool,
    pub limit_level: Option<f64>,
    pub stop_level: Option<f64>,
    pub trailing_stop: Option<bool>,
    pub trailing_stop_distance: Option<f64>,
    pub trailing_stop_increment: Option<f64>,
}

impl UpdatePositionRequest {
    /// Convenience constructor with only the mandatory field.
    pub fn new(guaranteed_stop: bool) -> Self {
        Self {
            guaranteed_stop,
            limit_level: None,
            stop_level: None,
            trailing_stop: None,
            trailing_stop_distance: None,
            trailing_stop_increment: None,
        }
    }
}

// ---------------------------------------------------------------------------
// Close position request
// ---------------------------------------------------------------------------

/// Request body for `DELETE /positions/otc` (v1).
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ClosePositionRequest {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub deal_id: Option<DealId>,
    pub direction: Direction,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub epic: Option<Epic>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub expiry: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub level: Option<f64>,
    pub order_type: OrderType,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub quote_id: Option<String>,
    pub size: f64,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub time_in_force: Option<TimeInForce>,
}

// ---------------------------------------------------------------------------
// Internal: deal-reference-only response from POST /positions/otc
// ---------------------------------------------------------------------------

/// Wire response from `POST /positions/otc`: just a `dealReference`.
#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub(super) struct OpenPositionResponse {
    pub deal_reference: DealReference,
}

/// Wire response from `PUT /positions/otc/{dealId}`: just a `dealReference`.
#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub(super) struct UpdatePositionResponse {
    pub deal_reference: DealReference,
}

/// Wire response from `DELETE /positions/otc`: just a `dealReference`.
#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub(super) struct ClosePositionResponse {
    pub deal_reference: DealReference,
}

// ---------------------------------------------------------------------------
// Polars conversion
// ---------------------------------------------------------------------------

#[cfg(feature = "polars")]
impl crate::dataframe::IntoDataFrame for Vec<PositionV2> {
    /// Convert a list of v2 positions into a `polars::prelude::DataFrame`.
    ///
    /// Column layout:
    ///
    /// | column                   | dtype      | nullable |
    /// | ------------------------ | ---------- | -------- |
    /// | `deal_id`                | `Utf8`     | no       |
    /// | `deal_reference`         | `Utf8`     | no       |
    /// | `direction`              | `Utf8`     | no       |
    /// | `size`                   | `Float64`  | no       |
    /// | `level`                  | `Float64`  | no       |
    /// | `limit_level`            | `Float64`  | yes      |
    /// | `stop_level`             | `Float64`  | yes      |
    /// | `controlled_risk`        | `Boolean`  | no       |
    /// | `contract_size`          | `Float64`  | no       |
    /// | `created_date`           | `Utf8`     | no       |
    /// | `created_date_utc`       | `Datetime` | yes      |
    /// | `trailing_step`          | `Float64`  | yes      |
    /// | `trailing_stop_distance` | `Float64`  | yes      |
    /// | `market_epic`            | `Utf8`     | yes      |
    /// | `market_bid`             | `Float64`  | yes      |
    /// | `market_offer`           | `Float64`  | yes      |
    /// | `market_status`          | `Utf8`     | no       |
    fn to_dataframe(&self) -> crate::Result<polars::prelude::DataFrame> {
        use polars::prelude::*;

        let deal_id: Vec<&str> = self.iter().map(|p| p.deal_id.as_str()).collect();
        let deal_reference: Vec<&str> = self.iter().map(|p| p.deal_reference.as_str()).collect();
        let direction: Vec<&str> = self
            .iter()
            .map(|p| match p.direction {
                crate::models::common::Direction::Buy => "BUY",
                crate::models::common::Direction::Sell => "SELL",
            })
            .collect();
        let size: Vec<f64> = self.iter().map(|p| p.size).collect();
        let level: Vec<f64> = self.iter().map(|p| p.level).collect();
        let limit_level: Vec<Option<f64>> = self.iter().map(|p| p.limit_level).collect();
        let stop_level: Vec<Option<f64>> = self.iter().map(|p| p.stop_level).collect();
        let controlled_risk: Vec<bool> = self.iter().map(|p| p.controlled_risk).collect();
        let contract_size: Vec<f64> = self.iter().map(|p| p.contract_size).collect();
        let created_date: Vec<&str> = self.iter().map(|p| p.created_date.as_str()).collect();
        let created_date_utc: Vec<Option<NaiveDateTime>> =
            self.iter().map(|p| p.created_date_utc).collect();
        let trailing_step: Vec<Option<f64>> = self.iter().map(|p| p.trailing_step).collect();
        let trailing_stop_distance: Vec<Option<f64>> =
            self.iter().map(|p| p.trailing_stop_distance).collect();
        let market_epic: Vec<Option<&str>> = self
            .iter()
            .map(|p| {
                p.market
                    .epic
                    .as_ref()
                    .map(crate::models::common::Epic::as_str)
            })
            .collect();
        let market_bid: Vec<Option<f64>> = self.iter().map(|p| p.market.bid).collect();
        let market_offer: Vec<Option<f64>> = self.iter().map(|p| p.market.offer).collect();
        let market_status: Vec<&str> = self
            .iter()
            .map(|p| market_status_str(p.market.market_status))
            .collect();

        let created_date_utc_series = Series::new("created_date_utc".into(), created_date_utc);

        DataFrame::new(vec![
            Column::new("deal_id".into(), deal_id),
            Column::new("deal_reference".into(), deal_reference),
            Column::new("direction".into(), direction),
            Column::new("size".into(), size),
            Column::new("level".into(), level),
            Column::new("limit_level".into(), limit_level),
            Column::new("stop_level".into(), stop_level),
            Column::new("controlled_risk".into(), controlled_risk),
            Column::new("contract_size".into(), contract_size),
            Column::new("created_date".into(), created_date),
            created_date_utc_series.into(),
            Column::new("trailing_step".into(), trailing_step),
            Column::new("trailing_stop_distance".into(), trailing_stop_distance),
            Column::new("market_epic".into(), market_epic),
            Column::new("market_bid".into(), market_bid),
            Column::new("market_offer".into(), market_offer),
            Column::new("market_status".into(), market_status),
        ])
        .map_err(|e| crate::Error::Config(format!("polars conversion failed: {e}")))
    }
}

#[cfg(feature = "polars")]
fn market_status_str(s: crate::models::common::MarketStatus) -> &'static str {
    use crate::models::common::MarketStatus;
    match s {
        MarketStatus::Tradeable => "TRADEABLE",
        MarketStatus::EditsOnly => "EDITS_ONLY",
        MarketStatus::Closed => "CLOSED",
        MarketStatus::Offline => "OFFLINE",
        MarketStatus::OnAuction => "ON_AUCTION",
        MarketStatus::OnAuctionNoEdits => "ON_AUCTION_NO_EDITS",
        MarketStatus::Suspended => "SUSPENDED",
        MarketStatus::Unknown => "UNKNOWN",
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn close_request_serializes_without_nulls() {
        let req = ClosePositionRequest {
            deal_id: Some(DealId::new("DIAAAAXPBQ23AAS")),
            direction: Direction::Sell,
            epic: None,
            expiry: None,
            level: None,
            order_type: OrderType::Market,
            quote_id: None,
            size: 1.0,
            time_in_force: None,
        };

        let json = serde_json::to_string(&req).expect("serialises");
        assert!(
            !json.contains("null"),
            "no null fields expected, got: {json}"
        );
        assert!(json.contains("\"dealId\":\"DIAAAAXPBQ23AAS\""));
        assert!(json.contains("\"direction\":\"SELL\""));
        assert!(json.contains("\"orderType\":\"MARKET\""));
        assert!(json.contains("\"size\":1"));
        for absent in ["epic", "expiry", "level", "quoteId", "timeInForce"] {
            assert!(
                !json.contains(absent),
                "{absent} should be skipped, got: {json}"
            );
        }
    }
}