cripsy-binary-api 0.3.1

API moderna multibroker para opciones binarias (Rust, sync y async)
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
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
//! Implementación del contrato `Broker` para IQ Option.

use async_trait::async_trait;
use futures_util::stream::unfold;
use serde_json::json;
use tokio::sync::mpsc;

use crate::broker::config::BrokerParams;
use crate::broker::error::BrokerError;
use crate::broker::iq_option::endpoints::{msg, option_type_id};
use crate::broker::iq_option::mapper;
use crate::broker::iq_option::session::IqOptionBroker;
use crate::broker::session::Session;
use crate::broker::trait_::{Broker, CandleStream, EventStream};
use crate::domain::*;

#[async_trait]
impl Broker for IqOptionBroker {
    fn name(&self) -> &'static str {
        "iq_option"
    }

    async fn connect(params: BrokerParams) -> Result<Self, BrokerError>
    where
        Self: Sized,
    {
        Self::nuevo(params).await
    }

    async fn disconnect(&mut self) -> Result<(), BrokerError> {
        self.cerrar_salida();
        let mut estado = self.estado.write().await;
        estado.conectado = false;
        Ok(())
    }

    async fn is_connected(&self) -> bool {
        self.estado.read().await.conectado
    }

    async fn session(&self) -> Result<Session, BrokerError> {
        self.estado_lectura()
            .await
            .session
            .clone()
            .ok_or_else(|| BrokerError::Internal("sesión no inicializada".into()))
    }

    // -------- Cuenta y balance --------

    fn iqoption_extras(&self) -> Option<&dyn crate::broker::extras::IqOptionExtras> {
        Some(self)
    }

    async fn get_balance(&self) -> Result<Balance, BrokerError> {
        let estado = self.estado_lectura().await;
        let balance_id = estado
            .session
            .as_ref()
            .and_then(|s| s.balance_id)
            .ok_or_else(|| BrokerError::Rejected("sesión sin balance activo".into()))?;
        let bal = estado
            .balances
            .iter()
            .find(|b| b.id == balance_id)
            .cloned()
            .ok_or_else(|| BrokerError::Rejected("balance activo no encontrado".into()))?;
        Ok(Balance {
            amount: bal.amount,
            currency: bal.currency,
            account_type: bal.account_type,
            balance_id: bal.id,
        })
    }

    async fn get_account_type(&self) -> Result<AccountType, BrokerError> {
        self.estado_lectura()
            .await
            .session
            .as_ref()
            .map(|s| s.account_type)
            .ok_or_else(|| BrokerError::Internal("sesión no inicializada".into()))
    }

    async fn switch_account(&mut self, kind: AccountType) -> Result<(), BrokerError> {
        // `api_profile_changebalance` (WS) + refresco de balances (`get-balances`).
        let balance_id = {
            let estado = self.estado_lectura().await;
            estado
                .balances
                .iter()
                .find(|b| b.account_type == kind)
                .map(|b| b.id)
                .ok_or_else(|| BrokerError::Rejected(format!("no hay cuenta {kind:?}")))?
        };
        self.enviar_sin_espera(
            "api_profile_changebalance",
            &json!({ "balance_id": balance_id }),
        )
        .await?;

        // Si el refresco falla, el cambio NO se da por hecho: el error se
        // propaga y la sesión conserva la cuenta anterior.
        let lista = self
            .solicitar(msg::GET_BALANCES, &json!({}), "balances")
            .await?;
        let balances = mapper::balances_desde_lista(&lista);
        if balances.is_empty() {
            return Err(BrokerError::Rejected(
                "el bróker no devolvió balances tras el cambio de cuenta".into(),
            ));
        }
        if !balances.iter().any(|b| b.id == balance_id) {
            return Err(BrokerError::Rejected(format!(
                "el bróker no confirmó el balance {balance_id}"
            )));
        }
        let mut estado = self.estado.write().await;
        estado.balances = balances;
        if let Some(s) = estado.session.as_mut() {
            s.balance_id = Some(balance_id);
            s.account_type = kind;
        }
        Ok(())
    }

    // -------- Activos y mercado --------

    async fn list_assets(&self) -> Result<Vec<Asset>, BrokerError> {
        Ok(self.catalogo.read().await.clone())
    }

    async fn asset_status(&self, symbol: &str) -> Result<MarketStatus, BrokerError> {
        let catalogo = self.catalogo.read().await;
        catalogo
            .iter()
            .find(|a| a.symbol == symbol)
            .map(|a| a.status)
            .ok_or_else(|| {
                BrokerError::Domain(crate::domain::error::DomainError::AssetNotFound(
                    symbol.into(),
                ))
            })
    }

    // -------- Velas --------

    async fn get_candles(&self, req: CandleRequest) -> Result<Vec<Candle>, BrokerError> {
        let catalogo = self.catalogo.read().await;
        let active_id = catalogo
            .iter()
            .find(|a| a.symbol == req.symbol)
            .map(|a| a.opcode)
            .ok_or_else(|| {
                BrokerError::Domain(crate::domain::error::DomainError::AssetNotFound(
                    req.symbol.clone(),
                ))
            })?;
        drop(catalogo);

        let ahora = self.hora_servidor_segs().await;
        // El bróker solo devuelve velas CERRADAS: `to` = cierre de vela cerrada.
        let to = req
            .end_time
            .unwrap_or_else(|| mapper::cierre_de_vela_cerrada(ahora, req.interval as i64, 90));
        let respuesta = self
            .solicitar(
                msg::SEND_MESSAGE,
                &json!({
                    "name": msg::QUOTES_HISTORY_GET_CANDLES,
                    "version": "2.0",
                    "body": {
                        "active_id": active_id,
                        "size": req.interval,
                        "count": req.count,
                        "split_normalization": true,
                        "to": to
                    }
                }),
                msg::CANDLES,
            )
            .await?;
        let velas = respuesta
            .get("candles")
            .and_then(|c| c.as_array())
            .map(|lista| lista.iter().filter_map(mapper::vela_desde_json).collect())
            .unwrap_or_default();
        Ok(velas)
    }

    async fn subscribe_candles(&self, req: CandleSubscribe) -> Result<CandleStream, BrokerError> {
        let catalogo = self.catalogo.read().await;
        let active_id = catalogo
            .iter()
            .find(|a| a.symbol == req.symbol)
            .map(|a| a.opcode)
            .ok_or_else(|| {
                BrokerError::Domain(crate::domain::error::DomainError::AssetNotFound(
                    req.symbol.clone(),
                ))
            })?;
        drop(catalogo);

        // Stream multi-tamaño: un único canal por símbolo con el tamaño pedido
        // (0 = todos los tamaños).
        self.suscribir(
            &format!("velas:{}", req.symbol),
            msg::SUBSCRIBE_MESSAGE,
            &json!({
                "name": msg::CANDLES_GENERATED,
                "params": { "routingFilters": { "active_id": active_id.to_string() } }
            }),
        )
        .await?;

        // Canal por suscripción. Al soltar el `CandleStream` el receptor queda
        // cerrado y la suscripción se retira en el siguiente despacho de velas
        // (`sub_velas.retain`): no hay API de baja explícita. El canal es
        // ilimitado, así que el consumidor debe drenarlo.
        let (tx, rx) = mpsc::unbounded_channel::<Candle>();
        self.sub_velas
            .write()
            .await
            .push(crate::broker::iq_option::session::SubVelas {
                symbol: req.symbol.clone(),
                interval: req.interval,
                tx,
            });
        let stream = unfold(rx, |mut rx| async move {
            rx.recv().await.map(|c| (Ok::<Candle, BrokerError>(c), rx))
        });
        Ok(Box::pin(stream))
    }

    // -------- Payout --------

    async fn get_payout(
        &self,
        symbol: &str,
        expiration_seconds: u64,
    ) -> Result<Payout, BrokerError> {
        let catalogo = self.catalogo.read().await;
        let asset = catalogo
            .iter()
            .find(|a| a.symbol == symbol)
            .ok_or_else(|| {
                BrokerError::Domain(crate::domain::error::DomainError::AssetNotFound(
                    symbol.into(),
                ))
            })?;
        let profit_percent = asset.payout_percent.unwrap_or(0.0);
        // El payout digital en vivo (price-splitter) se añade en la Fase 5.
        Ok(Payout {
            symbol: symbol.to_string(),
            expiration_seconds,
            profit_percent,
            is_spot: asset.class == AssetClass::Digital,
            strike: None,
            closed: asset.status == MarketStatus::Closed,
        })
    }

    async fn sell_option(&self, order_id: &OrderId) -> Result<(), BrokerError> {
        // Mensaje `sell-options` v2.0 con la lista de ids (verificado contra
        // el canal del bróker; el bróker confirma con `sold-options`).
        let id_num = order_id
            .parse::<i64>()
            .map_err(|_| BrokerError::Rejected(format!("id de orden no numerico: {order_id}")))?;
        let respuesta = self
            .solicitar(
                msg::SEND_MESSAGE,
                &json!({
                    "name": msg::SELL_OPTIONS,
                    "version": "2.0",
                    "body": { "options_ids": [id_num] }
                }),
                msg::SOLD_OPTIONS,
            )
            .await?;
        // El broker responde con el detalle de la venta o con error.
        if respuesta.get("message").is_some() {
            return Err(BrokerError::Rejected(
                respuesta
                    .get("message")
                    .and_then(|m| m.as_str())
                    .unwrap_or("venta rechazada")
                    .to_string(),
            ));
        }
        Ok(())
    }

    async fn get_position_history(
        &self,
        symbol: Option<&str>,
        limit: usize,
        offset: usize,
    ) -> Result<PositionHistory, BrokerError> {
        let balance_id = self
            .estado_lectura()
            .await
            .session
            .as_ref()
            .and_then(|s| s.balance_id);
        let limit = limit.min(100) as u64;
        let respuesta = self
            .solicitar(
                msg::SEND_MESSAGE,
                &json!({
                    "name": msg::PORTFOLIO_GET_HISTORY,
                    "body": {
                        "instrument_types": ["turbo-option", "binary-option", "digital-option"],
                        "limit": limit,
                        "offset": offset as u64,
                        "start": 0,
                        "end": 0,
                        "user_balance_id": balance_id
                    }
                }),
                msg::HISTORY_POSITIONS,
            )
            .await?;
        let historial = mapper::historial_desde_positions(&respuesta, offset)
            .ok_or_else(|| BrokerError::Rejected("respuesta sin positions".into()))?;
        if let Some(simbolo) = symbol {
            if historial.positions.iter().all(|p| p.symbol != simbolo) {
                return Err(BrokerError::Domain(
                    crate::domain::error::DomainError::OperationNotFound(format!(
                        "sin historial para {simbolo}"
                    )),
                ));
            }
        }
        Ok(historial)
    }

    // -------- Órdenes --------

    async fn buy_binary(&self, req: BuyBinaryRequest) -> Result<OrderId, BrokerError> {
        // BLITZ tiene su propia petición (duración en segundos y payout): se
        // delega en `buy_blitz` para no transportar segundos en
        // `expiration_minutes`.
        if req.option_type == OptionType::Blitz {
            let payout = {
                let catalogo = self.catalogo.read().await;
                catalogo
                    .iter()
                    .find(|a| a.symbol == req.symbol && a.class == AssetClass::Blitz)
                    .and_then(|a| a.payout_percent)
                    .unwrap_or(0.0)
            };
            return self
                .buy_blitz(BuyBlitzRequest {
                    symbol: req.symbol,
                    amount: req.amount,
                    direction: req.direction,
                    expiration_size_secs: req.expiration_minutes,
                    profit_percent: payout,
                })
                .await;
        }

        let asset = {
            let catalogo = self.catalogo.read().await;
            catalogo
                .iter()
                .find(|a| {
                    a.symbol == req.symbol
                        && (a.class == AssetClass::Binary || a.class == AssetClass::Turbo)
                })
                .cloned()
        }
        .ok_or_else(|| {
            BrokerError::Domain(crate::domain::error::DomainError::AssetNotFound(
                req.symbol.clone(),
            ))
        })?;

        if asset.status == MarketStatus::Closed {
            return Err(BrokerError::Domain(
                crate::domain::error::DomainError::MarketClosed(req.symbol),
            ));
        }
        if let Some(extras) = &asset.extras {
            if let Some(min) = extras.min_amount {
                if req.amount < min {
                    return Err(BrokerError::Domain(
                        crate::domain::error::DomainError::InvalidAmount(format!("mínimo {min}")),
                    ));
                }
            }
        }

        let mut body = json!({
            "active_id": asset.opcode,
            "user_balance_id": self.estado_lectura().await.session.as_ref()
                .and_then(|s| s.balance_id),
            "price": req.amount,
            "direction": match req.direction { Direction::Call => "call", Direction::Put => "put" }
        });

        // Clásica v1.0: expiración por tabla de minutos/cuartos de hora.
        let (expired, idx) =
            mapper::tiempo_de_expiracion(self.hora_servidor_segs().await, req.expiration_minutes);
        body["expired"] = json!(expired);
        body["option_type_id"] = json!(if idx < 5 {
            option_type_id::TURBO
        } else {
            option_type_id::BINARY
        });

        let (version, esperada) = ("1.0", msg::BUY_COMPLETE);
        match self
            .solicitar(
                msg::SEND_MESSAGE,
                &json!({ "name": msg::OPEN_OPTION, "version": version, "body": body }),
                esperada,
            )
            .await
        {
            Ok(respuesta) => {
                let id = respuesta
                    .get("id")
                    .and_then(|i| i.as_i64())
                    .or_else(|| respuesta.pointer("/result/id").and_then(|i| i.as_i64()))
                    .ok_or_else(|| {
                        BrokerError::Rejected(
                            respuesta
                                .get("message")
                                .and_then(|m| m.as_str())
                                .unwrap_or("respuesta sin id de orden")
                                .to_string(),
                        )
                    })?;
                // Base de la reconciliación: hasta que llegue un cierre.
                self.abiertas.write().await.insert(id.to_string());
                Ok(id.to_string())
            }
            Err(e) => Err(e),
        }
    }

    /// Lee el resultado de la caché local, alimentada por los eventos de
    /// cierre (`option-closed`, `socket-option-closed`, `position-changed`).
    ///
    /// **No hace ninguna petición al bróker**: si el cierre aún no ha llegado
    /// por eventos, devuelve `OperationResult::Pending`.
    async fn get_operation_result(
        &self,
        order_id: &OrderId,
    ) -> Result<OperationResult, BrokerError> {
        let cache = self.resultados.read().await;
        Ok(cache
            .get(order_id)
            .copied()
            .unwrap_or(OperationResult::Pending))
    }

    // -------- Eventos --------

    async fn events(&self) -> Result<EventStream, BrokerError> {
        let rx = self.eventos.subscribe();
        let stream = unfold(rx, |mut rx| async move {
            match rx.recv().await {
                Ok(ev) => Some((ev, rx)),
                // Pérdida de continuidad: se informa y el stream continúa, en
                // vez de cerrarse como si la sesión hubiera terminado.
                Err(tokio::sync::broadcast::error::RecvError::Lagged(n)) => {
                    Some((Event::EventsLost { count: n }, rx))
                }
                Err(tokio::sync::broadcast::error::RecvError::Closed) => None,
            }
        });
        Ok(Box::pin(stream))
    }
}

impl IqOptionBroker {
    pub async fn get_technical_indicators(
        &self,
        symbol: &str,
    ) -> Result<TechnicalIndicators, BrokerError> {
        let asset_id = {
            let catalogo = self.catalogo.read().await;
            catalogo
                .iter()
                .find(|a| a.symbol == symbol)
                .map(|a| a.opcode)
                .ok_or_else(|| {
                    BrokerError::Domain(crate::domain::error::DomainError::AssetNotFound(
                        symbol.into(),
                    ))
                })?
        };
        // Mensaje: `trading-signals.get-technical-indicators` v1.0, body {id}.
        // El `id` DEBE ser entero (string → INVALID_SCHEMA). Respuesta:
        // `technical-indicators` con `msg.indicators`, o `msg.message` de error.
        let respuesta = self
            .solicitar(
                msg::SEND_MESSAGE,
                &json!({
                    "name": msg::TECHNICAL_INDICATORS,
                    "version": "1.0",
                    "body": { "id": asset_id }
                }),
                msg::TECHNICAL_INDICATORS_RESPONSE,
            )
            .await?;
        mapper::indicadores_desde_respuesta(&respuesta, symbol).map_err(BrokerError::Rejected)
    }

    pub async fn get_asset_profile(&self, symbol: &str) -> Result<AssetProfile, BrokerError> {
        let asset_id = {
            let catalogo = self.catalogo.read().await;
            catalogo
                .iter()
                .find(|a| a.symbol == symbol)
                .map(|a| a.opcode)
                .ok_or_else(|| {
                    BrokerError::Domain(crate::domain::error::DomainError::AssetNotFound(
                        symbol.into(),
                    ))
                })?
        };
        // GraphQL: el broker responde `financial-information` con data.active.
        let respuesta = self
            .solicitar(
                msg::SEND_MESSAGE,
                &json!({
                    "name": msg::GET_FINANCIAL_INFORMATION,
                    "version": "1.0",
                    "body": {
                        "query": mapper::QUERY_ASSET_PROFILE,
                        "operationName": "GetAssetProfileInfo",
                        "variables": { "activeId": asset_id }
                    }
                }),
                msg::FINANCIAL_INFORMATION,
            )
            .await?;
        mapper::perfil_desde_financial(symbol, &respuesta).map_err(BrokerError::Rejected)
    }

    pub async fn subscribe_live_deals(&self, symbol: &str) -> Result<(), BrokerError> {
        let active_id = {
            let catalogo = self.catalogo.read().await;
            catalogo
                .iter()
                .find(|a| a.symbol == symbol)
                .map(|a| a.opcode)
                .ok_or_else(|| {
                    BrokerError::Domain(crate::domain::error::DomainError::AssetNotFound(
                        symbol.into(),
                    ))
                })?
        };
        // Ambos canales: el agregado y el específico de binarias.
        self.suscribir(
            &format!("live-deals:{symbol}"),
            msg::SUBSCRIBE_MESSAGE,
            &json!({
                "name": msg::LIVE_DEAL,
                "version": "1.0",
                "params": { "routingFilters": { "instrument_active_id": active_id } }
            }),
        )
        .await?;
        self.suscribir(
            &format!("live-deals-bin:{symbol}"),
            msg::SUBSCRIBE_MESSAGE,
            &json!({
                "name": msg::LIVE_DEAL_BINARY,
                "version": "1.0",
                "params": { "routingFilters": { "active_id": active_id, "option_type": "binary-option" } }
            }),
        )
        .await?;
        Ok(())
    }

    pub async fn subscribe_commission_changes(&self) -> Result<(), BrokerError> {
        // Los tres tipos de opción: el bróker emite por instrumento.
        for instrumento in ["binary-option", "turbo-option", "digital-option"] {
            self.suscribir(
                &format!("comision:{instrumento}"),
                msg::SUBSCRIBE_MESSAGE,
                &json!({
                    "name": msg::COMMISSION_CHANGED,
                    "version": "1.0",
                    "params": { "routingFilters": { "instrument_type": instrumento } }
                }),
            )
            .await?;
        }
        Ok(())
    }

    pub async fn buy_blitz(&self, req: BuyBlitzRequest) -> Result<OrderId, BrokerError> {
        // El blitz se resuelve contra el grupo blitz del catálogo, cuyos
        // metadatos (duraciones en segundos, rollovers) son propios.
        let asset = {
            let catalogo = self.catalogo.read().await;
            catalogo
                .iter()
                .find(|a| a.symbol == req.symbol && a.class == AssetClass::Blitz)
                .cloned()
        }
        .ok_or_else(|| {
            BrokerError::Domain(crate::domain::error::DomainError::AssetNotFound(
                req.symbol.clone(),
            ))
        })?;

        if asset.status == MarketStatus::Closed {
            return Err(BrokerError::Domain(
                crate::domain::error::DomainError::MarketClosed(req.symbol),
            ));
        }
        let extras = asset
            .extras
            .as_ref()
            .ok_or_else(|| BrokerError::Rejected("activo sin metadata de blitz".into()))?;
        if let Some(min) = extras.min_amount {
            if req.amount < min {
                return Err(BrokerError::Domain(
                    crate::domain::error::DomainError::InvalidAmount(format!("mínimo {min}")),
                ));
            }
        }

        // Duraciones admitidas: `option.expiration_times` ∪ `rollovers[].expiration_size`.
        let mut permitidos = extras.expiration_times_secs.clone().unwrap_or_default();
        for r in extras.rollovers.as_ref().into_iter().flatten() {
            let s = r.expiration_size as u64;
            if !permitidos.contains(&s) {
                permitidos.push(s);
            }
        }
        permitidos.sort_unstable();
        let secs = req.expiration_size_secs;
        if !permitidos.is_empty() && !permitidos.contains(&secs) {
            return Err(BrokerError::Rejected(format!(
                "duración {secs}s no admitida; admite {permitidos:?}"
            )));
        }

        // Payout: el de la petición si viene informado; si no, el del catálogo.
        let payout = if req.profit_percent > 0.0 {
            req.profit_percent
        } else {
            asset.payout_percent.unwrap_or(0.0)
        };

        let body = json!({
            "active_id": asset.opcode,
            "user_balance_id": self.estado_lectura().await.session.as_ref()
                .and_then(|s| s.balance_id),
            "price": req.amount,
            "direction": match req.direction { Direction::Call => "call", Direction::Put => "put" },
            // `expired` debe quedar alineado al múltiplo de la duración.
            "expired": mapper::expiracion_blitz_alineada(self.hora_servidor_segs().await, secs),
            "expiration_size": secs,
            // El bróker exige entero (rechaza "86.0").
            "profit_percent": payout.round() as i64,
            "option_type_id": option_type_id::BLITZ,
        });

        let respuesta = self
            .solicitar(
                msg::SEND_MESSAGE,
                &json!({ "name": msg::OPEN_OPTION, "version": "2.0", "body": body }),
                msg::OPTION,
            )
            .await?;
        let id = respuesta
            .get("id")
            .and_then(|i| i.as_i64())
            .or_else(|| respuesta.pointer("/result/id").and_then(|i| i.as_i64()))
            .ok_or_else(|| {
                BrokerError::Rejected(
                    respuesta
                        .get("message")
                        .and_then(|m| m.as_str())
                        .unwrap_or("respuesta sin id de orden blitz")
                        .to_string(),
                )
            })?;
        self.abiertas.write().await.insert(id.to_string());
        Ok(id.to_string())
    }

    pub async fn buy_digital_spot(&self, req: BuyDigitalRequest) -> Result<OrderId, BrokerError> {
        let (asset_id, periodo) = {
            let catalogo = self.catalogo.read().await;
            let asset = catalogo
                .iter()
                .find(|a| a.symbol == req.symbol)
                .ok_or_else(|| {
                    BrokerError::Domain(crate::domain::error::DomainError::AssetNotFound(
                        req.symbol.clone(),
                    ))
                })?;
            if asset.status == MarketStatus::Closed {
                return Err(BrokerError::Domain(
                    crate::domain::error::DomainError::MarketClosed(req.symbol),
                ));
            }
            (asset.opcode, req.duration_minutes * 60)
        };

        // Suscripción price-splitter (payout + instrument_id en vivo).
        self.suscribir(
            &format!("splitter:{asset_id}"),
            msg::SUBSCRIBE_MESSAGE,
            &json!({
                "name": msg::PRICE_SPLITTER,
                "version": "1.0",
                "params": { "routingFilters": { "instrument_type": "digital-option", "asset_id": asset_id } }
            }),
        )
        .await?;
        let mut rx = self.precios.subscribe();

        // Espera el mensaje del periodo pedido: instrumento SPOT (strike "SPT")
        // e instrument_index del MISMO mensaje (evita carreras de rollover).
        let (instrument_id, asset_id_json, instrument_index) = loop {
            let msg = tokio::time::timeout(std::time::Duration::from_secs(15), rx.recv())
                .await
                .map_err(|_| BrokerError::Timeout)?
                .map_err(|e| BrokerError::Internal(format!("canal de precios cerrado: {e}")))?;
            if msg.get("period").and_then(|p| p.as_u64()) != Some(periodo) {
                continue;
            }
            if let Some(prices) = msg.pointer("/prices").and_then(|p| p.as_array()) {
                let spot = prices
                    .iter()
                    .find(|p| p.get("strike").and_then(|s| s.as_str()) == Some("SPT"));
                if let Some(spot) = spot {
                    let campo = match req.direction {
                        Direction::Call => "/call/symbol",
                        Direction::Put => "/put/symbol",
                    };
                    if let Some(simbolo) = spot.pointer(campo).and_then(|s| s.as_str()) {
                        let idx = msg
                            .get("instrument_index")
                            .and_then(|i| i.as_i64())
                            .unwrap_or(0);
                        let aid = msg.get("asset_id").cloned().unwrap_or(json!(asset_id));
                        break (simbolo.to_string(), aid, idx);
                    }
                }
            }
        };
        let balance_id = self
            .estado_lectura()
            .await
            .session
            .as_ref()
            .and_then(|s| s.balance_id);
        match self
            .solicitar(
                msg::SEND_MESSAGE,
                &json!({
                    "name": msg::PLACE_DIGITAL_OPTION,
                    // v2.0 verificada en vivo: amount como STRING, con asset_id e
                    // instrument_index del mismo mensaje de price-splitter.
                    "version": "2.0",
                    "body": {
                        "user_balance_id": balance_id,
                        "instrument_id": instrument_id,
                        "asset_id": asset_id_json,
                        "instrument_index": instrument_index,
                        "amount": format!("{:.2}", req.amount)
                    }
                }),
                msg::DIGITAL_OPTION_PLACED,
            )
            .await
        {
            Ok(respuesta) => {
                let id = respuesta
                    .get("id")
                    .and_then(|i| i.as_i64())
                    .ok_or_else(|| {
                        BrokerError::Rejected(
                            respuesta
                                .get("message")
                                .and_then(|m| m.as_str())
                                .unwrap_or("respuesta sin id de orden digital")
                                .to_string(),
                        )
                    })?;
                // Correlación para `position-changed`: instrumento → id de orden.
                self.digitales
                    .write()
                    .await
                    .insert(instrument_id.clone(), id.to_string());
                self.abiertas.write().await.insert(id.to_string());
                Ok(id.to_string())
            }
            Err(e) => Err(e),
        }
    }
}