Mason’s Line Indicator
// 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="Mason’s Line Indicator", shorttitle="Mason’s Line Indicator", format=format.price, precision=4)

// 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)
bubble_value = input.float(0.5, "bubble_value", minval=0, maxval=1, step=0.025)
user_choice = input.bool(true, "Normalized average satisfaction")
smooth_normalized  = input.int(2, title="Smooth normalized value by periode mutlipicator", minval=1, maxval=10, step=1)

// 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)

// Calculate the closing price for a 0.5 air bubble
// ________________________________________________________________________________________________________________________________                      

price_bubble_05 = bubble_value * (highest_high - lowest_low) + lowest_low

// 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)

// Plot the normalized average satisfaction between 0 and 1 or the average satisfaction
// ________________________________________________________________________________________________________________________________                      

hline(bubble_value, linestyle=hline.style_dashed, linewidth=2, color=color.new(#F7931A, 40), title='buble value')

// Plot the selected satisfaction plot (normalized or not) and the corresponding SMA
// ________________________________________________________________________________________________________________________________                      

plot(user_choice ? normalized_satisfaction : average_satisfaction, color=color.new(#008000, 0), title='Masons Satisfaction')
plot(user_choice ? ta.sma(normalized_satisfaction, sma_period) : ta.sma(average_satisfaction, sma_period))



