Indicators··5 min read

Turn-of-Month Countdown

5 references, link-verified · 3 primaryEditor of record: Shane CantyStandards review editorial standard · audit log

Traders and portfolio managers encounter recurring patterns near month-end: fund rebalancing, options expiry, option assignment, and tax-loss harvesting can all cluster at the calendar boundary. This indicator counts down the days remaining in the current month and visually highlights the final N days with background shading, allowing rapid identification of the "turn-of-month" window at a glance.

//@version=6
indicator("Turn-of-Month Countdown", overlay=true)

// User inputs
threshold = input.int(5, title="Days Before Month-End to Highlight", minval=1, maxval=15)
shade_color = input.color(color.new(color.orange, 80), title="Highlight Color")
show_countdown = input.bool(true, title="Show Countdown Label")

// Calculate days in current month (accounting for leap years)
is_leap_year = (year(time) % 4 == 0) and ((year(time) % 100 != 0) or (year(time) % 400 == 0))
month_num = month(time)
days_in_month = month_num == 2 ? (is_leap_year ? 29 : 28) : 
                (month_num == 4 or month_num == 6 or month_num == 9 or month_num == 11) ? 30 : 31

// Get current day of month and calculate days remaining
current_day = dayofmonth(time)
days_remaining = days_in_month - current_day

// Highlight condition: in the final N days of the month
is_turn_of_month = days_remaining >= 0 and days_remaining <= threshold

// Apply background shading to chart
bgcolor(is_turn_of_month ? shade_color : na)

// Display countdown label at right edge of chart
if show_countdown and barstate.islast
    label_text = "Days Left: " + str.tostring(math.max(days_remaining, 0))
    label_color = is_turn_of_month ? color.orange : color.gray
    label.new(bar_index, high * 1.001, label_text, color=label_color, 
              textcolor=color.white, style=label.style_label_down, size=size.small)

How the code works

The script first determines how many days exist in the current month by inspecting month(time). For February, it checks whether the year is a leap year using the standard Gregorian rule: divisible by 4, except century years unless divisible by 400. Months with 30 days (April, June, September, November) and those with 31 days are hard-coded in a ternary chain.

dayofmonth(time) returns the numeric day of the month (1-31). Subtracting this from the total days in the month yields days_remaining. The condition is_turn_of_month triggers when days remaining fall within the user-defined threshold (e.g., 5 days) and remain non-negative.

bgcolor() fills the chart background with the highlight color whenever the condition is true, creating continuous shading across all bars in the final days of the month. This runs on every bar, so the highlighting updates in real time.

On the last bar of each chart, a label is positioned above the price to show the exact countdown. The label color shifts from gray to orange when the window is active, providing immediate visual confirmation of proximity to month-end.

Reading it on a chart

As trading days progress through the month, the background remains clear. Once the threshold is reached, for example, with a 5-day setting, starting on the 26th of a 31-day month, the chart background shifts to orange. This shading persists through the final trading days. In the top-right corner, the "Days Left" label displays the running countdown: 5, 4, 3, 2, 1, 0. On the first bar of the new month, the shading vanishes and the countdown resets to the total days in the new month minus one.

Traders use this to stay aware of structural calendar events without checking a wall calendar. Combined with volume profile or other indicators, it can reveal whether volatility or unusual order flow coincides with month-end flows. The indicator makes no claims about direction or magnitude of these effects; it simply marks when they typically occur.

Limitations

This indicator does not predict market behavior; it identifies a calendar window. Month-end effects vary substantially across asset classes, and some markets (crypto, forex) trade 24/7 without a "month-end" in the traditional sense. The indicator shades based on calendar days, not trading days, so weekends and holidays are counted equally. A threshold of 5 days will include Saturday and Sunday in some months, potentially obscuring the true number of liquidity-providing trading days remaining.

The label is placed on the last bar only, so it does not scroll with price action if the chart is zoomed or panned; users must keep the rightmost bars visible to read the countdown. On intraday charts with a small threshold, the shading may flicker if bars resolve at the day boundary between highlight and non-highlight periods. The indicator carries no information about specific rebalancing or expiry dates; traders must cross-reference with their broker's calendar for precise settlement events.

Key definitions

Turn-of-month effect: An empirical tendency (not guaranteed) for certain market behaviors, volatility, returns, options assignment, to cluster in the final days or first days of a calendar month due to portfolio rebalancing, fund accounting, and derivatives expiry.

Leap year: A calendar year divisible by 4, except for years divisible by 100 unless also divisible by 400. Leap years have 366 days; February has 29 days instead of 28.

Background shading: A feature of Pine Script that fills the chart background with a solid color over a range of bars; used here to visually delimit the window of interest.

Dayofmonth(): A Pine Script function returning the numeric day (1-31) of the current bar's timestamp.

References


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-09-27. Educational research on historical data, not financial advice.

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. Found an error? Email support@prop-ledger.org and the paper is corrected or withdrawn.