1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
use clap::{Parser, Subcommand};
use colored::Colorize;
use redfolder::calendar::CalendarClient;
use redfolder::config::RedFolderConfig;
use redfolder::engine::BlackoutEngine;
use redfolder::error::Result;
use std::path::PathBuf;
use std::time::Duration;
#[derive(Parser, Debug)]
#[command(
name = "redfolder",
author = "0xbarss",
version,
about = "Economic calendar client and trading blackout engine for algorithmic traders & prop firms",
long_about = "Monitors high-impact macroeconomic releases (ForexFactory / FairEconomy calendar)\nand manages dynamic blackout windows, protecting algorithmic bots and prop firm accounts\nagainst spread spikes and slippage."
)]
struct Cli {
/// Custom cache directory for economic calendar data
#[arg(long, global = true)]
cache_dir: Option<PathBuf>,
#[command(subcommand)]
command: Commands,
}
#[derive(Subcommand, Debug)]
enum Commands {
/// Check whether trading is currently allowed or blocked by a news blackout
Status {
/// Filter by currency (e.g. USD, EUR, GBP, or All)
#[arg(short, long, default_value = "USD")]
currency: String,
/// Minimum impact level to consider (High, Medium, Low)
#[arg(short, long, default_value = "High")]
impact: String,
/// Output results as JSON
#[arg(long)]
json: bool,
},
/// List upcoming economic blackout windows within a given time horizon
Upcoming {
/// Hours to look ahead
#[arg(short = 'H', long, default_value_t = 24)]
hours: u32,
/// Filter by currency (e.g. USD, EUR, GBP, or All)
#[arg(short, long, default_value = "USD")]
currency: String,
/// Minimum impact level to consider (High, Medium, Low)
#[arg(short, long, default_value = "High")]
impact: String,
/// Output results as JSON
#[arg(long)]
json: bool,
},
/// Force a fresh download and cache synchronization from ForexFactory
Sync,
/// Watch blackout status in real-time with automatic countdowns
Watch {
/// Refresh interval in seconds
#[arg(short, long, default_value_t = 5)]
interval: u64,
/// Filter by currency
#[arg(short, long, default_value = "USD")]
currency: String,
/// Minimum impact level
#[arg(short, long, default_value = "High")]
impact: String,
},
}
fn build_cli_config(currency: String, impact: String) -> Result<RedFolderConfig> {
RedFolderConfig::builder()
.currencies(vec![currency])
.impacts(vec![impact])
.try_build()
.map_err(|e| {
eprintln!("{}: {}", "Invalid configuration".red().bold(), e);
e
})
}
#[tokio::main]
async fn main() -> Result<()> {
let cli = Cli::parse();
let cache_dir = cli
.cache_dir
.or_else(|| Some(CalendarClient::default_cache_dir()));
// Configure a 15-minute default cache TTL to prevent rate limit hammering
let client = CalendarClient::new(cache_dir).with_ttl(Duration::from_secs(900));
match cli.command {
Commands::Status {
currency,
impact,
json,
} => {
let events = client.fetch_or_cached().await?;
let config = build_cli_config(currency.clone(), impact)?;
let engine = BlackoutEngine::compile(&events, &[&config], chrono::Utc::now());
let current = engine.status(&config);
if json {
let status_json = serde_json::json!({
"currency": currency,
"in_blackout": current.is_some(),
"active_window": current,
});
println!("{}", serde_json::to_string_pretty(&status_json)?);
} else {
match current {
Some(window) => {
println!(
"\n{}",
"========================================================"
.red()
.bold()
);
println!(
" {} {}",
"🔴 STATUS:".bold(),
"TRADING BLACKOUT ACTIVE".red().bold()
);
println!(
"{}",
"========================================================"
.red()
.bold()
);
println!(
" {} {} minutes",
"Remaining Time:".bold(),
window.remaining_minutes().to_string().yellow().bold()
);
println!(
" {} until {}",
window.start.format("%Y-%m-%d %H:%M UTC"),
window.end.format("%H:%M UTC").to_string().cyan()
);
println!("\n {}", "Triggered Events:".bold());
for ev in &window.events {
println!(
" - [{}] {} ({}) @ {}",
ev.country.yellow(),
ev.title.white().bold(),
ev.impact.red(),
ev.event_time.format("%H:%M UTC")
);
}
println!();
}
None => {
println!(
"\n{}",
"========================================================"
.green()
.bold()
);
println!(
" {} {}",
"🟢 STATUS:".bold(),
"TRADING PERMITTED (NO ACTIVE BLACKOUT)".green().bold()
);
println!(
"{}",
"========================================================"
.green()
.bold()
);
println!(" Currency: {}", currency.cyan());
let upcoming = engine.upcoming_blackouts(&config, 12);
if let Some(next) = upcoming.first() {
let mins_until = (next.start - chrono::Utc::now()).num_minutes();
println!(
" Next Blackout in: {} mins ({})",
mins_until.to_string().yellow().bold(),
next.summary_title()
);
} else {
println!(" No blackouts scheduled within next 12 hours.");
}
println!();
}
}
}
}
Commands::Upcoming {
hours,
currency,
impact,
json,
} => {
let events = client.fetch_or_cached().await?;
let config = build_cli_config(currency.clone(), impact)?;
let engine = BlackoutEngine::compile(&events, &[&config], chrono::Utc::now());
let upcoming = engine.upcoming_blackouts(&config, hours);
if json {
println!("{}", serde_json::to_string_pretty(&upcoming)?);
} else {
println!(
"\n{}",
format!("── Upcoming Blackout Windows (Next {hours} Hours, {currency}) ──")
.cyan()
.bold()
);
if upcoming.is_empty() {
println!(
" {}",
"No upcoming blackout windows in this time window.".green()
);
} else {
for (i, w) in upcoming.iter().enumerate() {
let mins_until = (w.start - chrono::Utc::now()).num_minutes();
println!(
"\n{}. {} (in {} mins, duration: {} mins)",
i + 1,
w.summary_title().white().bold(),
mins_until.to_string().yellow(),
w.duration_minutes().to_string().cyan()
);
println!(
" Window: {} -> {}",
w.start.format("%Y-%m-%d %H:%M UTC"),
w.end.format("%H:%M UTC")
);
for ev in &w.events {
println!(
" • [{}] {} [{}]",
ev.country.yellow(),
ev.title,
ev.impact.red()
);
}
}
}
println!();
}
}
Commands::Sync => {
println!(
"{}",
"Syncing economic calendar from ForexFactory...".cyan()
);
let events = client.force_fetch().await?;
if events.is_empty() {
println!(
"{} {}",
"⚠".yellow().bold(),
"Remote calendar returned 0 events; preserved existing cache if present."
.yellow()
);
} else {
println!(
"{} Downloaded and cached {} economic events.",
"✔".green().bold(),
events.len().to_string().yellow().bold()
);
}
if let Some(path) = client.cache_path() {
println!(" Cache file: {}", path.display().to_string().dimmed());
}
}
Commands::Watch {
interval,
currency,
impact,
} => {
println!(
"{}",
"Starting live RedFolder watcher (press Ctrl+C to exit)...".cyan()
);
let config = build_cli_config(currency.clone(), impact)?;
let mut events = client.fetch_or_cached().await?;
let mut engine = BlackoutEngine::compile(&events, &[&config], chrono::Utc::now());
let mut last_fetch = std::time::Instant::now();
let mut last_status = false;
loop {
// Re-fetch calendar only when cache TTL (15 minutes) expires
if last_fetch.elapsed() >= Duration::from_secs(900) {
match client.fetch_or_cached().await {
Ok(new_events) => {
events = new_events;
engine =
BlackoutEngine::compile(&events, &[&config], chrono::Utc::now());
last_fetch = std::time::Instant::now();
}
Err(e) => {
eprintln!(
"\n{}: Failed to refresh calendar: {e}",
"Warning".yellow().bold()
);
}
}
}
let current = engine.status(&config);
let in_blackout = current.is_some();
let now_str = chrono::Utc::now().format("%H:%M:%S UTC").to_string();
if in_blackout != last_status {
last_status = in_blackout;
if in_blackout {
println!(
"\n[{}] 🚨 {}",
now_str,
"ENTERING BLACKOUT WINDOW!".red().bold()
);
} else {
println!(
"\n[{}] 🟢 {}",
now_str,
"BLACKOUT CLEARED. TRADING PERMITTED.".green().bold()
);
}
}
if let Some(w) = current {
print!(
"\r[{}] 🔴 IN BLACKOUT: {} (remaining: {}m) ",
now_str,
w.summary_title().red().bold(),
w.remaining_minutes().to_string().yellow().bold()
);
} else {
let upcoming = engine.upcoming_blackouts(&config, 12);
if let Some(next) = upcoming.first() {
let mins_until = (next.start - chrono::Utc::now()).num_minutes();
print!(
"\r[{}] 🟢 CLEAR: next blackout in {}m ({}) ",
now_str,
mins_until.to_string().yellow(),
next.summary_title().dimmed()
);
} else {
print!(
"\r[{}] 🟢 CLEAR: no blackout scheduled in next 12h ",
now_str
);
}
}
std::io::Write::flush(&mut std::io::stdout()).ok();
tokio::time::sleep(Duration::from_secs(interval)).await;
}
}
}
Ok(())
}