Turn-of-Month Countdown and Shading
The turn of the month often coincides with systematic portfolio rebalancing, fund flows, and options expiry clusters. This indicator displays a countdown to month-end and visually shades the surrounding days to help traders identify and prepare for calendar-driven market effects. It is useful for position managers watching year-end, quarter-end, and month-end risk windows, and for traders seeking to correlate volatility spikes with calendar events.
//@version=6
indicator("Turn-of-Month Countdown and Shading", overlay=true)
// Input parameters
daysBeforeMonthEnd = input.int(5, title="Days to shade before month-end", minval=0, maxval=15)
daysAfterMonthStart = input.int(5, title="Days to shade after month-start", minval=0, maxval=15)
shadingColor = input.color(color.new(color.orange, 80), title="Shading color")
textColor = input.color(color.white, title="Text color")
showCountdown = input.bool(true, title="Show countdown label")
// Calculate the current date
currentYear = year(time)
currentMonth = month(time)
currentDay = dayofmonth(time)
// Calculate days in current month
daysInMonth = currentMonth == 2
? (currentYear % 4 == 0 and (currentYear % 100 != 0 or currentYear % 400 == 0) ? 29 : 28)
: (currentMonth == 4 or currentMonth == 6 or currentMonth == 9 or currentMonth == 11 ? 30 : 31)
daysUntilMonthEnd = daysInMonth - currentDay
daysIntoMonth = currentDay
// Determine if we are in the shading window
isBeforeMonthEnd = daysUntilMonthEnd <= daysBeforeMonthEnd
isAfterMonthStart = daysIntoMonth <= daysAfterMonthStart
isInShadingWindow = isBeforeMonthEnd or isAfterMonthStart
// Apply background shading
bgcolor(isInShadingWindow ? shadingColor : na)
// Calculate countdown display
countdownText = ""
if isBeforeMonthEnd
countdownText := str.tostring(daysUntilMonthEnd) + " days to month-end"
else if isAfterMonthStart
countdownText := str.tostring(daysIntoMonth) + " days into month"
else
countdownText := "Mid-month"
// Display countdown label on chart
if showCountdown
label.new(bar_index, high, countdownText,
color=color.new(color.black, 100),
textcolor=textColor,
style=label.style_label_lower_left,
size=size.small)
How the code works
The indicator calculates the total days in the current month by referencing the Gregorian calendar rules: February has 29 days in leap years (divisible by 4, except centuries unless divisible by 400), April, June, September, and November have 30 days, and remaining months have 31 days. The current day of month is extracted using the dayofmonth() function applied to the chart's bar timestamp.
Days remaining until month-end are calculated as daysInMonth, currentDay. The indicator then checks whether the current bar falls within the configurable shading windows: either within daysBeforeMonthEnd days before the month's final bar, or within daysAfterMonthStart days after the first bar of the new month.
When the bar meets either condition, bgcolor() applies the shading color (with configurable transparency) to the bar's background. Simultaneously, a countdown label is generated and positioned on the chart, updating on every bar to show either days remaining (e.g., "5 days to month-end") or days elapsed into the new month. All inputs, shading width, color, transparency, and label visibility, are user-configurable via the settings panel.
Reading it on a chart
When viewing a chart with this indicator active, watch for the orange-tinted background (or chosen shading color) appearing at predictable calendar intervals. The label displays a numeric countdown visible on each candle, informing the trader of proximity to the month boundary. During mid-month periods, the background remains clear and the label shows "Mid-month."
The indicator becomes most valuable on weekly and monthly timeframes, where the turn-of-month window is wide enough to observe relative to the chart's zoom. On intraday charts (5-minute, 15-minute), the shading may appear briefly only on the final days of the month, making it useful for day traders synchronizing with month-end risk windows, corporate earnings seasons, or index rebalancing events.
The countdown allows traders to set alerts (external to this script) at custom intervals: for instance, a trader managing equity exposure might set an alert when the label reads "3 days to month-end," triggering a review of portfolio concentration or hedging ratios before known rebalancing windows.
Limitations
The indicator uses the chart's bar timestamp and does not account for market holidays or weekends in its countdown. For example, if the month-end falls on a Friday, the last trading day may be Thursday, yet the label will count Friday as day 31. Traders should manually adjust the shading windows or cross-reference the calendar when precise alignment with the final trading day is critical.
The script applies shading to all bars in the window regardless of trading volume or market conditions. A trader should not interpret the shading itself as a predictive signal; the shading is a visual aid for calendar awareness only. Month-end effects vary significantly across markets: equity index futures exhibit pronounced flow-driven volatility, while cryptocurrency markets may show muted calendar seasonality. The indicator does not differentiate between asset classes.
also, the countdown label updates on every bar formation, which can produce visual clutter on fast timeframes. Disabling the countdown label via the settings panel is recommended for intraday trading.
Key definitions
Turn-of-month: The period spanning the final trading days of a calendar month and the opening trading days of the subsequent month, often associated with portfolio rebalancing, options expiry, and fund flows.
Day-of-month: The numeric position of a date within its calendar month, ranging from 1 (first day) to 28, 29, 30, or 31 (final day).
Leap year: A year with 366 days instead of 365, occurring every four years (with exceptions for century years) to keep the calendar aligned with Earth's orbit.
Background color: A shaded overlay applied to price bars to visually distinguish a defined period or condition without altering the underlying price display.
Countdown: A numeric or label-based display showing time or periods remaining until a defined event.
References
- CME Group, "Equity Index Futures Specs and Roll Dates," CME Group (2025). [Contract specifications including standard expiry and roll schedules.]
- FINRA, "Key Dates and Events," FINRA (2025). Https://www.finra.org/investors/key-dates-and-events
- Investopedia, "Month-End Effect," Investopedia. [Secondary source describing market effects around calendar month boundaries.]
Educational research on historical data only. Not investment advice, not a signal, and never a performance promise. Past results do not predict future performance. Every reference is link-verified before publication and every paper is re-audited weekly against the library's editorial standard.
Last reviewed by the PropLedger research pipeline: 2026-10-12. Educational research on historical data, not financial advice.
Keep reading
Swing Structure: BOS & CHoCH Marker
Free open-source Pine Script indicator: swing high/low structure marker (BOS and CHoCH labels). Full code and a plain-English walkthrough.
Prior Day High, Low, and Settlement
Free open-source Pine Script indicator: prior day high, low, and settlement levels. Full code and a plain-English walkthrough.
Seasonality Heat Strip: Month-of-Year Average Returns
Free open-source Pine Script indicator: seasonality heat strip: month-of-year average returns. Full code and a plain-English walkthrough.