Indicator · Pine Script v5 · On the chart

Moon Phase and MA

by mrgunkin

This indicator calculates the lunar age from the chart date and marks a new moon above the bar and a full moon below the bar.

View on GitHub

Repaints

GitHub stars1on the repository
Pine versionv5runs as is
Lines of code97
Settings7inputs
LicenceMIT
Last update1 year ago6 Mar 2025

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

97 lines of Pine Script v5 from Moon Phase and MA/Moon Phase and MA.pine, published under MIT. Keep the author's credit and licence lines when you reuse it.

Moon Phase and MA.pine
//@version=5
//
// @author MrGunkin 
//
indicator('Moon Phase and MA', overlay=true)

//аргументы
normalize(_v) =>
    x = _v
    x -= math.floor(x)
    if x < 0
        x += 1
        x
    x

calcPhase(_year, _month, _day) =>

    int y = na
    int m = na
    float k1 = na
    float k2 = na
    float k3 = na
    float jd = na
    float ip = na

//расчеты
    y := _year - math.floor((12 - _month) / 10)
    m := _month + 9
    if m >= 12
        m -= 12
        m

    k1 := math.floor(365.25 * (y + 4712))
    k2 := math.floor(30.6 * m + 0.5)
    k3 := math.floor(math.floor(y / 100 + 49) * 0.75) - 38

    jd := k1 + k2 + _day + 59
    if jd > 2299160
        jd -= k3
        jd

    ip := normalize((jd - 2451550.1) / 29.530588853)
    age = ip * 29.53
    age
age = calcPhase(year, month, dayofmonth)
moon = math.floor(age)[1] > math.floor(age) ? 1 : math.floor(age)[1] < 15 and math.floor(age) >= 15 ? -1 : na

//аргументы MA
MAtype = input.string(options=['sma', 'ema'], defval='sma', title='Choose MA type')
show_1D_200MA = input(true, title='1D 200MA')
show_1W_20MA = input(true, title='1W 20MA')
show_1W_200MA = input(true, title='1W 200MA')
show_custom_MA1 = input(false, title='Show 1 custom MA')
custom_MA1 = input(13, title='1 custom MA Length')
custom_TF1 = input.timeframe(title='1 custom MA Timeframe', defval='D')
src = close
shift = time + 5 * (time - time[1])

//вывод на экран
//луна
plotshape(moon == 1, 'Новолуние', shape.circle, location.abovebar, color.new(color.red, 35), size=size.small, offset = 0, text='Новолуние', textcolor=color.new(color.red, 35))
plotshape(moon == -1, 'Полнолуние', shape.circle, location.belowbar, color.new(color.green, 35), size=size.small, offset = 0, text='Полнолуние', textcolor=color.new(color.green, 35))

//1D MA
D1_ma200 = MAtype == 'sma' ? ta.sma(src, 200) : ta.ema(src, 200)
out_D1_ma200 = request.security(syminfo.tickerid, 'D', D1_ma200, barmerge.gaps_off, barmerge.lookahead_on)
plot(show_1D_200MA and out_D1_ma200 ? out_D1_ma200 : na, color=color.new(color.olive, 20), linewidth=2, title='MA200 1D')
Label_D1_ma200 = show_1D_200MA and out_D1_ma200 > 0 ? label.new(bar_index, close, xloc=xloc.bar_time, style=label.style_none, textcolor=color.olive, text='MA200' + '-D') : na
label.set_y(Label_D1_ma200, out_D1_ma200)
label.set_x(Label_D1_ma200, shift)
label.delete(Label_D1_ma200[1])

//1W MA
W1_ma20 = MAtype == 'sma' ? ta.sma(src, 20) : ta.ema(src, 20)
W1_ma200 = MAtype == 'sma' ? ta.sma(src, 200) : ta.ema(src, 200)
out_W1_ma20 = request.security(syminfo.tickerid, 'W', W1_ma20, barmerge.gaps_off, barmerge.lookahead_on)
out_W1_ma200 = request.security(syminfo.tickerid, 'W', W1_ma200, barmerge.gaps_off, barmerge.lookahead_on)
plot(show_1W_20MA and out_W1_ma20 ? out_W1_ma20 : na, color=color.new(color.orange, 35), linewidth=2, title='MA20 1W')
plot(show_1W_200MA and out_W1_ma200 ? out_W1_ma200 : na, color=color.new(color.yellow, 40), linewidth=2, title='MA200 1W')
Label_W1_ma20 = show_1W_20MA and out_W1_ma20 > 0 ? label.new(bar_index, close, xloc=xloc.bar_time, style=label.style_none, textcolor=color.new(color.orange, 35), text='MA20' + '-W') : na
label.set_y(Label_W1_ma20, out_W1_ma20)
label.set_x(Label_W1_ma20, shift)
label.delete(Label_W1_ma20[1])
Label_W1_ma200 = show_1W_200MA and out_W1_ma200 > 0 ? label.new(bar_index, close, xloc=xloc.bar_time, style=label.style_none, textcolor=color.new(color.yellow, 40), text='MA200' + '-W') : na
label.set_y(Label_W1_ma200, out_W1_ma200)
label.set_x(Label_W1_ma200, shift)
label.delete(Label_W1_ma200[1])

//custom 1
MA_custom_ma1 = MAtype == 'sma' ? ta.sma(src, custom_MA1) : ta.ema(src, custom_MA1)
out_custom_ma1 = request.security(syminfo.tickerid, custom_TF1, MA_custom_ma1, barmerge.gaps_off, barmerge.lookahead_on)
plot(show_custom_MA1 and out_custom_ma1 ? out_custom_ma1 : na, color=color.new(color.red, 0), linewidth=2, title='1 Custom MA')
custom1_MA_lenght_text = str.tostring(custom_MA1)
Label_custom1_MA = show_custom_MA1 and out_custom_ma1 > 0 ? label.new(bar_index, close, xloc=xloc.bar_time, style=label.style_none, textcolor=color.red, text='MA' + custom1_MA_lenght_text + '-' + custom_TF1) : na
label.set_y(Label_custom1_MA, out_custom_ma1)
label.set_x(Label_custom1_MA, shift)
label.delete(Label_custom1_MA[1])

Overview

Moon Phase and MA also plots the close as a daily 200-period moving average and weekly 20- and 200-period moving averages, each as a simple or exponential average. Toggles show or hide those lines, and further inputs add one custom length and timeframe. Everything is drawn on the price chart. Higher-timeframe averages use lookahead, so values can reflect unfinished higher-timeframe data.

Details

Published by

mrgunkin on GitHub

Type

Indicator, Pine Script v5

Draws

On the price chart

Category

Moving averages

Markets

Any symbol

Timeframes

Any timeframe

Risk Notes

  • Signals can change on the live bar or after it closes (repainting)
  • Reads higher-timeframe data with lookahead on, so the history can show signals that were not there in real time

Settings

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

SettingTypeDefaultVariable
Choose MA typestringsmaMAtype
1D 200MAbooltrueshow_1D_200MA
1W 20MAbooltrueshow_1W_20MA
1W 200MAbooltrueshow_1W_200MA
Show 1 custom MAboolfalseshow_custom_MA1
1 custom MA Lengthint13custom_MA1
1 custom MA TimeframetimeframeDcustom_TF1
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

948 stars

The Adaptive Laguerre Filter calculates a smoothed, dynamically adjusted trendline plotted directly on the main price chart. It dynamically adjusts its damping factor (alpha) based on recent price range variations and median percentile calculations over a spec

Indicator v4 TrendMoving averages
Updated 10 days ago · GPL-3.0 GitHub repository of Adaptive Laguerre Filter

This overlay indicator plots a Supertrend from an ATR length and factor, four exponential moving averages, and a selectable VWAP or TWAP taken from a chosen timeframe on the price chart. It also shows MACD and stochastic with adjustable lengths, smoothing, ver

Indicator v5 Oscillators & momentumMoving averages
Updated 1 year ago · MPL-2.0 GitHub repository of Momentum Setup: Vijay Thakare Option Buying Scalping Setup
330 stars

This script plots three moving averages on the price chart: a fast EMA or SMA with 10 periods, a slow EMA or SMA with 20 periods, and a trend EMA or SMA with 100 periods. It signals trend changes by drawing a white cross when the fast moving average crosses ab

Indicator v4 TrendMoving averages
Updated 2 months ago · None stated GitHub repository of 3 EMA/SMA + Cross [Alorse]
318 stars

This indicator plots four customizable moving averages directly on the main price chart using default lengths of 3, 8, 21, and 50 periods. It fills the space between adjacent averages in green or maroon depending on trend alignment. Additionally, it computes B

Indicator v5 Moving averagesTrend
Updated 2 months ago · MIT GitHub repository of Multiple Moving Averages
285 stars

Mean Reversion Study is an indicator that identifies trade setups combining exponential moving averages, RSI extremes, and engulfing candlestick patterns. It highlights bullish setups when a bullish engulfing candle forms below the EMA while RSI is oversold, a

Indicator v4 Mean reversionCandlestick patterns
Updated 3 years ago · None stated GitHub repository of Mean Reversion Study

NVDA Trend Ribbon

by longsurf-ai
277 stars

This indicator plots three exponential moving averages and ATR volatility envelopes on the price chart. It compares the fast and slow EMAs to shade a trend ribbon and marks confirmed upward and downward crossovers with triangle labels. Settings change the thre

Indicator v6 TrendMoving averages
Updated yesterday · MIT GitHub repository of NVDA Trend Ribbon

More indicators

Trade Your Charts With a Regulated Broker

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

Compare brokers