VolaSync V2.5 | Clean

laevangelistabr · study · 364 行 · 点赞 47 · TradingView 原页

本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。

Pine Script

//@version=6
indicator("VolaSync V2.5 | Clean", shorttitle="VolaSync V2.5", overlay=true, max_boxes_count=300, max_lines_count=100, max_labels_count=300)
 
plot(close, title="VolaSync Output Anchor", display=display.none)
 
//==============================================================================
// VOLASYNC V2.5 — CLEAN
// - Standard/real OHLC for visible price and session ranges.
// - Internal Heikin Ashi for the VolaSync confirmation engine by default.
// - Persistent bullish/bearish regime using RSI + MFI + EMA direction.
// - Single clean ATR/Trend line: no envelope, no liquidity zones.
// - Historical session ranges: Asia, London, New York AM and New York PM.
// - NO automatic liquidity sweep, BOS/MSS or qualified-setup interpretation.
// - The trader identifies liquidity/structure manually; VolaSync confirms regime.
//==============================================================================
 
//------------------------------------------------------------------------------
// 01. VOLASYNC ENGINE
//------------------------------------------------------------------------------
groupEngine = "01. VolaSync Engine"
momentumLengthInput = input.int(14, "Momentum Length", minval=2, group=groupEngine)
bullThresholdInput = input.float(66.0, "Bull Threshold", minval=50.0, maxval=100.0, step=0.1, group=groupEngine)
bearThresholdInput = input.float(30.0, "Bear Threshold", minval=0.0, maxval=50.0, step=0.1, group=groupEngine)
emaLengthInput = input.int(5, "EMA Length", minval=1, group=groupEngine)
 
//------------------------------------------------------------------------------
// 02. VISUAL
//------------------------------------------------------------------------------
groupVisual = "02. Visual"
colorCandlesInput = input.bool(false, "Color Candles by VolaSync Regime", group=groupVisual)
showSignalsInput = input.bool(true, "Show Regime Change Markers", group=groupVisual)
signalMarkerSizeInput = input.string("Small", "Signal Marker Size", options=["Tiny", "Small", "Normal"], group=groupVisual)
signalOffsetMultiplierInput = input.float(0.35, "Signal Offset Multiplier", minval=0.1, step=0.05, group=groupVisual)
bullColorInput = input.color(color.rgb(0, 184, 148), "Bull Color", group=groupVisual)
bearColorInput = input.color(color.rgb(225, 70, 70), "Bear Color", group=groupVisual)
neutralColorInput = input.color(color.rgb(128, 135, 145), "Neutral Color", group=groupVisual)
 
//------------------------------------------------------------------------------
// 03. ATR / TREND LINE
//------------------------------------------------------------------------------
groupBand = "03. ATR / Trend Line"
showTrendBandInput = input.bool(true, "Show ATR / Trend Line", group=groupBand)
bandMaLengthInput = input.int(20, "Band MA Length", minval=1, group=groupBand)
atrLengthInput = input.int(30, "ATR Length", minval=1, group=groupBand)
atrMultiplierInput = input.float(0.30, "ATR Multiplier", minval=0.01, step=0.01, group=groupBand)
maxPricePercentageInput = input.float(0.30, "Max Price Percentage (%)", minval=0.01, step=0.01, group=groupBand)
bandLookbackInput = input.int(20, "Band Lookback", minval=0, group=groupBand)
bandExpansionInput = input.float(4.0, "Band Expansion", minval=0.1, step=0.1, group=groupBand)
 
//------------------------------------------------------------------------------
// 04. SESSIONS
//------------------------------------------------------------------------------
groupSessions = "04. Sessions"
sessionTimezoneInput = input.string("America/New_York", "Session Timezone", group=groupSessions)
showSessionRangesInput = input.bool(true, "Show Session Ranges", group=groupSessions)
sessionHistoryDaysInput = input.int(15, "Historical Days of Sessions", minval=1, maxval=30, group=groupSessions)
asiaSessionInput = input.session("2000-0000", "Asia", group=groupSessions)
londonSessionInput = input.session("0200-0500", "London", group=groupSessions)
nyAmSessionInput = input.session("0930-1100", "New York AM", group=groupSessions)
nyPmSessionInput = input.session("1330-1600", "New York PM", group=groupSessions)
asiaColorInput = input.color(color.rgb(80, 160, 255), "Asia Color", group=groupSessions)
londonColorInput = input.color(color.rgb(0, 188, 212), "London Color", group=groupSessions)
nyAmColorInput = input.color(color.rgb(255, 152, 0), "NY AM Color", group=groupSessions)
nyPmColorInput = input.color(color.rgb(156, 39, 176), "NY PM Color", group=groupSessions)
 
//------------------------------------------------------------------------------
// 05. DEVELOPMENT
//------------------------------------------------------------------------------
groupDevelopment = "05. Development"
signalSourceInput = input.string("Internal Heikin Ashi", "Signal Engine Source", options=["Internal Heikin Ashi", "Standard OHLC", "Legacy Chart Source"], group=groupDevelopment)
debugModeInput = input.bool(false, "Debug Mode", group=groupDevelopment)
 
if barstate.isfirst and bearThresholdInput >= bullThresholdInput
    runtime.error("VolaSync input error: Bear Threshold must be lower than Bull Threshold.")
 
//------------------------------------------------------------------------------
// HELPERS
//------------------------------------------------------------------------------
f_boolToInt(bool value) =>
    value ? 1 : 0
 
f_rangeWeightedMa(float srcHigh, float srcLow, float srcClose, int length) =>
    float weightedSum = 0.0
    float weightSum = 0.0
    for index = 0 to length - 1
        float candleRange = srcHigh[index] - srcLow[index]
        float candleClose = srcClose[index]
        bool validSample = not na(candleRange) and not na(candleClose) and candleRange > 0.0
        if validSample
            weightedSum += candleClose * candleRange
            weightSum += candleRange
    weightSum > 0.0 ? weightedSum / weightSum : na
 
f_trimBoxHistory(array<box> boxes, int maxCount) =>
    while array.size(boxes) > maxCount
        box oldBox = array.shift(boxes)
        if not na(oldBox)
            box.delete(oldBox)
 
f_trimLabelHistory(array<label> labels, int maxCount) =>
    while array.size(labels) > maxCount
        label oldLabel = array.shift(labels)
        if not na(oldLabel)
            label.delete(oldLabel)
 
//------------------------------------------------------------------------------
// MODULE 0 — DATA SOURCES
//------------------------------------------------------------------------------
string standardTicker = ticker.new(syminfo.prefix, syminfo.ticker)
string haTicker = ticker.heikinashi(standardTicker)
 
[realOpen, realHigh, realLow, realClose] = request.security(standardTicker, timeframe.period, [open, high, low, close], gaps=barmerge.gaps_off, lookahead=barmerge.lookahead_off)
realAtrValue = request.security(standardTicker, timeframe.period, ta.atr(atrLengthInput), gaps=barmerge.gaps_off, lookahead=barmerge.lookahead_off)
 
[haRsi, haMfi, haEma] = request.security(haTicker, timeframe.period, [ta.rsi(close, momentumLengthInput), ta.mfi(hlc3, momentumLengthInput), ta.ema(close, emaLengthInput)], gaps=barmerge.gaps_off, lookahead=barmerge.lookahead_off)
[standardRsi, standardMfi, standardEma] = request.security(standardTicker, timeframe.period, [ta.rsi(close, momentumLengthInput), ta.mfi(hlc3, momentumLengthInput), ta.ema(close, emaLengthInput)], gaps=barmerge.gaps_off, lookahead=barmerge.lookahead_off)
 
legacyRsi = ta.rsi(close, momentumLengthInput)
legacyMfi = ta.mfi(hlc3, momentumLengthInput)
legacyEma = ta.ema(close, emaLengthInput)
 
//------------------------------------------------------------------------------
// MODULE 1 — MOMENTUM ENGINE
//------------------------------------------------------------------------------
rsiValue = signalSourceInput == "Internal Heikin Ashi" ? haRsi : signalSourceInput == "Standard OHLC" ? standardRsi : legacyRsi
mfiValue = signalSourceInput == "Internal Heikin Ashi" ? haMfi : signalSourceInput == "Standard OHLC" ? standardMfi : legacyMfi
volaSyncMomentum = (rsiValue + mfiValue) / 2.0
 
//------------------------------------------------------------------------------
// MODULE 2 — DIRECTION ENGINE
//------------------------------------------------------------------------------
directionEma = signalSourceInput == "Internal Heikin Ashi" ? haEma : signalSourceInput == "Standard OHLC" ? standardEma : legacyEma
emaRising = not na(directionEma[1]) and directionEma > directionEma[1]
emaFalling = not na(directionEma[1]) and directionEma < directionEma[1]
 
bullCondition = volaSyncMomentum > bullThresholdInput and emaRising and barstate.isconfirmed
bearCondition = volaSyncMomentum < bearThresholdInput and emaFalling and barstate.isconfirmed
 
//------------------------------------------------------------------------------
// MODULE 3 — REGIME / STATE ENGINE
//------------------------------------------------------------------------------
var int regime = 0
int previousRegime = nz(regime[1], 0)
 
if barstate.isconfirmed
    if bullCondition
        regime := 1
    else if bearCondition
        regime := -1
    else
        regime := previousRegime
else
    regime := previousRegime
 
buySignal = barstate.isconfirmed and regime == 1 and previousRegime != 1
sellSignal = barstate.isconfirmed and regime == -1 and previousRegime != -1
 
//------------------------------------------------------------------------------
// MODULE 4 — ATR / TREND LINE
//------------------------------------------------------------------------------
rangeWeightedMa = f_rangeWeightedMa(realHigh, realLow, realClose, bandMaLengthInput)
atrValue = realAtrValue
atrBandWidth = atrValue * atrMultiplierInput
percentageBandWidth = realClose * (maxPricePercentageInput / 100.0)
rawBand = math.min(atrBandWidth, percentageBandWidth)
bandWidth = rawBand[bandLookbackInput] * bandExpansionInput
 
trendCenter = regime == 1 ? rangeWeightedMa - bandWidth : regime == -1 ? rangeWeightedMa + bandWidth : na
upperBandInternal = not na(trendCenter) ? trendCenter + bandWidth : na
lowerBandInternal = not na(trendCenter) ? trendCenter - bandWidth : na
bandMainColor = regime == 1 ? color.rgb(0, 200, 170) : regime == -1 ? color.rgb(225, 70, 70) : neutralColorInput
 
plot(showTrendBandInput ? trendCenter : na, "VolaSync ATR / Trend Line", color=bandMainColor, linewidth=2)
 
// Candles remain real/standard. This option changes only their color.
candleColor = regime == 1 ? bullColorInput : regime == -1 ? bearColorInput : neutralColorInput
barcolor(colorCandlesInput ? candleColor : na)
 
//------------------------------------------------------------------------------
// MODULE 5 — REGIME CHANGE MARKERS
//------------------------------------------------------------------------------
buyMarkerY = not na(lowerBandInternal) and not na(bandWidth) ? lowerBandInternal - (bandWidth * signalOffsetMultiplierInput) : realLow
sellMarkerY = not na(upperBandInternal) and not na(bandWidth) ? upperBandInternal + (bandWidth * signalOffsetMultiplierInput) : realHigh
 
useTinySignalMarker = signalMarkerSizeInput == "Tiny"
useSmallSignalMarker = signalMarkerSizeInput == "Small"
useNormalSignalMarker = signalMarkerSizeInput == "Normal"
 
plotshape(showSignalsInput and buySignal and useTinySignalMarker ? buyMarkerY : na, title="VolaSync BUY Tiny", style=shape.triangleup, location=location.absolute, color=bullColorInput, size=size.tiny)
plotshape(showSignalsInput and sellSignal and useTinySignalMarker ? sellMarkerY : na, title="VolaSync SELL Tiny", style=shape.triangledown, location=location.absolute, color=bearColorInput, size=size.tiny)
plotshape(showSignalsInput and buySignal and useSmallSignalMarker ? buyMarkerY : na, title="VolaSync BUY Small", style=shape.triangleup, location=location.absolute, color=bullColorInput, size=size.small)
plotshape(showSignalsInput and sellSignal and useSmallSignalMarker ? sellMarkerY : na, title="VolaSync SELL Small", style=shape.triangledown, location=location.absolute, color=bearColorInput, size=size.small)
plotshape(showSignalsInput and buySignal and useNormalSignalMarker ? buyMarkerY : na, title="VolaSync BUY Normal", style=shape.triangleup, location=location.absolute, color=bullColorInput, size=size.normal)
plotshape(showSignalsInput and sellSignal and useNormalSignalMarker ? sellMarkerY : na, title="VolaSync SELL Normal", style=shape.triangledown, location=location.absolute, color=bearColorInput, size=size.normal)
 
//------------------------------------------------------------------------------
// MODULE 6 — SESSION DETECTION
//------------------------------------------------------------------------------
asiaTime = time(timeframe.period, asiaSessionInput, sessionTimezoneInput)
londonTime = time(timeframe.period, londonSessionInput, sessionTimezoneInput)
nyAmTime = time(timeframe.period, nyAmSessionInput, sessionTimezoneInput)
nyPmTime = time(timeframe.period, nyPmSessionInput, sessionTimezoneInput)
 
inAsia = not na(asiaTime)
inLondon = not na(londonTime)
inNyAm = not na(nyAmTime)
inNyPm = not na(nyPmTime)
 
wasInAsia = bar_index > 0 ? inAsia[1] : false
wasInLondon = bar_index > 0 ? inLondon[1] : false
wasInNyAm = bar_index > 0 ? inNyAm[1] : false
wasInNyPm = bar_index > 0 ? inNyPm[1] : false
 
asiaStart = inAsia and not wasInAsia
londonStart = inLondon and not wasInLondon
nyAmStart = inNyAm and not wasInNyAm
nyPmStart = inNyPm and not wasInNyPm
 
//------------------------------------------------------------------------------
// MODULE 7 — HISTORICAL SESSION RANGES
//------------------------------------------------------------------------------
// These boxes are contextual references only. They DO NOT generate signals.
 
var float asiaHigh = na
var float asiaLow = na
var box currentAsiaBox = na
var label currentAsiaLabel = na
var array<box> asiaRangeBoxes = array.new_box()
var array<label> asiaRangeLabels = array.new_label()
 
var float londonHigh = na
var float londonLow = na
var box currentLondonBox = na
var label currentLondonLabel = na
var array<box> londonRangeBoxes = array.new_box()
var array<label> londonRangeLabels = array.new_label()
 
var float nyAmHigh = na
var float nyAmLow = na
var box currentNyAmBox = na
var label currentNyAmLabel = na
var array<box> nyAmRangeBoxes = array.new_box()
var array<label> nyAmRangeLabels = array.new_label()
 
var float nyPmHigh = na
var float nyPmLow = na
var box currentNyPmBox = na
var label currentNyPmLabel = na
var array<box> nyPmRangeBoxes = array.new_box()
var array<label> nyPmRangeLabels = array.new_label()
 
// ASIA
if asiaStart
    asiaHigh := realHigh
    asiaLow := realLow
    currentAsiaBox := na
    currentAsiaLabel := na
    if showSessionRangesInput
        currentAsiaBox := box.new(left=bar_index, top=realHigh, right=bar_index, bottom=realLow, xloc=xloc.bar_index, border_color=color.new(asiaColorInput, 45), bgcolor=color.new(asiaColorInput, 94), border_width=1)
        currentAsiaLabel := label.new(bar_index, realHigh, "ASIA", xloc=xloc.bar_index, style=label.style_label_down, color=color.new(asiaColorInput, 84), textcolor=asiaColorInput, size=size.tiny)
        array.push(asiaRangeBoxes, currentAsiaBox)
        array.push(asiaRangeLabels, currentAsiaLabel)
        f_trimBoxHistory(asiaRangeBoxes, sessionHistoryDaysInput)
        f_trimLabelHistory(asiaRangeLabels, sessionHistoryDaysInput)
else if inAsia
    asiaHigh := na(asiaHigh) ? realHigh : math.max(asiaHigh, realHigh)
    asiaLow := na(asiaLow) ? realLow : math.min(asiaLow, realLow)
    if showSessionRangesInput and not na(currentAsiaBox)
        box.set_right(currentAsiaBox, bar_index)
        box.set_top(currentAsiaBox, asiaHigh)
        box.set_bottom(currentAsiaBox, asiaLow)
    if showSessionRangesInput and not na(currentAsiaLabel)
        label.set_x(currentAsiaLabel, bar_index)
        label.set_y(currentAsiaLabel, asiaHigh)
 
// LONDON
if londonStart
    londonHigh := realHigh
    londonLow := realLow
    currentLondonBox := na
    currentLondonLabel := na
    if showSessionRangesInput
        currentLondonBox := box.new(left=bar_index, top=realHigh, right=bar_index, bottom=realLow, xloc=xloc.bar_index, border_color=color.new(londonColorInput, 45), bgcolor=color.new(londonColorInput, 94), border_width=1)
        currentLondonLabel := label.new(bar_index, realHigh, "LONDON", xloc=xloc.bar_index, style=label.style_label_down, color=color.new(londonColorInput, 84), textcolor=londonColorInput, size=size.tiny)
        array.push(londonRangeBoxes, currentLondonBox)
        array.push(londonRangeLabels, currentLondonLabel)
        f_trimBoxHistory(londonRangeBoxes, sessionHistoryDaysInput)
        f_trimLabelHistory(londonRangeLabels, sessionHistoryDaysInput)
else if inLondon
    londonHigh := na(londonHigh) ? realHigh : math.max(londonHigh, realHigh)
    londonLow := na(londonLow) ? realLow : math.min(londonLow, realLow)
    if showSessionRangesInput and not na(currentLondonBox)
        box.set_right(currentLondonBox, bar_index)
        box.set_top(currentLondonBox, londonHigh)
        box.set_bottom(currentLondonBox, londonLow)
    if showSessionRangesInput and not na(currentLondonLabel)
        label.set_x(currentLondonLabel, bar_index)
        label.set_y(currentLondonLabel, londonHigh)
 
// NEW YORK AM
if nyAmStart
    nyAmHigh := realHigh
    nyAmLow := realLow
    currentNyAmBox := na
    currentNyAmLabel := na
    if showSessionRangesInput
        currentNyAmBox := box.new(left=bar_index, top=realHigh, right=bar_index, bottom=realLow, xloc=xloc.bar_index, border_color=color.new(nyAmColorInput, 45), bgcolor=color.new(nyAmColorInput, 94), border_width=1)
        currentNyAmLabel := label.new(bar_index, realHigh, "NY AM", xloc=xloc.bar_index, style=label.style_label_down, color=color.new(nyAmColorInput, 84), textcolor=nyAmColorInput, size=size.tiny)
        array.push(nyAmRangeBoxes, currentNyAmBox)
        array.push(nyAmRangeLabels, currentNyAmLabel)
        f_trimBoxHistory(nyAmRangeBoxes, sessionHistoryDaysInput)
        f_trimLabelHistory(nyAmRangeLabels, sessionHistoryDaysInput)
else if inNyAm
    nyAmHigh := na(nyAmHigh) ? realHigh : math.max(nyAmHigh, realHigh)
    nyAmLow := na(nyAmLow) ? realLow : math.min(nyAmLow, realLow)
    if showSessionRangesInput and not na(currentNyAmBox)
        box.set_right(currentNyAmBox, bar_index)
        box.set_top(currentNyAmBox, nyAmHigh)
        box.set_bottom(currentNyAmBox, nyAmLow)
    if showSessionRangesInput and not na(currentNyAmLabel)
        label.set_x(currentNyAmLabel, bar_index)
        label.set_y(currentNyAmLabel, nyAmHigh)
 
// NEW YORK PM
if nyPmStart
    nyPmHigh := realHigh
    nyPmLow := realLow
    currentNyPmBox := na
    currentNyPmLabel := na
    if showSessionRangesInput
        currentNyPmBox := box.new(left=bar_index, top=realHigh, right=bar_index, bottom=realLow, xloc=xloc.bar_index, border_color=color.new(nyPmColorInput, 45), bgcolor=color.new(nyPmColorInput, 94), border_width=1)
        currentNyPmLabel := label.new(bar_index, realHigh, "NY PM", xloc=xloc.bar_index, style=label.style_label_down, color=color.new(nyPmColorInput, 84), textcolor=nyPmColorInput, size=size.tiny)
        array.push(nyPmRangeBoxes, currentNyPmBox)
        array.push(nyPmRangeLabels, currentNyPmLabel)
        f_trimBoxHistory(nyPmRangeBoxes, sessionHistoryDaysInput)
        f_trimLabelHistory(nyPmRangeLabels, sessionHistoryDaysInput)
else if inNyPm
    nyPmHigh := na(nyPmHigh) ? realHigh : math.max(nyPmHigh, realHigh)
    nyPmLow := na(nyPmLow) ? realLow : math.min(nyPmLow, realLow)
    if showSessionRangesInput and not na(currentNyPmBox)
        box.set_right(currentNyPmBox, bar_index)
        box.set_top(currentNyPmBox, nyPmHigh)
        box.set_bottom(currentNyPmBox, nyPmLow)
    if showSessionRangesInput and not na(currentNyPmLabel)
        label.set_x(currentNyPmLabel, bar_index)
        label.set_y(currentNyPmLabel, nyPmHigh)
 
//------------------------------------------------------------------------------
// MODULE 8 — ALERTS
//------------------------------------------------------------------------------
alertcondition(buySignal, title="VolaSync BUY", message="VolaSync bullish regime confirmed")
alertcondition(sellSignal, title="VolaSync SELL", message="VolaSync bearish regime confirmed")
alertcondition(buySignal or sellSignal, title="VolaSync Signal", message="VolaSync regime transition confirmed")
 
//------------------------------------------------------------------------------
// MODULE 9 — DEBUG
//------------------------------------------------------------------------------
plot(debugModeInput ? volaSyncMomentum : na, "Debug - VolaSync Momentum", color=color.blue, display=display.data_window)
plot(debugModeInput ? rsiValue : na, "Debug - RSI", color=color.purple, display=display.data_window)
plot(debugModeInput ? mfiValue : na, "Debug - MFI", color=color.orange, display=display.data_window)
plot(debugModeInput ? directionEma : na, "Debug - EMA", color=color.yellow, display=display.data_window)
plot(debugModeInput ? regime : na, "Debug - Regime", color=color.gray, display=display.data_window)
plot(debugModeInput ? f_boolToInt(buySignal) : na, "Debug - BUY", color=color.green, display=display.data_window)
plot(debugModeInput ? f_boolToInt(sellSignal) : na, "Debug - SELL", color=color.red, display=display.data_window)

← 返回列表