Cosmic Pi Troll Cycle
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © cosmic_indicators / Trollplogen

//@version=4
study("Cosmic Pi Troll Cycle", overlay = true)

// --------------------------------------------------------------------------------
// Variables
// --------------------------------------------------------------------------------

// Input
asset           = input(title = "Asset Model", type=input.string, defval="BTC", options=["BTC","ETH","LINK","Custom"], group = "Model and Indicator")
indicatorType   = input(title = "Type of Indicator", type=input.string, defval="Top", options=["Top","Bottom"], group = "Model and Indicator")
tresh           = input(title = "Width of SMA Band [%]", type = input.float, defval = 10, group = "Warning Threshold")
show_fill       = input(title = "Show SMA Band", type = input.bool, defval = true, group = "View")
show_warning    = input(title = "Show Warning", type = input.bool, defval = true, group = "View")

CustomNumerator_len     = input(title = "Long SMA Length", type = input.integer, defval = 280, group = "Custom Settings")
CustomDenominator_len   = input(title = "Short SMA Length", type = input.integer, defval = 138, group = "Custom Settings")
CustomMult              = input(title = "Scaling", type = input.float, defval = 1.8, group = "Custom Settings")

numerator_len = 200
denominator_len = 100
mult = 2.0
show_top_markers = true
show_btm_markers = false

// Troll SMAs
if (asset == "BTC")
    numerator_len := 355
    denominator_len := 113
    if (indicatorType == "Top")
        mult := 2
    else if (indicatorType == "Bottom")
        mult := 0.7
else if (asset == "ETH")
    numerator_len   := 455
    denominator_len := 183
    if (indicatorType == "Top")
        mult := 1.95
    else if (indicatorType == "Bottom")
        mult := 0.6
else if (asset == "LINK")
    numerator_len   := 271
    denominator_len := 93
    if (indicatorType == "Top")
        mult := 1.6
    else if (indicatorType == "Bottom")
        mult := 0.75
else if (asset == "Custom")
    numerator_len   := CustomNumerator_len
    denominator_len := CustomDenominator_len
    mult            := CustomMult

// Colors
teal_rgb = #005843
teal_0 = color.new(teal_rgb, 0)
teal_33 = color.new(teal_rgb, 33)
violet_rgb = #aa47bc
violet_0 = color.new(violet_rgb, 0)
violet_33 = color.new(violet_rgb, 33)
red_rgb = #ff5252
red_0 = color.new(red_rgb, 0)
transparent = color.new(color.black, 100)

// --------------------------------------------------------------------------------
// Logic
// --------------------------------------------------------------------------------

numerator = sma(close, numerator_len) * mult
denominator = sma(close, denominator_len)
basis = avg(denominator, numerator)

TopWarningLine = denominator * (100+tresh)/100
BottomWarningLine = numerator * (100+tresh)/100

// --------------------------------------------------------------------------------
// View
// --------------------------------------------------------------------------------

numerator_plot = plot(numerator, display = display.none, title = "Numerator")
denominator_plot = plot(denominator, display = display.none, title = "Denominator")
basis_plot = plot(basis, display = display.none, title = "Basis")
//TopWarning_plot = plot(TopWarningLine, title = "Top Warning Buffer", color = red_0)
//BottomWarning_plot = plot(BottomWarningLine, title = "Bottom Warning Buffer", color = red_0)

fill(numerator_plot, 
  basis_plot, 
  color = show_fill ? teal_33 : transparent,
  title = "Cosmic Pi Troll Cycle Numerator Fill")
fill(denominator_plot, basis_plot, 
  color = show_fill ? violet_33 : transparent,
  title = "Cosmic Pi Troll Cycle Denominator Fill")

if (indicatorType == "Top")
    show_top_markers := true
    show_btm_markers := false
else if (indicatorType == "Bottom")
    show_top_markers := false
    show_btm_markers := true
    
plotshape(show_warning and show_top_markers and crossunder(numerator, TopWarningLine),
  color = color.yellow,
  style = shape.triangledown,
  location = location.abovebar,
  size = size.normal,
  title = "Cosmic Pi Troll Cycle Top Warning Marker")
plotshape(show_warning and show_btm_markers and crossover(BottomWarningLine, denominator),
  color = color.yellow,
  style = shape.triangledown,
  location = location.abovebar,
  size = size.normal,
  title = "Cosmic Pi Troll Cycle Bottom Warning Marker")
plotshape(show_top_markers and crossunder(numerator, denominator),
  color = teal_0,
  style = shape.circle,
  location = location.abovebar,
  size = size.normal,
  title = "Cosmic Pi Troll Cycle Primary Top Marker")
plotshape(show_btm_markers and crossover(numerator, denominator), 
  color = violet_0, 
  style = shape.circle, 
  location = location.belowbar, 
  size = size.normal,
  title = "Cosmic Pi Troll Cycle Primary Bottom Marker")
