use chrono::{DateTime, Datelike, Duration, Utc};
use super::period::{
daily_close_for_local_day, next_daily_close_after_with, next_monthly_close_after_with,
next_weekly_close_after_with,
};
use super::session::{next_session_after_with, session_bounds_with};
use super::{CalendarResolution, MarketHours, SessionKind};
#[must_use]
pub fn candle_end_with(
hours: &MarketHours,
t: DateTime<Utc>,
res: CalendarResolution,
kind: SessionKind,
) -> Option<DateTime<Utc>> {
if is_zero_interval(res) {
return None;
}
match res {
CalendarResolution::Seconds(s) => Some(t + Duration::seconds(i64::from(s))),
CalendarResolution::Minutes(m) => {
fixed_grid_end(hours, t, Duration::minutes(i64::from(m)), kind)
}
CalendarResolution::Hours(h) => {
fixed_grid_end(hours, t, Duration::hours(i64::from(h)), kind)
}
CalendarResolution::Daily => next_daily_close_after_with(hours, t, kind),
CalendarResolution::Weekly => next_weekly_close_after_with(hours, t, kind),
CalendarResolution::Monthly => next_monthly_close_after_with(hours, t, kind),
}
}
fn is_zero_interval(res: CalendarResolution) -> bool {
matches!(
res,
CalendarResolution::Seconds(0)
| CalendarResolution::Minutes(0)
| CalendarResolution::Hours(0)
)
}
fn fixed_grid_end(
hours: &MarketHours,
t: DateTime<Utc>,
step: Duration,
kind: SessionKind,
) -> Option<DateTime<Utc>> {
let (open, close) = session_bounds_with(hours, t, kind)?;
let anchor = if t < open { open } else { t };
Some((anchor + step).min(close))
}
#[inline]
#[must_use]
pub fn candle_end(
hours: &MarketHours,
t: DateTime<Utc>,
interval: CalendarResolution,
) -> Option<DateTime<Utc>> {
candle_end_with(hours, t, interval, SessionKind::Both)
}
#[inline]
#[must_use]
pub fn candle_start(
hours: &MarketHours,
t: DateTime<Utc>,
interval: CalendarResolution,
) -> Option<DateTime<Utc>> {
candle_start_with(hours, t, interval, SessionKind::Both)
}
#[must_use]
pub fn candle_start_with(
hours: &MarketHours,
t: DateTime<Utc>,
interval: CalendarResolution,
kind: SessionKind,
) -> Option<DateTime<Utc>> {
if is_zero_interval(interval) {
return None;
}
match interval {
CalendarResolution::Seconds(_) => Some(t),
CalendarResolution::Minutes(_) | CalendarResolution::Hours(_) => {
let (session_open, _) = session_bounds_with(hours, t, kind)?;
Some(t.max(session_open))
}
CalendarResolution::Daily | CalendarResolution::Weekly | CalendarResolution::Monthly => {
period_start_of(hours, t, interval, kind)
}
}
}
fn period_start_of(
hours: &MarketHours,
t: DateTime<Utc>,
interval: CalendarResolution,
kind: SessionKind,
) -> Option<DateTime<Utc>> {
let period_end = match interval {
CalendarResolution::Daily => next_daily_close_after_with(hours, t, kind)?,
CalendarResolution::Weekly => next_weekly_close_after_with(hours, t, kind)?,
CalendarResolution::Monthly => next_monthly_close_after_with(hours, t, kind)?,
CalendarResolution::Seconds(_)
| CalendarResolution::Minutes(_)
| CalendarResolution::Hours(_) => return None,
};
let end_local = period_end.with_timezone(&hours.tz);
let end_day = end_local.date_naive();
let end_iso_week = end_local.iso_week();
let end_month = (end_local.year(), end_local.month());
let mut first_close = period_end;
if interval != CalendarResolution::Daily {
let mut day = end_day - Duration::days(1);
for _ in 0..31 {
let same_period = match interval {
CalendarResolution::Weekly => day.iso_week() == end_iso_week,
CalendarResolution::Monthly => (day.year(), day.month()) == end_month,
CalendarResolution::Daily
| CalendarResolution::Seconds(_)
| CalendarResolution::Minutes(_)
| CalendarResolution::Hours(_) => false,
};
if !same_period {
break;
}
if let Some(close) = daily_close_for_local_day(hours, day, kind) {
first_close = close;
}
day -= Duration::days(1);
}
}
let mut day = first_close.with_timezone(&hours.tz).date_naive() - Duration::days(1);
let mut previous_close = None;
for _ in 0..21 {
if let Some(close) = daily_close_for_local_day(hours, day, kind)
&& close < first_close
{
previous_close = Some(close);
break;
}
day -= Duration::days(1);
}
let previous_close = previous_close?;
let open_probe = previous_close.checked_sub_signed(Duration::nanoseconds(1))?;
let (period_start, _) = next_session_after_with(hours, open_probe, kind)?;
(period_start < first_close).then_some(period_start)
}
#[inline]
#[must_use]
pub fn time_end_of_day(hours: &MarketHours, t: DateTime<Utc>) -> Option<DateTime<Utc>> {
candle_end(hours, t, CalendarResolution::Daily)
}