Indicator · Pine Script v6 · On the chart

Moon Phases

by AkashSasikumar47

A Pine v6 indicator that calculates lunar phases from the date and shows them on the price chart.

View on GitHub

GitHub stars1on the repository
Pine versionv6current
Lines of code80
Settings2inputs
LicenceMIT
Last update2 weeks ago10 Oct 2026

Add It to Your TradingView Chart

  1. 1Copy the scriptPress Copy script above, or the copy button on the code below.
  2. 2Open the Pine EditorOpen a TradingView chart and choose Pine Editor at the bottom of the screen.
  3. 3Paste over the templateCreate a new indicator, select all of the template text and paste. Press Save and give it a name.
  4. 4Add it to the chartPress Add to chart, then adjust the settings with the gear icon next to its name.

Source Code

80 lines of Pine Script v6 from indicators/moon-phases.pine, published under MIT. Keep the author's credit and licence lines when you reuse it.

moon-phases.pine
//@version=6
indicator("Moon Phases", "MOON", overlay = true)

waxingColor = input.color(color.blue, "Waxing Moon")
waningColor = input.color(color.white, "Waning Moon")

if timeframe.ismonthly
    runtime.error("Unsupported timeframe.")

dayOfYear(barTime) =>
    yearValue = year(barTime)
    leapYear = yearValue % 400 == 0 or (yearValue % 4 == 0 and yearValue % 100 != 0)
    dayCount = array.from(31, leapYear ? 29 : 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31)
    monthValue = month(barTime)
    days = 0
    if monthValue > 1
        for i = 0 to monthValue - 2
            days += dayCount.get(i)
    days + dayofmonth(barTime)

unixTimeFromJulianDay(julianDay) =>
    z = math.floor(julianDay + 0.5)
    f = (julianDay + 0.5) % 1
    alpha = math.floor((z - 1867216.25) / 36524.25)
    a = z < 2299161 ? z : z + 1 + alpha - math.floor(alpha / 4.0)
    b = a + 1524
    c = math.floor((b - 122.1) / 365.25)
    d = math.floor(365.25 * c)
    e = math.floor((b - d) / 30.6001)
    dayFloat = b - d - math.floor(30.6001 * e) + f
    dayValue = int(dayFloat)
    monthValue = e < 13.5 ? int(e - 1) : int(e - 13)
    yearValue = monthValue > 2.5 ? int(c - 4716) : int(c - 4715)
    secondTotal = int(dayFloat % 1 * 60 * 60 * 24)
    hourValue = math.floor(secondTotal / 3600)
    minuteValue = math.floor((secondTotal - hourValue * 3600) / 60)
    secondValue = secondTotal % 60
    timestamp(yearValue, monthValue, dayValue, hourValue, minuteValue, secondValue)

lastMoonPhase(barTime) =>
    k = (dayOfYear(barTime) / 364.25 + year(barTime) - 1900) * 12.3685
    int moonPhase = na
    if k % 1.0 < 0.5
        k := math.floor(k)
        moonPhase := 1
    else
        k := math.floor(k) + 0.5
        moonPhase := -1
    t = k / 1236.85
    anomalySun = math.toradians(359.2242 + (29.10535608 * k) - (0.0000333 * math.pow(t, 2)) - (0.00000347 * math.pow(t, 3)))
    anomalyMoon = math.toradians(306.0253 + (385.81691806 * k) + (0.0107306 * math.pow(t, 2)) + (0.00001236 * math.pow(t, 3)))
    f = math.toradians(21.2964 + ((390.67050646 * k) - (0.0016528 * math.pow(t, 2)) - (0.00000239 * math.pow(t, 3))))
    dev = (0.1734 - (0.000393 * t)) * math.sin(anomalySun)
          + 0.0021 * math.sin(2 * anomalySun)
          - 0.4068 * math.sin(anomalyMoon)
          + 0.0161 * math.sin(2 * anomalyMoon)
          - 0.0004 * math.sin(3 * anomalyMoon)
          + 0.0104 * math.sin(2 * f)
          - 0.0051 * math.sin(anomalySun + anomalyMoon)
          - 0.0074 * math.sin(anomalySun - anomalyMoon)
          + 0.0004 * math.sin(2 * f + anomalySun)
          - 0.0004 * math.sin(2 * f - anomalySun)
          - 0.0006 * math.sin(2 * f + anomalyMoon)
          + 0.0010 * math.sin(2 * f - anomalyMoon)
          + 0.0005 * math.sin(anomalySun + 2 * anomalyMoon)
    julianDate = 2415020.75933 + (29.53058868 * k) + (0.0001178 * math.pow(t, 2)) - (0.000000155 * math.pow(t, 3)) +
          (0.00033 * math.sin(math.toradians(166.56) + (math.toradians(132.87) * t) - (math.toradians(0.009173) * math.pow(t, 2)))) + dev
    timeLastMoonPhase = unixTimeFromJulianDay(julianDate)
    timeLastMoonPhase := timeLastMoonPhase >= barTime ? timeLastMoonPhase[1] : timeLastMoonPhase
    phaseTimeChanged = ta.change(timeLastMoonPhase) != 0
    var int moonPhaseAdjusted = moonPhase
    moonPhaseAdjusted := barstate.isfirst or phaseTimeChanged ? moonPhase : moonPhaseAdjusted[1]
    moonType = ta.change(moonPhaseAdjusted) != 0 ? moonPhase : na
    [moonPhaseAdjusted, moonType]

[moonPhase, moonType] = lastMoonPhase(time_close)

plotshape(moonType == 1, "New Moon", shape.circle, location.abovebar, color.new(waxingColor, 50), size = size.normal, display = display.pane)
plotshape(moonType == -1, "Full Moon", shape.circle, location.belowbar, color.new(waningColor, 50), size = size.normal, display = display.pane)
bgcolor(color.new(moonPhase == 1 ? waxingColor : waningColor, 95))

Overview

Moon Phases marks new and full moons and tints the waxing and waning halves of each cycle in two colors that can be changed. It is a calendar tool for people who study cycles and not a trading signal. The script stops with an error on monthly charts.

Details

Published by

AkashSasikumar47 on GitHub

Draws

On the price chart

Markets

Any symbol

Timeframes

Any timeframe

Risk Notes

No repainting, lookahead or stacked entries were found in its code or README. That says nothing about how well it works: test it first.

Topics

pine-scriptpinescript-v6technical-analysistrading-indicatorstradingview

Settings

The inputs you can change from the gear icon on the chart. Defaults are the author's.

SettingTypeDefaultVariable
Waxing Mooncolorcolor.bluewaxingColor
Waning Mooncolorcolor.whitewaningColor
Third-party code. ForexR did not write, test or endorse this script, and the summary above was generated automatically from its code and README. Indicators describe the past, and a strategy's backtest can look better than live trading, especially when a script repaints. Check it in the Strategy Tester and on a demo account before you rely on it.

Similar Pine Scripts

947 stars

This indicator counts down to a fixed Litecoin halving timestamp in August 2019, using Greenwich Mean Time. It subtracts the current time from that stamp and formats the remaining days, hours, minutes, and seconds. The day, hour, and minute of the target are u

Indicator v4 Sessions & time
Updated 11 days ago · GPL-3.0 GitHub repository of Litecoin Halving UTC Countdown
575 stars

This indicator builds Market Profile Time Price Opportunity (TPO) structures across defined session timeframes directly on the main price chart. It visualizes the Point of Control (POC) and Value Area boundaries using dynamic box drawings. An integrated multi-

Indicator v5 Support & resistanceScreeners & dashboards
Updated 1 year ago · MPL-2.0 GitHub repository of Market Profile with Screener

This indicator identifies and plots key options expiration dates on the price chart. It calculates monthly OpEx (the third Friday of each month), quarterly OpEx (March, June, September, December), and corresponding VIX expiration dates, drawing vertical lines

Indicator v6 Sessions & time
Updated 2 months ago · MIT GitHub repository of Expiration Highlights: Opex, Quarterly Opex & VIX with Vanna/Charm
257 stars

This indicator plots auto trade signals for E-mini S&P 500 futures on the price chart. It computes an EMA200 filter along with day ETH RTH session levels and percentage-based entry zones. Signals trigger CALL or PUT cases when price crosses defined thresholds

Indicator v6 TrendSupport & resistance
Updated 6 weeks ago · None stated GitHub repository of Auto Trade Futures ES

Golden Bullet Strategy

by TradersPost
228 stars

The Golden Bullet Strategy is an overlay signal indicator based on Smart Money Concepts and time-based liquidity models. It identifies liquidity sweeps of a designated reference session range during a specified hunt window. It then confirms Market Structure Sh

Indicator v6 SMC / ICT / price actionSessions & time
Updated 3 months ago · None stated GitHub repository of Golden Bullet Strategy
219 stars

This indicator plots reference levels for previous daily high and low prices directly on the price chart. It requests daily timeframe data using the security function and plots the offset values as continuous lines, displaying resistance in red and support in

Indicator v4 Support & resistanceSessions & time
Updated 5 years ago · None stated GitHub repository of 昨日高低點標示

More Sessions & time scripts

Related on ForexR

Trade Your Charts With a Regulated Broker

Compare brokers that connect to TradingView, with spreads, minimum deposits and leverage side by side.

Compare brokers