AnadeA AATTOM Time Indicator v1.1

drfkn87 · study · 234 行 · 点赞 11 · TradingView 原页

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

Pine Script

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © AnadeA
//
// AnadeA AATTOM Time Indicator v1.1
// Marks the key time candles of the trading day (Asia, Frankfurt / London, New York) as boxes on intraday charts.
//   - Each box = the high-low of one time candle (1-minute candle by default), drawn the same on 1m, 5m or 15m charts.
//   - A box stays active until price leaves it and comes back ("hit"); then it turns dotted and its label shows the hit time.
//   - Yesterday's main New York candle is kept as two lines (high / low) that follow price.
//   - Times are read in the time zone you choose (default Asia/Tehran). Winter and summer lists are in the tooltip.
// Drawing tool only: it gives no signals and no trade advice.
 
//@version=6
indicator("AnadeA AATTOM Time Indicator v1.1", "AnadeA Time v1.1", overlay = true, max_boxes_count = 60, max_labels_count = 80, max_lines_count = 10)
 
// ───────────── Inputs ─────────────
string GT = "Time candles"
string tz      = input.string("Asia/Tehran", "Time zone of the times below", options = ["Asia/Tehran", "UTC", "Europe/London", "Europe/Berlin", "America/New_York", "Asia/Dubai", "Europe/Istanbul"], group = GT)
string tcList  = input.string("04:30,06:30,10:30,11:30,12:30,16:30,17:00,17:30,18:00,18:30,20:30", "Time candles", group = GT, tooltip = "Tehran time, US winter: 04:30,06:30,10:30,11:30,12:30,16:30,17:00,17:30,18:00,18:30,20:30 (main 18:30).\nTehran time, US summer: 04:30,06:30,09:30,10:30,11:30,15:30,16:00,16:30,17:00,17:30,19:30 (main 17:30; key 06:30,11:30,17:30).")
string tcBoldS = input.string("06:30,12:30,18:30", "Key candles (thicker border, bigger label)", group = GT)
string tcMainS = input.string("18:30", "Main NY candle (kept as yesterday's two lines)", group = GT)
 
string G6 = "Boxes"
string tcTF       = input.timeframe("1", "Box candle size", group = G6, tooltip = "1 = 1-minute time candles. Works on any chart up to 1H.")
bool   tcNeedLeave = input.bool(true, "Hit = price must leave the box first, then come back", group = G6)
float  tcLeaveMin = input.float(0.0, "...leaving by at least this much price (0 = any)", minval = 0, step = 0.1, group = G6)
int    tcExt      = input.int(3, "Active boxes reach this many bars past the last bar", minval = 0, maxval = 100, group = G6)
bool   tcLabels   = input.bool(true, "Time labels", group = G6)
bool   tcKeepFill = input.bool(false, "Hit boxes keep their full fill (off = faded / outline)", group = G6)
bool   tcHitExt   = input.bool(true, "Hit boxes keep reaching the right edge (off = box ends at the hit)", group = G6)
bool   tcHitFill  = input.bool(true, "Hit boxes get a faded fill (off = dotted outline only)", group = G6)
int    tcFadePct  = input.int(40, "How faded the fill of hit boxes is (%)", minval = 0, maxval = 100, group = G6)
int    tcHitW     = input.int(2, "Border width of hit boxes (dotted)", minval = 1, maxval = 4, group = G6)
bool   tcPin      = input.bool(true, "Labels of boxes / lines that start off-screen stay in the view", group = G6)
bool   showY      = input.bool(true, "Yesterday's main candle lines", group = G6)
 
string G7 = "Colours (session hours in the chosen time zone)"
color tcAsiaF = input.color(color.new(#B0E2D6, 60), "Asia (before 09:00) fill", group = G7, inline = "a")
color tcAsiaL = input.color(#00796B, "border", group = G7, inline = "a")
color tcEuF   = input.color(color.new(#BECDF5, 60), "Frankfurt / London (09:00-15:29) fill", group = G7, inline = "e")
color tcEuL   = input.color(#2846AA, "border", group = G7, inline = "e")
color tcNyF   = input.color(color.new(#FCDDAA, 60), "New York (15:30-19:59) fill", group = G7, inline = "n")
color tcNyL   = input.color(#C46400, "border", group = G7, inline = "n")
color tcMainF = input.color(color.new(#F8C4C8, 60), "Main NY candle fill", group = G7, inline = "m")
color tcMainL = input.color(#BE143C, "border", group = G7, inline = "m")
color tcLateF = input.color(color.new(#E2CEF0, 60), "Late NY (20:00 on) fill", group = G7, inline = "l")
color tcLateL = input.color(#783296, "border", group = G7, inline = "l")
color tcYCol  = input.color(#BE143C, "Yesterday's main candle lines", group = G7, inline = "y")
int   tcYW    = input.int(2, "width", minval = 1, maxval = 5, group = G7, inline = "y")
 
// ───────────── Time list ─────────────
f_min(string s) =>
    array<string> p = str.split(s, ":")
    int r = -1
    if array.size(p) == 2
        float h = str.tonumber(array.get(p, 0))
        float m = str.tonumber(array.get(p, 1))
        if not na(h) and not na(m) and h >= 0 and h < 24 and m >= 0 and m < 60
            r := int(h) * 60 + int(m)
    r
 
f_hm(int m) =>
    str.format("{0,number,00}:{1,number,00}", math.floor(m / 60), m % 60)
 
var array<int>    tcM   = array.new<int>()
var array<string> tcT   = array.new<string>()
var array<bool>   tcB   = array.new<bool>()
var int           tcMainM = f_min(str.replace_all(tcMainS, " ", ""))
if barstate.isfirst
    array<int> boldM = array.new<int>()
    for s in str.split(str.replace_all(tcBoldS, " ", ""), ",")
        array.push(boldM, f_min(s))
    for s in str.split(str.replace_all(tcList, " ", ""), ",")
        int m = f_min(s)
        if m >= 0
            array.push(tcM, m)
            array.push(tcT, f_hm(m))
            array.push(tcB, array.includes(boldM, m))
 
// ───────────── Time-candle boxes ─────────────
// Built from the box-size candles (1m by default) inside each chart bar, so the box is the same on 1m, 5m or 15m charts.
int    tcLenMin = math.max(1, int(timeframe.in_seconds(tcTF) / 60))
string tfReq    = timeframe.in_seconds(timeframe.period) <= timeframe.in_seconds(tcTF) ? timeframe.period : tcTF
[sT, sH, sL] = request.security_lower_tf(syminfo.tickerid, tfReq, [time, high, low])
 
var array<float> cHi   = array.new<float>()
var array<float> cLo   = array.new<float>()
var array<int>   cBar  = array.new<int>()
var array<bool>  cDone = array.new<bool>()
var array<int>   cSide = array.new<int>()
var array<int>   cHitB = array.new<int>()
var array<int>   cHitT = array.new<int>()
var array<box>   cBox  = array.new<box>()
var array<label> cLbl  = array.new<label>()
if barstate.isfirst and array.size(tcM) > 0
    for i = 0 to array.size(tcM) - 1
        array.push(cHi, na), array.push(cLo, na), array.push(cBar, na), array.push(cDone, false)
        array.push(cSide, 0), array.push(cHitB, na), array.push(cHitT, na)
        array.push(cBox, box(na)), array.push(cLbl, label(na))
 
// today's main candle (followed even if it is not in the list) and yesterday's copy
var float mHi = na
var float mLo = na
var int   mBar = na
var float yHi = na
var float yLo = na
var int   yBar = na
var line  yLH = na
var line  yLL = na
var label yTH = na
var label yTL = na
 
int  N        = array.size(tcM)
bool tcActive = timeframe.isintraday and timeframe.in_seconds(timeframe.period) <= 3600
bool tcHitNow = false
 
if tcActive
    if timeframe.change("D")                                   // new trading day: today's main -> yesterday, old boxes removed
        if not na(mHi)
            yHi := mHi, yLo := mLo, yBar := mBar
        mHi := na, mLo := na, mBar := na
        if N > 0
            for k = 0 to N - 1
                box.delete(array.get(cBox, k))
                label.delete(array.get(cLbl, k))
                array.set(cBox, k, box(na)), array.set(cLbl, k, label(na))
                array.set(cHi, k, na), array.set(cLo, k, na), array.set(cBar, k, na)
                array.set(cDone, k, false), array.set(cSide, k, 0)
                array.set(cHitB, k, na), array.set(cHitT, k, na)
 
    int nSub = array.size(sT)
    if nSub > 0
        for j = 0 to nSub - 1
            int   t = array.get(sT, j)
            float h = array.get(sH, j)
            float l = array.get(sL, j)
            int   m = hour(t, tz) * 60 + minute(t, tz)
            if tcMainM >= 0 and m >= tcMainM and m < tcMainM + tcLenMin
                mHi  := na(mHi) ? h : math.max(mHi, h)
                mLo  := na(mLo) ? l : math.min(mLo, l)
                mBar := na(mBar) ? time : mBar
            if N > 0
                for k = 0 to N - 1
                    int tm = array.get(tcM, k)
                    float hi = array.get(cHi, k)
                    if m >= tm and m < tm + tcLenMin
                        array.set(cHi, k, na(hi) ? h : math.max(hi, h))
                        array.set(cLo, k, na(array.get(cLo, k)) ? l : math.min(array.get(cLo, k), l))
                        if na(array.get(cBar, k))
                            array.set(cBar, k, time)
                    else if not na(hi) and m >= tm + tcLenMin and na(array.get(cHitB, k))
                        array.set(cDone, k, true)
                        float lo   = array.get(cLo, k)
                        int   side = tcNeedLeave ? array.get(cSide, k) : 2
                        if side == 0
                            if l > hi + tcLeaveMin
                                array.set(cSide, k, 1)
                            else if h < lo - tcLeaveMin
                                array.set(cSide, k, -1)
                        else
                            bool touch = (side == 1 and l <= hi) or (side == -1 and h >= lo) or (side == 2 and h >= lo and l <= hi)
                            if touch
                                array.set(cHitB, k, time)
                                array.set(cHitT, k, t)
                                tcHitNow := true
 
// colour c faded pct % (more transparent)
f_fade(color c, int pct) =>
    float t0 = color.t(c)
    color.new(c, math.min(100, t0 + (100 - t0) * pct / 100))
 
// drawing (last bar only: only today's boxes exist)
if tcActive and barstate.islast
    if N > 0
        for k = 0 to N - 1
            float hi = array.get(cHi, k)
            if not na(hi)
                float lo   = array.get(cLo, k)
                int   tm   = array.get(tcM, k)
                int   b0   = array.get(cBar, k)
                int   hb   = array.get(cHitB, k)
                bool  hit  = not na(hb)
                bool  key  = array.get(tcB, k)
                color fc = tm == tcMainM ? tcMainF : tm < 540 ? tcAsiaF : tm < 930 ? tcEuF : tm < 1200 ? tcNyF : tcLateF
                color lc = tm == tcMainM ? tcMainL : tm < 540 ? tcAsiaL : tm < 930 ? tcEuL : tm < 1200 ? tcNyL : tcLateL
                int   x2 = hit and not tcHitExt ? math.max(hb, b0) : time + tcExt * timeframe.in_seconds() * 1000
                color bg = not hit or tcKeepFill ? fc : tcHitFill ? f_fade(fc, tcFadePct) : color(na)
                box bx = array.get(cBox, k)
                if na(bx)
                    bx := box.new(b0, hi, x2, lo, xloc = xloc.bar_time)
                    array.set(cBox, k, bx)
                box.set_lefttop(bx, b0, hi)
                box.set_rightbottom(bx, x2, lo)
                box.set_bgcolor(bx, bg)
                box.set_border_color(bx, lc)
                box.set_border_width(bx, hit ? tcHitW : key ? 2 : 1)
                box.set_border_style(bx, hit ? line.style_dotted : line.style_solid)
                if tcLabels
                    string txt = array.get(tcT, k) + (not array.get(cDone, k) ? "  forming" : hit ? "  hit " + str.format_time(array.get(cHitT, k), "HH:mm", tz) : "")
                    string tip = array.get(tcT, k) + " candle  " + str.tostring(lo, format.mintick) + " - " + str.tostring(hi, format.mintick)
                    int xl = b0
                    if tcPin and chart.left_visible_bar_time > b0 and chart.left_visible_bar_time < x2
                        xl := chart.left_visible_bar_time
                        txt := "< " + txt
                    label lb = array.get(cLbl, k)
                    if na(lb)
                        lb := label.new(b0, hi, txt, xloc = xloc.bar_time, style = label.style_label_lower_left, color = color(na))
                        array.set(cLbl, k, lb)
                    label.set_xy(lb, xl, hi)
                    label.set_text(lb, txt)
                    label.set_tooltip(lb, tip)
                    label.set_textcolor(lb, lc)
                    label.set_size(lb, key ? size.normal : size.small)
    // yesterday's main candle = two lines that follow price
    if showY and not na(yHi)
        int    x2 = time + tcExt * timeframe.in_seconds() * 1000
        string mt = f_hm(tcMainM)
        if na(yLH)
            yLH := line.new(yBar, yHi, x2, yHi, xloc = xloc.bar_time, color = tcYCol, width = tcYW)
            yLL := line.new(yBar, yLo, x2, yLo, xloc = xloc.bar_time, color = tcYCol, width = tcYW)
            yTH := label.new(x2, yHi, "", xloc = xloc.bar_time, style = label.style_label_lower_left, color = color(na), textcolor = tcYCol, size = size.small)
            yTL := label.new(x2, yLo, "", xloc = xloc.bar_time, style = label.style_label_upper_left, color = color(na), textcolor = tcYCol, size = size.small)
        line.set_xy1(yLH, yBar, yHi), line.set_xy2(yLH, x2, yHi)
        line.set_xy1(yLL, yBar, yLo), line.set_xy2(yLL, x2, yLo)
        int  tx  = x2
        bool pin = tcPin and chart.right_visible_bar_time < x2
        if pin
            tx := math.max(yBar, chart.right_visible_bar_time - 2 * timeframe.in_seconds() * 1000)
        label.set_style(yTH, pin ? label.style_label_lower_right : label.style_label_lower_left)
        label.set_style(yTL, pin ? label.style_label_upper_right : label.style_label_upper_left)
        label.set_xy(yTH, tx, yHi), label.set_text(yTH, "Y " + mt + " high " + str.tostring(yHi, format.mintick))
        label.set_xy(yTL, tx, yLo), label.set_text(yTL, "Y " + mt + " low " + str.tostring(yLo, format.mintick))
 
// ───────────── Alert ─────────────
alertcondition(tcHitNow, "Time-candle box hit", "AnadeA Time: price came back to a time-candle box on {{ticker}} {{interval}}")

← 返回列表