Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge dev branch to main #156

Merged
merged 18 commits into from
Sep 15, 2023
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ Cargo.lock

# test data files
_data
tmp
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ rayon = "^1"
criterion = { version = "0.5", features = ["html_reports"] }
anyhow = "1"
protobuf = "3"
polars = "^0.32"
polars = { version = "^0.32", features = ["polars-io"] }

[[bench]]
harness = false
Expand Down
3 changes: 3 additions & 0 deletions Justfile
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ replay-example:
study-example:
cargo run --package tradingview-rs --example study

csv_export-example:
cargo run --package tradingview-rs --example csv_export

lines-of-code:
@git ls-files | grep '\.rs' | xargs wc -l

Expand Down
43 changes: 33 additions & 10 deletions examples/chart.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,44 @@ use tradingview_rs::{
},
models::Interval,
socket::DataServer,
user::User,
};

#[tokio::main]
async fn main() {
tracing_subscriber::fmt::init();

let session = env::var("TV_SESSION").unwrap();
let signature = env::var("TV_SIGNATURE").unwrap();
// let session = env::var("TV_SESSION").unwrap();
// let signature = env::var("TV_SIGNATURE").unwrap();

let user = User::build()
.session(&session, &signature)
.get()
.await
.unwrap();
// let user = User::build()
// .session(&session, &signature)
// .get()
// .await
// .unwrap();

// let access_id = env::var("CF_ACCESS_CLIENT_ID").unwrap();
// let access_secret = env::var("CF_ACCESS_CLIENT_SECRET").unwrap();

// let mut headers = HeaderMap::new();
// headers.insert("CF-Access-Client-Id", HeaderValue::from_static(&access_id));
// headers.insert(
// "CF-Access-Client-Secret",
// HeaderValue::from_static(&access_secret),
// );

// let client = Client::builder()
// .default_headers(headers)
// .build()
// .unwrap()
// .get("https://tvmisc.bitbytelab.io/api/token/free")
// .send()
// .await
// .unwrap()
// .json()
// .await
// .unwrap();

let auth_token = env::var("TV_AUTH_TOKEN").unwrap();

let handlers = ChartCallbackFn {
on_chart_data: Box::new(|data| Box::pin(on_chart_data(data))),
Expand All @@ -32,7 +55,7 @@ async fn main() {

let mut socket = WebSocket::build()
.server(DataServer::ProData)
.auth_token(user.auth_token)
.auth_token(auth_token)
.connect(handlers)
.await
.unwrap();
Expand Down Expand Up @@ -76,7 +99,7 @@ async fn main() {

async fn on_chart_data(data: ChartSeries) -> Result<(), tradingview_rs::error::Error> {
// info!("on_chart_data: {:?}", data);
let end = data.data.first().unwrap().0;
let end = data.data.first().unwrap().timestamp;
info!("on_chart_data: {:?} - {:?}", data.data.len(), end);
Ok(())
}
Expand Down
90 changes: 90 additions & 0 deletions examples/csv_export.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
use std::{env, process::exit};

use polars::prelude::*;

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(
"NASDAQ:AMZN",
ChartOptions {
resolution: Interval::Daily,
bar_count: 50_000,
..Default::default()
},
)
.await
.unwrap();
socket.subscribe().await;
}

async fn on_chart_data(data: ChartSeries) -> Result<(), tradingview_rs::error::Error> {
// Assuming you have the OHLCV data in a Vec<OHLCV> named `data`

// Extract the individual fields from OHLCV objects
let timestamps: Vec<f64> = data.data.iter().map(|item| item.timestamp).collect();
let opens: Vec<f64> = data.data.iter().map(|item| item.open).collect();
let highs: Vec<f64> = data.data.iter().map(|item| item.high).collect();
let lows: Vec<f64> = data.data.iter().map(|item| item.low).collect();
let closes: Vec<f64> = data.data.iter().map(|item| item.close).collect();
let volumes: Vec<f64> = data.data.iter().map(|item| item.volume).collect();

// Create a Polars DataFrame
let mut df = DataFrame::new(vec![
Series::new("timestamp", timestamps),
Series::new("open", opens),
Series::new("high", highs),
Series::new("low", lows),
Series::new("close", closes),
Series::new("volume", volumes),
])
.unwrap();

let csv_out = "tmp/nasdaq_amzn.csv";
if std::fs::metadata(csv_out).is_err() {
let f = std::fs::File::create(csv_out).unwrap();
CsvWriter::new(f).finish(&mut df).unwrap();
}

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);
exit(0);
}
2 changes: 1 addition & 1 deletion examples/replay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ async fn main() {
}

async fn on_chart_data(data: ChartSeries) -> Result<(), tradingview_rs::error::Error> {
let end = data.data.first().unwrap().0;
let end = data.data.first().unwrap().timestamp;
info!("on_chart_data: {:?} - {:?}", data.data.len(), end);
Ok(())
}
Expand Down
18 changes: 10 additions & 8 deletions examples/study.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,16 @@ use tradingview_rs::{
async fn main() {
tracing_subscriber::fmt::init();

let session = env::var("TV_SESSION").unwrap();
let signature = env::var("TV_SIGNATURE").unwrap();
let auth_token = env::var("TV_AUTH_TOKEN").unwrap();

let user = User::build()
.session(&session, &signature)
.get()
.await
.unwrap();
// let session = env::var("TV_SESSION").unwrap();
// let signature = env::var("TV_SIGNATURE").unwrap();

// let user = User::build()
// .session(&session, &signature)
// .get()
// .await
// .unwrap();

let handlers = ChartCallbackFn {
on_chart_data: Box::new(|data| Box::pin(on_chart_data(data))),
Expand All @@ -32,7 +34,7 @@ async fn main() {

let mut socket = WebSocket::build()
.server(DataServer::ProData)
.auth_token(user.auth_token)
.auth_token(auth_token)
.connect(handlers)
.await
.unwrap();
Expand Down
2 changes: 1 addition & 1 deletion src/chart/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ pub struct ChartDataChanges {
pub struct ChartSeries {
pub symbol_id: String,
pub interval: Interval,
pub data: Vec<(f64, f64, f64, f64, f64, f64)>,
pub data: Vec<OHLCV>,
}

#[derive(Debug, Default, Clone, Serialize)]
Expand Down
Loading