Skip to content

Commit 7645231

Browse files
committed
Formatting the files.
1 parent 3bf3826 commit 7645231

4 files changed

Lines changed: 188 additions & 116 deletions

File tree

examples/compare_multi_indicator_strategies.rs

Lines changed: 58 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use ta_lib_in_rust::strategy::daily::multi_indicator_daily_4;
77
fn main() -> Result<(), PolarsError> {
88
// Define the tickers to analyze
99
let tickers = vec!["AAPL", "GOOGL", "MSFT"];
10-
10+
1111
// Store results for each strategy and ticker
1212
struct StrategyResult {
1313
ticker: String,
@@ -19,15 +19,15 @@ fn main() -> Result<(), PolarsError> {
1919
max_drawdown: f64,
2020
profit_factor: f64,
2121
}
22-
22+
2323
let mut all_results: Vec<StrategyResult> = Vec::new();
24-
24+
2525
// Process each ticker
2626
for ticker in &tickers {
2727
println!("\n==============================================================");
2828
println!("ANALYZING {}", ticker);
2929
println!("==============================================================");
30-
30+
3131
// Load ticker's daily OHLCV data
3232
let file_path = format!("examples/{}_daily_ohlcv.csv", ticker);
3333

@@ -57,14 +57,20 @@ fn main() -> Result<(), PolarsError> {
5757
.collect()?;
5858

5959
println!("--------------------------------------------------------------");
60-
println!("COMPARATIVE ANALYSIS OF MULTI-INDICATOR TRADING STRATEGIES FOR {}", ticker);
60+
println!(
61+
"COMPARATIVE ANALYSIS OF MULTI-INDICATOR TRADING STRATEGIES FOR {}",
62+
ticker
63+
);
6164
println!("--------------------------------------------------------------\n");
6265

6366
// Save reference to close prices for performance calculations
6467
let close_prices = df.column("close")?;
65-
68+
6669
// Run Strategy 1
67-
println!("Running Strategy 1: Standard Multi-Indicator for {}...", ticker);
70+
println!(
71+
"Running Strategy 1: Standard Multi-Indicator for {}...",
72+
ticker
73+
);
6874
let strategy1_params = multi_indicator_daily_1::StrategyParams::default();
6975

7076
println!("Strategy 1 Parameters:");
@@ -106,7 +112,7 @@ fn main() -> Result<(), PolarsError> {
106112
println!("- Maximum Drawdown: {:.2}%", max_drawdown1 * 100.0);
107113
println!("- Profit Factor: {:.2}", profit_factor1);
108114
println!();
109-
115+
110116
// Save strategy 1 results
111117
all_results.push(StrategyResult {
112118
ticker: ticker.to_string(),
@@ -120,7 +126,10 @@ fn main() -> Result<(), PolarsError> {
120126
});
121127

122128
// Run Strategy 2
123-
println!("Running Strategy 2: Volatility-Focused Multi-Indicator for {}...", ticker);
129+
println!(
130+
"Running Strategy 2: Volatility-Focused Multi-Indicator for {}...",
131+
ticker
132+
);
124133
let strategy2_params = multi_indicator_daily_2::StrategyParams::default();
125134

126135
println!("Strategy 2 Parameters:");
@@ -165,7 +174,7 @@ fn main() -> Result<(), PolarsError> {
165174
println!("- Maximum Drawdown: {:.2}%", max_drawdown2 * 100.0);
166175
println!("- Profit Factor: {:.2}", profit_factor2);
167176
println!();
168-
177+
169178
// Save strategy 2 results
170179
all_results.push(StrategyResult {
171180
ticker: ticker.to_string(),
@@ -240,7 +249,7 @@ fn main() -> Result<(), PolarsError> {
240249
println!("- Maximum Drawdown: {:.2}%", max_drawdown3 * 100.0);
241250
println!("- Profit Factor: {:.2}", profit_factor3);
242251
println!();
243-
252+
244253
// Save strategy 3 results
245254
all_results.push(StrategyResult {
246255
ticker: ticker.to_string(),
@@ -254,7 +263,10 @@ fn main() -> Result<(), PolarsError> {
254263
});
255264

256265
// Run Strategy 4
257-
println!("Running Strategy 4: Hybrid Adaptive Strategy for {}...", ticker);
266+
println!(
267+
"Running Strategy 4: Hybrid Adaptive Strategy for {}...",
268+
ticker
269+
);
258270
let strategy4_params = multi_indicator_daily_4::StrategyParams::default();
259271

260272
println!("Strategy 4 Parameters:");
@@ -328,7 +340,7 @@ fn main() -> Result<(), PolarsError> {
328340
println!("- Maximum Drawdown: {:.2}%", max_drawdown4 * 100.0);
329341
println!("- Profit Factor: {:.2}", profit_factor4);
330342
println!();
331-
343+
332344
// Save strategy 4 results
333345
all_results.push(StrategyResult {
334346
ticker: ticker.to_string(),
@@ -464,31 +476,34 @@ fn main() -> Result<(), PolarsError> {
464476
);
465477
}
466478
}
467-
479+
468480
// Cross-ticker comparison for each strategy
469481
println!("\n==============================================================");
470482
println!("CROSS-TICKER COMPARISON BY STRATEGY");
471483
println!("==============================================================");
472-
484+
473485
// Group results by strategy
474486
let strategy_names = vec![
475487
"Standard Multi-Indicator",
476488
"Volatility-Focused Multi-Indicator",
477489
"Adaptive Trend-Filtered",
478-
"Hybrid Adaptive Strategy"
490+
"Hybrid Adaptive Strategy",
479491
];
480-
492+
481493
for strategy_name in &strategy_names {
482494
println!("\nStrategy: {}", strategy_name);
483495
println!("--------------------------");
484-
println!("{:<6} {:<15} {:<10} {:<10} {:<10} {:<10}",
485-
"Ticker", "Return (%)", "Final Value", "Trades", "Win Rate", "Max DD%");
486-
496+
println!(
497+
"{:<6} {:<15} {:<10} {:<10} {:<10} {:<10}",
498+
"Ticker", "Return (%)", "Final Value", "Trades", "Win Rate", "Max DD%"
499+
);
500+
487501
// Filter results for this strategy
488-
let strategy_results: Vec<&StrategyResult> = all_results.iter()
502+
let strategy_results: Vec<&StrategyResult> = all_results
503+
.iter()
489504
.filter(|r| r.strategy_name == *strategy_name)
490505
.collect();
491-
506+
492507
// Print results for each ticker
493508
for result in &strategy_results {
494509
println!(
@@ -501,35 +516,44 @@ fn main() -> Result<(), PolarsError> {
501516
result.max_drawdown * 100.0
502517
);
503518
}
504-
519+
505520
// Find best performing ticker for this strategy
506521
if !strategy_results.is_empty() {
507-
let best_ticker = strategy_results.iter()
522+
let best_ticker = strategy_results
523+
.iter()
508524
.max_by(|a, b| a.total_return.partial_cmp(&b.total_return).unwrap())
509525
.unwrap();
510-
511-
println!("\n{} performed best on {} with a {:.2}% return.",
512-
strategy_name, best_ticker.ticker, best_ticker.total_return);
526+
527+
println!(
528+
"\n{} performed best on {} with a {:.2}% return.",
529+
strategy_name, best_ticker.ticker, best_ticker.total_return
530+
);
513531
}
514532
}
515-
533+
516534
// Overall best combination
517535
println!("\n==============================================================");
518536
println!("OVERALL BEST STRATEGY-TICKER COMBINATION");
519537
println!("==============================================================");
520-
538+
521539
if !all_results.is_empty() {
522-
let best_overall = all_results.iter()
540+
let best_overall = all_results
541+
.iter()
523542
.max_by(|a, b| a.total_return.partial_cmp(&b.total_return).unwrap())
524543
.unwrap();
525-
526-
println!("The best overall performance was {} on {} with a {:.2}% return.",
527-
best_overall.strategy_name, best_overall.ticker, best_overall.total_return);
544+
545+
println!(
546+
"The best overall performance was {} on {} with a {:.2}% return.",
547+
best_overall.strategy_name, best_overall.ticker, best_overall.total_return
548+
);
528549
println!("Performance details:");
529550
println!("- Final Value: ${:.2}", best_overall.final_value);
530551
println!("- Number of Trades: {}", best_overall.num_trades);
531552
println!("- Win Rate: {:.2}%", best_overall.win_rate);
532-
println!("- Maximum Drawdown: {:.2}%", best_overall.max_drawdown * 100.0);
553+
println!(
554+
"- Maximum Drawdown: {:.2}%",
555+
best_overall.max_drawdown * 100.0
556+
);
533557
println!("- Profit Factor: {:.2}", best_overall.profit_factor);
534558
}
535559

examples/multi_indicator_strategy_for_daily_ohlcv.rs

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use ta_lib_in_rust::strategy::daily::multi_indicator_daily_1::{
99
fn main() -> Result<(), PolarsError> {
1010
// Define the tickers to analyze
1111
let tickers = vec!["AAPL", "GOOGL", "MSFT"];
12-
12+
1313
// Store results for each ticker
1414
let mut ticker_results = Vec::new();
1515

@@ -18,7 +18,7 @@ fn main() -> Result<(), PolarsError> {
1818
println!("\n--------------------------------------------------------------");
1919
println!("ANALYZING {}", ticker);
2020
println!("--------------------------------------------------------------");
21-
21+
2222
// Load ticker's daily OHLCV data
2323
let file_path = format!("examples/csv/{}_daily_ohlcv.csv", ticker);
2424

@@ -27,16 +27,17 @@ fn main() -> Result<(), PolarsError> {
2727
.with_has_header(true)
2828
.try_into_reader_with_file_path(Some(Path::new(&file_path).to_path_buf()))?
2929
.finish()?;
30-
30+
3131
// Create a DataFrame with lowercase column names expected by the indicators
32-
let lowercase_df = df.lazy()
32+
let lowercase_df = df
33+
.lazy()
3334
.select([
3435
col("Open").alias("open"),
3536
col("High").alias("high"),
3637
col("Low").alias("low"),
3738
col("Close").alias("close"),
3839
col("Volume").alias("volume"),
39-
col("Timestamp").alias("date")
40+
col("Timestamp").alias("date"),
4041
])
4142
.collect()?;
4243

@@ -74,7 +75,10 @@ fn main() -> Result<(), PolarsError> {
7475
let max_combinations = 50;
7576
let start_capital = 10000.0;
7677

77-
println!("Running backtests with different parameter combinations for {}...", ticker);
78+
println!(
79+
"Running backtests with different parameter combinations for {}...",
80+
ticker
81+
);
7882

7983
for sma_short in &sma_short_periods {
8084
for sma_long in &sma_long_periods {
@@ -132,8 +136,9 @@ fn main() -> Result<(), PolarsError> {
132136
match run_strategy(&lowercase_df, &params) {
133137
Ok(signals) => {
134138
// Calculate performance metrics
135-
let close_series =
136-
lowercase_df.column("close")?.clone();
139+
let close_series = lowercase_df
140+
.column("close")?
141+
.clone();
137142

138143
let (
139144
final_value,
@@ -218,7 +223,7 @@ fn main() -> Result<(), PolarsError> {
218223
// Save the best result for this ticker
219224
if let Some(best) = &best_params {
220225
ticker_results.push(best.clone());
221-
226+
222227
// Show the best parameters in detail
223228
println!("\nBest Parameter Combination for {}:", ticker);
224229
println!("--------------------------");
@@ -245,14 +250,16 @@ fn main() -> Result<(), PolarsError> {
245250
println!("Profit Factor: {:.2}", best.profit_factor);
246251
}
247252
}
248-
253+
249254
// Compare results across tickers
250255
println!("\n--------------------------------------------------------------");
251256
println!("CROSS-TICKER COMPARISON");
252257
println!("--------------------------------------------------------------");
253-
println!("{:<6} {:<15} {:<10} {:<10} {:<10} {:<10}",
254-
"Ticker", "Return (%)", "Final Value", "Trades", "Win Rate", "Max DD%");
255-
258+
println!(
259+
"{:<6} {:<15} {:<10} {:<10} {:<10} {:<10}",
260+
"Ticker", "Return (%)", "Final Value", "Trades", "Win Rate", "Max DD%"
261+
);
262+
256263
for result in &ticker_results {
257264
println!(
258265
"{:<6} {:<15.2} {:<10.2} {:<10} {:<10.2}% {:<10.2}%",
@@ -264,15 +271,18 @@ fn main() -> Result<(), PolarsError> {
264271
result.max_drawdown * 100.0
265272
);
266273
}
267-
274+
268275
// Find best performing ticker
269276
if !ticker_results.is_empty() {
270-
let best_ticker = ticker_results.iter()
277+
let best_ticker = ticker_results
278+
.iter()
271279
.max_by(|a, b| a.total_return.partial_cmp(&b.total_return).unwrap())
272280
.unwrap();
273-
274-
println!("\n{} had the best performance with a {:.2}% return.",
275-
best_ticker.ticker, best_ticker.total_return);
281+
282+
println!(
283+
"\n{} had the best performance with a {:.2}% return.",
284+
best_ticker.ticker, best_ticker.total_return
285+
);
276286
}
277287

278288
Ok(())

0 commit comments

Comments
 (0)