Skip to content

Instantly share code, notes, and snippets.

@githubdebugger
Last active September 2, 2024 14:49
Show Gist options
  • Select an option

  • Save githubdebugger/fa1e4d032ca4290430fbca6f8f7c8c3c to your computer and use it in GitHub Desktop.

Select an option

Save githubdebugger/fa1e4d032ca4290430fbca6f8f7c8c3c to your computer and use it in GitHub Desktop.
quote_to_polars_df
use serde::Deserialize;
#[derive(Deserialize)]
pub struct QuoteResponse {
pub status: String,
pub data: std::collections::HashMap<String, Quote>,
}
#[derive(Deserialize)]
pub struct Quote {
pub instrument_token: u32,
pub timestamp: String,
pub last_trade_time: Option<String>,
pub last_price: f64,
pub last_quantity: i64,
pub buy_quantity: i64,
pub sell_quantity: i64,
pub volume: i64,
pub average_price: f64,
pub oi: f64,
pub oi_day_high: f64,
pub oi_day_low: f64,
pub net_change: f64,
pub lower_circuit_limit: f64,
pub upper_circuit_limit: f64,
pub ohlc: OHLC,
pub depth: MarketDepth,
}
#[derive(Deserialize)]
pub struct OHLC {
pub open: f64,
pub high: f64,
pub low: f64,
pub close: f64,
}
#[derive(Deserialize)]
pub struct MarketDepth {
pub buy: Vec<DepthItem>,
pub sell: Vec<DepthItem>,
}
#[derive(Deserialize)]
pub struct DepthItem {
pub price: f64,
pub quantity: i64,
pub orders: i64,
}
#[derive(Deserialize)]
pub struct OHLCQuoteResponse {
pub status: String,
pub data: std::collections::HashMap<String, OHLCQuote>,
}
#[derive(Deserialize)]
pub struct OHLCQuote {
pub instrument_token: u32,
pub last_price: f64,
pub ohlc: OHLC,
}
#[derive(Deserialize)]
pub struct LTPQuoteResponse {
pub status: String,
pub data: std::collections::HashMap<String, LTPQuote>,
}
#[derive(Deserialize)]
pub struct LTPQuote {
pub instrument_token: u32,
pub last_price: f64,
}
pub fn quote_to_polars_df(quote: HashMap<String, Quote>) -> Result<DataFrame, PolarsError> {
let len = quote.len();
let mut symbols = Vec::with_capacity(len);
let mut instrument_tokens = Vec::with_capacity(len);
let mut timestamps = Vec::with_capacity(len);
let mut last_trade_times = Vec::with_capacity(len);
let mut last_prices = Vec::with_capacity(len);
let mut last_quantities = Vec::with_capacity(len);
let mut buy_quantities = Vec::with_capacity(len);
let mut sell_quantities = Vec::with_capacity(len);
let mut volumes = Vec::with_capacity(len);
let mut average_prices = Vec::with_capacity(len);
let mut ois = Vec::with_capacity(len);
let mut oi_day_highs = Vec::with_capacity(len);
let mut oi_day_lows = Vec::with_capacity(len);
let mut net_changes = Vec::with_capacity(len);
let mut lower_circuit_limits = Vec::with_capacity(len);
let mut upper_circuit_limits = Vec::with_capacity(len);
let mut opens = Vec::with_capacity(len);
let mut highs = Vec::with_capacity(len);
let mut lows = Vec::with_capacity(len);
let mut closes = Vec::with_capacity(len);
for (symbol, q) in quote {
symbols.push(symbol);
instrument_tokens.push(q.instrument_token);
timestamps.push(q.timestamp.clone());
last_trade_times.push(q.last_trade_time.clone());
last_prices.push(q.last_price);
last_quantities.push(q.last_quantity);
buy_quantities.push(q.buy_quantity);
sell_quantities.push(q.sell_quantity);
volumes.push(q.volume);
average_prices.push(q.average_price);
ois.push(q.oi);
oi_day_highs.push(q.oi_day_high);
oi_day_lows.push(q.oi_day_low);
net_changes.push(q.net_change);
lower_circuit_limits.push(q.lower_circuit_limit);
upper_circuit_limits.push(q.upper_circuit_limit);
opens.push(q.ohlc.open);
highs.push(q.ohlc.high);
lows.push(q.ohlc.low);
closes.push(q.ohlc.close);
}
let df = DataFrame::new(vec![
Series::new("symbol", symbols),
Series::new("instrument_token", instrument_tokens),
Series::new("timestamp", timestamps),
Series::new("last_trade_time", last_trade_times),
Series::new("last_price", last_prices),
Series::new("last_quantity", last_quantities),
Series::new("buy_quantity", buy_quantities),
Series::new("sell_quantity", sell_quantities),
Series::new("volume", volumes),
Series::new("average_price", average_prices),
Series::new("oi", ois),
Series::new("oi_day_high", oi_day_highs),
Series::new("oi_day_low", oi_day_lows),
Series::new("net_change", net_changes),
Series::new("lower_circuit_limit", lower_circuit_limits),
Series::new("upper_circuit_limit", upper_circuit_limits),
Series::new("open", opens),
Series::new("high", highs),
Series::new("low", lows),
Series::new("close", closes),
])?;
Ok(df)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment