Super Alwing

alwing3 · study · 361 行 · 点赞 25 · TradingView 原页

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

Pine Script

//@version=6
indicator("MNQ Session Liquidity V1.7.1", overlay=true, max_lines_count=500, max_labels_count=500, max_boxes_count=200)
 
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// 1. 设置
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
 
string TZ = "America/New_York"
 
groupSession = "Session Settings"
asiaSession   = input.session("2000-2200", "Asia", group=groupSession)
londonSession = input.session("2200-0000", "London", group=groupSession)
nyAmSession   = input.session("0930-1100", "New York AM", group=groupSession)
nyPmSession   = input.session("1330-1600", "New York PM", group=groupSession)
 
groupDisplay = "Display Settings"
showBoxes       = input.bool(true, "Show Session Boxes", group=groupDisplay)
showSessionNames = input.bool(true, "Show Session Names", group=groupDisplay)
showLevels      = input.bool(true, "Show High / Low Lines", group=groupDisplay)
showLabels      = input.bool(true, "Show Price Labels", group=groupDisplay)
showPDH_PDL     = input.bool(true, "Show Previous Day High / Low", group=groupDisplay)
historyDays     = input.int(14, "Historical Liquidity Days", minval=1, maxval=30, group=groupDisplay)
labelOffset     = input.int(8, "Right Label Offset", minval=1, maxval=50, group=groupDisplay)
 
groupColors = "Session Colors"
asiaColor   = input.color(color.rgb(220, 70, 110), "Asia", group=groupColors)
londonColor = input.color(color.rgb(0, 180, 210), "London", group=groupColors)
nyAmColor   = input.color(color.rgb(240, 145, 40), "New York AM", group=groupColors)
nyPmColor   = input.color(color.rgb(60, 120, 220), "New York PM", group=groupColors)
dailyColor  = input.color(color.gray, "PDH / PDL", group=groupColors)
 
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// 2. 纽约日期
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
 
f_dateKey(int t) =>
    year(t, TZ) * 10000 + month(t, TZ) * 100 + dayofmonth(t, TZ)
 
f_dateText(int key) =>
    int yy = int(math.floor(key / 10000))
    int mm = int(math.floor((key % 10000) / 100))
    int dd = key % 100
    str.tostring(yy) + "-" + str.tostring(mm, "00") + "-" + str.tostring(dd, "00")
 
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// 3. 流动性储存
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
 
var array<line> levelLines = array.new<line>()
var array<label> levelLabels = array.new<label>()
var array<float> levelPrices = array.new<float>()
var array<int> levelDates = array.new<int>()
var array<int> sweepDates = array.new<int>()
var array<bool> levelIsHigh = array.new<bool>()
var array<string> levelNames = array.new<string>()
 
var array<box> sessionBoxes = array.new<box>()
var array<label> sessionTitleLabels = array.new<label>()
var array<int> sessionObjectTimes = array.new<int>()
 
f_addLevel(float price, bool isHigh, string sessionName, int sessionDate, color col) =>
    string side = isHigh ? "High" : "Low"
    string labelText = sessionName + " " + side + " | " + f_dateText(sessionDate) + " | " + str.tostring(price, format.mintick)
 
    line ln = na
    label lb = na
 
    if showLevels
        ln := line.new(
             x1=bar_index,
             y1=price,
             x2=bar_index + 1,
             y2=price,
             xloc=xloc.bar_index,
             extend=extend.right,
             color=col,
             width=1)
 
    if showLabels
        lb := label.new(
             x=bar_index + labelOffset,
             y=price,
             text=labelText,
             xloc=xloc.bar_index,
             style=label.style_label_left,
             color=col,
             textcolor=color.white,
             size=size.normal)
 
    array.push(levelLines, ln)
    array.push(levelLabels, lb)
    array.push(levelPrices, price)
    array.push(levelDates, sessionDate)
    array.push(sweepDates, na)
    array.push(levelIsHigh, isHigh)
    array.push(levelNames, sessionName)
 
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// 4. Session 处理函数
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
 
f_session(string sessionTime, string sessionName, color col) =>
    bool inSession = not na(time(timeframe.period, sessionTime, TZ))
    bool wasInSession = inSession[1]
 
    bool sessionStart = inSession and not wasInSession
    bool sessionEnd = not inSession and wasInSession
 
    var float sessionHigh = na
    var float sessionLow = na
    var int sessionStartBar = na
    var int sessionDate = na
    var box activeBox = na
    var label activeTitle = na
 
    if sessionStart
        sessionHigh := high
        sessionLow := low
        sessionStartBar := bar_index
        sessionDate := f_dateKey(time)
 
        activeBox := na
        activeTitle := na
 
        if showBoxes
            activeBox := box.new(
                 left=bar_index,
                 top=sessionHigh,
                 right=bar_index,
                 bottom=sessionLow,
                 xloc=xloc.bar_index,
                 border_color=col,
                 border_width=1,
                 bgcolor=color.new(col, 85))
 
        if showSessionNames
            activeTitle := label.new(
                 x=bar_index,
                 y=sessionHigh,
                 text=sessionName,
                 xloc=xloc.bar_index,
                 style=label.style_label_down,
                 color=col,
                 textcolor=color.white,
                 size=size.normal)
 
        array.push(sessionBoxes, activeBox)
        array.push(sessionTitleLabels, activeTitle)
        array.push(sessionObjectTimes, time)
 
    if inSession
        sessionHigh := na(sessionHigh) ? high : math.max(sessionHigh, high)
        sessionLow := na(sessionLow) ? low : math.min(sessionLow, low)
 
        int titleMid = int(math.round((sessionStartBar + bar_index) / 2.0))
 
        if showBoxes and not na(activeBox)
            box.set_right(activeBox, bar_index)
            box.set_top(activeBox, sessionHigh)
            box.set_bottom(activeBox, sessionLow)
 
        if showSessionNames and not na(activeTitle)
            label.set_xy(activeTitle, titleMid, sessionHigh)
            label.set_text(activeTitle, sessionName)
 
    if sessionEnd
        if not na(sessionHigh) and not na(sessionLow)
            f_addLevel(sessionHigh, true, sessionName, sessionDate, col)
            f_addLevel(sessionLow, false, sessionName, sessionDate, col)
 
        activeBox := na
        activeTitle := na
        sessionHigh := na
        sessionLow := na
        sessionStartBar := na
        sessionDate := na
 
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// 5. 执行四个 Session
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
 
f_session(asiaSession, "Asia", asiaColor)
f_session(londonSession, "London", londonColor)
f_session(nyAmSession, "NY AM", nyAmColor)
f_session(nyPmSession, "NY PM", nyPmColor)
 
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// 6. PDH / PDL
// 上一根完整 TradingView Daily K 线
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
 
float prevDayHigh = request.security(
     syminfo.tickerid,
     "D",
     high[1],
     gaps=barmerge.gaps_off,
     lookahead=barmerge.lookahead_on)
 
float prevDayLow = request.security(
     syminfo.tickerid,
     "D",
     low[1],
     gaps=barmerge.gaps_off,
     lookahead=barmerge.lookahead_on)
 
var line pdhLine = na
var line pdlLine = na
var label pdhLabel = na
var label pdlLabel = na
 
bool newDailyBar = timeframe.change("D")
 
if showPDH_PDL and not na(prevDayHigh) and not na(prevDayLow)
    if na(pdhLine) or newDailyBar
        if not na(pdhLine)
            line.delete(pdhLine)
        if not na(pdlLine)
            line.delete(pdlLine)
        if not na(pdhLabel)
            label.delete(pdhLabel)
        if not na(pdlLabel)
            label.delete(pdlLabel)
 
        pdhLine := line.new(
             bar_index, prevDayHigh,
             bar_index + 1, prevDayHigh,
             xloc=xloc.bar_index,
             extend=extend.right,
             color=dailyColor,
             width=2,
             style=line.style_dashed)
 
        pdlLine := line.new(
             bar_index, prevDayLow,
             bar_index + 1, prevDayLow,
             xloc=xloc.bar_index,
             extend=extend.right,
             color=dailyColor,
             width=2,
             style=line.style_dashed)
 
        if showLabels
            pdhLabel := label.new(
                 bar_index + labelOffset,
                 prevDayHigh,
                 "PDH | " + str.tostring(prevDayHigh, format.mintick),
                 xloc=xloc.bar_index,
                 style=label.style_label_left,
                 color=dailyColor,
                 textcolor=color.white,
                 size=size.normal)
 
            pdlLabel := label.new(
                 bar_index + labelOffset,
                 prevDayLow,
                 "PDL | " + str.tostring(prevDayLow, format.mintick),
                 xloc=xloc.bar_index,
                 style=label.style_label_left,
                 color=dailyColor,
                 textcolor=color.white,
                 size=size.normal)
 
    if not na(pdhLine)
        line.set_y1(pdhLine, prevDayHigh)
        line.set_y2(pdhLine, prevDayHigh)
 
    if not na(pdlLine)
        line.set_y1(pdlLine, prevDayLow)
        line.set_y2(pdlLine, prevDayLow)
 
    if not na(pdhLabel)
        label.set_xy(pdhLabel, bar_index + labelOffset, prevDayHigh)
        label.set_text(pdhLabel, "PDH | " + str.tostring(prevDayHigh, format.mintick))
 
    if not na(pdlLabel)
        label.set_xy(pdlLabel, bar_index + labelOffset, prevDayLow)
        label.set_text(pdlLabel, "PDL | " + str.tostring(prevDayLow, format.mintick))
 
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// 7. Sweep 检测及清理
// High: high > level
// Low: low < level
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
 
int todayKey = f_dateKey(time)
 
if array.size(levelPrices) > 0
    for i = array.size(levelPrices) - 1 to 0
        float price = array.get(levelPrices, i)
        bool isHigh = array.get(levelIsHigh, i)
        int originalDate = array.get(levelDates, i)
        int sweptDate = array.get(sweepDates, i)
 
        bool crossed = isHigh ? high > price : low < price
 
        if na(sweptDate) and crossed
            array.set(sweepDates, i, todayKey)
            sweptDate := todayKey
 
        bool expiredBySweep = not na(sweptDate) and todayKey > sweptDate
 
        int originalYear = int(math.floor(originalDate / 10000))
        int originalMonth = int(math.floor((originalDate % 10000) / 100))
        int originalDay = originalDate % 100
        int originalTime = timestamp(TZ, originalYear, originalMonth, originalDay, 0, 0)
 
        bool expiredByAge = time - originalTime > historyDays * 86400000
 
        if expiredBySweep or expiredByAge
            line oldLine = array.get(levelLines, i)
            label oldLabel = array.get(levelLabels, i)
 
            if not na(oldLine)
                line.delete(oldLine)
            if not na(oldLabel)
                label.delete(oldLabel)
 
            array.remove(levelLines, i)
            array.remove(levelLabels, i)
            array.remove(levelPrices, i)
            array.remove(levelDates, i)
            array.remove(sweepDates, i)
            array.remove(levelIsHigh, i)
            array.remove(levelNames, i)
 
        else
            label currentLabel = array.get(levelLabels, i)
            if not na(currentLabel)
                label.set_x(currentLabel, bar_index + labelOffset)
 
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// 8. Session Box 和名称历史清理
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
 
if array.size(sessionObjectTimes) > 0
    for i = array.size(sessionObjectTimes) - 1 to 0
        int createdTime = array.get(sessionObjectTimes, i)
 
        if time - createdTime > historyDays * 86400000
            box oldBox = array.get(sessionBoxes, i)
            label oldTitle = array.get(sessionTitleLabels, i)
 
            if not na(oldBox)
                box.delete(oldBox)
 
            if not na(oldTitle)
                label.delete(oldTitle)
 
            array.remove(sessionBoxes, i)
            array.remove(sessionTitleLabels, i)
            array.remove(sessionObjectTimes, i)
 
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// 9. PDH / PDL 标签位置更新
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
 
if showPDH_PDL
    if not na(pdhLabel)
        label.set_x(pdhLabel, bar_index + labelOffset)
    if not na(pdlLabel)
        label.set_x(pdlLabel, bar_index + labelOffset)

← 返回列表