Moon Phases + Daily, Weekly, Monthly, Quarterly & Yearly Breaks
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// This work is licensed under a Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0) https://creativecommons.org/licenses/by-nc-sa/4.0/
//@version=5

indicator("Moon + DWMQY • Yata", 
     overlay = true, 
	 scale=scale.none)

// --------------------
groupMP = "Moon Phases"
// --------------------

new = input.time(defval=timestamp("2021-01-13:05:00"), title="Reference Date", inline="D", group=groupMP)

show_moon = input.bool(true, title="Show Moons |", inline="MO", group=groupMP)
show_bg	= input.bool(true, title="Background |", inline="MO", group=groupMP)
show_vl	= input.bool(false, title="Lines", inline="MO", group=groupMP)

buy = input.string("Full Moon", options=["New Moon", "Full Moon", "Higher Moon", "Lower Moon"], title="", inline="MO1", group=groupMP)
sell = input.string("New Moon", options=["New Moon", "Full Moon", "Higher Moon", "Lower Moon"], title="", inline="MO2", group=groupMP)

nmooncol = input.color(color.new(color.black, 20), title="", inline="MO2", group=groupMP)
fmooncol = input.color(color.new(color.silver, 20), title="", inline="MO1", group=groupMP)

longcol = input.color(color.new(color.silver, 90), title="|", inline="MO1", group=groupMP)
shortcol = input.color(color.new(color.silver, 95), title="|", inline="MO2", group=groupMP)

longvlcol = input.color(color.new(color.silver, 80), title="|", inline="MO1", group=groupMP)
shortvlcol = input.color(color.new(color.silver, 80), title="|", inline="MO2", group=groupMP)

// --------------------

n = bar_index
cycle = 2551442876.8992
day = 8.64e+7
diff = (new + time + day * 2) % cycle / cycle

// --------------------

newmoon = ta.crossover(diff, .5)
fullmoon = diff < diff[1]

plotshape(show_moon and newmoon ? low : na, "New Moon", shape.circle, location.bottom, nmooncol, size=size.small)
plotshape(show_moon and fullmoon ? high : na, "Full Moon", shape.circle, location.top, fmooncol, size=size.small)

plot(show_vl and newmoon ? 1 : na, style=plot.style_histogram, color=longvlcol, title="New Moon")
plot(show_vl and fullmoon ? 1 : na, style=plot.style_histogram, color=shortvlcol, title="Full Moon")

// --------------------

msrc = close
var bool long = na
var bool short = na
if buy == "New Moon"
    long := newmoon
    long
else if buy == "Full Moon"
    long := fullmoon
    long
else if buy == "Higher Moon"
    long := ta.valuewhen(newmoon or fullmoon, msrc, 0) > ta.valuewhen(newmoon or fullmoon, msrc, 1)
    long
else
    long := ta.valuewhen(newmoon or fullmoon, msrc, 0) < ta.valuewhen(newmoon or fullmoon, msrc, 1)
    long
	
// --------------------

if sell == "New Moon"
    short := newmoon
    short
else if sell == "Full Moon"
    short := fullmoon
    short
else if sell == "Higher Moon"
    short := ta.valuewhen(newmoon or fullmoon, msrc, 0) > ta.valuewhen(newmoon or fullmoon, msrc, 1)
    short
else
    short := ta.valuewhen(newmoon or fullmoon, msrc, 0) < ta.valuewhen(newmoon or fullmoon, msrc, 1)
    short
	
// --------------------

var pos = 0
if long
    pos := 1

if short
    pos := -1

bgcolor(show_bg and pos == 1 ? longcol : show_bg ? shortcol : na)

// ------------------------------
groupCA = "D, W, M, Q & Y Breaks"
// ------------------------------

color_d = input.color(color.new(color.silver, 80)	, title=""	, inline="d", group=groupCA)
color_w = input.color(color.new(#42a5f5, 60)		, title=""	, inline="w", group=groupCA)
color_m = input.color(color.new(#388e3c, 50)		, title=""	, inline="m", group=groupCA)
color_q = input.color(color.new(#fbc02d, 40)		, title=""	, inline="q", group=groupCA)
color_y = input.color(color.new(#d32f2f, 20)		, title=""	, inline="y", group=groupCA)

show_d = input.bool(false, title="Show Daily"		, inline="d", group=groupCA)
show_w = input.bool(false, title="Show Weekly"		, inline="w", group=groupCA)
show_m = input.bool(true, title="Show Monthly"		, inline="m", group=groupCA)
show_q = input.bool(true, title="Show Quarterly"	, inline="q", group=groupCA)
show_y = input.bool(true, title="Show Yearly"		, inline="y", group=groupCA)

// ------------------------------

is_new(resolution) =>
    t = time(resolution)
    not na(t) and (na(t[1]) or t > t[1])

is_new_year() =>
    is_new("12M")
is_new_quarter() =>
    is_new("3M") and (timeframe.isweekly or timeframe.isdaily or timeframe.isintraday)
is_new_month() =>
    is_new("M") and not timeframe.ismonthly
is_new_week() =>
    is_new("W") and not timeframe.isweekly and not timeframe.ismonthly
is_new_day() =>
    is_new("D")
is_minutes() =>
    timeframe.isminutes

// ------------------------------

plot_year() =>
    is_new_year()
plot_quarter() =>
   is_new_quarter() //not is_new_year() and is_new_quarter()
plot_month() =>
    is_new_month() //not is_new_year() and not is_new_quarter() and is_new_month()
plot_week() =>
    is_new_week() //not is_new_year() and not is_new_quarter() and not is_new_month() and is_new_week()
plot_day() =>
    is_new_day() and is_minutes() //not is_new_year() and not is_new_quarter() and not is_new_month() and not is_new_week() and is_new_day() and is_minutes()

plot(show_y and plot_year() ? 1 : na	, style=plot.style_histogram, linewidth=2, color=color_y, title="Year Break")
plot(show_q and plot_quarter() ? 1 : na	, style=plot.style_histogram, linewidth=1, color=color_q, title="Quarter Break")
plot(show_m and plot_month() ? 1 : na	, style=plot.style_histogram, linewidth=1, color=color_m, title="Month Break")
plot(show_w and plot_week() ? 1 : na	, style=plot.style_histogram, linewidth=1, color=color_w, title="Week Break")
plot(show_d and plot_day() ? 1 : na		, style=plot.style_histogram, linewidth=1, color=color_d, title="Day Break")