Wave Trend
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Ox_kali
// ________________________________________________________________________________________________________________________________       
                                  
//  ██████╗ ██╗  ██╗        ██╗  ██╗ █████╗ ██╗     ██╗     █████╗ ██╗      ██████╗  ██████╗ ██████╗ ██╗████████╗██╗  ██╗███╗   ███╗
// ██╔═══██╗╚██╗██╔╝        ██║ ██╔╝██╔══██╗██║     ██║    ██╔══██╗██║     ██╔════╝ ██╔═══██╗██╔══██╗██║╚══██╔══╝██║  ██║████╗ ████║
// ██║   ██║ ╚███╔╝         █████╔╝ ███████║██║     ██║    ███████║██║     ██║  ███╗██║   ██║██████╔╝██║   ██║   ███████║██╔████╔██║
// ██║   ██║ ██╔██╗         ██╔═██╗ ██╔══██║██║     ██║    ██╔══██║██║     ██║   ██║██║   ██║██╔══██╗██║   ██║   ██╔══██║██║╚██╔╝██║
// ╚██████╔╝██╔╝ ██╗███████╗██║  ██╗██║  ██║███████╗██║    ██║  ██║███████╗╚██████╔╝╚██████╔╝██║  ██║██║   ██║   ██║  ██║██║ ╚═╝ ██║
//  ╚═════╝ ╚═╝  ╚═╝╚══════╝╚═╝  ╚═╝╚═╝  ╚═╝╚══════╝╚═╝    ╚═╝  ╚═╝╚══════╝ ╚═════╝  ╚═════╝ ╚═╝  ╚═╝╚═╝   ╚═╝   ╚═╝  ╚═╝╚═╝     ╚═╝
// ________________________________________________________________________________________________________________________________       
                                                                                                                                                                                                                                                  
//@version=5
// Indicator title and short title 
// ________________________________________________________________________________________________________________________________

indicator(title="Wave Trend", shorttitle="Wave Trend", overlay=true)

// Parameters - user input settings
// ________________________________________________________________________________________________________________________________

min_max_period = input.int(365, title="Period for min and max (days)", minval=1, maxval=365, step=20)
satisfaction_period = input.int(365, title="Period for average satisfaction (days)", minval=1, maxval=365, step=20)
sma_period = input.int(365, title="Period for SMA", minval=1, maxval=1000, step=10)
smooth_normalized  = input.int(2, title="Smooth normalized value by periode mutlipicator", minval=1, maxval=10, step=1)
color_map_choice = input.bool(true, "value/sma or value/scale")
user_choice = input.bool(false, "Normalized/average satisfaction")

// Calculate the highest high and lowest low over a given period
// ________________________________________________________________________________________________________________________________

lowest_low = ta.lowest(low, min_max_period)
highest_high = ta.highest(high, min_max_period)

// Calculate the air bubble position in the tube
// ________________________________________________________________________________________________________________________________

air_bubble = (close - lowest_low) / (highest_high - lowest_low)

// Calculate the average satisfaction of masons who invested in the last 365 days
// ________________________________________________________________________________________________________________________________

average_satisfaction = 0.0
for i = 0 to math.min(bar_index, satisfaction_period - 1)
    average_satisfaction := average_satisfaction + ((close - close[i]) / (highest_high - lowest_low))
average_satisfaction := average_satisfaction / math.min(bar_index + 1, satisfaction_period)

// Calculate the average satisfaction over the entire period
// ________________________________________________________________________________________________________________________________

satisfaction_moyenne_ensemble = ta.sma(average_satisfaction, bar_index + 1)

// Create a variable to store the normalized average satisfaction
// ________________________________________________________________________________________________________________________________

normalized_satisfaction = 0.0

// Calculate the minimum and maximum satisfaction over the selected period
// ________________________________________________________________________________________________________________________________

min_satisfaction = ta.lowest(average_satisfaction, satisfaction_period*smooth_normalized)
max_satisfaction = ta.highest(average_satisfaction, satisfaction_period*smooth_normalized)

// Normalize the average satisfaction between 0 and 1
// ________________________________________________________________________________________________________________________________

normalized_satisfaction := (average_satisfaction - min_satisfaction) / (max_satisfaction - min_satisfaction)

// Calculate the difference between normalized_satisfaction and its SMA
// ________________________________________________________________________________________________________________________________

sma_normalized_satisfaction = ta.sma(normalized_satisfaction, sma_period)
sma_average_satisfaction = ta.sma(average_satisfaction, sma_period)

// Calculate the difference between normalized_satisfaction and its SMA
// ________________________________________________________________________________________________________________________________

diff_normalized_satisfaction = normalized_satisfaction - sma_normalized_satisfaction

// Calculate the difference between average satisfaction and its SMA
// ________________________________________________________________________________________________________________________________

diff_average_satisfaction = average_satisfaction - sma_average_satisfaction

// User choice variable normalized/average for diff sma display calculation
// ________________________________________________________________________________________________________________________________

user_choice_normalized_average = user_choice ? diff_normalized_satisfaction : diff_average_satisfaction

// User choice variable normalized/average for scall display calculation
// ________________________________________________________________________________________________________________________________

satisfaction_user_choice= user_choice ? normalized_satisfaction : average_satisfaction

// Create a function to map the color gradient based on the diff_normalized_satisfaction value
// ________________________________________________________________________________________________________________________________

color_map_sma(_val) => _val < -0.25 ? color.new(color.rgb(203, 67, 53), 0) : _val < 0 ? color.new(color.rgb(241, 148, 138), 0) : _val < 0.25 ? color.new(color.rgb(118, 215, 196), 0) : color.new(color.rgb(23, 165, 137), 0)

// // Create a function to map the color gradient based on the normalized_satisfaction value
// ________________________________________________________________________________________________________________________________

color_map_buble_value(_val) => _val < 0.1 ? color.new(color.rgb(203, 67, 53), 0) : _val < 0.3 ? color.new(#f57769, 0) : _val < 0.5 ? color.new(color.rgb(241, 148, 138), 0) : _val < 0.6 ? color.new(color.rgb(118, 215, 196), 0) : _val < 0.8 ? color.new(#3db9a0, 0) :  color.new(#268b77, 0)

// Plot the shape.square with a color gradient based on the diff_normalized_satisfaction value
// ________________________________________________________________________________________________________________________________

plotshape(true, style=shape.square, location=location.bottom, color=color_map_choice ? color_map_sma(user_choice_normalized_average) : color_map_buble_value(satisfaction_user_choice), size="tiny")

