binary_options_tools 0.2.0

High-level library for binary options trading automation. Supports PocketOption and ExpertOption with real-time data streaming, WebSocket API access, and automated trading strategies.
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
use std::{
    collections::HashMap,
    sync::{
        atomic::{AtomicBool, Ordering},
        Arc,
    },
    time::Duration,
};

use async_trait::async_trait;
use binary_options_tools_core_pre::{
    error::CoreError,
    reimports::{AsyncReceiver, AsyncSender, Message},
    traits::{ApiModule, Rule, RunnerCommand},
};
use rust_decimal::Decimal;
use serde::Deserialize;
use tokio::sync::oneshot;
use tracing::{info, warn};
use uuid::Uuid;

use crate::pocketoption::{
    error::{PocketError, PocketResult},
    state::State,
    types::Deal,
};

const UPDATE_OPENED_DEALS: &str = r#"451-["updateOpenedDeals","#;
const UPDATE_CLOSED_DEALS: &str = r#"451-["updateClosedDeals","#;
const SUCCESS_CLOSE_ORDER: &str = r#"451-["successcloseOrder","#;

#[derive(Debug)]
pub enum Command {
    CheckResult(Uuid, oneshot::Sender<PocketResult<Deal>>),
}

#[derive(Debug)]
pub enum CommandResponse {
    CheckResult(Box<Deal>),
    DealNotFound(Uuid),
}

#[derive(Debug, PartialEq, Eq, Clone, Copy)]
enum ExpectedMessage {
    UpdateClosedDeals,
    UpdateOpenedDeals,
    SuccessCloseOrder,
    None,
}

#[derive(Deserialize)]
struct CloseOrder {
    #[serde(rename = "profit")]
    _profit: Decimal,
    deals: Vec<Deal>,
}

#[derive(Clone)]
pub struct DealsHandle {
    sender: AsyncSender<Command>,
    _receiver: AsyncReceiver<CommandResponse>,
}

impl DealsHandle {
    pub async fn check_result(&self, trade_id: Uuid) -> PocketResult<Deal> {
        let (tx, rx) = oneshot::channel();
        self.sender
            .send(Command::CheckResult(trade_id, tx))
            .await
            .map_err(CoreError::from)?;

        match rx.await {
            Ok(result) => result,
            Err(_) => Err(CoreError::Other("DealsApiModule responder dropped".into()).into()),
        }
    }

    pub async fn check_result_with_timeout(
        &self,
        trade_id: Uuid,
        timeout: Duration,
    ) -> PocketResult<Deal> {
        let (tx, rx) = oneshot::channel();
        self.sender
            .send(Command::CheckResult(trade_id, tx))
            .await
            .map_err(CoreError::from)?;

        match tokio::time::timeout(timeout, rx).await {
            Ok(Ok(result)) => result,
            Ok(Err(_)) => Err(CoreError::Other("DealsApiModule responder dropped".into()).into()),
            Err(_) => Err(PocketError::Timeout {
                task: "check_result".to_string(),
                context: format!("Waiting for trade '{trade_id}' result"),
                duration: timeout,
            }),
        }
    }
}

/// An API module responsible for listening to deal updates,
/// maintaining the shared `TradeState`, and checking trade results.
pub struct DealsApiModule {
    state: Arc<State>,
    ws_receiver: AsyncReceiver<Arc<Message>>,
    command_receiver: AsyncReceiver<Command>,
    _command_responder: AsyncSender<CommandResponse>,
    // Map of Trade ID -> List of waiters expecting the result
    waiting_requests: HashMap<Uuid, Vec<oneshot::Sender<PocketResult<Deal>>>>,
}

impl DealsApiModule {
    async fn process_text_data(&mut self, text: &str, expected: ExpectedMessage) {
        match expected {
            ExpectedMessage::UpdateOpenedDeals => match serde_json::from_str::<Vec<Deal>>(text) {
                Ok(deals) => {
                    self.state.trade_state.update_opened_deals(deals).await;
                }
                Err(e) => warn!("Failed to parse UpdateOpenedDeals (text): {:?}", e),
            },
            ExpectedMessage::UpdateClosedDeals => match serde_json::from_str::<Vec<Deal>>(text) {
                Ok(deals) => {
                    self.state
                        .trade_state
                        .update_closed_deals(deals.clone())
                        .await;
                    for deal in deals {
                        if let Some(waiters) = self.waiting_requests.remove(&deal.id) {
                            info!("Trade closed: {:?}", deal);
                            for tx in waiters {
                                let _ = tx.send(Ok(deal.clone()));
                            }
                        }
                    }
                }
                Err(e) => warn!("Failed to parse UpdateClosedDeals (text): {:?}", e),
            },
            ExpectedMessage::SuccessCloseOrder => {
                // Try parsing as CloseOrder struct first
                match serde_json::from_str::<CloseOrder>(text) {
                    Ok(close_order) => {
                        self.state
                            .trade_state
                            .update_closed_deals(close_order.deals.clone())
                            .await;
                        for deal in close_order.deals {
                            if let Some(waiters) = self.waiting_requests.remove(&deal.id) {
                                info!("Trade closed: {:?}", deal);
                                for tx in waiters {
                                    let _ = tx.send(Ok(deal.clone()));
                                }
                            }
                        }
                    }
                    Err(_) => {
                        // Fallback: Try parsing as Vec<Deal> (sometimes API sends just the list)
                        match serde_json::from_str::<Vec<Deal>>(text) {
                            Ok(deals) => {
                                self.state
                                    .trade_state
                                    .update_closed_deals(deals.clone())
                                    .await;
                                for deal in deals {
                                    if let Some(waiters) = self.waiting_requests.remove(&deal.id) {
                                        info!("Trade closed (fallback): {:?}", deal);
                                        for tx in waiters {
                                            let _ = tx.send(Ok(deal.clone()));
                                        }
                                    }
                                }
                            }
                            Err(e) => warn!("Failed to parse SuccessCloseOrder (text): {:?}", e),
                        }
                    }
                }
            }
            ExpectedMessage::None => {}
        }
    }
}

#[async_trait]
impl ApiModule<State> for DealsApiModule {
    type Command = Command;
    type CommandResponse = CommandResponse;
    type Handle = DealsHandle;

    fn new(
        state: Arc<State>,
        command_receiver: AsyncReceiver<Self::Command>,
        command_responder: AsyncSender<Self::CommandResponse>,
        ws_receiver: AsyncReceiver<Arc<Message>>,
        _ws_sender: AsyncSender<Message>,
        _: AsyncSender<RunnerCommand>,
    ) -> Self {
        Self {
            state,
            ws_receiver,
            command_receiver,
            _command_responder: command_responder,
            waiting_requests: HashMap::new(),
        }
    }

    fn create_handle(
        sender: AsyncSender<Self::Command>,
        receiver: AsyncReceiver<Self::CommandResponse>,
    ) -> Self::Handle {
        DealsHandle {
            sender,
            _receiver: receiver,
        }
    }

    async fn run(&mut self) -> binary_options_tools_core_pre::error::CoreResult<()> {
        let mut expected = ExpectedMessage::None;
        loop {
            tokio::select! {
                biased;
                msg_res = self.ws_receiver.recv() => {
                    match msg_res {
                        Ok(msg) => {
                            tracing::debug!("Received message: {:?}", msg);
                            match msg.as_ref() {
                                Message::Text(text) => {
                                    let mut data_text = None;
                                    let mut current_expected = ExpectedMessage::None;
                                    if text.starts_with(UPDATE_OPENED_DEALS) {
                                        current_expected = ExpectedMessage::UpdateOpenedDeals;
                                        data_text = text.strip_prefix(UPDATE_OPENED_DEALS);
                                    } else if text.starts_with(UPDATE_CLOSED_DEALS) {
                                        current_expected = ExpectedMessage::UpdateClosedDeals;
                                        data_text = text.strip_prefix(UPDATE_CLOSED_DEALS);
                                    } else if text.starts_with(SUCCESS_CLOSE_ORDER) {
                                        current_expected = ExpectedMessage::SuccessCloseOrder;
                                        data_text = text.strip_prefix(SUCCESS_CLOSE_ORDER);
                                    }

                                    if let Some(data) = data_text {
                                        let trimmed = data.trim();

                                        // Socket.IO 4.x binary placeholder check
                                        if trimmed.contains(r#""_placeholder":true"#) {
                                            tracing::debug!(target: "DealsApiModule", "Detected binary placeholder, waiting for binary payload for {:?}", current_expected);
                                            expected = current_expected;
                                            continue;
                                        }

                                        if !trimmed.is_empty() && trimmed != "]" && trimmed != ",]" {
                                            // It's a 1-step message, process the data now
                                            let json_data = trimmed.strip_suffix(']').unwrap_or(trimmed);
                                            self.process_text_data(json_data, current_expected).await;
                                            expected = ExpectedMessage::None;
                                            continue;
                                        } else {
                                            // Header-only, wait for data
                                            expected = current_expected;
                                            continue;
                                        }
                                    }

                                    if expected != ExpectedMessage::None {
                                        // Handle data as text if expected is set and this is not a header
                                        self.process_text_data(text, expected).await;
                                        expected = ExpectedMessage::None;
                                    }
                                },
                                Message::Binary(data) => {
                                    // Handle binary messages
                                    match expected {
                                        ExpectedMessage::UpdateOpenedDeals => {
                                            match serde_json::from_slice::<Vec<Deal>>(data) {
                                                Ok(deals) => {
                                                    self.state.trade_state.update_opened_deals(deals).await;
                                                },
                                                Err(e) => warn!("Failed to parse UpdateOpenedDeals (binary): {:?}", e),
                                            }
                                        }
                                        ExpectedMessage::UpdateClosedDeals => {
                                            match serde_json::from_slice::<Vec<Deal>>(data) {
                                                Ok(deals) => {
                                                    self.state.trade_state.update_closed_deals(deals.clone()).await;
                                                    for deal in deals {
                                                        if let Some(waiters) = self.waiting_requests.remove(&deal.id) {
                                                            info!("Trade closed: {:?}", deal);
                                                            for tx in waiters {
                                                                let _ = tx.send(Ok(deal.clone()));
                                                            }
                                                        }
                                                    }
                                                },
                                                Err(e) => warn!("Failed to parse UpdateClosedDeals (binary): {:?}", e),
                                            }
                                        }
                                        ExpectedMessage::SuccessCloseOrder => {
                                            match serde_json::from_slice::<CloseOrder>(data) {
                                                Ok(close_order) => {
                                                    self.state.trade_state.update_closed_deals(close_order.deals.clone()).await;
                                                    for deal in close_order.deals {
                                                        if let Some(waiters) = self.waiting_requests.remove(&deal.id) {
                                                            info!("Trade closed: {:?}", deal);
                                                            for tx in waiters {
                                                                let _ = tx.send(Ok(deal.clone()));
                                                            }
                                                        }
                                                    }
                                                },
                                                Err(_) => {
                                                     // Fallback: Try parsing as Vec<Deal>
                                                     match serde_json::from_slice::<Vec<Deal>>(data) {
                                                        Ok(deals) => {
                                                            self.state.trade_state.update_closed_deals(deals.clone()).await;
                                                            for deal in deals {
                                                                if let Some(waiters) = self.waiting_requests.remove(&deal.id) {
                                                                    info!("Trade closed (fallback): {:?}", deal);
                                                                    for tx in waiters {
                                                                        let _ = tx.send(Ok(deal.clone()));
                                                                    }
                                                                }
                                                            }
                                                        }
                                                        Err(e) => warn!("Failed to parse SuccessCloseOrder (binary): {:?}", e),
                                                    }
                                                }
                                            }
                                        },
                                        ExpectedMessage::None => {
                                            let payload_preview = if data.len() > 64 {
                                                format!("Payload ({} bytes, truncated): {:?}", data.len(), &data[..64])
                                            } else {
                                                format!("Payload ({} bytes): {:?}", data.len(), data)
                                            };
                                            warn!(target: "DealsApiModule", "Received unexpected binary message when no header was seen. {}", payload_preview);
                                        }
                                    }
                                    expected = ExpectedMessage::None;
                                },
                                _ => {}
                            }
                        }
                        Err(_) => break,
                    }
                }
                cmd_res = self.command_receiver.recv() => {
                    match cmd_res {
                        Ok(cmd) => {
                            match cmd {
                                Command::CheckResult(trade_id, responder) => {
                                    if self.state.trade_state.contains_opened_deal(trade_id).await {
                                        // If the deal is still opened, add it to the waitlist
                                        self.waiting_requests.entry(trade_id).or_default().push(responder);
                                    } else if let Some(deal) = self.state.trade_state.get_closed_deal(trade_id).await {
                                        // If the deal is already closed, send the result immediately
                                        let _ = responder.send(Ok(deal));
                                    } else {
                                        // If the deal is not found, send a DealNotFound response
                                        let _ = responder.send(Err(PocketError::DealNotFound(trade_id)));
                                    }
                                }
                            }
                        }
                        Err(_) => break,
                    }
                }
            }
        }
        Ok(())
    }

    fn rule(_: Arc<State>) -> Box<dyn Rule + Send + Sync> {
        // This rule will match messages like:
        // 451-["updateOpenedDeals",...]
        // 451-["updateClosedDeals",...]
        // 451-["successcloseOrder",...]

        Box::new(DealsUpdateRule::new(vec![
            UPDATE_CLOSED_DEALS,
            UPDATE_OPENED_DEALS,
            SUCCESS_CLOSE_ORDER,
        ]))
    }
}

/// Create a new custom rule that matches the specific patterns and also returns true for strings
/// that starts with any of the patterns
struct DealsUpdateRule {
    valid: AtomicBool,
    patterns: Vec<String>,
}

impl DealsUpdateRule {
    /// Create a new MultiPatternRule with the specified patterns
    ///
    /// # Arguments
    /// * `patterns` - The string patterns to match against incoming messages
    pub fn new(patterns: Vec<impl ToString>) -> Self {
        Self {
            valid: AtomicBool::new(false),
            patterns: patterns.into_iter().map(|p| p.to_string()).collect(),
        }
    }
}

impl Rule for DealsUpdateRule {
    fn call(&self, msg: &Message) -> bool {
        match msg {
            Message::Text(text) => {
                for pattern in &self.patterns {
                    if text.starts_with(pattern) {
                        let remaining = &text[pattern.len()..];
                        let trimmed_rem = remaining.trim();
                        let has_placeholder = trimmed_rem.contains(r#""_placeholder":true"#);
                        let is_header_only = trimmed_rem.is_empty()
                            || trimmed_rem == "]"
                            || trimmed_rem == ",]"
                            || has_placeholder;

                        if is_header_only {
                            self.valid.store(true, Ordering::SeqCst);
                            return true;
                        } else {
                            self.valid.store(false, Ordering::SeqCst);
                            return true;
                        }
                    }
                }

                if let Some(start) = text.find('[') {
                    if let Ok(value) = serde_json::from_str::<serde_json::Value>(&text[start..]) {
                        if let Some(arr) = value.as_array() {
                            if arr.first().and_then(|v| v.as_str()).is_some() {
                                // It's an event, but doesn't match our pattern.
                                // Ignore it and don't consume 'valid'.
                                return false;
                            }
                        }
                    }
                }

                if self.valid.load(Ordering::SeqCst) {
                    self.valid.store(false, Ordering::SeqCst);
                    return true;
                }
                false
            }
            Message::Binary(_) => {
                if self.valid.load(Ordering::SeqCst) {
                    self.valid.store(false, Ordering::SeqCst);
                    true
                } else {
                    false
                }
            }
            _ => false,
        }
    }

    fn reset(&self) {
        self.valid.store(false, Ordering::SeqCst)
    }
}