CryptonianFibMath

AYEHAN · library · 120 行 · 点赞 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 pure mathematics library. Part 0 / v0.1.0.
// NO INPUTS, NO REQUEST.SECURITY, NO DRAWINGS, NO STATE AND NO FUTURE DATA.
library("CryptonianFibMath", overlay = false)
 
//@function Absolute price displacement between two pivots.
//@param a First pivot price.
//@param b Second pivot price.
//@returns Absolute displacement, or na when the price pair is unavailable.
export swingSize(float a, float b) =>
    na(a) or na(b) ? na : math.abs(b - a)
 
//@function Verify that an anchor pair has a meaningful (positive) displacement.
//@param a Earlier anchor price.
//@param b Later anchor price.
//@param minMove Minimum permitted absolute price move.
//@returns True if valid and separated by the requested amount.
export validSwing(float a, float b, float minMove) =>
    not na(a) and not na(b) and not na(minMove) and minMove > 0 and math.abs(b - a) >= minMove
 
//@function Retracement of a movement A -> B. 0 = B, 1 = A, 0.618 = 61.8% back from B.
//@param a First anchor A.
//@param b Last anchor B.
//@param ratio Retracement fraction, e.g. 0.618.
//@returns Absolute retracement price, or na for unavailable inputs.
export retracement(float a, float b, float ratio) =>
    na(a) or na(b) or na(ratio) ? na : b + (a - b) * ratio
 
//@function Extension of A -> B. 1 = B, 1.618 projects 61.8% beyond B.
//@param a First anchor A.
//@param b Last anchor B.
//@param ratio Extension multiple, e.g. 1.618.
//@returns Absolute extension price.
export extension(float a, float b, float ratio) =>
    na(a) or na(b) or na(ratio) ? na : a + (b - a) * ratio
 
//@function Three-point Fibonacci projection: project AB from C by a selected multiple.
//@param a Origin of impulse AB.
//@param b End of impulse AB.
//@param c Start of new projected leg.
//@param ratio Size multiple of AB.
//@returns Projected endpoint from C.
export projection(float a, float b, float c, float ratio) =>
    na(a) or na(b) or na(c) or na(ratio) ? na : c + (b - a) * ratio
 
//@function Absolute price distance between a level and an observed price.
//@param price Observed price.
//@param level Reference level.
//@returns Positive absolute distance.
export distance(float price, float level) =>
    na(price) or na(level) ? na : math.abs(price - level)
 
//@function Distance normalized to a nonzero structural range (not a probability).
//@param price Observed price.
//@param level Reference price.
//@param structuralRange Positive reference range.
//@returns Distance / range, or na for a zero/invalid range.
export normalizedDistance(float price, float level, float structuralRange) =>
    na(price) or na(level) or na(structuralRange) or structuralRange <= 0 ? na : math.abs(price - level) / structuralRange
 
//@function Whether a price is inside a region, allowing a caller-specified absolute tolerance.
//@param price Observed price.
//@param a One zone boundary.
//@param b Other zone boundary.
//@param tolerance Nonnegative absolute buffer around the region.
//@returns True only for valid inputs and an inclusive match.
export inZone(float price, float a, float b, float tolerance) =>
    not na(price) and not na(a) and not na(b) and not na(tolerance) and tolerance >= 0 and price >= math.min(a, b) - tolerance and price <= math.max(a, b) + tolerance
 
//@function Round a price to the nearest instrument tick (caller passes syminfo.mintick).
//@param price Price to round.
//@param tickSize Tick size (must be positive).
//@returns Rounded price or na for invalid inputs.
export roundToTick(float price, float tickSize) =>
    na(price) or na(tickSize) or tickSize <= 0 ? na : math.round(price / tickSize) * tickSize
 
//@function Geometry-only reward/risk; no fill or profitability claims.
//@param entry Hypothetical trade entry.
//@param stop Structural stop.
//@param target Projected target.
//@returns Reward/risk if target and stop bracket the entry in a valid direction.
export rewardRisk(float entry, float stop, float target) =>
    float risk = math.abs(entry - stop)
    float reward = math.abs(target - entry)
    bool aligned = (target - entry) * (entry - stop) > 0
    na(entry) or na(stop) or na(target) or risk <= 0 or not aligned ? na : reward / risk
 
//@function Extrapolate a whole-number bar count using a Fibonacci/time multiple.
//@param baseBars Original positive bar count.
//@param ratio Positive time ratio.
//@returns Number of bars, or na when unavailable/invalid.
export projectedBars(int baseBars, float ratio) =>
    na(baseBars) or na(ratio) or baseBars <= 0 or ratio <= 0 ? int(na) : math.max(1, int(math.round(baseBars * ratio)))
 
//@function Common chart Fibonacci ratios by index (0..15).
// Includes conventional midpoint 0.500 and sqrt-like derived ratios.
//@param index Index from 0 to 15.
//@returns Ratio or na for an unrecognized index.
export ratioAt(int index) =>
    switch index
        0  => 0.0
        1  => 0.236
        2  => 0.382
        3  => 0.5
        4  => 0.618
        5  => 0.786
        6  => 0.886
        7  => 1.0
        8  => 1.272
        9  => 1.414
        10 => 1.618
        11 => 2.0
        12 => 2.618
        13 => 3.618
        14 => 4.236
        15 => 0.705
        => float(na)

← 返回列表