Moving Averages Trend Indicator
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Dr_Roboto
// Computes a metric (0.0-1.0) based on the relative (above/below) relationship of price (close) and the moving averages of 
// WMA0 (20day), SMA1 (50day), SMA2 (100day), SMA3  (200day).
// Metric = 1.0 when price > SMA0 > SMA1 > SMA2 > SMA3
// Metric = 0.0 when price < SMA0 < SMA1 < SMA2 < SMA3
// This metric helps you track the price "trend." Color of the metric helps you track if the price is a bullish (green) or bearish (red). Blue indicates neutral.
//
// Settings:
// Time Frame is adjustable
// SMA lengths are adjustable
// Threshold for bullish/bearish is adjustable


//@version=4
study("Moving Average Trend Indicator", overlay=false, resolution="")

//=================================================================================================================================
//                                          Moving Average (VWMA)
//                    This code matches the "Four SMA  Trends" Indicator and cRSI + Waves Strategy
//=================================================================================================================================
// --- Weight Moving Average for first trend ---
smaLen0 = input(defval=20, title="WMA #0 Length", type=input.integer, minval=1, maxval=200)
sma0 = wma(close, smaLen0)
sma0_high = wma(high, smaLen0)
sma0_low = wma(low, smaLen0)

// --- Regular SMA ---
price = close
smaLen1 = input(defval=50, title="SMA #1 Length", type=input.integer, minval=1, maxval=200)
sma1 = sma(price,smaLen1)
// plot(sma1, title='SMA #1', color = color.blue, linewidth=smaLineWidth)

smaLen2 = input(defval=100, title="SMA #2 Length", type=input.integer, minval=1, maxval=200) 
sma2 = sma(price,smaLen2)
// plot(sma2, title='SMA #2', color = color.maroon, linewidth=smaLineWidth)

smaLen3 = input(defval=200, title="SMA #2 Length", type=input.integer, minval=1, maxval=200) 
sma3 = sma(price,smaLen3)
// plot(sma3, title='SMA #3', color = color.black, linewidth=smaLineWidth)




//=================================================================================================================================
//                                          Trends
//=================================================================================================================================
// Lower weight for price and faster SMAs as the are a type of noise on the overall trend
maxTrendStrength = 0
weight = 1

// price
weight := 1
maxTrendStrength := maxTrendStrength + 4*weight
trendStrengthP = 0
if close > sma0
    trendStrengthP := trendStrengthP + weight
if close > sma1
    trendStrengthP := trendStrengthP + weight
if close > sma2
    trendStrengthP := trendStrengthP + weight
if close > sma3
    trendStrengthP := trendStrengthP + weight

// sma0
weight := 2
maxTrendStrength := maxTrendStrength + 3*weight
trendStrength0 = 0
if sma0 > sma1
    trendStrength0 := trendStrength0 + weight
if sma0 > sma2
    trendStrength0 := trendStrength0 + weight
if sma0 > sma3
    trendStrength0 := trendStrength0 + weight
    
// sma1
weight := 3
maxTrendStrength := maxTrendStrength + 2*weight
trendStrength1 = 0
if sma1 > sma2
    trendStrength1 := trendStrength1 + weight
if sma1 > sma3
    trendStrength1 := trendStrength1 + weight
    
// sma2
weight := 4
maxTrendStrength := maxTrendStrength + 1*weight
trendStrength2 = 0
if sma2 > sma3
    trendStrength2 := trendStrength2 + weight
    
// sum the individual trend strengths
trendStrength = trendStrengthP + trendStrength0 + trendStrength1 + trendStrength2

// normalize strength to 1.0
trendStrength := trendStrength/maxTrendStrength

// filter out noise using sma
maLen = 5
//trendStrengthSmoothS = sma(trendStrength,maLen) // most phase lag
//trendStrengthSmoothE = ema(trendStrength,maLen) // middle
trendStrengthSmoothW = wma(trendStrength,maLen) // least phase lag


//=================================================================================================================================
//                                          Plotting
//=================================================================================================================================
hline(1, title='zero', color=color.green, linestyle=hline.style_dashed, linewidth=1)
hline(0.5, title='zero', color=color.gray, linestyle=hline.style_dashed, linewidth=1)
hline(0, title='zero', color=color.red, linestyle=hline.style_dashed, linewidth=1)

// Trend Color
bear = input(defval=0.35, title="Bear Threshold", type=input.float, minval=0, maxval=1)
bull = input(defval=0.65, title="Bull Threshold", type=input.float, minval=0, maxval=1)

trendColor = color.blue
if trendStrengthSmoothW < bear
    trendColor := color.red
else if trendStrengthSmoothW > bull 
    trendColor := color.green

// plot(trendStrengthP, title='trendStrength', color = color.red, linewidth=1)
// plot(trendStrength0, title='trendStrength', color = color.maroon, linewidth=1)
// plot(trendStrength1, title='trendStrength', color = color.orange, linewidth=1)
// plot(trendStrength2, title='trendStrength', color = color.yellow, linewidth=1)
//plot(trendStrength, title='trendStrength', color = color.blue, linewidth=3)
// plot(trendStrength, title='trendStrength', color = trendColor, linewidth=3)

// plot(trendStrengthSmoothS, title='trendStrengthSmooth', color = color.red, linewidth=2)
// plot(trendStrengthSmoothE, title='trendStrengthSmooth', color = color.green, linewidth=2)
plot(trendStrengthSmoothW, title='trendStrengthSmooth', color = trendColor, linewidth=2)
