ATH Levels V3
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © daytask
//
//
// V2 features
// - hide/show level lines
// - configurable lookback period whe nsearching for highest high
// - configurable rightmargin for last bar (before displaying level info)
// - only shows levels with a pot size % set
//
// V3 features (9th May 2020)
// - Hide the ATH level enabling zooming into lower levels with higher resolution

//@version=4
study("ATH Levels", overlay=true)

potSize = input(defval=1000,title="Pot size", type=input.float, minval = 1)
LookbackPeriod = input(defval = 365, title = "Days to Lookback")
RightMargin = input(defval = 10, title = "Margin from last bar") 
ShowLines = input(defval = true, title = "Show level lines")
HideATH = input(defval = false, title = "Hide ATH level") 

percPotLevel1 = input(defval=0, title="Level 1 pot %", type=input.float, minval = 0, maxval=100)
percPotLevel2 = input(defval=0, title="Level 2 pot %", type=input.float, minval = 0, maxval=100)
percPotLevel3 = input(defval=10, title="Level 3 pot %", type=input.float, minval = 0, maxval=100)
percPotLevel4 = input(defval=20, title="Level 4 pot %", type=input.float, minval = 0, maxval=100)
percPotLevel5 = input(defval=25, title="Level 5 pot %", type=input.float, minval = 0, maxval=100)
percPotLevel6 = input(defval=30, title="Level 6 pot %", type=input.float, minval = 0, maxval=100)
percPotLevel7 = input(defval=10, title="Level 7 pot %", type=input.float, minval = 0, maxval=100)
percPotLevel8 = input(defval=5, title="Level 8 pot %", type=input.float, minval = 0, maxval=100)

// everything is rendered in ms, and we determine the size of thos ms in the candle width (in time)
var lastCandleSpacing = RightMargin
var lineLength = 30

var levelColors = array.new_color(8)
var c_fillColor =  #24a06a
array.set(levelColors, 0, color.new(c_fillColor, 40))
array.set(levelColors, 1, color.new(c_fillColor, 35))
array.set(levelColors, 2, color.new(c_fillColor, 30))
array.set(levelColors, 3, color.new(c_fillColor, 25))
array.set(levelColors, 4, color.new(c_fillColor, 20))
array.set(levelColors, 5, color.new(c_fillColor, 10))
array.set(levelColors, 6, color.new(c_fillColor, 5))
array.set(levelColors, 7, color.new(c_fillColor, 0))

timePerCandle = time[0] - time[1]
highestHigh = highest(close, LookbackPeriod)

leftMarginPosition = time[0] + (timePerCandle * lastCandleSpacing)
lineEndRight =  leftMarginPosition + (timePerCandle * lineLength)
ath = highestHigh[0]    

LEVEL1 = 0.9
LEVEL2 = 0.8
LEVEL3 = 0.7
LEVEL4 = 0.6
LEVEL5 = 0.5
LEVEL6 = 0.4
LEVEL7 = 0.3
LEVEL8 = 0.2

AthLevelValue(n) =>
    ath*n

LevelPercOffset(LEVEL) =>
    (1-LEVEL)*100
    
AthLine(LEVEL, value)  => 
    var line ln = na
    line.delete(ln)
    if value > 0
        level = AthLevelValue(LEVEL)
        ln:=line.new(x1=leftMarginPosition , y1=level, x2=lineEndRight , y2=level, xloc=xloc.bar_time,  color=c_fillColor, style=line.style_dashed, extend=extend.none)
    
AthLabel(LEVEL, percPotLevel, labelColor)  => 
    var label lb = na
    label.delete(lb)
    if percPotLevel > 0
        level = AthLevelValue(LEVEL)
        potValueLevel = potSize * (percPotLevel / 100)
        // labelColor = level < close[0] ? colorLevelAbovePrice :colorLevelUnderPrice
        lb:=label.new(x=leftMarginPosition, y=level, text="ATH -"+tostring(LevelPercOffset(LEVEL))+"% = "+tostring(level)+"\n"+tostring(percPotLevel)+"% Pot size = $"+tostring(potValueLevel), color=labelColor, textcolor=color.white, style=label.style_label_lower_left, xloc=xloc.bar_time, textalign=text.align_left)
        
        

// now we render the levels off the latest highest high (only do this on the last bar)
if barstate.islast
     
    // Draw lines (if has settings % value)
    if ShowLines
        athLine = line.new(x1=leftMarginPosition , y1=ath, x2=lineEndRight , y2=ath, xloc=xloc.bar_time, style=line.style_dashed, extend=extend.none, color=c_fillColor)
        AthLine(LEVEL1, percPotLevel1)
        AthLine(LEVEL2, percPotLevel2)
        AthLine(LEVEL3, percPotLevel3)
        AthLine(LEVEL4, percPotLevel4)
        AthLine(LEVEL5, percPotLevel5)
        AthLine(LEVEL6, percPotLevel6)
        AthLine(LEVEL7, percPotLevel7)
        AthLine(LEVEL8, percPotLevel8)
    
    // Draw labels
    if HideATH == false
        label.new(x=leftMarginPosition, y=ath, text="ATH = "+tostring(ath)+", Total pot = $"+tostring(potSize), color=color.gray, textcolor=color.white, style=label.style_label_lower_left, xloc=xloc.bar_time, textalign=text.align_left)
    AthLabel(LEVEL1, percPotLevel1, array.get(levelColors, 0))
    AthLabel(LEVEL2, percPotLevel2, array.get(levelColors, 1))
    AthLabel(LEVEL3, percPotLevel3, array.get(levelColors, 2))
    AthLabel(LEVEL4, percPotLevel4, array.get(levelColors, 3))
    AthLabel(LEVEL5, percPotLevel5, array.get(levelColors, 4))
    AthLabel(LEVEL6, percPotLevel6, array.get(levelColors, 5))
    AthLabel(LEVEL7, percPotLevel7, array.get(levelColors, 6))
    AthLabel(LEVEL8, percPotLevel8, array.get(levelColors, 7))
    
    
    
    
    
    
    