Skip to content

Commit

Permalink
added some docs
Browse files Browse the repository at this point in the history
  • Loading branch information
nguyenthdat committed Sep 15, 2023
1 parent 65f6290 commit c8c7157
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 0 deletions.
63 changes: 63 additions & 0 deletions examples/csv_export.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
use std::env;

use tracing::info;
use tradingview_rs::{
chart::{
session::{ChartCallbackFn, WebSocket},
ChartOptions, ChartSeries,
},
models::Interval,
socket::DataServer,
};

#[tokio::main]
async fn main() {
tracing_subscriber::fmt::init();
let auth_token = env::var("TV_AUTH_TOKEN").unwrap();

let handlers = ChartCallbackFn {
on_chart_data: Box::new(|data| Box::pin(on_chart_data(data))),
on_symbol_resolved: Box::new(|data| Box::pin(on_symbol_resolved(data))),
on_series_completed: Box::new(|data| Box::pin(on_series_completed(data))),
};

let mut socket = WebSocket::build()
.server(DataServer::ProData)
.auth_token(auth_token)
.connect(handlers)
.await
.unwrap();

socket
.set_market(
"BINANCE:BTCUSDT",
ChartOptions {
resolution: Interval::OneMinute,
bar_count: 50_000,
..Default::default()
},
)
.await
.unwrap();
socket.subscribe().await;
}

async fn on_chart_data(data: ChartSeries) -> Result<(), tradingview_rs::error::Error> {
let end = data.data.first().unwrap().timestamp;
info!("on_chart_data: {:?} - {:?}", data.data.len(), end);
Ok(())
}

async fn on_symbol_resolved(
data: tradingview_rs::chart::SymbolInfo,
) -> Result<(), tradingview_rs::error::Error> {
info!("on_symbol_resolved: {:?}", data);
Ok(())
}

async fn on_series_completed(
data: tradingview_rs::chart::SeriesCompletedMessage,
) -> Result<(), tradingview_rs::error::Error> {
info!("on_series_completed: {:?}", data);
Ok(())
}
58 changes: 58 additions & 0 deletions src/chart/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@ use rayon::prelude::*;
use serde_json::Value;
use std::sync::Mutex;

/// Extracts OHLCV data from a `ChartResponseData` object and returns a vector of `OHLCV` structs.
///
/// # Arguments
///
/// * `data` - A reference to a `ChartResponseData` object containing the series data.
///
/// # Returns
///
/// A vector of `OHLCV` structs containing the extracted data.
pub fn extract_ohlcv_data(data: &ChartResponseData) -> Vec<OHLCV> {
data.series
.iter()
Expand All @@ -20,13 +29,31 @@ pub fn extract_ohlcv_data(data: &ChartResponseData) -> Vec<OHLCV> {
.collect()
}

/// Extracts the data from a `StudyResponseData` object and returns it as a vector of vectors.
///
/// # Arguments
///
/// * `data` - A reference to a `StudyResponseData` object.
///
/// # Returns
///
/// A vector of vectors containing the data from the `StudyResponseData` object.
pub fn extract_studies_data(data: &StudyResponseData) -> Vec<Vec<f64>> {
data.studies
.iter()
.map(|point| point.value.clone())
.collect()
}

/// Extracts OHLCV data from a `ChartResponseData` struct in parallel.
///
/// # Arguments
///
/// * `data` - A reference to a `ChartResponseData` struct.
///
/// # Returns
///
/// A vector of `OHLCV` structs.
pub fn par_extract_ohlcv_data(data: &ChartResponseData) -> Vec<OHLCV> {
data.series
.par_iter()
Expand All @@ -41,6 +68,12 @@ pub fn par_extract_ohlcv_data(data: &ChartResponseData) -> Vec<OHLCV> {
.collect()
}

/// Sorts an array of OHLCV tuples by their timestamp in ascending order.
///
/// # Arguments
///
/// * `tuples` - A mutable reference to an array of OHLCV tuples to be sorted.
///
pub fn sort_ohlcv_tuples(tuples: &mut [OHLCV]) {
tuples.sort_by(|a, b| {
a.timestamp
Expand All @@ -49,6 +82,15 @@ pub fn sort_ohlcv_tuples(tuples: &mut [OHLCV]) {
});
}

/// Update an OHLCV data point in a vector of OHLCV data.
///
/// If a data point with the same timestamp as `new_data` exists in `data`, it will be updated with `new_data`.
/// Otherwise, `new_data` will be added to `data` and the vector will be sorted by timestamp.
///
/// # Arguments
///
/// * `data` - A mutable reference to a vector of OHLCV data.
/// * `new_data` - The new OHLCV data point to update or add to `data`.
pub fn update_ohlcv_data_point(data: &mut Vec<OHLCV>, new_data: OHLCV) {
if let Some(index) = data
.iter()
Expand All @@ -65,6 +107,20 @@ pub fn update_ohlcv_data_point(data: &mut Vec<OHLCV>, new_data: OHLCV) {
}
}

/// Updates the OHLCV data by merging the new data into the old data.
///
/// # Arguments
///
/// * `old_data` - A mutable reference to the old OHLCV data.
/// * `new_data` - A reference to the new OHLCV data to be merged.
///
/// # Example
///
/// ```
/// use tradingview_rs::tools::update_ohlcv_data;
/// use tradingview_rs::models::OHLCV;
///
/// ```
pub fn update_ohlcv_data(old_data: &mut Vec<OHLCV>, new_data: &Vec<OHLCV>) {
let mutex = Mutex::new(old_data);
new_data.par_iter().for_each(|op| {
Expand All @@ -73,6 +129,8 @@ pub fn update_ohlcv_data(old_data: &mut Vec<OHLCV>, new_data: &Vec<OHLCV>) {
});
}

/// Returns the string value at the given index in the provided slice of `Value`s.
/// If the value at the given index is not a string, an empty string is returned.
pub fn get_string_value(values: &[Value], index: usize) -> String {
match values.get(index) {
Some(value) => match value.as_str() {
Expand Down

0 comments on commit c8c7157

Please sign in to comment.