本页源码来自 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/
// © julzALGO
//@version=6
indicator('Price and Volume Swing', shorttitle = 'PAVS', overlay = true, max_lines_count = 500, max_labels_count = 500)
// Momentum settings
momentumPeriod = input.int(14, 'Momentum Period', minval = 1)
upperSwingLevel = input.int(70, "Upper Swing Level", minval = 51)
lowerSwingLevel = input.int(30, "Lower Swing Level", maxval = 49)
// Normalize cumulative volume-weighted price returns into a 0-100 momentum score.
priceVolumeTotal = ta.cum(ta.change(close) / close[1] * nz(volume))
momentumScore = ta.rsi(priceVolumeTotal, momentumPeriod)
scoreText = str.tostring(momentumScore, "#.##")
// Classify the current momentum band.
inUpperBand = momentumScore >= upperSwingLevel
inLowerBand = momentumScore <= lowerSwingLevel
// Track the developing swing.
var int swingPhase = na
var float upperExtreme = na
var float lowerExtreme = na
var label upperMarker = na
var label lowerMarker = na
var line upperSegment = na
var line lowerSegment = na
markerSpacing = ta.atr(5) * 0.5
// Seed the phase at the first threshold event.
if na(swingPhase)
swingPhase := inUpperBand ? 1 : inLowerBand ? 2 : na
// Alternate between upper and lower threshold events.
startsUpperSwing = swingPhase == 2 and inUpperBand
startsLowerSwing = swingPhase == 1 and inLowerBand
// Create the upper endpoint.
if startsUpperSwing
upperExtreme := high
upperMarker := label.new(bar_index, high + markerSpacing, scoreText,
style=label.style_circle,
size=size.tiny,
color=color.rgb(255,82,82,33))
if not na(lowerMarker)
upperSegment := line.new(
x1 = bar_index,
y1 = high,
x2 = label.get_x(lowerMarker),
y2 = label.get_y(lowerMarker),
color = color.red,
width = 1)
swingPhase := 1
// Create the lower endpoint.
if startsLowerSwing
lowerExtreme := low
lowerMarker := label.new(bar_index, low - markerSpacing, scoreText,
style=label.style_circle,
size=size.tiny,
color=color.rgb(33,149,243,27))
if not na(upperMarker)
lowerSegment := line.new(
x1 = bar_index,
y1 = low,
x2 = label.get_x(upperMarker),
y2 = label.get_y(upperMarker),
color = color.green,
width = 1)
swingPhase := 2
// Move active endpoints as qualifying price extremes develop.
// Upper endpoint update
if inUpperBand and not na(upperMarker)
if na(upperExtreme) or high >= upperExtreme
upperExtreme := high
label.set_xy(upperMarker, bar_index, high)
label.set_text(upperMarker, scoreText)
if not na(upperSegment)
line.set_xy1(upperSegment, bar_index, high)
// Lower endpoint update
if inLowerBand and not na(lowerMarker)
if na(lowerExtreme) or low <= lowerExtreme
lowerExtreme := low
label.set_xy(lowerMarker, bar_index, low)
label.set_text(lowerMarker, scoreText)
if not na(lowerSegment)
line.set_xy1(lowerSegment, bar_index, low)
// Highlight stretched momentum readings.
// Fixed stretch boundaries
upperStretch = momentumScore >= 90
lowerStretch = momentumScore <= 10
// Compact chart markers
plotshape(upperStretch, style=shape.triangledown, location=location.abovebar, size=size.tiny, color=color.red)
plotshape(lowerStretch, style=shape.triangleup, location=location.belowbar, size=size.tiny, color=color.blue)
// PAVS dashboard
showDashboard = input.bool(true, "Show PAVS Dashboard")
tf1 ="1S", tf2 ="5S", tf3 ="10S", tf4 ="15S", tf5 ="30S", tf6 ="45S"
tf7 ="1", tf8 ="5", tf9 ="15", tf10="30", tf11="45"
tf12="60", tf13="120", tf14="180", tf15="240", tf16="D"
// Recalculate momentum in the requested timeframe context.
timeframeVolumeTotal = ta.cum(ta.change(close)/close[1]*nz(volume))
timeframeScore = ta.rsi(timeframeVolumeTotal, momentumPeriod)
// Identify the most recent threshold side.
latestSwingPhase() => ta.valuewhen(timeframeScore <= lowerSwingLevel, bar_index, 0) > ta.valuewhen(timeframeScore >= upperSwingLevel, bar_index, 0) ? 1 : -1
// Dashboard categories use fixed score boundaries.
bandBackground(value)=> value>=80?color.rgb(153, 27, 27):value>=70?color.rgb(185, 50, 40):value<=20?color.rgb(49, 46, 129):value<=30?color.rgb(7, 89, 133):color.rgb(55, 65, 81)
// Format period names.
timeframeCaption(timeframeCode) => str.contains(timeframeCode, "S") ? str.replace(timeframeCode, "S", "s") : timeframeCode=="60" ? "1h" : timeframeCode=="120" ? "2h" : timeframeCode=="180" ? "3h" : timeframeCode=="240" ? "4h" : timeframeCode=="D" ? "1D" : timeframeCode+"m"
// Assemble the score and phase indicator.
scoreCaption(value,phase)=> str.tostring(value,"#.1")+" "+(phase==1?"↑":"↓")
// Dashboard layout
var table dashboard = table.new(position.middle_right, 2, 17, frame_color=color.rgb(100, 116, 139), frame_width=1, border_color=color.rgb(71, 85, 105), border_width=1)
// Supported periods
timeframes = array.from(tf1,tf2,tf3,tf4,tf5,tf6,tf7,tf8,tf9,tf10,tf11,tf12,tf13,tf14,tf15,tf16)
// Per-period results
var float[] momentumValues = array.new_float()
var int[] phaseValues = array.new_int()
array.clear(momentumValues)
array.clear(phaseValues)
// Evaluate every period on each chart update.
for i = 0 to array.size(timeframes) - 1
requestedTimeframe = array.get(timeframes, i)
// Requested momentum score
score = request.security(
syminfo.tickerid,
requestedTimeframe,
timeframeScore,
barmerge.gaps_off, barmerge.lookahead_off)
// Requested swing phase
phase = request.security(
syminfo.tickerid,
requestedTimeframe,
latestSwingPhase(),
barmerge.gaps_off, barmerge.lookahead_off)
array.push(momentumValues, score)
array.push(phaseValues, phase)
// Render the latest dashboard values.
if barstate.islast and showDashboard
table.cell(dashboard,0,0,"Period",bgcolor=color.rgb(24, 24, 27),text_color=color.white)
table.cell(dashboard,1,0,"Momentum / Swing",bgcolor=color.rgb(24, 24, 27),text_color=color.white)
for i = 0 to array.size(timeframes) - 1
requestedTimeframe = array.get(timeframes, i)
score = array.get(momentumValues, i)
phase = array.get(phaseValues, i)
table.cell(dashboard,0,i+1,timeframeCaption(requestedTimeframe), bgcolor=color.rgb(24, 24, 27), text_color=color.white)
table.cell(dashboard,1,i+1,scoreCaption(score,phase), bgcolor=bandBackground(score), text_color=color.white)