TTG FTFC with 3 Time frame candles

TheTradingGoddess · study · 395 行 · 点赞 21 · TradingView 原页

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

Pine Script

//@version=6
indicator("TTG FTFC with 3 Time frame candles",
     overlay=true,
     max_boxes_count=20,
     max_lines_count=30,
     max_labels_count=50)
 
//==================================================
// LIVE CANDLE TIMEFRAMES
//==================================================
groupTF = "Live Candle Timeframes"
 
tf1 = input.timeframe("15", "Candle 1", group=groupTF)
tf2 = input.timeframe("60", "Candle 2", group=groupTF)
tf3 = input.timeframe("D", "Candle 3", group=groupTF)
 
//==================================================
// LIVE CANDLE DISPLAY
//==================================================
groupDisplay = "Live Candle Display"
 
showCandles = input.bool(true, "Show Live Candles", group=groupDisplay)
 
rightOffset = input.int(12, "Right Side Offset",
     minval=3, maxval=100, group=groupDisplay)
 
candleWidth = input.int(5, "Candle Width",
     minval=1, maxval=20, group=groupDisplay)
 
candleSpacing = input.int(4, "Candle Spacing",
     minval=1, maxval=30, group=groupDisplay)
 
showTFLabels = input.bool(true, "Show Timeframe Labels", group=groupDisplay)
 
labelPosition = input.string("Bottom", "Timeframe Label Position",
     options=["Top", "Bottom"], group=groupDisplay)
 
labelOffsetTicks = input.int(10, "Label Distance (Ticks)",
     minval=0, maxval=500, group=groupDisplay)
 
showCountdown = input.bool(true, "Show Countdown Timers", group=groupDisplay)
 
showPriceLine = input.bool(false, "Show Current Price Line", group=groupDisplay)
 
//==================================================
// CANDLE COLORS
//==================================================
groupColors = "Candle Colors"
 
bullColor = input.color(color.rgb(32, 214, 128),
     "Bullish Candle", group=groupColors)
 
bearColor = input.color(color.rgb(242, 80, 100),
     "Bearish Candle", group=groupColors)
 
wickColorMode = input.string("Match Candle", "Wick Color",
     options=["Match Candle", "Custom"], group=groupColors)
 
customWickColor = input.color(color.gray,
     "Custom Wick Color", group=groupColors)
 
tfFontColor = input.color(color.black,
     "Timeframe Label Font Color", group=groupColors)
 
countdownFontColor = input.color(color.black,
     "Countdown Font Color", group=groupColors)
 
priceLineColor = input.color(color.gray,
     "Price Line Color", group=groupColors)
 
//==================================================
// 8 LEVEL FTFC SETTINGS
//==================================================
groupFTFC = "8 Level STRAT FTFC Settings"
 
showFTFC = input.bool(true, "Show 8 Level FTFC", group=groupFTFC)
 
ftfcPosition = input.string("Middle Left", "FTFC Position",
     options=["Top Left", "Top Center", "Top Right",
              "Middle Left", "Middle Center", "Middle Right",
              "Bottom Left", "Bottom Center", "Bottom Right"],
     group=groupFTFC)
 
ftfcTextSize = input.string("Large", "FTFC Font Size",
     options=["Tiny", "Small", "Normal", "Large", "Huge"],
     group=groupFTFC)
 
ftfcBullColor = input.color(color.rgb(76, 175, 80),
     "FTFC Bullish", group=groupFTFC)
 
ftfcBearColor = input.color(color.rgb(255, 82, 82),
     "FTFC Bearish", group=groupFTFC)
 
ftfcInsideColor = input.color(color.yellow,
     "FTFC Inside Bar (1)", group=groupFTFC)
 
ftfcOutsideColor = input.color(color.yellow,
     "FTFC Outside Bar (3)", group=groupFTFC)
 
ftfcNeutralColor = input.color(color.gray,
     "FTFC Neutral", group=groupFTFC)
 
ftfcBgColor = input.color(color.new(color.black, 100),
     "FTFC Background", group=groupFTFC)
 
//==================================================
// EIGHT INDIVIDUAL FTFC TIMEFRAMES
//==================================================
groupFTFCTF = "FTFC Timeframes"
 
enableF1 = input.bool(true, "Show 15M", group=groupFTFCTF)
enableF2 = input.bool(true, "Show 30M", group=groupFTFCTF)
enableF3 = input.bool(true, "Show 1H", group=groupFTFCTF)
enableF4 = input.bool(true, "Show 4H", group=groupFTFCTF)
enableF5 = input.bool(true, "Show Daily", group=groupFTFCTF)
enableF6 = input.bool(true, "Show Weekly", group=groupFTFCTF)
enableF7 = input.bool(true, "Show Monthly", group=groupFTFCTF)
enableF8 = input.bool(true, "Show Quarterly", group=groupFTFCTF)
 
ftfcTF1 = input.timeframe("15", "FTFC 1", group=groupFTFCTF)
ftfcTF2 = input.timeframe("30", "FTFC 2", group=groupFTFCTF)
ftfcTF3 = input.timeframe("60", "FTFC 3", group=groupFTFCTF)
ftfcTF4 = input.timeframe("240", "FTFC 4", group=groupFTFCTF)
ftfcTF5 = input.timeframe("D", "FTFC 5", group=groupFTFCTF)
ftfcTF6 = input.timeframe("W", "FTFC 6", group=groupFTFCTF)
ftfcTF7 = input.timeframe("M", "FTFC 7", group=groupFTFCTF)
ftfcTF8 = input.timeframe("3M", "FTFC 8", group=groupFTFCTF)
 
//==================================================
// HELPERS
//==================================================
f_position(pos) =>
    switch pos
        "Top Left" => position.top_left
        "Top Center" => position.top_center
        "Top Right" => position.top_right
        "Middle Left" => position.middle_left
        "Middle Center" => position.middle_center
        "Middle Right" => position.middle_right
        "Bottom Left" => position.bottom_left
        "Bottom Center" => position.bottom_center
        "Bottom Right" => position.bottom_right
        => position.middle_left
 
f_textSize(s) =>
    switch s
        "Tiny" => size.tiny
        "Small" => size.small
        "Large" => size.large
        "Huge" => size.huge
        => size.normal
 
f_tfName(tf) =>
    seconds = timeframe.in_seconds(tf)
    tf == "3M" ? "Q" :
     tf == "M" ? "M" :
     tf == "W" ? "W" :
     tf == "D" ? "D" :
     seconds < 3600 ? str.tostring(int(seconds / 60)) :
     seconds < 86400 ? (seconds == 3600 ? "H" : str.tostring(int(seconds / 3600)) + "H") :
     tf
 
f_candleTFName(tf) =>
    seconds = timeframe.in_seconds(tf)
    tf == "3M" ? "Q" :
     tf == "M" ? "M" :
     tf == "W" ? "W" :
     tf == "D" ? "D" :
     seconds < 3600 ? str.tostring(int(seconds / 60)) + "M" :
     seconds < 86400 ? str.tostring(int(seconds / 3600)) + "H" :
     tf
 
f_countdown(closeTime) =>
    remaining = na(closeTime) ? 0 : math.max(0, int((closeTime - timenow) / 1000))
    hours = int(math.floor(remaining / 3600))
    minutes = int(math.floor((remaining % 3600) / 60))
    seconds = remaining % 60
 
    hh = str.tostring(hours, "00")
    mm = str.tostring(minutes, "00")
    ss = str.tostring(seconds, "00")
 
    hours > 0 ? hh + ":" + mm + ":" + ss : mm + ":" + ss
 
//==================================================
// LIVE OHLC REQUESTS
//==================================================
[o1, h1, l1, c1, t1] = request.security(
     syminfo.tickerid, tf1,
     [open, high, low, close, time_close],
     lookahead=barmerge.lookahead_off)
 
[o2, h2, l2, c2, t2] = request.security(
     syminfo.tickerid, tf2,
     [open, high, low, close, time_close],
     lookahead=barmerge.lookahead_off)
 
[o3, h3, l3, c3, t3] = request.security(
     syminfo.tickerid, tf3,
     [open, high, low, close, time_close],
     lookahead=barmerge.lookahead_off)
 
//==================================================
// STRAT FTFC CLASSIFICATION
//==================================================
//  2 = 2U
// -2 = 2D
//  1 = Inside Bar
//  3 = Outside Bar
//==================================================
f_ftfcStrat() =>
    inside = high <= high[1] and low >= low[1]
    outside = high > high[1] and low < low[1]
    twoUp = high > high[1] and low >= low[1]
    twoDown = low < low[1] and high <= high[1]
 
    inside ? 1 :
     outside ? 3 :
     twoUp ? 2 :
     twoDown ? -2 : 0
 
//==================================================
// EIGHT FTFC REQUESTS
//==================================================
ftfc1 = request.security(syminfo.tickerid, ftfcTF1, f_ftfcStrat())
ftfc2 = request.security(syminfo.tickerid, ftfcTF2, f_ftfcStrat())
ftfc3 = request.security(syminfo.tickerid, ftfcTF3, f_ftfcStrat())
ftfc4 = request.security(syminfo.tickerid, ftfcTF4, f_ftfcStrat())
ftfc5 = request.security(syminfo.tickerid, ftfcTF5, f_ftfcStrat())
ftfc6 = request.security(syminfo.tickerid, ftfcTF6, f_ftfcStrat())
ftfc7 = request.security(syminfo.tickerid, ftfcTF7, f_ftfcStrat())
ftfc8 = request.security(syminfo.tickerid, ftfcTF8, f_ftfcStrat())
 
//==================================================
// LIVE CANDLE OBJECTS
//==================================================
var box[] bodies = array.new_box()
var line[] wicks = array.new_line()
var label[] tfLabels = array.new_label()
var label[] countdownLabels = array.new_label()
 
var line priceLine = na
 
if barstate.isfirst
    for i = 0 to 2
        array.push(bodies, box.new(bar_index, close, bar_index + 1, open))
        array.push(wicks, line.new(bar_index, high, bar_index, low))
 
        array.push(tfLabels, label.new(
             bar_index, low, "",
             style=label.style_none,
             textcolor=tfFontColor))
 
        array.push(countdownLabels, label.new(
             bar_index, close, "",
             style=label.style_none,
             textcolor=countdownFontColor,
             size=size.tiny))
 
//==================================================
// UPDATE LIVE CANDLES
//==================================================
f_updateCandle(idx, x, o, h, l, c, closeTime, tf) =>
    body = array.get(bodies, idx)
    wick = array.get(wicks, idx)
    tfLabel = array.get(tfLabels, idx)
    countdownLabel = array.get(countdownLabels, idx)
 
    candleColor = c >= o ? bullColor : bearColor
    selectedWickColor = wickColorMode == "Match Candle" ? candleColor : customWickColor
 
    leftX = x - candleWidth
    rightX = x + candleWidth
 
    topY = math.max(o, c)
    bottomY = math.min(o, c)
 
    if topY == bottomY
        topY += syminfo.mintick * 0.5
        bottomY -= syminfo.mintick * 0.5
 
    // CANDLE BODY
    box.set_left(body, leftX)
    box.set_right(body, rightX)
    box.set_top(body, topY)
    box.set_bottom(body, bottomY)
    box.set_bgcolor(body, showCandles ? candleColor : color.new(candleColor, 100))
    box.set_border_color(body, showCandles ? candleColor : color.new(candleColor, 100))
 
    // WICK
    line.set_xy1(wick, x, h)
    line.set_xy2(wick, x, l)
    line.set_color(wick, showCandles ? selectedWickColor : color.new(selectedWickColor, 100))
 
    // TIMEFRAME LABEL
    labelDistance = syminfo.mintick * labelOffsetTicks
    labelY = labelPosition == "Top" ? h + labelDistance : l - labelDistance
 
    label.set_x(tfLabel, x)
    label.set_y(tfLabel, labelY)
    label.set_text(tfLabel, showCandles and showTFLabels ? f_candleTFName(tf) : "")
    label.set_textcolor(tfLabel, tfFontColor)
 
    // COUNTDOWN
    label.set_x(countdownLabel, x)
    label.set_y(countdownLabel, (o + c) / 2)
    label.set_text(countdownLabel,
         showCandles and showCountdown ? f_countdown(closeTime) : "")
    label.set_textcolor(countdownLabel, countdownFontColor)
 
//==================================================
// FTFC DISPLAY HELPERS
//==================================================
f_ftfcColor(v) =>
    v == 2 ? ftfcBullColor :
     v == -2 ? ftfcBearColor :
     v == 1 ? ftfcInsideColor :
     v == 3 ? ftfcOutsideColor :
     ftfcNeutralColor
 
f_ftfcSymbol(v) =>
    v == 2 ? "⬆" :
     v == -2 ? "⬇" :
     v == 1 ? "1" :
     v == 3 ? "3" : "—"
 
//==================================================
// FTFC TABLE
//==================================================
var table ftfcTable = table.new(
     f_position(ftfcPosition),
     8, 2,
     border_width=0)
 
f_drawFTFC(col, tf, direction, enabled) =>
    active = enabled and showFTFC
    signalColor = f_ftfcColor(direction)
 
    header = active ? f_tfName(tf) : ""
    symbol = active ? f_ftfcSymbol(direction) : ""
 
    // TIMEFRAME LABEL
    table.cell(ftfcTable, col, 0, header,
         bgcolor=ftfcBgColor,
         text_color=signalColor,
         text_size=f_textSize(ftfcTextSize),
         text_halign=text.align_center)
 
    // LARGE ARROW OR STRAT NUMBER
    table.cell(ftfcTable, col, 1, symbol,
         bgcolor=ftfcBgColor,
         text_color=signalColor,
         text_size=size.huge,
         text_halign=text.align_center)
 
//==================================================
// DRAW EVERYTHING
//==================================================
if barstate.islast
    step = candleWidth * 2 + candleSpacing
 
    x1 = bar_index + rightOffset
    x2 = x1 + step
    x3 = x2 + step
 
    f_updateCandle(0, x1, o1, h1, l1, c1, t1, tf1)
    f_updateCandle(1, x2, o2, h2, l2, c2, t2, tf2)
    f_updateCandle(2, x3, o3, h3, l3, c3, t3, tf3)
 
    // FTFC TABLE
    f_drawFTFC(0, ftfcTF1, ftfc1, enableF1)
    f_drawFTFC(1, ftfcTF2, ftfc2, enableF2)
    f_drawFTFC(2, ftfcTF3, ftfc3, enableF3)
    f_drawFTFC(3, ftfcTF4, ftfc4, enableF4)
    f_drawFTFC(4, ftfcTF5, ftfc5, enableF5)
    f_drawFTFC(5, ftfcTF6, ftfc6, enableF6)
    f_drawFTFC(6, ftfcTF7, ftfc7, enableF7)
    f_drawFTFC(7, ftfcTF8, ftfc8, enableF8)
 
    // OPTIONAL PRICE LINE
    if showPriceLine
        if na(priceLine)
            priceLine := line.new(
                 bar_index, close,
                 x3 + candleWidth + 2, close,
                 style=line.style_dotted,
                 color=priceLineColor)
        else
            line.set_xy1(priceLine, bar_index, close)
            line.set_xy2(priceLine, x3 + candleWidth + 2, close)
            line.set_color(priceLine, priceLineColor)
    else
        if not na(priceLine)
            line.delete(priceLine)
            priceLine := na

← 返回列表