|
| 1 | +#![allow(clippy::unused_unit)] |
| 2 | +use polars::prelude::*; |
| 3 | +use pyo3_polars::derive::polars_expr; |
| 4 | +use serde::Deserialize; |
| 5 | + |
| 6 | +#[derive(Deserialize)] |
| 7 | +pub struct PriceByVolumeKwargs { |
| 8 | + window_size: i32, |
| 9 | + bins: i32, |
| 10 | + center_label: bool, |
| 11 | +} |
| 12 | + |
| 13 | +// fn price_by_volume_dtype(input_fields: &[Field]) -> PolarsResult<Field> { |
| 14 | +// let field = Field::new( |
| 15 | +// "pbv", |
| 16 | +// DataType::List(Box::new(input_fields[1].dtype.clone())), |
| 17 | +// ); |
| 18 | +// Ok(field) |
| 19 | +// } |
| 20 | + |
| 21 | +fn price_by_volume_dtype(input_fields: &[Field]) -> PolarsResult<Field> { |
| 22 | + let field_price = Field::new( |
| 23 | + "price", |
| 24 | + DataType::List(Box::new(input_fields[0].dtype.clone())), |
| 25 | + ); |
| 26 | + let field_volume = Field::new( |
| 27 | + "volume", |
| 28 | + DataType::List(Box::new(input_fields[1].dtype.clone())), |
| 29 | + ); |
| 30 | + let v: Vec<Field> = vec![field_price, field_volume]; |
| 31 | + Ok(Field::new("pbv", DataType::Struct(v))) |
| 32 | +} |
| 33 | + |
| 34 | +#[polars_expr(output_type_func=price_by_volume_dtype)] |
| 35 | +fn price_by_volume(inputs: &[Series], kwargs: PriceByVolumeKwargs) -> PolarsResult<Series> { |
| 36 | + let price = &inputs[0].to_float()?; |
| 37 | + let volume = &inputs[1].to_float()?; |
| 38 | + let window_size = kwargs.window_size as usize; |
| 39 | + let bins = kwargs.bins; |
| 40 | + let mut pbv = vec![]; |
| 41 | + let mut label = vec![]; |
| 42 | + for i in 1..(price.len() + 1) { |
| 43 | + println!("i: {}", i); |
| 44 | + if i < (window_size) { |
| 45 | + pbv.push(None); |
| 46 | + label.push(None); |
| 47 | + } else { |
| 48 | + let mut volume_at_price = vec![]; |
| 49 | + let mut price_label = vec![]; |
| 50 | + let start = (i - window_size) as i64; |
| 51 | + let window_price = price.slice(start, window_size); |
| 52 | + let window_volume = volume.slice(start, window_size); |
| 53 | + let max_price: f64 = window_price.max()?.unwrap(); |
| 54 | + let min_price: f64 = window_price.min()?.unwrap(); |
| 55 | + let range = max_price - min_price; |
| 56 | + let interval = range / bins as f64; |
| 57 | + for n in 0..bins { |
| 58 | + let lower_bound = min_price + n as f64 * interval; |
| 59 | + let upper_bound = min_price + (n + 1) as f64 * interval; |
| 60 | + let center = (lower_bound + upper_bound) / 2.0; |
| 61 | + if n == bins - 1 { |
| 62 | + println!("start: {}, lower: {}", start, lower_bound); |
| 63 | + let v: f64 = window_volume |
| 64 | + .filter(&window_price.gt_eq(lower_bound)?)? |
| 65 | + .sum()?; |
| 66 | + volume_at_price.push(v); |
| 67 | + } else { |
| 68 | + println!( |
| 69 | + "start: {}, lower: {}, upper: {}", |
| 70 | + start, lower_bound, upper_bound |
| 71 | + ); |
| 72 | + let mask = window_price.gt_eq(lower_bound)? & window_price.lt(upper_bound)?; |
| 73 | + let v = window_volume.filter(&mask)?.sum()?; |
| 74 | + volume_at_price.push(v); |
| 75 | + } |
| 76 | + if kwargs.center_label { |
| 77 | + price_label.push(center); |
| 78 | + } else { |
| 79 | + price_label.push(lower_bound); |
| 80 | + } |
| 81 | + } |
| 82 | + println!("{:?}", volume_at_price); |
| 83 | + pbv.push(Some(Series::new("volume", &volume_at_price))); |
| 84 | + label.push(Some(Series::new("price", &price_label))); |
| 85 | + } |
| 86 | + } |
| 87 | + let label_series = Series::new("price", &label); |
| 88 | + let pbv_series = Series::new("volume", &pbv); |
| 89 | + let out = StructChunked::new("pbv", &vec![label_series, pbv_series])?; |
| 90 | + Ok(out.into_series()) |
| 91 | +} |
0 commit comments