MK Levels: Day, Overnight, ORB and POC Levels

Mkandlapalli · study · 232 行 · 点赞 38 · 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/
// © Mkandlapalli. If you reuse any part of this code, credit the author and the original script,
// as TradingView's House Rules require.
//@version=6
indicator("MK Levels", shorttitle = "MK Levels", overlay = true, max_lines_count = 50, max_labels_count = 50)
// MK Levels - the key reference levels for futures day trading, drawn for the current day only.
// Day-session levels on intraday charts of 60 minutes or less; weekly, monthly and ATH on any timeframe. New York time. Works on any futures symbol (NQ, ES, GC and their micros).
//   PDH / PDL  High and low of the prior regular session (09:30-16:00 ET).
//   ONH / ONL  High and low of the overnight session (18:00-09:30 ET). Developing during the night, fixed at 09:30.
//   ORH / ORL  High and low of the opening range, the first 15 or 30 minutes after 09:30 ET (your choice). Shown 09:30-16:00.
//   POC        Price row with the most volume in the prior regular session. Built from this chart's own bars: each bar's
//              volume is placed at its HL2 price, in rows of N ticks. Lower timeframes give a more precise POC.
//   PWH / PWL  Prior week high and low.   PMH / PML  Prior month high and low.   ATH  All-time high of the symbol.
// Lines start at the 18:00 ET open of the current trading day, so older days stay off the chart.
// Every level comes from completed bars or completed periods: nothing repaints and no future data is used.
// Alerts: price crossing any level. Educational tool only; not financial advice.
//
// HOW TO USE AND CHANGE THE SETTINGS
// 1. Add it: Indicators > search "MK Levels" > click it. Best on a 1- to 15-minute futures chart.
// 2. Open the settings: double-click the "MK Levels" name at the top left of the chart, or hover it and click the gear icon.
//    Hover the (i) next to any setting to see what it does.
// 3. Inputs tab, "Levels": tick or untick each level group to show or hide it.
//      Opening range length: choose 15 min or 30 min.
//      POC row size: smaller = more precise POC, larger = smoother. About 4 ticks for ES/NQ, 2 for GC.
// 4. Inputs tab, "Style": colour of each level group, line width, price labels on/off, and how close two levels must be
//    to share one label (0.05% of price by default).
// 5. Alerts: click Alert (clock icon) > Condition: "MK Levels" > pick a level, for example "Cross prior-day high".
// 6. To reset everything: in the settings window, click Defaults (bottom left) > Reset settings.
// Line styles: solid = daily levels, dashed = weekly and monthly, dotted = all-time high.
 
const string TZ = "America/New_York"
 
// Day-session levels (prior day, overnight, opening range, POC) need an intraday chart of 60 minutes or less.
// On higher timeframes only the prior week, prior month and all-time high are drawn.
bool intra = timeframe.isintraday and timeframe.in_seconds() <= 3600
 
// ---------- inputs
string gL = "Levels"
bool showPD   = input.bool(true, "Prior-day high / low", group = gL, tooltip = "PDH / PDL: high and low of the prior regular session, 09:30-16:00 New York time. Intraday charts only.")
bool showON   = input.bool(true, "Overnight high / low", group = gL, tooltip = "ONH / ONL: high and low from 18:00 to 09:30 New York time. Updates live overnight and locks at 09:30. Intraday charts only.")
bool showPOC  = input.bool(true, "Prior-day POC", group = gL, tooltip = "POC: the price where the most volume traded in the prior regular session. Intraday charts only.")
bool showOR   = input.bool(true, "Opening range high / low", group = gL, tooltip = "ORH / ORL: high and low of the first 15 or 30 minutes after the 09:30 open (pick the length below). Shown 09:30-16:00.")
string orLen  = input.string("15 min", "Opening range length", options = ["15 min", "30 min"], group = gL, tooltip = "Range of the first 15 or 30 minutes after the 09:30 ET open. Needs a chart timeframe no longer than the range (for example 1, 3, 5 or 15 minutes).")
bool showPW   = input.bool(true, "Prior week high / low", group = gL, tooltip = "PWH / PWL: high and low of the last completed week. Works on any timeframe.")
bool showPM   = input.bool(true, "Prior month high / low", group = gL, tooltip = "PMH / PML: high and low of the last completed month. Works on any timeframe.")
bool showATH  = input.bool(true, "All-time high", group = gL, tooltip = "ATH: the highest price in this symbol's history on your data feed. Updates live if price makes a new high.")
int  rowTicks = input.int(4, "POC row size (ticks)", minval = 1, maxval = 100, group = gL, tooltip = "Height of each volume row. Use about 4 for ES/NQ and 2 for GC on a 1- to 5-minute chart.")
 
string gS = "Style"
color cPD     = input.color(#2962FF, "Prior-day color", group = gS)
color cON     = input.color(#FF9800, "Overnight color", group = gS)
color cPOC    = input.color(#E91E63, "POC color", group = gS)
color cOR     = input.color(#00BCD4, "Opening range color", group = gS)
color cPW     = input.color(#AB47BC, "Prior week color", group = gS)
color cPM     = input.color(#43A047, "Prior month color", group = gS)
color cATH    = input.color(#FFD600, "All-time high color", group = gS)
int   lineW   = input.int(1, "Line width", minval = 1, maxval = 4, group = gS, tooltip = "Thickness of every level line (1 = thin, 4 = thick).")
bool  showLbl = input.bool(true, "Labels with prices", group = gS, tooltip = "Price tags at the right edge of the chart, for example PDH 28860.25.")
float mergePct = input.float(0.05, "Merge labels closer than (% of price)", minval = 0, maxval = 1, step = 0.01, group = gS, tooltip = "Levels this close share one label so none are hidden. 0.05% is about 14 points on NQ, 3 on ES and 2 on GC.")
 
// ---------- sessions (New York time)
bool inRth    = not na(time(timeframe.period, "0930-1600", TZ))
bool inOn     = not na(time(timeframe.period, "1800-0930", TZ))
bool rthStart = inRth and not inRth[1]
bool rthEnd   = not inRth and inRth[1]
bool onStart  = inOn and not inOn[1]
 
// start of the current trading day (the 18:00 ET open): lines are drawn from here
var int dayStartIdx = 0
if onStart
    dayStartIdx := bar_index
 
float rowSize = rowTicks * syminfo.mintick
 
// ---------- regular session: high, low and volume by price row
var float rthH = na
var float rthL = na
var map<int, float> vp = map.new<int, float>()
var float pdH   = na
var float pdL   = na
var float pdPOC = na
 
if rthStart
    rthH := high
    rthL := low
    vp.clear()
else if inRth
    rthH := math.max(rthH, high)
    rthL := math.min(rthL, low)
 
if inRth
    int k = int(math.round(hl2 / rowSize))
    float prev = vp.contains(k) ? vp.get(k) : 0.0
    vp.put(k, prev + nz(volume))
 
if rthEnd and not na(rthH)
    pdH := rthH
    pdL := rthL
    float bestV = 0.0
    int bestK = na
    for key in vp.keys()
        float v = vp.get(key)
        if v > bestV
            bestV := v
            bestK := key
    pdPOC := na(bestK) ? na : bestK * rowSize
 
// ---------- overnight session: developing until 09:30, then fixed
var float onH  = na
var float onL  = na
var float onHf = na
var float onLf = na
 
if onStart
    onH := high
    onL := low
else if inOn
    onH := math.max(onH, high)
    onL := math.min(onL, low)
 
if rthStart and not na(onH)
    onHf := onH
    onLf := onL
 
float onHShow = inOn ? onH : onHf
float onLShow = inOn ? onL : onLf
 
// ---------- opening range: 09:30 plus 15 or 30 minutes, developing inside the window, fixed after it, cleared at 16:00
int    orMin  = orLen == "30 min" ? 30 : 15
string orSess = orMin == 30 ? "0930-1000" : "0930-0945"
bool   orOk   = timeframe.in_seconds() <= orMin * 60
bool   inOr   = not na(time(timeframe.period, orSess, TZ))
var float orH = na
var float orL = na
var int orStartIdx = 0
 
if inOr and not inOr[1]
    orH := high
    orL := low
    orStartIdx := bar_index
else if inOr
    orH := math.max(orH, high)
    orL := math.min(orL, low)
 
float orHShow = orOk and inRth ? orH : na
float orLShow = orOk and inRth ? orL : na
 
// ---------- prior week, prior month (completed periods only) and all-time high
float pwH = request.security(syminfo.tickerid, "W", high[1], lookahead = barmerge.lookahead_on)
float pwL = request.security(syminfo.tickerid, "W", low[1], lookahead = barmerge.lookahead_on)
float pmH = request.security(syminfo.tickerid, "M", high[1], lookahead = barmerge.lookahead_on)
float pmL = request.security(syminfo.tickerid, "M", low[1], lookahead = barmerge.lookahead_on)
// highest monthly high up to last month, then raised by any new high on this chart since
float athPrior = request.security(syminfo.tickerid, "M", ta.max(high)[1], lookahead = barmerge.lookahead_on)
var float ath = na
ath := math.max(nz(ath, high), high, nz(athPrior, high))
 
// ---------- drawing: current-day lines and right-edge labels, rebuilt on the last bar
var array<line>  lns  = array.new<line>()
var array<label> lbls = array.new<label>()
 
f_add(array<float> ys, array<string> ts, array<color> cs, array<int> xs, array<string> st, bool show, float y, string t, color c, int x, string s) =>
    if show and not na(y)
        ys.push(y)
        ts.push(t + " " + str.tostring(y, format.mintick))
        cs.push(c)
        xs.push(x)
        st.push(s)
 
f_draw(float y, string t, color c) =>
    label.new(bar_index + 3, y, t, style = label.style_label_left, color = color.new(c, 25), textcolor = color.white, size = size.normal)
 
if barstate.islast
    for l in lns
        l.delete()
    lns.clear()
    for l in lbls
        l.delete()
    lbls.clear()
    array<float>  ys = array.new<float>()
    array<string> ts = array.new<string>()
    array<color>  cs = array.new<color>()
    array<int>    xs = array.new<int>()
    array<string> st = array.new<string>()
    f_add(ys, ts, cs, xs, st, showATH, ath, "ATH", cATH, dayStartIdx, line.style_dotted)
    f_add(ys, ts, cs, xs, st, showPM, pmH, "PMH", cPM, dayStartIdx, line.style_dashed)
    f_add(ys, ts, cs, xs, st, showPW, pwH, "PWH", cPW, dayStartIdx, line.style_dashed)
    f_add(ys, ts, cs, xs, st, showPD and intra, pdH, "PDH", cPD, dayStartIdx, line.style_solid)
    f_add(ys, ts, cs, xs, st, showON and intra, onHShow, "ONH", cON, dayStartIdx, line.style_solid)
    f_add(ys, ts, cs, xs, st, showOR and intra, orHShow, "ORH" + str.tostring(orMin), cOR, orStartIdx, line.style_solid)
    f_add(ys, ts, cs, xs, st, showPOC and intra, pdPOC, "POC", cPOC, dayStartIdx, line.style_solid)
    f_add(ys, ts, cs, xs, st, showOR and intra, orLShow, "ORL" + str.tostring(orMin), cOR, orStartIdx, line.style_solid)
    f_add(ys, ts, cs, xs, st, showON and intra, onLShow, "ONL", cON, dayStartIdx, line.style_solid)
    f_add(ys, ts, cs, xs, st, showPD and intra, pdL, "PDL", cPD, dayStartIdx, line.style_solid)
    f_add(ys, ts, cs, xs, st, showPW, pwL, "PWL", cPW, dayStartIdx, line.style_dashed)
    f_add(ys, ts, cs, xs, st, showPM, pmL, "PML", cPM, dayStartIdx, line.style_dashed)
    if ys.size() > 0
        for i = 0 to ys.size() - 1
            lns.push(line.new(xs.get(i), ys.get(i), bar_index + 3, ys.get(i), color = cs.get(i), width = lineW, style = st.get(i)))
    if showLbl and ys.size() > 0
        array<int> idx = ys.sort_indices(order.descending)
        float tol = close * mergePct / 100
        float gy = na
        float prevY = na
        string txt = ""
        color gc = na
        for i in idx
            float y = ys.get(i)
            if na(prevY) or prevY - y > tol
                if not na(gy)
                    lbls.push(f_draw(gy, txt, gc))
                gy := y
                txt := ts.get(i)
                gc := cs.get(i)
            else
                txt := txt + "  ·  " + ts.get(i)
            prevY := y
        lbls.push(f_draw(gy, txt, gc))
 
// ---------- alerts
alertcondition(ta.cross(close, pdH), "Cross prior-day high", "{{ticker}} crossed the prior-day high")
alertcondition(ta.cross(close, pdL), "Cross prior-day low", "{{ticker}} crossed the prior-day low")
alertcondition(ta.cross(close, onHShow), "Cross overnight high", "{{ticker}} crossed the overnight high")
alertcondition(ta.cross(close, onLShow), "Cross overnight low", "{{ticker}} crossed the overnight low")
alertcondition(ta.cross(close, pdPOC), "Cross prior-day POC", "{{ticker}} crossed the prior-day POC")
alertcondition(not inOr and ta.cross(close, orHShow), "Cross opening range high", "{{ticker}} crossed the opening range high")
alertcondition(not inOr and ta.cross(close, orLShow), "Cross opening range low", "{{ticker}} crossed the opening range low")
alertcondition(ta.cross(close, pwH), "Cross prior week high", "{{ticker}} crossed the prior week high")
alertcondition(ta.cross(close, pwL), "Cross prior week low", "{{ticker}} crossed the prior week low")
alertcondition(ta.cross(close, pmH), "Cross prior month high", "{{ticker}} crossed the prior month high")
alertcondition(ta.cross(close, pmL), "Cross prior month low", "{{ticker}} crossed the prior month low")
alertcondition(ta.crossover(high, ath[1]), "New all-time high", "{{ticker}} made a new all-time high")

← 返回列表