CRT Cycle V5.1 Beta - MTF

ahmad-haimor · study · 130 行 · 点赞 11 · TradingView 原页

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

Pine Script

//@version=6
indicator("CRT Cycle V5.1 Beta - MTF", overlay = true, max_lines_count = 2)
 
bool showRange = input.bool(true, "Show current mother candle")
int lineBars = input.int(4, "Line length in timeframe intervals", minval = 1, maxval = 20)
color rangeColor = input.color(color.rgb(70, 188, 180), "CRT line color")
 
// Return the current direction and whether this candle closed outside.
f_classify(float refHi, float refLo, float hi, float lo, float cls) =>
    bool broke = cls > refHi or cls < refLo
    bool sweptHigh = hi > refHi
    bool sweptLow = lo < refLo
    int direction = 0
    if broke
        direction := cls > refHi ? 1 : -1
    else if sweptHigh != sweptLow
        direction := sweptLow ? 1 : -1
    [direction, broke, sweptHigh and sweptLow]
 
// The live reference follows CRT. A pending target keeps its original price.
f_engine(bool acceptBar) =>
    var float refHi = na
    var float refLo = na
    var int refTime = na
    var int status = 0
    var float target = na
    var int targetDirection = 0
 
    if acceptBar
        if na(refTime)
            refHi := high
            refLo := low
            refTime := time
        else
            [direction, broke, bothSides] = f_classify(refHi, refLo, high, low, close)
            bool targetHit = targetDirection == 1 ? high >= target : targetDirection == -1 ? low <= target : false
            status := direction
 
            if direction != 0
                // A new CRT takes priority even if the previous target was hit.
                target := broke ? na : direction == 1 ? refHi : refLo
                targetDirection := broke ? 0 : direction
                refHi := high
                refLo := low
                refTime := time
            else if targetHit
                target := na
                targetDirection := 0
                if not bothSides
                    refHi := high
                    refLo := low
                    refTime := time
 
    [status, refHi, refLo, refTime]
 
f_previousStatus() =>
    [status, _, _, _] = f_engine(true)
    status[1]
 
f_intrabarStatus() =>
    [status, _, _, _] = f_engine(true)
    [status, time_close, status[1], time_close[1]]
 
f_status(string tf, int chartSeconds, int chartStatus) =>
    int seconds = timeframe.in_seconds(tf)
    var int latestLower = na
    int status = na
 
    if seconds == chartSeconds
        status := chartStatus
    else if seconds > chartSeconds
        status := request.security(syminfo.tickerid, tf, f_previousStatus(), lookahead = barmerge.lookahead_on)
    else
        [states, closes, previousStates, previousCloses] = request.security_lower_tf(syminfo.tickerid, tf, f_intrabarStatus())
        int last = array.size(states) - 1
        int cutoff = barstate.isrealtime ? math.min(time_close, timenow) : time_close
 
        if last >= 0
            // A lower candle can straddle the chart candle's close.
            if array.get(closes, last) <= cutoff
                latestLower := array.get(states, last)
            else if array.get(previousCloses, last) <= cutoff
                latestLower := array.get(previousStates, last)
 
        status := latestLower
 
    status
 
int chartSeconds = timeframe.in_seconds()
[chartStatus, chartHi, chartLo, chartMother] = f_engine(barstate.isconfirmed)
 
int s30 = f_status("30", chartSeconds, chartStatus)
int s60 = f_status("60", chartSeconds, chartStatus)
int s240 = f_status("240", chartSeconds, chartStatus)
int s360 = f_status("360", chartSeconds, chartStatus)
int s720 = f_status("720", chartSeconds, chartStatus)
 
// Chart range. The same two lines are updated when the mother changes.
bool supported = timeframe.isminutes and (timeframe.multiplier == 30 or timeframe.multiplier == 60 or timeframe.multiplier == 240 or timeframe.multiplier == 360 or timeframe.multiplier == 720)
var line upper = na
var line lower = na
 
if barstate.islast and supported and showRange and not na(chartMother)
    int endTime = chartMother + lineBars * chartSeconds * 1000
    if na(upper)
        upper := line.new(chartMother, chartHi, endTime, chartHi, xloc = xloc.bar_time, color = rangeColor, width = 2)
        lower := line.new(chartMother, chartLo, endTime, chartLo, xloc = xloc.bar_time, color = rangeColor, width = 2)
    else
        line.set_xy1(upper, chartMother, chartHi)
        line.set_xy2(upper, endTime, chartHi)
        line.set_xy1(lower, chartMother, chartLo)
        line.set_xy2(lower, endTime, chartLo)
 
f_row(table panel, int row, string tf, int status) =>
    string caption = na(status) ? "—" : status == 1 ? "BULLISH — صاعد" : status == -1 ? "BEARISH — هابط" : "UNCERTAIN — عدم يقين"
    color background = na(status) ? color.gray : status == 1 ? color.rgb(27, 112, 68) : status == -1 ? color.rgb(154, 47, 54) : color.rgb(223, 185, 70)
    color ink = status == 0 ? color.black : color.white
    table.cell(panel, 0, row, tf, bgcolor = color.black, text_color = color.white)
    table.cell(panel, 1, row, caption, bgcolor = background, text_color = ink)
 
var table dashboard = table.new(position.top_right, 2, 6, border_width = 1)
 
if barstate.islast
    table.cell(dashboard, 0, 0, "TF", bgcolor = color.black, text_color = color.white)
    table.cell(dashboard, 1, 0, "CRT", bgcolor = color.black, text_color = color.white)
    f_row(dashboard, 1, "30m", s30)
    f_row(dashboard, 2, "1H", s60)
    f_row(dashboard, 3, "4H", s240)
    f_row(dashboard, 4, "6H", s360)
    f_row(dashboard, 5, "12H", s720)

← 返回列表