Levels Of Greed
// 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 Greed", overlay=true)

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

// The indicator inputs
length = input.int(defval=70, minval=30, maxval=150, title = "Window", 
   tooltip="Higher values are better for measuring longer up-trends.")
stability = input.int(defval=20, 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 recent lowest price
current_low= ta.lowest(high,length)

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

// Define the condition that an up-move has started (the price is above the latest  
// low for a specified amount of bars)
condition1 = ta.highest(current_low,stability)
   == ta.lowest(current_low,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
greed_level_1 = current_low + 1*stdev
greed_level_2 = current_low + 2*stdev
greed_level_3 = current_low + 3*stdev
greed_level_4 = current_low + 4*stdev
greed_level_5 = current_low + 5*stdev
greed_level_6 = current_low + 6*stdev
greed_level_7 = current_low + 7*stdev

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

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

// Plot the levels of fear
plot(greed_level_1, color= greed_levels_color, linewidth=1)
plot(greed_level_2, color= greed_levels_color, linewidth=2)
plot(greed_level_3, color= greed_levels_color, linewidth=3)
plot(greed_level_4, color= greed_levels_color, linewidth=4)
plot(greed_level_5, color= greed_levels_color, linewidth=5)
plot(greed_level_6, color= greed_levels_color, linewidth=6)
plot(greed_level_7, color= greed_levels_color, linewidth=7)

// Prolongate the levels of fear when a decline is over and a rebound has started
greed_levels_color_previous = condition1==false?color.green:na
plot(ta.valuewhen(condition1==true, greed_level_1, 1), 
   color= greed_levels_color_previous, linewidth=1)
   
plot(ta.valuewhen(condition1==true, greed_level_2, 1), 
   color= greed_levels_color_previous, linewidth=2)
   
plot(ta.valuewhen(condition1==true, greed_level_3, 1), 
   color= greed_levels_color_previous, linewidth=3)
   
plot(ta.valuewhen(condition1==true, greed_level_4, 1), 
   color= greed_levels_color_previous, linewidth=4)

plot(ta.valuewhen(condition1==true, greed_level_5, 1), 
   color= greed_levels_color_previous, linewidth=5)
   
plot(ta.valuewhen(condition1==true, greed_level_6, 1), 
   color= greed_levels_color_previous, linewidth=6)
   
plot(ta.valuewhen(condition1==true, greed_level_7, 1), 
   color= greed_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 emoji labels if the Cool Guys Mode is selected
plotchar(coolmode and draw_label_condition?greed_level_1:na,location=location.absolute, char = "😌", size=size.small)
plotchar(coolmode and draw_label_condition?greed_level_2:na,location=location.absolute, char = "😋", size=size.small)
plotchar(coolmode and draw_label_condition?greed_level_3:na,location=location.absolute, char = "😃", size=size.small)
plotchar(coolmode and draw_label_condition?greed_level_4:na,location=location.absolute, char = "😮", size=size.small)
plotchar(coolmode and draw_label_condition?greed_level_5:na,location=location.absolute, char = "😍", size=size.small)
plotchar(coolmode and draw_label_condition?greed_level_6:na,location=location.absolute, char = "🤪", size=size.small)
plotchar(coolmode and draw_label_condition?greed_level_7:na,location=location.absolute, char = "🤑", size=size.small)

// Plot the number labels if the Serious Guys Mode is selected
plotchar(coolmode==false and draw_label_condition?greed_level_1:na,location=location.absolute, char = "1", size=size.small)
plotchar(coolmode==false and draw_label_condition?greed_level_2:na,location=location.absolute, char = "2", size=size.small)
plotchar(coolmode==false and draw_label_condition?greed_level_3:na,location=location.absolute, char = "3", size=size.small)
plotchar(coolmode==false and draw_label_condition?greed_level_4:na,location=location.absolute, char = "4", size=size.small)
plotchar(coolmode==false and draw_label_condition?greed_level_5:na,location=location.absolute, char = "5", size=size.small)
plotchar(coolmode==false and draw_label_condition?greed_level_6:na,location=location.absolute, char = "6", size=size.small)
plotchar(coolmode==false and draw_label_condition?greed_level_7:na,location=location.absolute, char = "7", size=size.small)

