Astro: Planetary Speed
// 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: Planetary Speed", 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"])
showlast = input.int(1000, "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("Small", title="Label Size", options=["Auto", "Tiny", "Small", "Normal", "Large", "Huge"], inline="4l")
col_lab = input.color(color.white, "Label Color")
d_t = input.bool(false, "Date & Time?", tooltip="Leave this unchecked to display date only.", inline="4l")
vTxtSize=iTxtSize == "Auto" ? size.auto : iTxtSize == "Tiny" ? size.tiny : iTxtSize == "Small" ? size.small : iTxtSize == "Normal" ? size.normal : iTxtSize == "Large" ? size.large : size.huge

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 = true // input.bool(true, "Geocentric?", tooltip="Leave this box unchecked for heliocentric.")
geoout = geo ? 1 : 0

J2000(JDN) => JDN - 2451545.0 //J2000
JDN(t, d) => d == 0 ? (t / 86400000) + 2440587.5 + (tz / 24) : int((t / 86400000) + 2440587.5 + (tz / 24) - 1) + 0.5

day = J2000(JDN(gt, 0))
dayr = J2000(JDN(gt, 1))

planetspeed(planetlong) => AL.AngToCirc(planetlong - planetlong[1])

moonspeed = planetspeed(AL.getmoon(geoout, day, dayr, latitude, longitude))
sunspeed = planetspeed(AL.getsun(geoout, day, dayr, latitude, longitude, tz))
mercspeed = planetspeed(AL.getplanet(1,geoout, day, dayr, latitude, longitude, tz))
venuspeed = planetspeed(AL.getplanet(2,geoout, day, dayr, latitude, longitude, tz))
eartspeed = planetspeed(AL.getplanet(3,geoout, day, dayr, latitude, longitude, tz))
marsspeed = planetspeed(AL.getplanet(4,geoout, day, dayr, latitude, longitude, tz))
jupispeed = planetspeed(AL.getplanet(5,geoout, day, dayr, latitude, longitude, tz))
satuspeed = planetspeed(AL.getplanet(6,geoout, day, dayr, latitude, longitude, tz))
uranspeed = planetspeed(AL.getplanet(7,geoout, day, dayr, latitude, longitude, tz))
neptspeed = planetspeed(AL.getplanet(8,geoout, day, dayr, latitude, longitude, tz))
plutspeed = planetspeed(AL.getplanet(9,geoout, day, dayr, latitude, longitude, tz))

speed_out = planet_in == "☉︎ Sun" ? sunspeed : planet_in == "☽︎ Moon" ? moonspeed : planet_in == "☿ Mercury" ? mercspeed : planet_in == "♀ Venus" ? venuspeed : planet_in == "🜨 Earth" ? eartspeed : planet_in == "♂ Mars" ? marsspeed : planet_in == "♃ Jupiter" ? jupispeed : planet_in == "♄ Saturn" ? satuspeed : planet_in == "⛢ Uranus" ? uranspeed : planet_in == "♆ Neptune" ? neptspeed : planet_in == "♇ Pluto" ? plutspeed : 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

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

var label speed = na
if barstate.islast
    speed := label.new(bar_index, y=speed_out, text=planet_in, size=vTxtSize, style=label.style_label_left, color=color.new(color.white,100), textcolor=color.new(col_out,0), tooltip=planet_in + " Speed:\n" + str.tostring(speed_out, "#.##") + "° per bar") //AL.deg_to_time(speed_out))
    label.delete(speed[1])

//-------------------------------Pivots---------------------------------------//
pivot_in = speed_out
left = 3
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_lab, style=label.style_label_down, tooltip=planet_in + " Speed:\n" + str.tostring(speed_out, "#.##") + "° per bar\n" + "Perihelion")

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_lab, style=label.style_label_up, tooltip=planet_in + " Speed:\n" + str.tostring(speed_out, "#.##") + "° per bar\n" + "Aphelion")

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