本页源码来自 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/
// © AYEHAN
//@version=6
//@description [Cryptonian] Fibonacci confirmed swing scanner. v1 / engine v0.2.0.
// ALL candidate pivots must be provided by the host chart after right-side confirmation.
// No requests, inputs, forward-looking calculations, plots, or signal generation.
library("CryptonianFibStructure", overlay = false)
//@type Independent, chart-timeframe confirmed-pivot tracker for ONE structural degree.
//@field lastKind Last unpaired pivot: 1 high / -1 low / 0 none.
//@field lastPrice Price of last unpaired pivot.
//@field lastBar Original bar index of that pivot (not when it was recognized).
//@field lastTime Original time of that pivot.
//@field swingId Number of published, confirmed swings for this degree.
//@field fromPrice Published earlier pivot price.
//@field toPrice Published later pivot price.
//@field fromBar Published earlier pivot bar.
//@field toBar Published later pivot bar.
//@field fromTime Published earlier pivot time.
//@field toTime Published later pivot time.
//@field confirmationBar Actual bar that confirmed the later pivot.
//@field confirmationTime Closing time of the confirmation bar.
//@field magnitude ATR units of the published price move (descriptive, NOT a probability).
//@field eventOnThisBar True ONLY on the bar when a new swing has been confirmed.
//@field ambiguousPairs Number of dual-extreme confirmations skipped.
//@field failedFilters Number of opposite-pivot pairs rejected by minimum filters.
//@field updatedPending Number of improvements to an unpaired same-kind pivot.
export type Track
int lastKind = 0
float lastPrice = na
int lastBar = na
int lastTime = na
int swingId = 0
float fromPrice = na
float toPrice = na
int fromBar = na
int toBar = na
int fromTime = na
int toTime = na
int confirmationBar = na
int confirmationTime = na
float magnitude = na
bool eventOnThisBar = false
int ambiguousPairs = 0
int failedFilters = 0
int updatedPending = 0
//@function Advance ONE confirmed-bar structural degree. Caller must gate with barstate.isconfirmed.
//@param state Persistent Track instance for this degree.
//@param pivotHigh Confirmed ta.pivothigh value, otherwise na.
//@param pivotLow Confirmed ta.pivotlow value, otherwise na.
//@param pivotBar Original pivot bar_index, computed by host as bar_index - rightBars.
//@param pivotTime Original pivot time, computed by host as time[rightBars].
//@param confirmedBar Current closed bar_index; no backdated publishing.
//@param confirmedTime Current time_close; time confirmation became available.
//@param minimumMove Minimum swing absolute price movement (normally ticks * syminfo.mintick).
//@param minimumBars Minimum positive separation between actual pivots.
//@param minimumATR Additional minimum swing displacement in multiples of ATR (zero to disable).
//@param pivotATR Confirmed ATR at the pivot, e.g. ta.atr(14)[rightBars].
//@returns Mutated Track, including a one-bar publication pulse and immutable-by-convention last published anchors.
export advance(Track state, float pivotHigh, float pivotLow, int pivotBar, int pivotTime, int confirmedBar, int confirmedTime, float minimumMove, int minimumBars, float minimumATR, float pivotATR) =>
// Always reset the publication pulse on every CONFIRMED host bar.
state.eventOnThisBar := false
bool hasHigh = not na(pivotHigh)
bool hasLow = not na(pivotLow)
if hasHigh and hasLow
// One candle can qualify as both pivots. We cannot infer intrabar order.
state.ambiguousPairs := state.ambiguousPairs + 1
else if hasHigh or hasLow
int kind = hasHigh ? 1 : -1
float price = hasHigh ? pivotHigh : pivotLow
bool validChronology = not na(pivotBar) and not na(pivotTime) and pivotBar <= confirmedBar
if validChronology
if state.lastKind == 0
state.lastKind := kind
state.lastPrice := price
state.lastBar := pivotBar
state.lastTime := pivotTime
else if kind == state.lastKind
// Only the UNPAIRED anchor can be replaced. Published structures never move.
bool moreExtreme = kind == 1 ? price > state.lastPrice : price < state.lastPrice
if moreExtreme and pivotBar > state.lastBar
state.lastPrice := price
state.lastBar := pivotBar
state.lastTime := pivotTime
state.updatedPending := state.updatedPending + 1
else
// Opposite pivots: qualify the move and commit it on THIS confirming bar.
int separation = pivotBar - state.lastBar
float distance = math.abs(price - state.lastPrice)
bool atrAvailable = minimumATR <= 0 or (not na(pivotATR) and pivotATR > 0)
bool qualifies = separation >= math.max(1, minimumBars) and distance >= minimumMove and atrAvailable and (minimumATR <= 0 or distance >= minimumATR * pivotATR)
if qualifies
state.swingId := state.swingId + 1
state.fromPrice := state.lastPrice
state.toPrice := price
state.fromBar := state.lastBar
state.toBar := pivotBar
state.fromTime := state.lastTime
state.toTime := pivotTime
state.confirmationBar := confirmedBar
state.confirmationTime := confirmedTime
state.magnitude := not na(pivotATR) and pivotATR > 0 ? distance / pivotATR : na
state.eventOnThisBar := true
else
state.failedFilters := state.failedFilters + 1
// Mirrors v0.1.0: the newest opposite pivot becomes the pending anchor
// even when the previous swing was too small or too brief to publish.
state.lastKind := kind
state.lastPrice := price
state.lastBar := pivotBar
state.lastTime := pivotTime
state
//@function Expose whether a swing was published by this independent degree.
//@param state State of the degree.
//@returns True once a confirmed swing exists.
export hasSwing(Track state) =>
state.swingId > 0 and not na(state.fromPrice) and not na(state.toPrice)
//@function Structural direction from last published price pair.
//@param state State of the degree.
//@returns 1 bullish, -1 bearish, 0 when no published swing.
export direction(Track state) =>
not hasSwing(state) ? 0 : state.toPrice > state.fromPrice ? 1 : -1