Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ exclude = [
]

[dependencies]
polars = { version = "0.47.1", features = ["lazy", "strings", "temporal", "rolling_window", "parquet", "dtype-categorical", "dtype-struct"] }
polars = { version = "0.53", features = ["lazy", "strings", "temporal", "rolling_window", "parquet", "dtype-categorical", "dtype-struct", "timezones", "regex"] }
chrono = "0.4.34"
thiserror = "2.0.11" # Consider updating if needed, check compatibility
ndarray = "0.16.1" # Consider updating if needed, check compatibility
Expand Down Expand Up @@ -102,4 +102,4 @@ path = "examples/trend_adx_basic.rs"
[lib]
name = "rustalib"
path = "src/lib.rs"
doctest = false
doctest = false
2 changes: 1 addition & 1 deletion examples/bollinger_bands_basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ fn main() -> Result<(), PolarsError> {
);

// Create DataFrame with price data
let df = DataFrame::new(vec![close_prices.clone().into()])?;
let df = DataFrame::new(close_prices.len(), vec![close_prices.clone().into()])?;

// Calculate Bollinger Bands with standard parameters (20-period, 2 standard deviations)
let (middle, upper, lower) = calculate_bollinger_bands(&df, 20, 2.0, "close")?;
Expand Down
10 changes: 5 additions & 5 deletions examples/debug_array_lengths.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,10 @@ fn main() -> Result<(), PolarsError> {
println!(" closes_series: {}", closes_series.len());

// Create DataFrame
let mut df = DataFrame::new(vec![
dates_series.clone().into(),
closes_series.clone().into(),
])?;
let mut df = DataFrame::new(
dates_series.len(),
vec![dates_series.clone().into(), closes_series.clone().into()],
)?;

println!("\nDataFrame height: {}", df.height());

Expand All @@ -144,7 +144,7 @@ fn main() -> Result<(), PolarsError> {
println!("DataFrame height: {}", df.height());

// Try to add the test vector to the DataFrame
let test_series = Series::new("test".into(), test_vec);
let test_series = Column::new("test".into(), test_vec);
match df.with_column(test_series) {
Ok(new_df) => {
println!("Success! New DataFrame height: {}", new_df.height());
Expand Down
2 changes: 1 addition & 1 deletion examples/general/basic_indicators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ fn main() -> Result<(), PolarsError> {
);

// Create DataFrame with price data
let df = DataFrame::new(vec![close_prices.clone().into()])?;
let df = DataFrame::new(close_prices.len(), vec![close_prices.clone().into()])?;

println!("Basic Technical Indicators Example\n");

Expand Down
2 changes: 1 addition & 1 deletion examples/macd_basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ fn main() -> Result<(), PolarsError> {
);

// Create DataFrame with price data
let df = DataFrame::new(vec![close_prices.clone().into()])?;
let df = DataFrame::new(close_prices.len(), vec![close_prices.clone().into()])?;

// Calculate MACD with standard parameters
// fast_period = 12, slow_period = 26, signal_period = 9
Expand Down
7 changes: 5 additions & 2 deletions examples/mean_reversion_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ fn main() -> Result<(), PolarsError> {
let values = Series::new("value".into(), &[10.0, 11.0, 12.0, 13.0, 14.0]);

// Create DataFrame
let mut df = DataFrame::new(vec![dates.clone().into(), values.clone().into()])?;
let mut df = DataFrame::new(
values.len(),
vec![dates.clone().into(), values.clone().into()],
)?;

// Print information
println!("Original DataFrame:");
Expand All @@ -45,7 +48,7 @@ fn main() -> Result<(), PolarsError> {
let doubles_series = Series::new("doubles".into(), doubles);

// Add the new column
df.with_column(doubles_series)?;
df.with_column(doubles_series.into_column())?;
println!("\nDataFrame with new column:");
println!("{}", df);

Expand Down
2 changes: 1 addition & 1 deletion examples/moving_averages_basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ fn main() -> Result<(), PolarsError> {
);

// Create DataFrame with price data
let df = DataFrame::new(vec![close_prices.clone().into()])?;
let df = DataFrame::new(close_prices.len(), vec![close_prices.clone().into()])?;

// Calculate different moving averages
let sma_10 = calculate_sma(&df, "close", 10)?;
Expand Down
12 changes: 6 additions & 6 deletions examples/options/vertical_spreads.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ fn main() -> Result<(), PolarsError> {
let description = Series::new("description".into(), description_vec);

// Create a DataFrame with our spread data
let mut df = DataFrame::new(vec![
let mut df = DataFrame::new(short_strike.len(), vec![
short_strike.into(),
long_strike.into(),
short_price.into(),
Expand Down Expand Up @@ -123,11 +123,11 @@ fn main() -> Result<(), PolarsError> {
}

// Add calculated metrics to the dataframe
df.with_column(Series::new("max_profit".into(), max_profit))?;
df.with_column(Series::new("max_loss".into(), max_loss))?;
df.with_column(Series::new("breakeven".into(), breakeven))?;
df.with_column(Series::new("risk_reward".into(), risk_reward))?;
df.with_column(Series::new("strike_width".into(), strike_width))?;
df.with_column(Column::new("max_profit".into(), max_profit))?;
df.with_column(Column::new("max_loss".into(), max_loss))?;
df.with_column(Column::new("breakeven".into(), breakeven))?;
df.with_column(Column::new("risk_reward".into(), risk_reward))?;
df.with_column(Column::new("strike_width".into(), strike_width))?;

// Display the results
println!("Vertical Spread Metrics:");
Expand Down
2 changes: 1 addition & 1 deletion examples/rsi_basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ fn main() -> Result<(), PolarsError> {
);

// Create DataFrame with date and price data
let df = DataFrame::new(vec![close_prices.clone().into()])?;
let df = DataFrame::new(close_prices.len(), vec![close_prices.clone().into()])?;

// Calculate RSI with 5-period setting for this short example
let rsi = calculate_rsi(&df, 5, "close")?;
Expand Down
2 changes: 1 addition & 1 deletion examples/simple_dataframe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ fn main() -> Result<(), PolarsError> {
let values = Series::new("value".into(), &[10.0, 11.0, 12.0]);

// Create DataFrame
let df = DataFrame::new(vec![dates.clone().into(), values.clone().into()])?;
let df = DataFrame::new(3, vec![dates.clone().into(), values.clone().into()])?;

// Print information
println!(
Expand Down
37 changes: 20 additions & 17 deletions examples/stock/mean_reversion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,14 +163,17 @@ fn main() -> Result<(), PolarsError> {
println!(" Volume count: {}", volumes.len());

// Create DataFrame
let mut df = DataFrame::new(vec![
dates.into(),
opens.into(),
highs.into(),
lows.into(),
closes.clone().into(),
volumes.into(),
])?;
let mut df = DataFrame::new(
opens.len(),
vec![
dates.into(),
opens.into(),
highs.into(),
lows.into(),
closes.clone().into(),
volumes.into(),
],
)?;

// Calculate indicators
// 1. Calculate SMA
Expand All @@ -180,15 +183,15 @@ fn main() -> Result<(), PolarsError> {
sma_20.len(),
df.height()
);
df.with_column(sma_20)?;
df.with_column(sma_20.into_column())?;

// 2. Calculate Bollinger Bands
let (middle, upper, lower) = calculate_bollinger_bands(&df, 20, 2.0, "close")?;
println!("Middle Band length: {}, Upper Band length: {}, Lower Band length: {}, DataFrame height: {}",
println!("Middle Band length: {}, Upper Band length: {}, Lower Band length: {}, DataFrame height: {}",
middle.len(), upper.len(), lower.len(), df.height());
df.with_column(middle)?;
df.with_column(upper)?;
df.with_column(lower)?;
df.with_column(middle.into_column())?;
df.with_column(upper.into_column())?;
df.with_column(lower.into_column())?;

// 3. Calculate RSI
let rsi = calculate_rsi(&df, 14, "close")?;
Expand Down Expand Up @@ -223,12 +226,12 @@ fn main() -> Result<(), PolarsError> {
padded_rsi.len(),
df.height()
);
df.with_column(padded_rsi)?;
df.with_column(padded_rsi.into_column())?;
} else {
// Rename RSI column to ensure consistency
let rsi_values = rsi.f64()?.to_vec();
let renamed_rsi = Series::new("rsi".into(), rsi_values);
df.with_column(renamed_rsi)?;
df.with_column(renamed_rsi.into_column())?;
}

// 4. Calculate Z-Score (a simpler measure of mean reversion)
Expand Down Expand Up @@ -343,7 +346,7 @@ fn calculate_z_score(df: &mut DataFrame, window: usize) -> Result<(), PolarsErro
z_scores.len(),
height
);
df.with_column(Series::new("z_score".into(), z_scores))?;
df.with_column(Column::new("z_score".into(), z_scores))?;

Ok(())
}
Expand Down Expand Up @@ -384,7 +387,7 @@ fn calculate_mean_reversion_signals(df: &mut DataFrame) -> Result<(), PolarsErro
);

// Add signals to dataframe
df.with_column(Series::new("signal".into(), signals))?;
df.with_column(Column::new("signal".into(), signals))?;

Ok(())
}
39 changes: 21 additions & 18 deletions examples/stock/trend_following.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,14 +163,17 @@ fn main() -> Result<(), PolarsError> {
println!(" Volume count: {}", volumes.len());

// Create DataFrame
let mut df = DataFrame::new(vec![
dates.into(),
opens.into(),
highs.into(),
lows.into(),
closes.clone().into(),
volumes.into(),
])?;
let mut df = DataFrame::new(
dates.len(),
vec![
dates.into(),
opens.into(),
highs.into(),
lows.into(),
closes.clone().into(),
volumes.into(),
],
)?;

// Calculate indicators for trend following
// 1. Calculate short and long SMAs
Expand All @@ -182,15 +185,15 @@ fn main() -> Result<(), PolarsError> {
let sma_50_values = sma_50.f64()?.to_vec();

// Add to dataframe
df.with_column(Series::new("sma_20".into(), sma_20_values))?;
df.with_column(Series::new("sma_50".into(), sma_50_values))?;
df.with_column(Column::new("sma_20".into(), sma_20_values))?;
df.with_column(Column::new("sma_50".into(), sma_50_values))?;

// 2. Calculate MACD
let (macd_line, signal_line) = calculate_macd(&df, 12, 26, 9, "close")?;

// Add MACD and signal line to the dataframe
df.with_column(macd_line.with_name("macd".into()))?;
df.with_column(signal_line.with_name("macd_signal".into()))?;
df.with_column(macd_line.with_name("macd".into()).into_column())?;
df.with_column(signal_line.with_name("macd_signal".into()).into_column())?;

// Calculate MACD histogram
// First get the values as f64 arrays
Expand All @@ -206,7 +209,7 @@ fn main() -> Result<(), PolarsError> {
}

// Add the histogram to the dataframe
df.with_column(Series::new("macd_histogram".into(), histogram))?;
df.with_column(Column::new("macd_histogram".into(), histogram))?;

// 3. Calculate ADX (Average Directional Index) for trend strength
let adx = calculate_adx(&df, 14)?;
Expand All @@ -226,10 +229,10 @@ fn main() -> Result<(), PolarsError> {
padded_adx.push(val);
}

df.with_column(Series::new("adx".into(), padded_adx))?;
df.with_column(Column::new("adx".into(), padded_adx))?;
} else {
let adx_values = adx.f64()?.to_vec();
df.with_column(Series::new("adx".into(), adx_values))?;
df.with_column(Column::new("adx".into(), adx_values))?;
}

// 4. Calculate RSI
Expand All @@ -250,10 +253,10 @@ fn main() -> Result<(), PolarsError> {
padded_rsi.push(val);
}

df.with_column(Series::new("rsi".into(), padded_rsi))?;
df.with_column(Column::new("rsi".into(), padded_rsi))?;
} else {
let rsi_values = rsi.f64()?.to_vec();
df.with_column(Series::new("rsi".into(), rsi_values))?;
df.with_column(Column::new("rsi".into(), rsi_values))?;
}

// 5. Generate trend signals
Expand Down Expand Up @@ -359,7 +362,7 @@ fn calculate_trend_signals(df: &mut DataFrame) -> Result<(), PolarsError> {
}

// Add signals to dataframe
df.with_column(Series::new("signal".into(), signals))?;
df.with_column(Column::new("signal".into(), signals))?;

Ok(())
}
23 changes: 13 additions & 10 deletions examples/stock_mr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,13 @@ fn main() -> Result<(), PolarsError> {
}

// Create DataFrame
let mut df = DataFrame::new(vec![
Series::new("date".into(), dates).into(),
Series::new("close".into(), closes.clone()).into(),
])?;
let mut df = DataFrame::new(
dates.len(),
vec![
Series::new("date".into(), dates).into(),
Series::new("close".into(), closes.clone()).into(),
],
)?;

println!("Price data created with {} days", df.height());

Expand Down Expand Up @@ -118,15 +121,15 @@ fn main() -> Result<(), PolarsError> {
}

// Add calculated columns to DataFrame
df.with_column(Series::new("sma".into(), sma_vals))?;
df.with_column(Series::new("upper_band".into(), upper_band))?;
df.with_column(Series::new("lower_band".into(), lower_band))?;
df.with_column(Series::new("rsi".into(), rsi_vals))?;
df.with_column(Series::new("z_score".into(), z_scores))?;
df.with_column(Column::new("sma".into(), sma_vals))?;
df.with_column(Column::new("upper_band".into(), upper_band))?;
df.with_column(Column::new("lower_band".into(), lower_band))?;
df.with_column(Column::new("rsi".into(), rsi_vals))?;
df.with_column(Column::new("z_score".into(), z_scores))?;

// Clone signals before adding to DataFrame
let signals_copy = signals.clone();
df.with_column(Series::new("signal".into(), signals))?;
df.with_column(Column::new("signal".into(), signals))?;

// Print results
println!("\nMean Reversion Analysis:");
Expand Down
2 changes: 1 addition & 1 deletion examples/test_case_sensitive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ fn main() -> Result<(), Box<dyn Error>> {
println!("=====================================\n");

// Create a sample DataFrame with financial data with differently cased headers
let df = DataFrame::new(vec![
let df = DataFrame::new(3, vec![
Series::new("DATE".into(), &["2024-01-01", "2024-01-02", "2024-01-03"]).into(),
Series::new("Open".into(), &[150.0, 152.0, 151.0]).into(),
Series::new("HIGH".into(), &[155.0, 156.0, 154.0]).into(),
Expand Down
2 changes: 1 addition & 1 deletion examples/test_df.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ fn main() -> Result<(), PolarsError> {
let s2 = Series::new("b".into(), &[4, 5, 6]);

// Create DataFrame
let df = DataFrame::new(vec![s1.into(), s2.into()])?;
let df = DataFrame::new(3, vec![s1.into(), s2.into()])?;

// Print the DataFrame
println!("Test DataFrame");
Expand Down
19 changes: 11 additions & 8 deletions examples/test_headerless.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,17 @@ fn main() -> Result<(), Box<dyn Error>> {
);

// Create DataFrame
let mut df = DataFrame::new(vec![
dates.into(),
opens.into(),
highs.into(),
lows.into(),
closes.into(),
volumes.into(),
])?;
let mut df = DataFrame::new(
5,
vec![
dates.into(),
opens.into(),
highs.into(),
lows.into(),
closes.into(),
volumes.into(),
],
)?;

println!("Original DataFrame with generic column names:");
println!("{}\n", df);
Expand Down
Loading