CryptonianFibTrade

AYEHAN · library · 134 行 · 点赞 0 · 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 Trade Engine v1. Confirmed-bar entries, frozen geometry, unambiguous closed-bar lifecycle.
// Signals are reference-price decisions, NOT broker fills. Never infer intrabar order.
library("CryptonianFibTrade", overlay=false)
 
//@enum Terminal states are one-way. TP1 is a managed-open position; the original targets never move.
export enum TradeStatus
    none = "NONE"
    active = "ACTIVE"
    tp1 = "TP1 / BE ARMED"
    tp2 = "TP2"
    stop = "STOP"
    breakeven = "BREAK EVEN"
    ambiguous = "AMBIGUOUS"
 
//@type One independently snapshotted reference trade. No UDT alias to forecast/watch.
//@field valid Whether this entry passed all constraints at the closed signal bar.
//@field id Unique in this chart calculation.
//@field watchId Source forecast ID; never reuse to open another trade.
//@field direction 1 long, -1 short.
//@field signalBar Actual confirmation / signal bar; never an earlier swing pivot bar.
//@field signalTime Signal candle time_close.
//@field entry Frozen SIGNAL CLOSE reference price (not a broker fill).
//@field originalStop Frozen thesis invalidation used as initial reference stop.
//@field target1 Frozen 1R or configured R first target.
//@field target2 Frozen source forecast objective.
//@field managedStop Dynamic stop; after TP1, set to entry for NEXT bar onward.
//@field rewardRisk R:R to target2 at publication time.
//@field status One-way lifecycle state.
//@field tp1Bar First confirmed bar touching target1.
//@field exitBar Confirmed terminal event bar.
//@field exitPrice Reference exit level, na when order within candle is unknowable.
export type Position
    bool valid = false
    int id = 0
    int watchId = 0
    int direction = 0
    int signalBar = na
    int signalTime = na
    float entry = na
    float originalStop = na
    float target1 = na
    float target2 = na
    float managedStop = na
    float rewardRisk = na
    TradeStatus status = TradeStatus.none
    int tp1Bar = na
    int exitBar = na
    float exitPrice = na
 
//@function Create only on the ACTUAL first completed confirmation candle of a still-active source watch. The host enforces this.
//@param newId Monotonic trade identity.
//@param sourceWatchId Source watch identity.
//@param dir 1 long, -1 short.
//@param signalClose Completed host candle CLOSE used as reference entry.
//@param thesisStop Watch's immutable original invalidation.
//@param sourceObjective Watch's immutable original objective.
//@param confirmedBar Signal bar index.
//@param confirmedTime Signal candle close timestamp.
//@param requiredRR Minimum reward/risk at close.
//@param tp1RiskMultiple TP1 distance as a multiple of original risk, strictly less than RR.
//@param tick Symbol minimum tick.
//@returns A valid immutable-by-contract trade plan or invalid instance.
export createPosition(int newId, int sourceWatchId, int dir, float signalClose, float thesisStop, float sourceObjective, int confirmedBar, int confirmedTime, float requiredRR, float tp1RiskMultiple, float tick) =>
    Position result = Position.new()
    bool basic = newId > 0 and sourceWatchId > 0 and (dir == 1 or dir == -1) and not na(signalClose) and not na(thesisStop) and not na(sourceObjective) and not na(confirmedBar) and not na(confirmedTime) and tick > 0
    bool paramsOK = requiredRR >= 1.0 and tp1RiskMultiple >= 0.5
    float risk = dir == 1 ? signalClose - thesisStop : thesisStop - signalClose
    float reward = dir == 1 ? sourceObjective - signalClose : signalClose - sourceObjective
    float rr = risk > 0 ? reward / risk : na
    bool geometry = basic and paramsOK and risk >= tick * 2.0 and reward > risk * tp1RiskMultiple and not na(rr) and rr >= requiredRR
    if geometry
        result.valid := true
        result.id := newId
        result.watchId := sourceWatchId
        result.direction := dir
        result.signalBar := confirmedBar
        result.signalTime := confirmedTime
        result.entry := signalClose
        result.originalStop := thesisStop
        result.target1 := signalClose + dir * risk * tp1RiskMultiple
        result.target2 := sourceObjective
        result.managedStop := thesisStop
        result.rewardRisk := rr
        result.status := TradeStatus.active
    result
 
//@function One-bar deterministic transition from COMPLETED candle OHLC, only AFTER signal candle.
// TP1 arms BE for later candles, not retroactively inside its touching bar.
// If competing endpoints are inside the same completed candle, event is AMBIGUOUS.
//@param trade Existing reference trade.
//@param confirmedHigh Closed bar's high.
//@param confirmedLow Closed bar's low.
//@param confirmedBar Closed bar index.
//@returns Same reference with frozen entry/original SL/targets and one-way state transition.
export advance(Position trade, float confirmedHigh, float confirmedLow, int confirmedBar) =>
    if trade.valid and confirmedBar > trade.signalBar and (trade.status == TradeStatus.active or trade.status == TradeStatus.tp1)
        bool isLong = trade.direction == 1
        bool stopTouched = isLong ? confirmedLow <= trade.managedStop : confirmedHigh >= trade.managedStop
        bool finalTouched = isLong ? confirmedHigh >= trade.target2 : confirmedLow <= trade.target2
        bool firstTouched = isLong ? confirmedHigh >= trade.target1 : confirmedLow <= trade.target1
        if stopTouched and (finalTouched or (trade.status == TradeStatus.active and firstTouched))
            trade.status := TradeStatus.ambiguous
            trade.exitBar := confirmedBar
            trade.exitPrice := na
        else if stopTouched
            trade.status := trade.status == TradeStatus.tp1 ? TradeStatus.breakeven : TradeStatus.stop
            trade.exitBar := confirmedBar
            trade.exitPrice := trade.managedStop
        else if finalTouched
            trade.status := TradeStatus.tp2
            trade.exitBar := confirmedBar
            trade.exitPrice := trade.target2
        else if trade.status == TradeStatus.active and firstTouched
            trade.status := TradeStatus.tp1
            trade.tp1Bar := confirmedBar
            // Effective only on next bar: intrabar chronology is not inferred.
            trade.managedStop := trade.entry
    trade
 
//@function Whether the trade remains open in the indicator's reference lifecycle.
export isOpen(Position trade) =>
    trade.valid and (trade.status == TradeStatus.active or trade.status == TradeStatus.tp1)
 
//@function Whether a terminal outcome was permanently recorded.
export isClosed(Position trade) =>
    trade.valid and (trade.status == TradeStatus.tp2 or trade.status == TradeStatus.stop or trade.status == TradeStatus.breakeven or trade.status == TradeStatus.ambiguous)
 
//@function Compact status text, never a win probability.
export statusText(Position trade) =>
    not trade.valid ? "NONE" : trade.status == TradeStatus.active ? "ACTIVE" : trade.status == TradeStatus.tp1 ? "TP1 · BE ARMED" : trade.status == TradeStatus.tp2 ? "TP2 COMPLETE" : trade.status == TradeStatus.stop ? "STOPPED" : trade.status == TradeStatus.breakeven ? "BREAK EVEN" : trade.status == TradeStatus.ambiguous ? "AMBIGUOUS" : "NONE"

← 返回列表