Levels Of Fear [AstrideUnicorn]
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © AstrideUnicorn

//@version=5
indicator("Levels Of Fear", overlay=true)

// Define the source price series for the indicator calculations
source = close

// The indicator inputs
length = input.int(defval=60, minval=30, maxval=150, title = "Window", 
   tooltip="Higher values are better for measuring deeper and longer declines.")
stability = input.int(defval=10, minval=10, maxval=20, 
   title = "Levels Stability", tooltip="The higher the value is, 
      the more stable and long the levels are, but the lag slightly increases.")
mode = input.string(defval="Cool Guys Mode", options=['Cool Guys Mode','Serious Guys Mode'])

// Calculate the current base level as the resent highest price
current_high = ta.highest(high,length)

// Calculate the rolling standard deviation of the source price
stdev = ta.stdev(source,4*length)

// Define the condition that a decline has started (the price is below current  
// high for a specified amount of bars)
condition1 = ta.highest(current_high,stability)
   == ta.lowest(current_high,stability)

// During a decline don't update the rolling standard deviation on each bar,  
// to fix the distance between the levels of fear
if condition1
    stdev:=stdev[1]
    
// Calculate the levels of fear
fear_level_1 = current_high - 1*stdev
fear_level_2 = current_high - 2*stdev
fear_level_3 = current_high - 3*stdev
fear_level_4 = current_high - 4*stdev
fear_level_5 = current_high - 5*stdev
fear_level_6 = current_high - 6*stdev
fear_level_7 = current_high - 7*stdev

// Store the previous value of base level
current_high_previous = ta.valuewhen(condition1==true, current_high, 1)

// Define the conditional color of the fear levels to make them visible only 
//during a decline
fear_levels_color = condition1?color.red:na

// Plot the levels of fear
plot(fear_level_1, color= fear_levels_color, linewidth=1)
plot(fear_level_2, color= fear_levels_color, linewidth=2)
plot(fear_level_3, color= fear_levels_color, linewidth=3)
plot(fear_level_4, color= fear_levels_color, linewidth=4)
plot(fear_level_5, color= fear_levels_color, linewidth=5)
plot(fear_level_6, color= fear_levels_color, linewidth=6)
plot(fear_level_7, color= fear_levels_color, linewidth=7)

// Prolongate the levels of fear when a decline is over and a rebound has started
fear_levels_color_previous = condition1==false?color.red:na
plot(ta.valuewhen(condition1==true, fear_level_1, 1), 
   color= fear_levels_color_previous, linewidth=1)
   
plot(ta.valuewhen(condition1==true, fear_level_2, 1), 
   color= fear_levels_color_previous, linewidth=2)
   
plot(ta.valuewhen(condition1==true, fear_level_3, 1), 
   color= fear_levels_color_previous, linewidth=3)
   
plot(ta.valuewhen(condition1==true, fear_level_4, 1), 
   color= fear_levels_color_previous, linewidth=4)

plot(ta.valuewhen(condition1==true, fear_level_5, 1), 
   color= fear_levels_color_previous, linewidth=5)
   
plot(ta.valuewhen(condition1==true, fear_level_6, 1), 
   color= fear_levels_color_previous, linewidth=6)
   
plot(ta.valuewhen(condition1==true, fear_level_7, 1), 
   color= fear_levels_color_previous, linewidth=7)

// Condition that defines the position of the fear levels labels: on the fourth 
// bar after a new set of fear levels is formed.
draw_label_condition = ta.barssince(condition1[1]==false and condition1==true)==3

// Condition that checks if the mode for the labels is the Cool Guys Mode
coolmode = mode == "Cool Guys Mode"
        
// Plot the labels if the Cool Guys Mode is selected
plotchar(coolmode and draw_label_condition?fear_level_1:na,location=location.absolute, char = "😌", size=size.small)
plotchar(coolmode and draw_label_condition?fear_level_2:na,location=location.absolute, char = "🤔", size=size.small)
plotchar(coolmode and draw_label_condition?fear_level_3:na,location=location.absolute, char = "🤨", size=size.small)
plotchar(coolmode and draw_label_condition?fear_level_4:na,location=location.absolute, char = "😮", size=size.small)
plotchar(coolmode and draw_label_condition?fear_level_5:na,location=location.absolute, char = "🤬", size=size.small)
plotchar(coolmode and draw_label_condition?fear_level_6:na,location=location.absolute, char = "😖", size=size.small)
plotchar(coolmode and draw_label_condition?fear_level_7:na,location=location.absolute, char = "😱", size=size.small)

// Plot the labels if the Serious Guys Mode is selected
plotchar(coolmode==false and draw_label_condition?fear_level_1:na,location=location.absolute, char = "1", size=size.small)
plotchar(coolmode==false and draw_label_condition?fear_level_2:na,location=location.absolute, char = "2", size=size.small)
plotchar(coolmode==false and draw_label_condition?fear_level_3:na,location=location.absolute, char = "3", size=size.small)
plotchar(coolmode==false and draw_label_condition?fear_level_4:na,location=location.absolute, char = "4", size=size.small)
plotchar(coolmode==false and draw_label_condition?fear_level_5:na,location=location.absolute, char = "5", size=size.small)
plotchar(coolmode==false and draw_label_condition?fear_level_6:na,location=location.absolute, char = "6", size=size.small)
plotchar(coolmode==false and draw_label_condition?fear_level_7:na,location=location.absolute, char = "7", size=size.small)

