CryptonianFibForecast

AYEHAN · library · 134 行 · 点赞 1 · TradingView 原页

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

Pine Script

// 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 Forecast v1. Immutable-by-convention confirmed A-B-C geometry, projected D, and conservative confirmed-bar resolution. No drawings, inputs, market data requests or trade signals.
library("CryptonianFibForecast", overlay=false)
 
import AYEHAN/CryptonianFibMath/1 as FM
 
//@enum Lifecycle of an A-B-C -> D geometrical projection, NOT a trading signal.
export enum ABCDStatus
    none = "None"
    projected = "Projected"
    completed = "Target reached"
    invalidated = "Invalidated"
    ambiguous = "Ambiguous target/invalidation"
    retired = "Retired by new structure"
 
//@type Immutable-by-convention geometrical scenario based on TWO consecutive confirmed swings.
//@field valid True only after a qualified ABC sequence is confirmed and its D target is still in the future.
//@field id Sequential identity assigned by host upon publication.
//@field abSwingId Identity of the confirmed A-to-B swing.
//@field bcSwingId Identity of the confirmed B-to-C swing.
//@field a A pivot price.
//@field b B pivot price.
//@field c C pivot price.
//@field bcRatio Magnitude of BC divided by AB.
//@field target1 Exactly 1.000 * AB projected from C.
//@field target1272 1.272 * AB projected from C (secondary informational objective).
//@field direction 1 bullish CD projection; -1 bearish CD projection.
//@field publishedBar First bar on which a completed BC pivot makes this projection knowable.
//@field publishedTime Closing timestamp of publication bar.
//@field resolutionBar Bar of subsequent outcome, not retroactively placed at pivot C.
//@field status Projection-only lifecycle; never an entry or exit order.
export type ABCD
    bool valid = false
    int id = 0
    int abSwingId = 0
    int bcSwingId = 0
    float a = na
    float b = na
    float c = na
    float bcRatio = na
    float target1 = na
    float target1272 = na
    int direction = 0
    int publishedBar = na
    int publishedTime = na
    int resolutionBar = na
    ABCDStatus status = ABCDStatus.none
 
//@function Build an eligible A-B-C projection from consecutive CONFIRMED swings; caller must use a confirmed host bar. Reject patterns already consumed or invalid when first knowable.
//@param a Earlier A anchor.
//@param b Shared B anchor.
//@param c Confirmed C anchor.
//@param abSwingId Historical A-B swing identity.
//@param bcSwingId Historical B-C swing identity.
//@param abEndBar Original pivot B bar from A-B.
//@param bcStartBar Original pivot B bar from B-C.
//@param scenarioId Host-provided next candidate identifier.
//@param publishedBar Current confirmation bar_index, never historical C bar.
//@param publishedTime Current bar close timestamp.
//@param minBC Minimum qualifying BC / AB proportion.
//@param maxBC Maximum qualifying BC / AB proportion.
//@param currentClose Confirmed close when C first becomes known.
//@param currentHigh High of publication candle, for pre-consumption check.
//@param currentLow Low of publication candle, for pre-consumption check.
//@returns Candidate with valid=false if sequence is ineligible or already over before its publication.
export createABCD(float a, float b, float c, int abSwingId, int bcSwingId, int abEndBar, int bcStartBar, int scenarioId, int publishedBar, int publishedTime, float minBC, float maxBC, float currentClose, float currentHigh, float currentLow) =>
    ABCD result = ABCD.new()
    bool dataOK = not na(a) and not na(b) and not na(c) and not na(abEndBar) and not na(bcStartBar) and not na(publishedBar) and not na(publishedTime) and not na(minBC) and not na(maxBC) and not na(currentClose) and not na(currentHigh) and not na(currentLow)
    bool eligibleIds = abSwingId > 0 and bcSwingId > abSwingId and scenarioId > 0
    bool contiguous = abEndBar == bcStartBar
    bool validRatios = minBC > 0 and maxBC < 1.0 and minBC <= maxBC
    float ab = b - a
    float bc = c - b
    float proportion = ab != 0 ? math.abs(bc / ab) : na
    bool opposite = not na(proportion) and ab * bc < 0
    bool cWithinAB = ab > 0 ? c > a and c < b : ab < 0 ? c < a and c > b : false
    bool withinRatio = not na(proportion) and proportion >= minBC and proportion <= maxBC
    float d = FM.projection(a, b, c, 1.0)
    float d1272 = FM.projection(a, b, c, 1.272)
    bool bullish = ab > 0
    bool alreadyConsumed = bullish ? currentHigh >= d : currentLow <= d
    bool alreadyInvalid = bullish ? currentClose < c : currentClose > c
    bool usable = dataOK and eligibleIds and contiguous and validRatios and opposite and cWithinAB and withinRatio and not alreadyConsumed and not alreadyInvalid
    if usable
        result.valid := true
        result.id := scenarioId
        result.abSwingId := abSwingId
        result.bcSwingId := bcSwingId
        result.a := a
        result.b := b
        result.c := c
        result.bcRatio := proportion
        result.target1 := d
        result.target1272 := d1272
        result.direction := bullish ? 1 : -1
        result.publishedBar := publishedBar
        result.publishedTime := publishedTime
        result.status := ABCDStatus.projected
    result
 
//@function Resolve only on CONFIRMED, later host bars. A same-bar target touch and closing invalidation is AMBIGUOUS, never a fabricated win.
//@param scenario Previously published scenario.
//@param confirmedHigh High of current completed bar.
//@param confirmedLow Low of current completed bar.
//@param confirmedClose Close of current completed bar.
//@param confirmedBar Current completed bar_index.
//@returns Same UDT reference with only lifecycle and resolutionBar updated; geometry remains unchanged.
export advanceABCD(ABCD scenario, float confirmedHigh, float confirmedLow, float confirmedClose, int confirmedBar) =>
    if scenario.valid and scenario.status == ABCDStatus.projected and not na(scenario.publishedBar) and confirmedBar > scenario.publishedBar
        bool touched = scenario.direction == 1 ? confirmedHigh >= scenario.target1 : confirmedLow <= scenario.target1
        bool invalid = scenario.direction == 1 ? confirmedClose < scenario.c : confirmedClose > scenario.c
        if touched and invalid
            scenario.status := ABCDStatus.ambiguous
            scenario.resolutionBar := confirmedBar
        else if invalid
            scenario.status := ABCDStatus.invalidated
            scenario.resolutionBar := confirmedBar
        else if touched
            scenario.status := ABCDStatus.completed
            scenario.resolutionBar := confirmedBar
    scenario
 
//@function Permanently retire an unresolved old geometrical candidate when a newly confirmed structural event supersedes it.
//@param scenario Existing geometrical scenario.
//@param confirmedBar Current confirmed bar_index.
//@returns Scenario with only terminal status updated; original anchors/targets stay fixed.
export retireABCD(ABCD scenario, int confirmedBar) =>
    if scenario.valid and scenario.status == ABCDStatus.projected
        scenario.status := ABCDStatus.retired
        scenario.resolutionBar := confirmedBar
    scenario

← 返回列表