tradingview-rs 0.3.0

Tradingview datafeed api `tradingview-rs` project.
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
//! High-level client for fetching TradingView study and fundamental data.

use std::sync::{Arc, Mutex};
use std::time::Instant;

use serde::Deserialize;
use serde_json::Value;
use tracing::{debug, error, instrument, trace, warn};

use crate::Result;
use crate::chart::{DataPoint, SymbolInfo};
use crate::error::{Error, TradingViewError};
use crate::live::handler::command::Command;
use crate::live::handler::{CommandTx, Handler, HandlerFactory};
use crate::live::models::{DataServer, TradingViewDataEvent};
use crate::live::websocket::WebSocketClient;
use crate::study::request::StudyRequest;
use crate::study::result::StudyResult;
use crate::study::state::StudyState;
use crate::utils::symbol_init;

/// High-level client for fetching TradingView study and fundamental data.
pub struct StudyClient {
    pub(crate) auth_token: String,
    pub(crate) server: DataServer,
}

impl StudyClient {
    /// Creates a new [`StudyClient`] with the specified auth token and data server.
    pub fn new(auth_token: impl Into<String>, server: DataServer) -> Self {
        Self {
            auth_token: auth_token.into(),
            server,
        }
    }

    /// Retrieves study or fundamental data according to the given request.
    #[instrument(skip(self), fields(symbol, exchange))]
    pub async fn retrieve(&self, request: StudyRequest) -> Result<StudyResult> {
        let started = Instant::now();
        let (symbol, exchange) = request.resolve_symbol_exchange()?;
        debug!(symbol = %symbol, exchange = %exchange, "study retrieval started");

        let study_id = request
            .study_id
            .clone()
            .unwrap_or_else(|| format!("st_{}", crate::utils::gen_id()));
        let study_sub_id = "st1".to_string();

        let state = Arc::new(Mutex::new(StudyState::with_capacity_and_notify(
            request.base_bar_count as usize,
            Arc::new(tokio::sync::Notify::new()),
        )));

        let (cmd_tx, _cmd_rx) = tokio::sync::mpsc::channel::<Command>(16);
        let factory = StudyDataHandlerFactory::new(Arc::clone(&state), &study_id);
        let handler = factory.create(cmd_tx);

        let ws = WebSocketClient::builder()
            .auth_token(&self.auth_token)
            .server(self.server)
            .handler(handler)
            .build()
            .await?;

        // ── Protocol sequence ──────────────────────────────────────────
        // TradingView study retrieval protocol:
        //   1. chart_create_session → create chart session
        //   2. resolve_symbol       → resolve instrument and receive SymbolInfo
        //   3. create_series        → create base price series
        //   4. create_study         → attach study to chart series (sds_1)
        //   5. wait for matching study_completed or error
        let instrument = format!("{exchange}:{symbol}");
        let chart_session = format!("cs_{}", crate::utils::gen_id());
        let symbol_series_id = format!("sds_sym_{}", crate::utils::gen_id());
        let series_identifier = "sds_1".to_string();
        let series_id = "s1".to_string();

        // 1. Create chart session.
        ws.create_chart_session(&chart_session).await?;
        debug!(session = %chart_session, "chart session created");

        // 2. Resolve symbol within the session.
        let symbol_init_str = symbol_init().instrument(&instrument).call()?;
        ws.send(
            "resolve_symbol",
            &[
                Value::from(chart_session.as_str()),
                Value::from(symbol_series_id.as_str()),
                Value::from(symbol_init_str),
            ],
        )
        .await?;
        debug!(instrument = %instrument, "symbol resolution requested");

        // 3. Create base data series.
        let create_series_args = crate::historical::client::build_create_series_args(
            &chart_session,
            &series_identifier,
            &series_id,
            &symbol_series_id,
            request.interval,
            Some(request.base_bar_count),
            None,
        );
        ws.send("create_series", &create_series_args).await?;
        debug!(
            interval = ?request.interval,
            bars = request.base_bar_count,
            "base series created"
        );

        // 4. Create study attached to base series reference (sds_1).
        ws.create_study()
            .chart_session(&chart_session)
            .study_ids(&[&study_id, &study_sub_id])
            .chart_series_id(&series_identifier)
            .study(request.study)
            .call()
            .await?;
        debug!(study_id = %study_id, "study created");

        Arc::clone(&ws).spawn_reader_task();

        let result = tokio::time::timeout(request.timeout, Self::wait_for_completion(&state)).await;

        let mut state_guard = state.lock().unwrap();
        let total_points = state_guard.total_points;
        let data = state_guard.finalize();
        let elapsed = started.elapsed();

        match result {
            Ok(_) => {
                if state_guard.errored {
                    let msg = state_guard
                        .error_message
                        .take()
                        .unwrap_or_else(|| "study data retrieval failed".to_string());
                    return Err(Error::Internal(msg.into()));
                }
                let symbol_info = state_guard
                    .symbol_info
                    .take()
                    .ok_or_else(|| Error::Internal("no symbol info received".into()))?;
                Ok(StudyResult {
                    symbol_info,
                    data,
                    study_id,
                    total_points_received: total_points,
                    elapsed,
                })
            }
            Err(_) => Err(Error::Timeout("study data retrieval timed out".into())),
        }
    }

    pub(crate) async fn wait_for_completion(state: &Arc<Mutex<StudyState>>) {
        let notify = {
            let guard = state.lock().unwrap();
            guard.notify.clone()
        };
        loop {
            // Register as waiter before checking predicate to avoid lost wakeups.
            let notified = notify.notified();
            tokio::pin!(notified);
            notified.as_mut().enable();

            {
                let guard = state.lock().unwrap();
                if guard.is_done() {
                    break;
                }
            }

            notified.await;
        }
    }
}

// =============================================================================
// StudyDataHandler
// =============================================================================

/// Event handler that accumulates study data points into shared [`StudyState`].
#[derive(Clone)]
pub(crate) struct StudyDataHandler {
    state: Arc<Mutex<StudyState>>,
    study_id: String,
    #[allow(dead_code)]
    cmd_tx: CommandTx,
}

impl Handler for StudyDataHandler {
    fn handle_events(&self, event: TradingViewDataEvent, message: &[Value]) {
        match event {
            TradingViewDataEvent::OnSymbolResolved => {
                // resolve_symbol response: [session, symbol_series_id, SymbolInfo]
                if let Some(sym_info) = message.get(2)
                    && let Ok(info) = SymbolInfo::deserialize(sym_info)
                {
                    debug!(name = %info.name, "symbol resolved");
                    self.state.lock().unwrap().record_symbol_info(info);
                }
            }
            TradingViewDataEvent::OnChartData | TradingViewDataEvent::OnChartDataUpdate => {
                // timescale_update or du message:
                // message[0]: chart_session
                // message[1]: object with study_id or series_id keys
                if message.len() < 2 {
                    return;
                }
                if let Some(obj) = message[1].as_object()
                    && let Some(study_val) = obj.get(&self.study_id)
                    && let Some(st_arr) = study_val.get("st").and_then(|v| v.as_array())
                {
                    let mut points = Vec::with_capacity(st_arr.len());
                    for v in st_arr {
                        if let Ok(point) = DataPoint::deserialize(v) {
                            points.push(point);
                        }
                    }
                    if !points.is_empty() {
                        let count = points.len();
                        let mut state = self.state.lock().unwrap();
                        state.record_points(points, count);
                    }
                }
            }
            TradingViewDataEvent::OnStudyCompleted => {
                // study_completed message: [chart_session, study_id, study_sub_id]
                let completed_id = message.get(1).and_then(|v| v.as_str());
                if completed_id == Some(&self.study_id) {
                    debug!(study_id = %self.study_id, "study completed");
                    self.state.lock().unwrap().complete();
                } else {
                    trace!(
                        completed = ?completed_id,
                        expected = %self.study_id,
                        "ignoring study_completed for unrelated study"
                    );
                }
            }
            TradingViewDataEvent::OnError(tv_error) => {
                // If the error is a study_error with an explicit study_id not matching ours, ignore it.
                if tv_error == TradingViewError::StudyError
                    && let Some(err_study_id) = message.get(1).and_then(|v| v.as_str())
                    && err_study_id != self.study_id
                {
                    warn!(
                        err_study_id = %err_study_id,
                        expected = %self.study_id,
                        "ignoring study_error for unrelated study"
                    );
                    return;
                }

                error!(?tv_error, "tradingview study/protocol error");
                let err_details = message
                    .iter()
                    .filter_map(|v| v.as_str())
                    .collect::<Vec<_>>()
                    .join(" ");
                let msg = if err_details.is_empty() {
                    format!("tradingview error: {tv_error:?}")
                } else {
                    format!("tradingview error: {tv_error:?}: {err_details}")
                };
                let mut state = self.state.lock().unwrap();
                state.fail(msg);
            }
            _ => {}
        }
    }

    fn handle_quote_data(&self, _message: &[Value]) {}
    fn handle_series_data(&self, _event: TradingViewDataEvent, _messages: &[Value]) {}

    fn notify_error(&self, error: Error, _message: &[Value]) {
        warn!(?error, "study handler error");
        let mut state = self.state.lock().unwrap();
        if state.record_error() {
            state.fail(format!("too many errors: {error:?}"));
        }
    }
}

// =============================================================================
// StudyDataHandlerFactory
// =============================================================================

/// Factory for creating [`StudyDataHandler`] instances that share a common [`StudyState`].
pub(crate) struct StudyDataHandlerFactory {
    state: Arc<Mutex<StudyState>>,
    study_id: String,
}

impl StudyDataHandlerFactory {
    /// Create a new factory wrapping the given shared state and study ID.
    pub fn new(state: Arc<Mutex<StudyState>>, study_id: impl Into<String>) -> Self {
        Self {
            state,
            study_id: study_id.into(),
        }
    }
}

impl HandlerFactory for StudyDataHandlerFactory {
    type Handler = StudyDataHandler;

    fn create(&self, command_tx: CommandTx) -> Self::Handler {
        StudyDataHandler {
            state: Arc::clone(&self.state),
            study_id: self.study_id.clone(),
            cmd_tx: command_tx,
        }
    }
}

// =============================================================================
// Unit Tests
// =============================================================================

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

    fn setup_handler(study_id: &str) -> (StudyDataHandler, Arc<Mutex<StudyState>>) {
        let state = Arc::new(Mutex::new(StudyState::new()));
        let (cmd_tx, _cmd_rx) = tokio::sync::mpsc::channel(16);
        let handler = StudyDataHandler {
            state: Arc::clone(&state),
            study_id: study_id.to_string(),
            cmd_tx,
        };
        (handler, state)
    }

    #[test]
    fn test_matching_study_id_routing() {
        let (handler, state) = setup_handler("st_target");

        // Payload with target study and points
        let payload = json!([
            "cs_test",
            {
                "st_target": {
                    "st": [
                        {"i": -10, "v": [1600000000.0, 100.5, 200.5]},
                        {"i": -9, "v": [1600086400.0, 105.0, 210.0]}
                    ]
                }
            }
        ]);

        handler.handle_events(
            TradingViewDataEvent::OnChartData,
            payload.as_array().unwrap(),
        );

        let guard = state.lock().unwrap();
        assert_eq!(guard.data.len(), 2);
        assert_eq!(guard.data[0].index, -10);
        assert_eq!(guard.data[0].value, vec![1600000000.0, 100.5, 200.5]);
        assert_eq!(guard.data[1].index, -9);
        assert_eq!(guard.data[1].value, vec![1600086400.0, 105.0, 210.0]);
        assert_eq!(guard.total_points, 2);
    }

    #[test]
    fn test_ignoring_unrelated_studies() {
        let (handler, state) = setup_handler("st_target");

        // Payload with only unrelated study and base series
        let payload = json!([
            "cs_test",
            {
                "st_unrelated": {
                    "st": [
                        {"i": 0, "v": [1600000000.0, 999.0]}
                    ]
                },
                "sds_1": {
                    "s": [
                        {"i": 0, "v": [1600000000.0, 10.0, 20.0, 5.0, 15.0, 1000.0]}
                    ]
                }
            }
        ]);

        handler.handle_events(
            TradingViewDataEvent::OnChartDataUpdate,
            payload.as_array().unwrap(),
        );

        let guard = state.lock().unwrap();
        assert!(guard.data.is_empty());
        assert_eq!(guard.total_points, 0);
    }

    #[test]
    fn test_mixed_payload_extracts_only_target() {
        let (handler, state) = setup_handler("st_target");

        let payload = json!([
            "cs_test",
            {
                "st_unrelated": {
                    "st": [{"i": 1, "v": [100.0, 999.0]}]
                },
                "st_target": {
                    "st": [{"i": 2, "v": [200.0, 888.0]}]
                }
            }
        ]);

        handler.handle_events(
            TradingViewDataEvent::OnChartData,
            payload.as_array().unwrap(),
        );

        let guard = state.lock().unwrap();
        assert_eq!(guard.data.len(), 1);
        assert_eq!(guard.data[0].index, 2);
        assert_eq!(guard.data[0].value, vec![200.0, 888.0]);
    }

    #[test]
    fn test_completion_filtering() {
        let (handler, state) = setup_handler("st_target");

        // Unrelated completion
        let unrelated = json!(["cs_test", "st_other", "s1_st1"]);
        handler.handle_events(
            TradingViewDataEvent::OnStudyCompleted,
            unrelated.as_array().unwrap(),
        );
        assert!(!state.lock().unwrap().completed);

        // Matching completion
        let matching = json!(["cs_test", "st_target", "s1_st1"]);
        handler.handle_events(
            TradingViewDataEvent::OnStudyCompleted,
            matching.as_array().unwrap(),
        );
        assert!(state.lock().unwrap().completed);
    }

    #[test]
    fn test_study_error_filtering() {
        let (handler, state) = setup_handler("st_target");

        // Unrelated study error should be ignored
        let unrelated_err = json!(["cs_test", "st_other", "study initialization failed"]);
        handler.handle_events(
            TradingViewDataEvent::OnError(TradingViewError::StudyError),
            unrelated_err.as_array().unwrap(),
        );
        assert!(!state.lock().unwrap().errored);

        // Matching study error should fail state
        let matching_err = json!(["cs_test", "st_target", "invalid indicator parameters"]);
        handler.handle_events(
            TradingViewDataEvent::OnError(TradingViewError::StudyError),
            matching_err.as_array().unwrap(),
        );
        let guard = state.lock().unwrap();
        assert!(guard.errored);
        assert!(
            guard
                .error_message
                .as_ref()
                .unwrap()
                .contains("invalid indicator parameters")
        );
    }

    #[test]
    fn test_protocol_error_triggers_failure() {
        let (handler, state) = setup_handler("st_target");

        let protocol_err = json!(["cs_test", "critical error occurred"]);
        handler.handle_events(
            TradingViewDataEvent::OnError(TradingViewError::CriticalError),
            protocol_err.as_array().unwrap(),
        );
        let guard = state.lock().unwrap();
        assert!(guard.errored);
        assert!(guard.error_message.is_some());
    }

    #[test]
    fn test_sort_and_dedup_points_replaces_with_latest_point() {
        let mut state = StudyState::new();

        // Points arriving in chronological sequence:
        // 1. Point at ts=1600020000, idx=10 with initial value 10.0
        // 2. Point at ts=1600010000, idx=5 with value 100.0
        // 3. Update to ts=1600020000, idx=10 with NEW value -42.5 (should overwrite 10.0)
        // 4. Point at ts=1600000000, idx=1 with value 0.0
        let p1 = DataPoint {
            index: 10,
            value: vec![1600020000.0, 10.0],
        };
        let p2 = DataPoint {
            index: 5,
            value: vec![1600010000.0, 100.0],
        };
        let p1_update = DataPoint {
            index: 10,
            value: vec![1600020000.0, -42.5, 1e100],
        };
        let p0 = DataPoint {
            index: 1,
            value: vec![1600000000.0, 0.0],
        };

        state.record_points(vec![p1, p2, p1_update, p0], 4);
        let finalized = state.finalize();

        // Must have 3 points ordered ascending by timestamp
        assert_eq!(finalized.len(), 3);
        assert_eq!(finalized[0].value[0] as i64, 1600000000);
        assert_eq!(finalized[1].value[0] as i64, 1600010000);
        assert_eq!(finalized[2].value[0] as i64, 1600020000);
        // Latest point for index 10 must replace earlier value:
        assert_eq!(finalized[2].value[1], -42.5);
        assert_eq!(finalized[2].value[2], 1e100);
    }

    #[test]
    fn test_captured_time_scale_update_fixture() {
        let fixture_str = include_str!("../../tests/data/time_scale_update.json");
        let messages: Vec<Value> = serde_json::from_str(fixture_str).unwrap();

        // Track "st3" which appears in the fixture
        let (handler, state) = setup_handler("st3");

        for msg in &messages {
            let m = msg["m"].as_str().unwrap();
            let p = msg["p"].as_array().unwrap();
            let event = TradingViewDataEvent::from(m.to_string());
            handler.handle_events(event, p);
        }

        let mut guard = state.lock().unwrap();
        assert!(guard.completed, "st3 should have received study_completed");
        assert!(!guard.errored);
        assert!(!guard.data.is_empty(), "st3 should have captured points");

        let points = guard.finalize();
        // Points must be sorted ascending by timestamp
        for window in points.windows(2) {
            let ts_a = window[0].value[0] as i64;
            let ts_b = window[1].value[0] as i64;
            assert!(
                ts_a <= ts_b,
                "points must be ordered ascending: {ts_a} > {ts_b}"
            );
        }
    }

    #[test]
    fn test_captured_fixture_ignores_unrelated() {
        let fixture_str = include_str!("../../tests/data/time_scale_update.json");
        let messages: Vec<Value> = serde_json::from_str(fixture_str).unwrap();

        // Track a study ID not present in the fixture
        let (handler, state) = setup_handler("st_nonexistent");

        for msg in &messages {
            let m = msg["m"].as_str().unwrap();
            let p = msg["p"].as_array().unwrap();
            let event = TradingViewDataEvent::from(m.to_string());
            handler.handle_events(event, p);
        }

        let guard = state.lock().unwrap();
        assert!(!guard.completed);
        assert!(guard.data.is_empty());
    }
}