本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © MWRIGHT, INC
//@version=5
indicator(title="ATR Bands (Keltner Channel), Wick and SRSI Signals [MW]", shorttitle="ATR Wicks SRSI [MW]", overlay=true, max_labels_count = 500)
// ==================
// Inputs
// Channel Settings
i_ema = input.int(21, title="Baseline EMA Period", group="Channel Settings")
i_atr = input.int(21, title="ATR Period", group="Channel Settings")
i_multiplier = input.float(2.618, title="Multiplier", step=0.1, group="Channel Settings")
i_smoothing = input.int(3, title="Band Smoothing", step=1, group="Channel Settings")
// Additional Channels
i_halfOffset = input.bool(true, title="Half of Multiplier Offset", group="Additional Channels")
i_qtrOffset = input.bool(false, title="Quarter of Multiplier Offset", group="Additional Channels")
i_phiOffset = input.bool(false, title="Phi (Φ) Offset", group="Additional Channels")
// Wick Settings for Candle Filters
i_wickRatio = input.float(0.4, title="Wick Ratio for Bands", maxval=1, minval=0, step=0.01, group="Wick Settings for Candle Filters")
i_wickRatioMid = input.float(0.4, title="Wick Ratio for Baseline", minval=0, step=0.1, group="Wick Settings for Candle Filters")
i_useCandleBody = input.bool(false, title="Use Candle Body (rather than full candle Size)", group="Wick Settings for Candle Filters")
// Visual Preferences - Signals
i_showLabels = input.bool(true, title="Show Signals", group="Visual Preferences - Signals")
i_showHalfOffsetSignal = input.bool(false, title="Show Signals from 1/2 Band Offset", group="Visual Preferences - Signals")
i_showPhiOffsetSignal = input.bool(false, title="Show Signals From Phi Band Offset", group="Visual Preferences - Signals")
i_showBaselineSignal = input.bool(false, title="Show Baseline Signals", group="Visual Preferences - Signals")
// Visual Preferences - Bands
i_showBands = input.bool(true, title="Show ATR (Keltner) Bands", group="Visual Preferences - Bands")
i_fill = input.bool(true, title="Fill Bands", group="Visual Preferences - Bands")
// SRSI Settings
i_useSRSIFilter = input.bool(false, title="Use Stochastic RSI Filtering", group="Stochastic RSI Settings")
smoothK = input.int(3, "K", minval=1, group="Stochastic RSI Settings")
lengthRSI = input.int(14, "RSI Length", minval=1, group="Stochastic RSI Settings")
lengthStoch = input.int(8, "Stochastic Length", minval=1, group="Stochastic RSI Settings")
// ==================
// Calculations
// Calculate EMA and ATR
ema0 = ta.ema(close, i_ema)
ema = ta.ema(ema0,3)
atr = ta.atr(i_atr)
// Calculate Stochastic RSI values on the primary timeline
_rsi1 = ta.rsi(close, lengthRSI)
_k1 = ta.sma(ta.stoch(_rsi1, _rsi1, _rsi1, lengthStoch), smoothK)
// Calculate Keltner Channel
upperBand0 = ema + atr * i_multiplier
upperBand = ta.sma(upperBand0,i_smoothing)
lowerBand0 = ema - atr * i_multiplier
lowerBand = ta.sma(lowerBand0,i_smoothing)
upperHalfBand0 = ema + atr * i_multiplier / 2
upperHalfBand = ta.sma(upperHalfBand0,i_smoothing)
lowerHalfBand0 = ema - atr * i_multiplier / 2
lowerHalfBand = ema - atr * i_multiplier / 2
phi = 0.618
uppePhiBand0 = ema + atr * i_multiplier * phi
upperPhiBand = ta.sma(upperHalfBand0,i_smoothing)
lowerPhiBand0 = ema - atr * i_multiplier * phi
lowerPhiBand = ema - atr * i_multiplier * phi
// Calculate Signal
srsiFilterBear = _k1 > 80
srsiFilterBull = _k1 < 20
var bool closeBelowWickAbove = false
var bool closeAboveWickBelow = false
var bool closeBelowWickAbove_half = false
var bool closeAboveWickBelow_half = false
var bool closeBelowWickAbove_phi = false
var bool closeAboveWickBelow_phi = false
var bool closeBelowWickAbove_baseline = false
var bool closeAboveWickBelow_baseline = false
if i_useSRSIFilter
closeBelowWickAbove := close < upperBand and high > upperBand and srsiFilterBear
closeAboveWickBelow := close > lowerBand and low < lowerBand and srsiFilterBull
closeBelowWickAbove_half := close < upperHalfBand and high > upperHalfBand and srsiFilterBear
closeAboveWickBelow_half := close > lowerHalfBand and low < lowerHalfBand and srsiFilterBull
closeBelowWickAbove_phi := close < upperPhiBand and high > upperPhiBand and srsiFilterBear
closeAboveWickBelow_phi := close > lowerPhiBand and low < lowerPhiBand and srsiFilterBull
closeBelowWickAbove_baseline := close < ema and high > ema and srsiFilterBear
closeAboveWickBelow_baseline := close > ema and low < ema and srsiFilterBull
else
closeBelowWickAbove := close < upperBand and high > upperBand
closeAboveWickBelow := close > lowerBand and low < lowerBand
closeBelowWickAbove_half := close < upperHalfBand and high > upperHalfBand
closeAboveWickBelow_half := close > lowerHalfBand and low < lowerHalfBand
closeBelowWickAbove_phi := close < upperPhiBand and high > upperPhiBand
closeAboveWickBelow_phi := close > lowerPhiBand and low < lowerPhiBand
closeBelowWickAbove_baseline := close < ema and high > ema
closeAboveWickBelow_baseline := close > ema and low < ema
bodySize = math.abs(open-close)
candleSize = high-low
size = i_useCandleBody ? bodySize : candleSize
upperWickSize = high - math.max(open,close)
upperWickRatio = upperWickSize/size
lowerWickSize = math.min(open,close) - low
lowerWickRatio = lowerWickSize/size
bigUpperWick = upperWickRatio > i_wickRatio ? true : false
bigLowerWick = lowerWickRatio > i_wickRatio ? true : false
bigUpperWickMid = upperWickRatio > i_wickRatioMid ? true : false
bigLowerWickMid = lowerWickRatio > i_wickRatioMid ? true : false
wickedAboveBasis = bigUpperWickMid and closeBelowWickAbove_baseline and ta.falling(ema,14)
wickedBelowBasis = bigLowerWickMid and closeAboveWickBelow_baseline and ta.rising(ema,14)
// ==================
// DRAWRINGS
// Colors
green = color.new(#33cc33,0)
red = color.new(#cc3333,0)
green_half = color.new(#339933,0)
red_half = color.new(#993333,0)
green_phi = color.new(#336633,0)
red_phi = color.new(#663333,0)
// Keltner Bands - ATR Multiplier Signals
if closeBelowWickAbove and bigUpperWick
label.new(i_showLabels ? bar_index : na, high, text="S", style=label.style_label_down, color=red, textcolor=color.white, yloc=yloc.abovebar)
plotshape(closeBelowWickAbove and bigUpperWick, title="Trend Weakness - Upper Wick", location=location.abovebar, color=red, style=shape.triangledown, size=size.tiny)
if closeAboveWickBelow and bigLowerWick
label.new(i_showLabels ? bar_index : na, low, "B", style=label.style_label_up, color=green, textcolor=color.white, yloc=yloc.belowbar)
plotshape(closeAboveWickBelow and bigLowerWick, title="Trend Weakness - Lower Wick", location=location.belowbar, color=green, style=shape.triangleup, size=size.tiny)
// Kelter Bands - Half Multiplier Signals
if closeBelowWickAbove_half and bigUpperWick
label.new(i_showLabels and i_showHalfOffsetSignal ? bar_index : na, high, text="S1", style=label.style_label_down, color=red_half, textcolor=color.white, yloc=yloc.abovebar, size=size.small)
plotshape(closeBelowWickAbove_half and i_showHalfOffsetSignal and bigUpperWick, title="Trend Weakness - Upper Wick - Half Offset", location=location.abovebar, color=red_half, style=shape.triangledown, size=size.tiny)
if closeAboveWickBelow_half and bigLowerWick
label.new(i_showLabels and i_showHalfOffsetSignal ? bar_index : na, low, "B1", style=label.style_label_up, color=green_half, textcolor=color.white, yloc=yloc.belowbar, size=size.small)
plotshape(closeAboveWickBelow_half and i_showHalfOffsetSignal and bigLowerWick, title="Trend Weakness - Lower Wick - Half Offset", location=location.belowbar, color=green_half, style=shape.triangleup, size=size.tiny)
// Keltner Bands - Phi Multiplier Signals
if closeBelowWickAbove_phi and bigUpperWick
label.new(i_showLabels and i_showPhiOffsetSignal ? bar_index : na, high, text="S2", style=label.style_label_down, color=red_phi, textcolor=color.white, yloc=yloc.abovebar, size=size.small)
plotshape(closeBelowWickAbove_phi and i_showPhiOffsetSignal and bigUpperWick, title="Trend Weakness - Upper Wick - Half Offset", location=location.abovebar, color=red_phi, style=shape.triangledown, size=size.tiny)
if closeAboveWickBelow_phi and bigLowerWick
label.new(i_showLabels and i_showPhiOffsetSignal ? bar_index : na, low, "B2", style=label.style_label_up, color=green_phi, textcolor=color.white, yloc=yloc.belowbar, size=size.small)
plotshape(closeAboveWickBelow_phi and i_showPhiOffsetSignal and bigLowerWick, title="Trend Weakness - Lower Wick - Half Offset", location=location.belowbar, color=green_phi, style=shape.triangleup, size=size.tiny)
// Baseline Signals
if wickedAboveBasis
label.new(i_showLabels and i_showBaselineSignal ? bar_index : na, high, "S3", style=label.style_label_down, color=color.new(#663333,0), textcolor=color.white, yloc=yloc.abovebar, size=size.tiny)
if wickedBelowBasis
label.new(i_showLabels and i_showBaselineSignal ? bar_index : na, low, "B3", style=label.style_label_up, color=color.new(#336633,0), textcolor=color.white, yloc=yloc.belowbar, size=size.tiny)
plot(ema, color=color.new(color.yellow, 20))
uBand = plot(i_showBands ? upperBand : na, color=color.new(color.silver, 40))
lBand = plot(i_showBands ? lowerBand : na, color=color.new(color.silver, 40))
// Additional bands
uHalfBand = plot(i_halfOffset and i_showBands ? ema + atr * i_multiplier / 2 : na, color=color.new(color.silver, 60))
lHalfBand = plot(i_halfOffset and i_showBands ? ema - atr * i_multiplier / 2 : na, color=color.new(color.silver, 60))
uQtrBand = plot(i_qtrOffset and i_showBands ? ema + atr * i_multiplier / 4 : na, color=color.new(color.silver, 80))
lQtrBand = plot(i_qtrOffset and i_showBands ? ema - atr * i_multiplier / 4 : na, color=color.new(color.silver, 80))
uPhiBand = plot(i_phiOffset and i_showBands ? ema + atr * i_multiplier * 0.618 : na, color=color.new(color.orange, 60))
lPhiBand = plot(i_phiOffset and i_showBands ? ema - atr * i_multiplier * 0.618 : na, color=color.new(color.orange, 60))
// Fill bands
fillColor = i_fill and i_showBands ? color.new(color.blue, 90) : na
fill(uBand, lBand, color=fillColor)
fill(uHalfBand, lHalfBand, color=fillColor)
fill(uQtrBand, lQtrBand, color=fillColor)
fill(uPhiBand, lPhiBand, color=fillColor)