本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
// This Pine Script® code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © SMRTAlgo
//@version=6
indicator('Historical Impulse Zones [SMRT Algo]', overlay=true, max_bars_back=500, max_boxes_count=500, max_lines_count=500, max_labels_count=100)
groupMain = 'Main Settings'
src = input.source(close, 'Source', display=display.none, group=groupMain)
flatLimit = input.float(1.5, 'Gray Zone Threshold', minval=0, step=0.01, group=groupMain)
atrLength = input.int(14, 'ATR Length', minval=1, group=groupMain)
impulseATR = input.float(0.30, 'Impulse ATR Multiplier', minval=0.05, step=0.05, group=groupMain)
refLookback = input.int(20, 'Reference Lookback', minval=3, group=groupMain)
smoothLength = input.int(3, 'Sentiment Smoothing', minval=1, group=groupMain)
clrBull = input.color(#0df1c6, 'Colors ', inline='1', group=groupMain)
clrBear = input.color(#e91e63, '', inline='1', group=groupMain)
clrGray = input.color(color.gray, '', inline='1', group=groupMain)
groupDashboard = 'DASHBOARD SETTINGS'
showDash = input(true, 'Dashboard', group=groupDashboard)
dashPos = input.string('Top Right', 'Location', options=['Top Right', 'Bottom Right', 'Bottom Left'], group=groupDashboard, display=display.none)
textSize = input.string('Small', 'Size', options=['Tiny', 'Small', 'Normal'], group=groupDashboard, display=display.none)
// variables ----------------------------------------------
var listUp = array.new_float()
var listDown = array.new_float()
// calculations --------------------------------------------
atr = ta.atr(atrLength)
atrSafe = math.max(nz(atr), syminfo.mintick)
fallbackVol = ta.sma(high - low, atrLength)
volBase = math.max(nz(atr, fallbackVol), syminfo.mintick)
upImpulse = low > high[2] and close[1] > high[2] and low - high[2] > volBase * impulseATR
downImpulse = high < low[2] and close[1] < low[2] and low[2] - high > volBase * impulseATR
if upImpulse
array.unshift(listUp, low)
if array.size(listUp) > refLookback
array.pop(listUp)
if downImpulse
array.unshift(listDown, high)
if array.size(listDown) > refLookback
array.pop(listDown)
peakZone = ta.highest(high, refLookback)
floorZone = ta.lowest(low, refLookback)
upRef = array.size(listUp) > 0 ? array.avg(listUp) : na
downRef = array.size(listDown) > 0 ? array.avg(listDown) : na
activeUpRef = na(upRef) ? floorZone : upRef
activeDownRef = na(downRef) ? peakZone : downRef
bullRaw = activeUpRef != 0 ? (peakZone - activeUpRef) / activeUpRef * 100 : 0
bearRaw = activeDownRef != 0 ? (activeDownRef - floorZone) / activeDownRef * 100 : 0
bullForceRaw = math.min(math.max(bullRaw, 0), 100)
bearForceRaw = math.min(math.max(bearRaw, 0), 100)
bullATR = math.max((src - activeUpRef) / atrSafe, 0)
bearATR = math.max((activeDownRef - src) / atrSafe, 0)
bullATRForce = math.min(bullATR * 10, 100)
bearATRForce = math.min(bearATR * 10, 100)
bullForce = bullForceRaw * 0.55 + bullATRForce * 0.45
bearForce = bearForceRaw * 0.55 + bearATRForce * 0.45
rawSentiment = bullForce - bearForce
sentiment = ta.ema(rawSentiment, smoothLength)
flatState = math.abs(sentiment) < flatLimit
bullState = sentiment >= flatLimit
bearState = sentiment <= -flatLimit
trendColor = flatState ? clrGray : bullState ? clrBull : clrBear
trendStatus = flatState ? 'Stability Phase' : bullState ? 'Positive Flow' : 'Negative Flow'
// visuals --------------------------------------------
barcolor(trendColor, editable=false)
plotUpRef = plot(activeUpRef, color=color(na), editable=false)
plotDownRef = plot(activeDownRef, color=color(na), editable=false)
plotsrc = plot(src, color=color(na), editable=false)
middle = math.avg(open, close)
fill(plotsrc, plotUpRef, math.max(middle, upRef), math.min(ta.sma(upRef, 10), upRef), color.new(clrBull, 70), math.max(middle, upRef) <= upRef ? color(na) : color.new(chart.bg_color, 100), display = display.all)
fill(plotsrc, plotDownRef, math.max(ta.sma(downRef, 10), downRef), math.min(middle, downRef), math.min(middle, downRef) >= downRef ? color(na) : color.new(chart.bg_color, 100), color.new(clrBear, 70), display = display.all)
// table ----------------------------------------------------------------------
var table_position = dashPos == 'Bottom Left' ? position.bottom_left : dashPos == 'Top Right' ? position.top_right : position.bottom_right
var table_size = textSize == 'Tiny' ? size.tiny : textSize == 'Small' ? size.small : size.normal
var tabel = table.new(table_position, 5, 5, bgcolor=#1e222d, border_color=#373a46, border_width=1, frame_color=#373a46, frame_width=1)
tbSize = table_size
if showDash
if barstate.islast
tabel.cell(0, 0, 'Statistics', text_color=color.white, text_size=tbSize)
tabel.merge_cells(0, 0, 1, 0)
tabel.cell(0, 1, 'Sentiment', text_size=tbSize, text_color=#9399a2)
tabel.cell(1, 1, str.tostring(sentiment, '#.##'), text_size=tbSize, text_color=trendColor)
tabel.cell(0, 2, 'Status', text_size=tbSize, text_color=#9399a2)
tabel.cell(1, 2, trendStatus, text_size=tbSize, bgcolor=color.from_gradient(math.abs(sentiment), 0, 100, chart.bg_color, color.new(trendColor, 70)), text_color=trendColor)
tabel.cell(0, 3, 'Bull Power', text_size=tbSize, text_color=#9399a2)
tabel.cell(1, 3, str.tostring(bullForce, '#.##'), text_size=tbSize, text_color=clrBull)
tabel.cell(0, 4, 'Bear Power', text_size=tbSize, text_color=#9399a2)
tabel.cell(1, 4, str.tostring(bearForce, '#.##'), text_size=tbSize, text_color=clrBear)