0% found this document useful (0 votes)
26 views

Trend Filter

This document describes a technical analysis indicator that displays multiple moving averages and bands. It takes various user inputs for time periods and filtering options and outputs background colors and plots based on price relative to the moving averages and bands.

Uploaded by

aa bb
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

Trend Filter

This document describes a technical analysis indicator that displays multiple moving averages and bands. It takes various user inputs for time periods and filtering options and outputs background colors and plots based on price relative to the moving averages and bands.

Uploaded by

aa bb
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

// @version=5

indicator(title="[AA][Indicator]Trend Filter2 ", shorttitle="Trend Filter2",


overlay=true)

// Get user input


fastPeriod = input.int(5, minval=1, title="Fast Length")
mediumPeriod = input.int(15, minval=1, title="Medium Length")
longPeriod = input.int(60, minval=1, title="Slow Length")
showFast = input.bool(true, title="Show Fast backgound")
showFastTrend = input.bool(true, title="Show Fast Trend")
showMedium = input.bool(true, title="Show Medium background")
showMediumTrend = input.bool(true, title="Show MediumTrend")
showLong = input.bool(true, title="Show Long background")
showLongTrend = input.bool(true, title="Show Long Trend")

showBB =input.bool(true, title="Show BB")


h = ta.highest(fastPeriod)
l = ta.lowest(fastPeriod)
p = (h+l+close)/3

BC = (h + l) / 2
TC = (p - BC) + p
// medium period
mh = ta.highest(mediumPeriod)
ml = ta.lowest(mediumPeriod)
mp = (mh+ml+close)/3

mBC = (mh + ml) / 2


mTC = (mp - mBC) + mp

// long period
lh = ta.highest(longPeriod)
ll = ta.lowest(longPeriod)
lp = (h+l+close)/3

lBC = (lh + ll) / 2


lTC = (lp - lBC) + lp

src = close
len = input.int(21, minval=1, title='Long Length')
flen = input.int(3, minval=1, title='Fast Length')
//One bar has 5 period:
//Only Open--Close has changed volume
xobv = ta.cum(math.sign(ta.change(src)) * volume * math.abs(close - open) / (2 *
(high - low) - math.abs(close - open)))

xobvf = ta.ema(xobv, flen)


xobvl = ta.sma(xobv, len)

length = input.int(20, minval=1)


srcbb = input(close, title="Source")
mult = input.float(1.0, minval=0.001, maxval=50, title="StdDev")
basis = ta.sma(srcbb, length)
dev = mult * ta.stdev(srcbb, length)
upper = basis + dev
lower = basis - dev
offset = input.int(0, "Offset", minval = -500, maxval = 500)
plot(showBB?basis:na, "Basis", color=#000000, offset = offset)
p1 = plot(showBB?upper:na, "Upper", color=#000000, offset = offset)
p2 = plot(showBB?lower:na, "Lower", color=#000000, offset = offset)

fill( p1, p2, title = "Background", color=color.rgb(33, 150, 243, 95))

displacement = input.int(26, minval=1, title="Lagging Span")


donchian(len) => math.avg(ta.lowest(len), ta.highest(len))
conversionLine = donchian(fastPeriod)
baseLine = donchian(mediumPeriod)
leadLine1 = math.avg(conversionLine, baseLine)
leadLine2 = donchian(longPeriod)

// Draw cloud
// plot(conversionLine, color=#2962FF, title="Conversion Line",
display=display.none)
// plot(baseLine, color=#B71C1C, title="Base Line", display=display.none)

// p1 = plot(leadLine1, offset = displacement - 1, color=#A5D6A7, title="Leading


Span A", display=display.none)
// p2 = plot(leadLine2, offset = displacement - 1, color=#EF9A9A, title="Leading
Span B", display=display.none)
// fill(p1, p2, color = leadLine1 > leadLine2 ? color.rgb(67, 160, 71, 90) :
color.rgb(244, 67, 54, 90), display=display.none)

// // Track horizontal baseline


// var baseLineSaved = baseLine
// if baseLine != baseLine[1]
// baseLineSaved := baseLine
// else
// baseLineSaved := baseLineSaved

// Draw baseline
// plot(baseLineSaved, color=color.purple, style=plot.style_linebr, title="Base
Line Saved")

// Track fast horizontal baseline


var baseLineSaved = p
if p != p[1]
baseLineSaved := p
else
baseLineSaved := baseLineSaved

// Track horizontal baseline


var mbaseLineSaved = mp
if mp != mp[1]
mbaseLineSaved := mp
else
mbaseLineSaved := mbaseLineSaved

// Track horizontal baseline


var lbaseLineSaved = lp
if lp != lp[1]
lbaseLineSaved := lp
else
lbaseLineSaved := lbaseLineSaved

// // Draw fast line


plot(showFastTrend?baseLineSaved:na, color=color.black, style=plot.style_linebr,
title="pivot")
plot(showFastTrend?BC:na, color=color.green, style=plot.style_linebr, title="bc")
plot(showFastTrend?TC:na, color=color.red, style=plot.style_linebr, title="tc")

// // Draw mediumline
plot(showMediumTrend?mbaseLineSaved:na, color=color.black, style=plot.style_linebr,
title="mp")
plot(showMediumTrend?mBC:na, color=color.green, style=plot.style_linebr,
title="mbc")
plot(showMediumTrend?mTC:na, color=color.red, style=plot.style_linebr, title="mtc")

// // Draw longline
plot(showLongTrend?lbaseLineSaved:na, color=color.blue, style=plot.style_linebr,
title="lp")
plot(showLongTrend?lBC:na, color=color.green, style=plot.style_linebr, title="lbc")
plot(showLongTrend?lTC:na, color=color.red, style=plot.style_linebr, title="ltc")

// Track price trading above/below baseline


//isPriceAboveBaseLine = close > pbaseLineSaved and close >baseLineSaved
isPriceAboveBaseLine = lTC > lBC and close > lbaseLineSaved
priceTradingAboveBL = (isPriceAboveBaseLine and not na(lbaseLineSaved)) and
(close >upper or (close >close[2] and xobv >xobvf))
priceTradingBelowBL = (not isPriceAboveBaseLine and not na(lbaseLineSaved)) and
(close <upper or (close <close[2] and xobv < xobvf))

isPriceAboveFastLine = (TC > BC and close > baseLineSaved )


priceTradingAboveFastBL = isPriceAboveFastLine and not na(baseLineSaved) and
(close >upper or (close >close[2] and xobv >xobvf))
priceTradingBelowFastBL = not isPriceAboveFastLine and not na(baseLineSaved) and
(close <upper or (close <close[2] and xobv < xobvf))

isPriceAboveMedLine = mTC > mBC and close > mbaseLineSaved


priceTradingAboveMedBL = (isPriceAboveMedLine and not na(mbaseLineSaved)) and
(close >upper or (close >close[2] and xobv >xobvf))
priceTradingBelowMedBL = (not isPriceAboveMedLine and not na(mbaseLineSaved)) and
(close <upper or (close <close[2] and xobv < xobvf))

// Draw condition
bgcolor(showLong and priceTradingAboveBL ? color.new(color.green,80) : na)
bgcolor(showLong and priceTradingBelowBL ? color.new(color.red,80) : na)

bgcolor(showMedium and priceTradingAboveMedBL ? color.new(color.green,80) : na)


bgcolor(showMedium and priceTradingBelowMedBL ? color.new(color.red,80) : na)

bgcolor(showFast and priceTradingAboveFastBL ? color.new(color.green,80) : na)


bgcolor(showFast and priceTradingBelowFastBL ? color.new(color.red,80) : na)

// Track candle pattern tests of baseline (example purposes - needs more conditions
to be actually useful!)
candlePatternBull = priceTradingAboveBL and priceTradingAboveBL[1] and low[1] <
baseLineSaved and close > baseLineSaved
candlePatternBear = priceTradingBelowBL and priceTradingBelowBL[1] and high[1] >
baseLineSaved and close < baseLineSaved
plotshape(candlePatternBull, style=shape.triangleup, color=color.green,
location=location.belowbar)
plotshape(candlePatternBear, style=shape.triangledown, color=color.red)

You might also like