PHP Code:
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
//
//@version=6
indicator('Trend System', shorttitle = '.', overlay = true)
///////
// Input
fastlen1 = input(2, title = 'Fast Moving Average')
slowlen1 = input(3, title = 'Slow Moving Average')
signallen1 = input(3, title = 'Signal Line')
switch21 = input(true, title = 'Enable Moving Averages?')
// Calculation
fast1 = ta.ema(close, fastlen1)
slow1 = ta.ema(close, slowlen1)
MACD1= fast1 - slow1
signal1 = ta.ema(MACD1, signallen1)
histogr1 = MACD1 - signal1
// MACD, MA colors
MACDcolor1 = fast1 > slow1 ? color.lime : color.red
fastcolor1 = ta.change(fast1) > 0 ? color.lime : color.red
slowcolor1 = ta.change(slow1) > 0 ? color.lime: color.red
MACDupdowncolor1 = ta.change(MACD1) > 0 ? color.lime : color.red
// MACD histogram colors
histogrMACDcolor1 = MACD1 > histogr1 ? color.lime : color.red
histogrzerocolor1 = histogr1 > 0 ? color.lime : color.red
histogrupdowncolor1 = ta.change(histogr1) > 0 ? color.lime : color.red
// MACD signal line colors
signalMACDcolor1 = MACD1 > signal1 ? color.lime : color.red
signalzerocolor1 = signal1 > 0 ? color.lime : color.red
signalupdowncolor1 = ta.change(signal1) > 0 ? color.lime : color.red
S1 = plot(switch21 ? slow1 : na, color = MACDcolor1, linewidth = 1,title="MACD-Trend")
///////////////////////
///////////////////
// @variable Length for LOWESS calculation
int length = input.int(1, minval = 1, title = 'Length', group = 'LOWESS (Locally Weighted Scatterplot Smoothing)')
// @variable Number of bars to the left for pivot calculation
int leftBars = input.int(5, 'Length', group = 'Pivots')
// @variable Number of bars to the right for pivot calculation
int rightBars = leftBars - 4
// @variable Line object for high pivot
var line line_h = na
// @variable Line object for low pivot
var line line_l = na
// @variable Color for upward movements
color col_up = color.yellow
// @variable Color for downward movements
color col_dn = color.red
// Calculate pivot high and low
ph = ta.pivothigh(leftBars, rightBars)
pl = ta.pivotlow(leftBars, rightBars)
//@function Calculates LOWESS (Locally Weighted Scatterplot Smoothing)
//@param src (float) Source series
//@param length (int) Lookback period
//@returns (float) LOWESS value
lowess(src, length) =>
sum_w = 0.0
sum_wx = 0.0
sum_wy = 0.0
for i = 0 to length - 1 by 1
w = math.pow(1 - math.pow(i / length, 3), 3)
sum_w := sum_w + w
sum_wx := sum_wx + w * i
sum_wy := sum_wy + w * src[i]
sum_wy
a = sum_wy / sum_w
b = sum_wx / sum_w
a + b / (length - 1) / 2000
//@function Calculates Modified Adaptive Gaussian Moving Average
//@param src (float) Source series
//@param length (int) Lookback period
//@returns [float, float] Gaussian MA and smoothed Gaussian MA
GaussianMA(src, length) =>
h_l = array.new<float>(length)
float gma = 0.0
float sumOfWeights = 0.0
float sigma = (ta.atr(length) + ta.stdev(close, length)) / 2 // Volatility adaption
float highest = 0.0
float lowest = 0.0
float smoothed = 0.0
for i = 0 to length - 1 by 1
h_l.push(close[i])
highest := h_l.max()
lowest := h_l.min()
weight = math.exp(-math.pow((i - (length - 1)) / (2 * sigma), 2) / 2)
value = math.max(highest[i], highest) + math.min(lowest[i], lowest)
gma := gma + value * weight
sumOfWeights := sumOfWeights + weight
sumOfWeights
gma := gma / sumOfWeights / 2
smoothed := lowess(gma, 10)
[gma, smoothed]
[gma, smoothed] = GaussianMA(close, length)
smoothedColor = smoothed > smoothed[2] ? col_up : smoothed <= smoothed[2] ? col_dn : na
plot(smoothed, 'Pivot trend', smoothedColor)
/////////////////////////////