Bearish Market Indicator V2
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © fourkhansolo

//@version=5
indicator("Bearish Market Indicator - 4khansolo", shorttitle="BMI-4khansolo", overlay = true)
import fourkhansolo/L_Index_4khansolo/7 as index

// Color
whiteColor = color.white
yellowColor = color.yellow

// collecting closing at different ranges
high_ticker_d = request.security(syminfo.tickerid, "D", high, currency = syminfo.currency)
high_ticker_w = request.security(syminfo.tickerid, "W", high_ticker_d, currency = syminfo.currency)
high_ticker_m = request.security(syminfo.tickerid, "M", high_ticker_d, currency = syminfo.currency)
// Source: https://www.tradingview.com/pine-script-reference/v5/#fun_request{dot}security


// collecting highest from highest at different ranges
highest_highest_ticker_w = ta.highest(high_ticker_d,5)                      // 1 Week
highest_highest_ticker_d_m = ta.highest(high_ticker_d,30)                   // 1 Month
highest_highest_ticker_w_m = ta.highest(high_ticker_w,4)                    // 1 Month
// Source: https://www.tradingview.com/pine-script-reference/v5/#fun_ta{dot}highest




// ************************************************************ Parameter for Bearish Market Definition ******************************************************************************************************************************************************
// ........................................ Parameters for Bearish Market .....................................................................................................................................................
// Added Options for the Days of the Year ( Market Days Opened)
bmi_period = input.int(251, title="BMI-Period", options=[250,251,252,253,254,255,256,257,258,259,260])
daily_highs = request.security(syminfo.tickerid,"D",high, currency=syminfo.currency)
week52High= ta.highest(daily_highs,bmi_period)
bearish_market_value = week52High*(1-0.2)

bearish_by_definition = if close < bearish_market_value
    color.new(color.purple,0)
// Source: https://www.investopedia.com/terms/b/bearmarket.asp

plot(bearish_market_value, "Bearish Market By Definiton Value", color=color.new(color.yellow,50),style=plot.style_stepline_diamond)
week52High_p   = plot(week52High, title="52-Week High", color=color.green)
bearish_plot_1 = plot(week52High*(1.01), title="Bearish Market Low", display=display.none)
bearish_plot_2 = plot(week52High*(1.02), title="Bearish Market High", display=display.none)

fill(bearish_plot_1,bearish_plot_2, color=bearish_by_definition)



// ************************************************************ Dynamic Trends ******************************************************************************************************************************************************
// ........................................ Trends Based on Highs (exclusive)
// Period Based Trends trying to Mimicing manual trend lines [will improve further in the future update]
high_ticker_d_w = request.security(syminfo.tickerid, "W", highest_highest_ticker_w, currency = syminfo.currency)


smooth_high_1= ta.sma(high_ticker_d_w,5)
smooth_high_2 = ta.sma(smooth_high_1,5)
smooth_high_3 = ta.sma(smooth_high_2,5)
smooth_high_4 = ta.sma(smooth_high_3,5)
smooth_high_5 = ta.sma(smooth_high_4,5)
smooth_high_6 = ta.sma(smooth_high_5,5)
smooth_high_7 = ta.sma(smooth_high_6,5)
smooth_high_8 = ta.sma(smooth_high_7,5)
smooth_high_9 = ta.sma(smooth_high_8,5)
//plot(smooth_high_1,color=color.new(color.yellow,70))
//plot(smooth_high_2,color=color.new(color.yellow,60))
//plot(smooth_high_3,color=color.new(color.yellow,50))
//plot(smooth_high_4,color=color.new(color.yellow,40))
//plot(smooth_high_5,color=color.new(color.yellow,30))
//plot(smooth_high_6,color=color.new(color.yellow,20))
//plot(smooth_high_7,color=color.new(color.yellow,15))
//plot(smooth_high_8,color=color.new(color.yellow,10))
//plot(smooth_high_9,color=color.yellow)






// collecting closing at different ranges
low_ticker_d = request.security(syminfo.tickerid, "D", low, currency = syminfo.currency)
low_ticker_w = request.security(syminfo.tickerid, "W", low_ticker_d, currency = syminfo.currency)
low_ticker_m = request.security(syminfo.tickerid, "M", low_ticker_d, currency = syminfo.currency)
// Fiftytwo Week High
week51Low = ta.lowest(low_ticker_d,251)
plot(week51Low,"52 Week Low", color=color.red)

// collecting highest from highest at different ranges
lowest_lowest_ticker_w = ta.lowest(low_ticker_d,5)                      // 1 Week
lowest_lowest_ticker_d_m = ta.lowest(low_ticker_d,30)                   // 1 Month
lowest_lowest_ticker_w_m = ta.lowest(low_ticker_d,4)                    // 1 Month
//plot(highest_highest_ticker_d_m)

low_ticker_d_w = request.security(syminfo.tickerid, "W", lowest_lowest_ticker_w, currency = syminfo.currency)
//plot(high_ticker_d_w)

smooth_low_1= ta.sma(low_ticker_d_w,5)
smooth_low_2 = ta.sma(smooth_low_1,5)
smooth_low_3 = ta.sma(smooth_low_2,5)
smooth_low_4 = ta.sma(smooth_low_3,5)
smooth_low_5 = ta.sma(smooth_low_4,5)
smooth_low_6 = ta.sma(smooth_low_5,5)
smooth_low_7 = ta.sma(smooth_low_6,5)
smooth_low_8 = ta.sma(smooth_low_7,5)
smooth_low_9 = ta.sma(smooth_low_8,5)
//plot(smooth_low_1,color=color.new(color.green,70))
//plot(smooth_low_2,color=color.new(color.green,60))
//plot(smooth_low_3,color=color.new(color.green,50))
//plot(smooth_low_4,color=color.new(color.green,40))
//plot(smooth_low_5,color=color.new(color.green,30))
//plot(smooth_low_6,color=color.new(color.green,20))
//plot(smooth_low_7,color=color.new(color.green,15))
//plot(smooth_low_8,color=color.new(color.green,10))
//plot(smooth_low_9,color=color.green)



// ........................................ Super-Trends .....................................................................................................................................................
[supertrend, direction] = ta.supertrend(3, 10)
super_trend_red = ta.crossover(direction,0)
super_trend_green = ta.crossover(0,direction)
// Source: https://www.tradingview.com/pine-script-reference/v5/#fun_ta{dot}supertrend


green_1 = ta.crossover(close,smooth_high_9)
green_2 = ta.crossover (close[1],smooth_high_9[1])

red_green_signal = if (smooth_high_9 > close) and super_trend_red
    color.new(color.red,30)
else if (smooth_high_9 > close) or (direction > 0)
    color.new(color.red,70)
else if green_1 and (direction < 0)
    color.new(color.white,70)
else if green_2 and super_trend_green
    color.new(color.green,10) 

filer_52 = plot(week52High*1.04,display=display.none)
fill(week52High_p,filer_52 ,color=red_green_signal)


// ************************************************************ Parameter for Bearish Market Definition ******************************************************************************************************************************************************
// ............................................... Used in the Technical Indicators ..............................................................................................
close_d = request.security(syminfo.tickerid, "D", close)
close_w = request.security(syminfo.tickerid, "W", close)
close_m = request.security(syminfo.tickerid, "M", close)

open_d = request.security(syminfo.tickerid, "D", open)
open_w = request.security(syminfo.tickerid, "W", open)
open_m = request.security(syminfo.tickerid, "M", open) 


// ************************************************************************ Technical Indicators ************************************************************************************************************************************************
// ************************************************************ RSI  ************************************************************************************************************************
rsi_normal = ta.rsi(close,14)
rsi_ma_normal = ta.sma(rsi_normal,14)

fourteen_D_RSI = request.security(syminfo.tickerid, "D", rsi_normal)
fourteen_W_RSI = request.security(syminfo.tickerid, "W", rsi_normal)
fourteen_M_RSI = request.security(syminfo.tickerid, "M", rsi_normal)

// RSI Based
fourteen_D_RSI_ma = request.security(syminfo.tickerid, "D", rsi_ma_normal)
fourteen_W_RSI_ma = request.security(syminfo.tickerid, "W", rsi_ma_normal)
fourteen_M_RSI_ma = request.security(syminfo.tickerid, "M", rsi_ma_normal)

// RSI Color
fourteen_D_RSI_color = index.colorRSIfull(fourteen_D_RSI,fourteen_D_RSI_ma)
fourteen_W_RSI_color = index.colorRSIfull(fourteen_W_RSI,fourteen_W_RSI_ma)
fourteen_M_RSI_color = index.colorRSIfull(fourteen_M_RSI,fourteen_M_RSI_ma)

// RSI: String Conversion
fourteen_D_RSI_string= str.tostring(math.round(fourteen_D_RSI,2))  
fourteen_W_RSI_string= str.tostring(math.round(fourteen_W_RSI,2))
fourteen_M_RSI_string= str.tostring(math.round(fourteen_M_RSI,2))

// RSI-MA: String Conversion
fourteen_D_RSI_ma_string= str.tostring(math.round(fourteen_D_RSI_ma,2))  
fourteen_W_RSI_ma_string= str.tostring(math.round(fourteen_W_RSI_ma,2))  
fourteen_M_RSI_ma_string= str.tostring(math.round(fourteen_M_RSI_ma,2))  

fourteen_d_rsi_color = if fourteen_D_RSI >= 70
    color.red
else if fourteen_D_RSI <= 30
    color.red
else
    color.black
fourteen_m_rsi_color = if fourteen_M_RSI >= 70
    color.red
else if fourteen_M_RSI <= 30
    color.red
else
    color.black
// ************************************************************ Stochastic RSI  ************************************************************************************************************************
normal_daily_k = ta.sma(ta.stoch(rsi_normal, rsi_normal, rsi_normal, 14), 3)
normal_daily_d = ta.sma(normal_daily_k, 3)
// Stoch. RSI: K
fourteen_D_stochastic_RSI_k = request.security(syminfo.tickerid, "D", normal_daily_k)
fourteen_W_stochastic_RSI_k = request.security(syminfo.tickerid, "W", normal_daily_k)
fourteen_M_stochastic_RSI_k = request.security(syminfo.tickerid, "M", normal_daily_k)

// Stoch. RSI: D
fourteen_D_stochastic_RSI_d = request.security(syminfo.tickerid, "D", normal_daily_d)
fourteen_W_stochastic_RSI_d = request.security(syminfo.tickerid, "W", normal_daily_d)
fourteen_M_stochastic_RSI_d = request.security(syminfo.tickerid, "M", normal_daily_d)


// Stoch. RSI: String Conversion: K
fourteen_stoch_RSI_D_color =index.colorRSIfull(fourteen_D_stochastic_RSI_k,fourteen_D_stochastic_RSI_d)
fourteen_stoch_RSI_W_color =index.colorRSIfull(fourteen_W_stochastic_RSI_k,fourteen_W_stochastic_RSI_d)
fourteen_stoch_RSI_M_color =index.colorRSIfull(fourteen_M_stochastic_RSI_k,fourteen_M_stochastic_RSI_d)


// Stoch. RSI: String Conversion: K
fourteen_D_stoch_RSI_k_string = str.tostring(math.round(fourteen_D_stochastic_RSI_k,2))  
fourteen_W_stoch_RSI_k_string = str.tostring(math.round(fourteen_W_stochastic_RSI_k,2))
fourteen_M_stoch_RSI_k_string = str.tostring(math.round(fourteen_M_stochastic_RSI_k,2))

// Stoch. RSI: String Conversion: D
fourteen_D_stoch_RSI_d_string = str.tostring(math.round(fourteen_D_stochastic_RSI_d,2))  
fourteen_W_stoch_RSI_d_string = str.tostring(math.round(fourteen_W_stochastic_RSI_d,2))
fourteen_M_stoch_RSI_d_string = str.tostring(math.round(fourteen_M_stochastic_RSI_d,2))

// ************************************************************ MACD  ***********************************************************************************************************************
// ............................................... MACD ..............................................................................................
[macd_normal, signal_normal, histLine_d] = ta.macd(close, 12, 26, 9)

macdLine_d = request.security(syminfo.tickerid, "D", macd_normal)
signalLine_d = request.security(syminfo.tickerid, "D", signal_normal)
macdLine_w = request.security(syminfo.tickerid, "W", macd_normal)
signalLine_w = request.security(syminfo.tickerid, "W", signal_normal)
macdLine_m = request.security(syminfo.tickerid, "M", macd_normal)
signalLine_m = request.security(syminfo.tickerid, "M", signal_normal)

// MACD: String Conversion
macdLine_d_string= str.tostring(math.round(macdLine_d,2))  
macdLine_w_string= str.tostring(math.round(macdLine_w,2))
macdLine_m_string= str.tostring(math.round(macdLine_m,2))

signalLine_d_string= str.tostring(math.round(signalLine_d,2))
signalLine_w_string= str.tostring(math.round(signalLine_w,2))
signalLine_m_string= str.tostring(math.round(signalLine_m,2))

// MACD Color
macdLine_d_color = index.colorMACD(macdLine_d,signalLine_d)
macdLine_w_color = index.colorMACD(macdLine_w,signalLine_w)
macdLine_m_color = index.colorMACD(macdLine_m,signalLine_m)


// ............................................... Moving Average Metrics ..............................................................................................
// MA based on Daily Close
ma20_d = ta.sma(close_d,20)
ma50_d = ta.sma(close_d,50)
ma100_d = ta.sma(close_d,100)
ma200_d = ta.sma(close_d,200)

// MA 20 based 
sma_20_d = request.security(syminfo.tickerid, "D", ma20_d)
sma_20_w = request.security(syminfo.tickerid, "W", ma20_d)
sma_20_m = request.security(syminfo.tickerid, "M", ma20_d)
// MA 50 based 
sma_50_d = request.security(syminfo.tickerid, "D", ma50_d)
sma_50_w = request.security(syminfo.tickerid, "W", ma50_d)
sma_50_m = request.security(syminfo.tickerid, "M", ma50_d)
// MA 100 based 
sma_100_d = request.security(syminfo.tickerid, "D", ma100_d)
sma_100_w = request.security(syminfo.tickerid, "W", ma100_d)
sma_100_m = request.security(syminfo.tickerid, "M", ma100_d)
// MA 200 based 
sma_200_d = request.security(syminfo.tickerid, "D", ma200_d)
sma_200_w = request.security(syminfo.tickerid, "W", ma200_d)
sma_200_m = request.security(syminfo.tickerid, "M", ma200_d)
//Color
color_sma_20_d = index.colour(close_d,sma_20_d)
color_sma_20_w = index.colour(close_w,sma_20_w)
color_sma_20_m = index.colour(close_m,sma_20_m)
color_sma_50_d = index.colour(close_d,sma_50_d)
color_sma_50_w = index.colour(close_w,sma_50_w)
color_sma_50_m = index.colour(close_m,sma_50_m)
color_sma_100_d = index.colour(close_d,sma_100_d)
color_sma_100_w = index.colour(close_w,sma_100_w)
color_sma_100_m = index.colour(close_m,sma_100_m)
color_sma_200_d = index.colour(close_d,sma_200_d)
color_sma_200_w = index.colour(close_w,sma_200_w)
color_sma_200_m = index.colour(close_m,sma_200_m)

// String Conversion
sma_20_d_string = str.tostring(math.round(((close_d-sma_20_d)/sma_20_d)*100,2))  
sma_20_w_string = str.tostring(math.round(((close_w-sma_20_w)/sma_20_w)*100,2)) 
sma_20_m_string = str.tostring(math.round(((close_m-sma_20_m)/sma_20_m)*100,2)) 
sma_50_d_string = str.tostring(math.round(((close_d-sma_50_d)/sma_50_d)*100,2))  
sma_50_w_string = str.tostring(math.round(((close_w-sma_50_w)/sma_50_w)*100,2)) 
sma_50_m_string = str.tostring(math.round(((close_m-sma_50_m)/sma_50_m)*100,2)) 
sma_100_d_string = str.tostring(math.round(((close_d-sma_100_d)/sma_100_d)*100,2))  
sma_100_w_string = str.tostring(math.round(((close_w-sma_100_w)/sma_100_w)*100,2)) 
sma_100_m_string = str.tostring(math.round(((close_m-sma_100_m)/sma_100_m)*100,2)) 
sma_200_d_string = str.tostring(math.round(((close_d-sma_200_d)/sma_200_d)*100,2))  
sma_200_w_string = str.tostring(math.round(((close_w-sma_200_w)/sma_200_w)*100,2)) 
sma_200_m_string = str.tostring(math.round(((close_m-sma_200_m)/sma_200_m)*100,2)) 



// ************************************************************ Moving Average ************************************************************
// ............................................... Lagging: Indicators ..............................................................................................
technical_table = table.new(position = position.middle_right, rows = 5, columns = 4, bgcolor = color.new(color.black,0), border_width=1)
table.cell(technical_table, row = 0, column = 1, text = "D", text_color = whiteColor)
table.cell(technical_table, row = 0, column = 2, text = "W", text_color = whiteColor)
table.cell(technical_table, row = 0, column = 3, text = "M", text_color = whiteColor)

// Simple Moving Average: 20MA
table.cell(technical_table, row = 1, column = 0, text = "20MA (%)", text_color = whiteColor)
table.cell(technical_table, row = 1, column = 1, text = sma_20_d_string, text_color = whiteColor, bgcolor = color_sma_20_d)
table.cell(technical_table, row = 1, column = 2, text = sma_20_w_string, text_color = whiteColor, bgcolor = color_sma_20_w)
table.cell(technical_table, row = 1, column = 3, text = sma_20_m_string, text_color = whiteColor, bgcolor = color_sma_20_m)
 
// Simple Moving Average: 50MA
table.cell(technical_table, row = 2, column = 0, text = "50MA (%)", text_color = whiteColor)
table.cell(technical_table, row = 2, column = 1, text = sma_50_d_string, text_color = whiteColor, bgcolor = color_sma_50_d)
table.cell(technical_table, row = 2, column = 2, text = sma_50_w_string, text_color = whiteColor, bgcolor = color_sma_50_w)
table.cell(technical_table, row = 2, column = 3, text = sma_50_m_string, text_color = whiteColor, bgcolor = color_sma_50_m)
 
// Simple Moving Average: 100MA
table.cell(technical_table, row = 3, column = 0, text = "100MA (%)", text_color = whiteColor)
table.cell(technical_table, row = 3, column = 1, text = sma_100_d_string, text_color = whiteColor, bgcolor = color_sma_100_d)
table.cell(technical_table, row = 3, column = 2, text = sma_100_w_string, text_color = whiteColor, bgcolor = color_sma_100_w)
table.cell(technical_table, row = 3, column = 3, text = sma_100_m_string, text_color = whiteColor, bgcolor = color_sma_100_m)
 
// Simple Moving Average: 200MA
table.cell(technical_table, row = 4, column = 0, text = "200MA (%)", text_color = whiteColor)
table.cell(technical_table, row = 4, column = 1, text = sma_200_d_string, text_color = whiteColor, bgcolor = color_sma_200_d)
table.cell(technical_table, row = 4, column = 2, text = sma_200_w_string, text_color = whiteColor, bgcolor = color_sma_200_w)
table.cell(technical_table, row = 4, column = 3, text = sma_200_m_string, text_color = whiteColor, bgcolor = color_sma_200_m)
 

//  ************************************************************ Technical Oscillator  ************************************************************************************************************************
// ............................................... Technical  Oscillator..............................................................................................
technical_table_oscillator = table.new(position = position.bottom_right, rows = 4, columns = 9, bgcolor = color.new(color.black,10), border_width=2)
table.cell(technical_table_oscillator, row = 0, column = 0, text = "", text_color = yellowColor)
table.cell(technical_table_oscillator, row = 1, column = 0, text = "D", text_color = whiteColor)
table.cell(technical_table_oscillator, row = 2, column = 0, text = "W", text_color = whiteColor)
table.cell(technical_table_oscillator, row = 3, column = 0, text = "M", text_color = whiteColor)

// Stochastic RSI
table.cell(technical_table_oscillator, row = 0, column = 1, text = "Stochastic-RSI", text_color = whiteColor)
table.cell(technical_table_oscillator, row = 1, column = 1, text = fourteen_D_stoch_RSI_k_string + " | "+fourteen_D_stoch_RSI_d_string, text_color = whiteColor, bgcolor=fourteen_stoch_RSI_D_color)
table.cell(technical_table_oscillator, row = 2, column = 1, text = fourteen_W_stoch_RSI_k_string + " | "+fourteen_W_stoch_RSI_d_string, text_color = whiteColor, bgcolor=fourteen_stoch_RSI_W_color)
table.cell(technical_table_oscillator, row = 3, column = 1, text = fourteen_M_stoch_RSI_k_string + " | "+fourteen_M_stoch_RSI_d_string, text_color = whiteColor, bgcolor=fourteen_stoch_RSI_M_color)

// RSI
table.cell(technical_table_oscillator, row = 0, column = 2, text = "RSI", text_color = whiteColor)
table.cell(technical_table_oscillator, row = 1, column = 2, text = fourteen_D_RSI_string + " | "+fourteen_D_RSI_ma_string, text_color = whiteColor, bgcolor=fourteen_D_RSI_color)
table.cell(technical_table_oscillator, row = 2, column = 2, text = fourteen_W_RSI_string + " | "+fourteen_W_RSI_ma_string, text_color = whiteColor, bgcolor=fourteen_W_RSI_color)
table.cell(technical_table_oscillator, row = 3, column = 2, text = fourteen_M_RSI_string + " | "+fourteen_M_RSI_ma_string, text_color = whiteColor, bgcolor=fourteen_M_RSI_color)

// MACD
table.cell(technical_table_oscillator, row = 0, column = 3, text = "MACD", text_color = whiteColor)
table.cell(technical_table_oscillator, row = 1, column = 3, text = macdLine_d_string+" | "+ signalLine_d_string, text_color = whiteColor, bgcolor = macdLine_d_color)
table.cell(technical_table_oscillator, row = 2, column = 3, text = macdLine_w_string+" | "+ signalLine_w_string, text_color = whiteColor, bgcolor = macdLine_w_color)
table.cell(technical_table_oscillator, row = 3, column = 3, text = macdLine_m_string+" | "+ signalLine_m_string, text_color = whiteColor, bgcolor = macdLine_m_color)



