Astro: Celestial Coordinates
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © BarefootJoey

// ██████████████████████████████████████████████████████████████████████   _____ __   _      _______  _____  _______ _______ _______
// █▄─▄─▀██▀▄─██▄─▄▄▀█▄─▄▄─█▄─▄▄─█─▄▄─█─▄▄─█─▄─▄─███▄─▄█─▄▄─█▄─▄▄─█▄─█─▄█     |   | \  |      |______ |_____] |_____| |       |______
// ██─▄─▀██─▀─███─▄─▄██─▄█▀██─▄███─██─█─██─███─███─▄█─██─██─██─▄█▀██▄─▄██   __|__ |  \_|      ______| |       |     | |_____  |______ 
// █▄▄▄▄██▄▄█▄▄█▄▄█▄▄█▄▄▄▄▄█▄▄▄███▄▄▄▄█▄▄▄▄██▄▄▄██▄▄▄███▄▄▄▄█▄▄▄▄▄██▄▄▄██                                                            

//@version=5
indicator("Astro: Celestial Coordinates", overlay=false)

import BarefootJoey/AstroLib/1 as AL

planet_in = input.string("☿ Mercury", "Which planet?", options=["☉︎ Sun", "☽︎ Moon", "☿ Mercury", "♀ Venus", "🜨 Earth", "♂ Mars", "♃ Jupiter", "♄ Saturn", "⛢ Uranus", "♆ Neptune", "♇ Pluto"])
orbital_in = input.string("Declination", "Which Orbital Element?", options=["Longitude", "Latitude", "RA", "Altitude", "Declination"])
showlast = input.int(2000, "Show last?", minval=1, tooltip="Number of historical plots to display. The fewer plots, the faster the load time (especially on loweer timeframes). Use the Time Machine feature for distant historical analysis.")
iTxtSize=input.string("Normal", title="Label Size", options=["Auto", "Tiny", "Small", "Normal", "Large"], inline="4l")
vTxtSize=iTxtSize == "Auto" ? size.auto : iTxtSize == "Tiny" ? size.tiny : iTxtSize == "Small" ? size.small : iTxtSize == "Normal" ? size.normal : iTxtSize == "Large" ? size.large : size.small
d_t = input.bool(false, "Date & Time?", tooltip="Leave this unchecked to display date only.", inline="4l")
lookback = input.int(3, "Lookback", minval=3)
col_info = input.color(color.white, "Info Color")
position = input.string(position.top_center, "Info Position", [position.top_center, position.top_right, position.middle_right, position.bottom_right, position.bottom_center, position.bottom_left, position.middle_left, position.top_left])

grtm = "🕜 Time Machine 🕜"
gsd = input.bool(false, "Activate Time Machine", group=grtm)
sdms = input.time(timestamp("2022-04-20T00:00:00"), "Select Date", group=grtm)
gt = gsd ? sdms : time

grol = "🔭 Observer Location 🔭"
htz = input.float(0, "Timezone Hour", step=0.5, inline="2",  group=grol)
mtz = input.int(0, "Minute", minval=0, maxval=45, inline="2",  group=grol)
tz = htz + math.round(mtz / 60, 4)
latitude = input.float(0, "Latitude", inline="1",  group=grol)
longitude = input.float(0, "Longitude", inline="1",  group=grol)

geo = input.bool(true, "Geocentric?", tooltip="Leave this box unchecked for heliocentric. Only applies to Longitude calculations.")
geoout = geo ? 1 : 0

day = AL.J2000(AL.JDN(gt, 0, tz))
dayr = AL.J2000(AL.JDN(gt, 1, tz))

orbital_out = orbital_in == "Longitude" ? geoout : orbital_in == "Latitude" ? 2 : orbital_in == "RA" ? 5 : orbital_in == "Altitude" ? 4 : orbital_in == "Declination" ? 3 : na

moonout = AL.getmoon(orbital_out, day, dayr, latitude, longitude)
sunout = AL.getsun(orbital_out, day, dayr, latitude, longitude, tz)
mercout = AL.getplanet(1,orbital_out, day, dayr, latitude, longitude, tz)
venuout = AL.getplanet(2,orbital_out, day, dayr, latitude, longitude, tz)
eartout = AL.getplanet(3,orbital_out, day, dayr, latitude, longitude, tz)
marsout = AL.getplanet(4,orbital_out, day, dayr, latitude, longitude, tz)
jupiout = AL.getplanet(5,orbital_out, day, dayr, latitude, longitude, tz)
satuout = AL.getplanet(6,orbital_out, day, dayr, latitude, longitude, tz)
uranout = AL.getplanet(7,orbital_out, day, dayr, latitude, longitude, tz)
neptout = AL.getplanet(8,orbital_out, day, dayr, latitude, longitude, tz)
plutout = AL.getplanet(9,orbital_out, day, dayr, latitude, longitude, tz)

planet_out = planet_in == "☉︎ Sun" ? sunout : planet_in == "☽︎ Moon" ? moonout : planet_in == "☿ Mercury" ? mercout : planet_in == "♀ Venus" ? venuout : planet_in == "🜨 Earth" ? eartout : planet_in == "♂ Mars" ? marsout : planet_in == "♃ Jupiter" ? jupiout : planet_in == "♄ Saturn" ? satuout : planet_in == "⛢ Uranus" ? uranout : planet_in == "♆ Neptune" ? neptout : planet_in == "♇ Pluto" ? plutout : na
col_out = planet_in == "☉︎ Sun" ? color.yellow : planet_in == "☽︎ Moon" ? color.silver : planet_in == "☿ Mercury" ? color.maroon : planet_in == "♀ Venus" ? color.green : planet_in == "🜨 Earth" ? color.blue : planet_in == "♂ Mars" ? color.red : planet_in == "♃ Jupiter" ? color.orange : planet_in == "♄ Saturn" ? color.gray : planet_in == "⛢ Uranus" ? color.navy : planet_in == "♆ Neptune" ? color.fuchsia : planet_in == "♇ Pluto" ? color.black : na
post_tt_out = orbital_in == "Longitude" ? "°" : orbital_in == "Latitude" ? "°" : orbital_in == "RA" ? "" : orbital_in == "Altitude" ? "°" : orbital_in == "Declination" ? "°" : na

hi = planet_out[2]<planet_out[1] and planet_out[1]>planet_out
lo = planet_out[2]>planet_out[1] and planet_out[1]<planet_out
hi_out = ta.valuewhen(hi, planet_out, 0)
lo_out = ta.valuewhen(lo, planet_out, 0)

transp=0
plot(planet_out, color=color.new(col_out,transp), style=plot.style_line, show_last=showlast, linewidth=2)

var label orbital = na
if barstate.islast
    orbital := label.new(bar_index, y=planet_out, text=planet_in, size=size.small, style=label.style_label_left, color=color.new(color.white,100), textcolor=color.new(col_out,0), tooltip=planet_in + "\n" + orbital_in + "\n" + str.tostring(planet_out, "#.##") + post_tt_out) //AL.deg_to_time(speed_out))
    label.delete(orbital[1])

geo_tt = geo ? " (Geo)" : " (Helio)"
long_tt = orbital_in == "Longitude" ? geo_tt : ""

var table Info = na
table.delete(Info)
Info := table.new(position, 1, 1)
if barstate.islast
    table.cell(Info, 0, 0, 
      text = planet_in + " " + orbital_in + long_tt, 
      text_size = vTxtSize, 
      text_color = color.new(col_info,0))

//-------------------------------Pivots---------------------------------------//
pivot_in = planet_out
left = lookback
right = left

ms_per_bar = time[1] - time[2] 
pivot_time(now_time) => (time - (left * ms_per_bar)) 
time_out = str.format_time(pivot_time(time), d_t?"MM-dd-yyyy\nHH:mm":"MM-dd-yyyy", syminfo.timezone)

p_hi = ta.pivothigh(pivot_in, left, right)
var label hi_lab = na
if p_hi 
    hi_lab:= label.new(x=bar_index-left, y=p_hi, text=time_out, size=size.small, color=color.new(color.gray, 100), textcolor=col_info, style=label.style_label_down, tooltip=orbital_in + ": " + str.tostring(planet_out[left], "#.##") + post_tt_out)

p_lo = ta.pivotlow(pivot_in, left, right)
var label lo_lab = na
if p_lo 
    lo_lab:= label.new(x=bar_index-left, y=p_lo, text=time_out, size=size.small, color=color.new(color.gray, 100), textcolor=col_info, style=label.style_label_up, tooltip=orbital_in + ": " + str.tostring(planet_out[left], "#.##") + post_tt_out)

// EoS made w/ ❤ by @BarefootJoey ✌💗📈