Skip to content

Instantly share code, notes, and snippets.

@mxvin
Created December 10, 2020 08:04
Show Gist options
  • Save mxvin/e01a762e98a6d8b13d4b55bc61bef209 to your computer and use it in GitHub Desktop.
Save mxvin/e01a762e98a6d8b13d4b55bc61bef209 to your computer and use it in GitHub Desktop.
Pinescript: Modified moon phase (orig from /u/paaax)
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// ---------------------------© paaax----------------------------
// --------------------------------------------------------------
// ---------------- Author: Pascal Simon (paaax) ----------------
// --------------------------------------------------------------
// -------------------- www.pascal-simon.de ---------------------
// --------------------------------------------------------------
// ---------------- www.tradingview.com/u/paaax/-----------------
// --------------------------------------------------------------
// Source: https://gist.github.com/L-A/3497902#file-moonobject-js
// --------------------------------------------------------------
// @version=4
study("[PX] Moon Phase", overlay=true)
// INPUT --- {
fillBackground = input(true, "Fill Background?")
aheadConsent = input(false, "Enable Future Moon Phase || NOTE: ONLY ENABLE THIS TO SEE UPCOMING MOON PHASE, AND DISABLE IT IF YOU WANT TO SEE PAST MOONPHASE(back to normal). RESULT MAY NOT ACCURATE, SO PLEASE CONSULT TO ANOTHER MOONPHASE DATE SOURCE TO ADJUST EXACT FUTURE MOONPHASE DATE.")
futureDay = input(365, "Adjust Offset (Default 365/1y)")
if aheadConsent == false
futureDay := 0
//} --- INPUT
// FUNCTION --- {
normalize(_v) =>
x = _v
x := x - floor(x)
if x < 0
x := x + 1
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 - floor((12 - _month) / 10)
m := _month + 9
if m >= 12
m := m - 12
k1 := floor(365.25 * (y + 4712))
k2 := floor(30.6 * m + 0.5)
k3 := floor(floor((y / 100) + 49) * 0.75) - 38
jd := k1 + k2 + _day + 59
if jd > 2299160
jd := jd - k3
ip := normalize((jd - 2451550.1) / 29.530588853)
age = ip * 29.53
//} --- FUNCTION
// INIT --- {
age = calcPhase(year, month, dayofmonth)
moon =
floor(age)[1] > floor(age) ? 1 :
floor(age)[1] < 15 and floor(age) >= 15 ? -1 : na
//} --- INIT
// PLOT --- {
plotshape(
moon==1,
"Full Moon",
shape.circle,
location.top,
color.new(color.white, 20),
size=size.normal,
offset=futureDay
)
plotshape(
moon==-1,
"New Moon",
shape.circle,
location.bottom,
color.new(color.gray, 20),
size=size.normal,
offset=futureDay
)
var color col = na
if moon == 1 and fillBackground
col := color.navy
if moon == -1 and fillBackground
col := color.gray
bgcolor(col, title="Moon Phase", offset=futureDay)
//} --- PLOT
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment