MoM LEVELS

MarkLeRoy · study · 228 行 · 点赞 42 · TradingView 原页

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

Pine Script

// This Pine Script® code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © MarkLeRoy
 
//@version=6
indicator("MoM LEVELS", overlay=true, max_lines_count=500)
 
// ============================================================================
// INPUTS
// ============================================================================
grpPD = "Previous Day"
showPDH = input.bool(true,  "Previous Day High", group=grpPD)
showPDL = input.bool(true,  "Previous Day Low",  group=grpPD)
lblPDH  = input.string("PDH", "Text: Prev Day High", group=grpPD)
lblPDL  = input.string("PDL", "Text: Prev Day Low",  group=grpPD)
 
grpAsia = "Asia Session"
showAsia = input.bool(true, "Asia High / Low", group=grpAsia)
asiaSess = input.session("1800-0000", "Asia Session (exch time)", group=grpAsia)
lblAsiaH = input.string("Asia High", "Text: Asia High", group=grpAsia)
lblAsiaL = input.string("Asia Low",  "Text: Asia Low",  group=grpAsia)
 
grpLon = "London Session"
showLon = input.bool(true, "London High / Low", group=grpLon)
lonSess = input.session("0700-1000", "London Session (exch time)", group=grpLon)
lblLonH = input.string("London High", "Text: London High", group=grpLon)
lblLonL = input.string("London Low",  "Text: London Low",  group=grpLon)
 
grpNY = "New York Session"
showNY = input.bool(true, "New York High / Low", group=grpNY)
nySess = input.session("1330-1600", "New York Session (exch time)", group=grpNY)
lblNyH = input.string("NY High", "Text: NY High", group=grpNY)
lblNyL = input.string("NY Low",  "Text: NY Low",  group=grpNY)
 
grpFlip = "Daily Flip Line"
showFlip  = input.bool(true, "Show Daily Flip Line", group=grpFlip)
flipStyle = input.string("Dashed", "Flip Line Style", options=["Solid", "Dashed", "Dotted"], group=grpFlip)
flipWidth = input.int(2, "Flip Line Width", minval=1, maxval=5, group=grpFlip)
lblBullFlip = input.string("Bull Flip", "Text: bullish daily", group=grpFlip)
lblBearFlip = input.string("Bear Flip", "Text: bearish daily", group=grpFlip)
 
grpText = "Text Display"
showText  = input.bool(true, "Show Level Text", group=grpText)
textColor = input.color(color.new(color.black, 0), "Text Colour", group=grpText)
textSize  = input.string("Small", "Text Size", options=["Tiny", "Small", "Normal", "Large"], group=grpText)
// Padding: how far RIGHT of the current (live) candle the text column sits.
padBars   = input.int(20, "Padding (bars right of live candle)", minval=0, maxval=300, group=grpText)
alignMode = input.string("Left", "Text Align", options=["Left", "Right"], group=grpText)
 
grpStyle = "Style"
lineWidth = input.int(1, "Level Line Width", minval=1, maxval=5, group=grpStyle)
lineStyle = input.string("Dashed", "Level Line Style", options=["Solid", "Dashed", "Dotted"], group=grpStyle)
colPDH   = input.color(color.new(color.blue, 0),  "Prev Day High", group=grpStyle)
colPDL   = input.color(color.new(color.blue, 0),  "Prev Day Low",  group=grpStyle)
colAsiaH = input.color(color.new(color.orange, 0), "Asia High", group=grpStyle)
colAsiaL = input.color(color.new(color.orange, 0), "Asia Low",  group=grpStyle)
colLonH  = input.color(color.new(color.purple, 0), "London High", group=grpStyle)
colLonL  = input.color(color.new(color.purple, 0), "London Low",  group=grpStyle)
colNYH   = input.color(color.new(color.red, 0),    "New York High", group=grpStyle)
colNYL   = input.color(color.new(color.red, 0),    "New York Low",  group=grpStyle)
colBullFlip = input.color(color.new(color.green, 0), "Flip line (bullish daily)", group=grpStyle)
colBearFlip = input.color(color.new(color.red, 0),   "Flip line (bearish daily)", group=grpStyle)
 
// ============================================================================
// HELPERS
// ============================================================================
styleOf(string s) =>
    s == "Solid" ? line.style_solid : s == "Dashed" ? line.style_dashed : line.style_dotted
 
sizeOf(string s) =>
    s == "Tiny" ? size.tiny : s == "Small" ? size.small : s == "Normal" ? size.normal : size.large
 
lvlStyle = styleOf(lineStyle)
flStyle  = styleOf(flipStyle)
txtSize  = sizeOf(textSize)
 
// ---------------------------------------------------------------------------
// SHARED RIGHT EDGE
// All lines end here and all text sits here, so everything lines up in one
// vertical column at a fixed distance from the live candle.
// ---------------------------------------------------------------------------
rightEdge = bar_index + padBars
 
txtStyle = alignMode == "Left" ? label.style_label_left : label.style_label_right
 
drawText(label lb, float lvl, string txt) =>
    label.delete(lb)
    if showText and not na(lvl) and txt != ""
        label.new(rightEdge, lvl, txt,
                  style=txtStyle, color=color.new(color.black, 100),
                  textcolor=textColor, size=txtSize)
    else
        label(na)
 
// ============================================================================
// PREVIOUS DAY HIGH / LOW
// ============================================================================
pdh = request.security(syminfo.tickerid, "D", high[1],  barmerge.gaps_off, barmerge.lookahead_on)
pdl = request.security(syminfo.tickerid, "D", low[1],   barmerge.gaps_off, barmerge.lookahead_on)
isNewDay = ta.change(time("D")) != 0
 
var line  lnPDH = na
var line  lnPDL = na
var label txPDH = na
var label txPDL = na
 
if showPDH and isNewDay
    lnPDH := line.new(bar_index, pdh, rightEdge, pdh, color=colPDH, width=lineWidth, style=lvlStyle)
    lnPDL := line.new(bar_index, pdl, rightEdge, pdl, color=colPDL, width=lineWidth, style=lvlStyle)
 
if not na(lnPDH)
    line.set_x2(lnPDH, rightEdge)
    line.set_y1(lnPDH, pdh)
    line.set_y2(lnPDH, pdh)
if not na(lnPDL)
    line.set_x2(lnPDL, rightEdge)
    line.set_y1(lnPDL, pdl)
    line.set_y2(lnPDL, pdl)
 
txPDH := drawText(txPDH, pdh, lblPDH)
txPDL := drawText(txPDL, pdl, lblPDL)
 
// ============================================================================
// SESSION HIGH / LOW  (Asia, London, NY)
// ============================================================================
inSess(string sess) => not na(time(timeframe.period, sess))
 
asiaIn = inSess(asiaSess)
lonIn  = inSess(lonSess)
nyIn   = inSess(nySess)
 
var float asiaH = na, var float asiaL = na
var float lonH  = na, var float lonL  = na
var float nyH   = na, var float nyL   = na
 
if asiaIn
    asiaH := na(asiaH) ? high : math.max(asiaH, high)
    asiaL := na(asiaL) ? low  : math.min(asiaL, low)
if lonIn
    lonH := na(lonH) ? high : math.max(lonH, high)
    lonL := na(lonL) ? low  : math.min(lonL, low)
if nyIn
    nyH := na(nyH) ? high : math.max(nyH, high)
    nyL := na(nyL) ? low  : math.min(nyL, low)
 
asiaStart = asiaIn  and not asiaIn[1]
lonStart  = lonIn   and not lonIn[1]
nyStart   = nyIn    and not nyIn[1]
asiaEnd   = not asiaIn and asiaIn[1]
lonEnd    = not lonIn  and lonIn[1]
nyEnd     = not nyIn   and nyIn[1]
 
var line  lnAsiaH = na, var line  lnAsiaL = na
var line  lnLonH  = na, var line  lnLonL  = na
var line  lnNyH   = na, var line  lnNyL   = na
var label txAsiaH = na, var label txAsiaL = na
var label txLonH  = na, var label txLonL  = na
var label txNyH   = na, var label txNyL   = na
 
if asiaStart
    asiaH := high, asiaL := low
if lonStart
    lonH := high, lonL := low
if nyStart
    nyH := high, nyL := low
 
if asiaEnd and showAsia
    lnAsiaH := line.new(bar_index, asiaH, rightEdge, asiaH, color=colAsiaH, width=lineWidth, style=lvlStyle)
    lnAsiaL := line.new(bar_index, asiaL, rightEdge, asiaL, color=colAsiaL, width=lineWidth, style=lvlStyle)
if lonEnd and showLon
    lnLonH := line.new(bar_index, lonH, rightEdge, lonH, color=colLonH, width=lineWidth, style=lvlStyle)
    lnLonL := line.new(bar_index, lonL, rightEdge, lonL, color=colLonL, width=lineWidth, style=lvlStyle)
if nyEnd and showNY
    lnNyH := line.new(bar_index, nyH, rightEdge, nyH, color=colNYH, width=lineWidth, style=lvlStyle)
    lnNyL := line.new(bar_index, nyL, rightEdge, nyL, color=colNYL, width=lineWidth, style=lvlStyle)
 
txAsiaH := drawText(txAsiaH, asiaH, lblAsiaH)
txAsiaL := drawText(txAsiaL, asiaL, lblAsiaL)
txLonH  := drawText(txLonH,  lonH,  lblLonH)
txLonL  := drawText(txLonL,  lonL,  lblLonL)
txNyH   := drawText(txNyH,   nyH,   lblNyH)
txNyL   := drawText(txNyL,   nyL,   lblNyL)
 
extend(line ln, float lvl) =>
    if not na(ln)
        line.set_x2(ln, rightEdge)
        line.set_y1(ln, lvl)
        line.set_y2(ln, lvl)
 
extend(lnAsiaH, asiaH)
extend(lnAsiaL, asiaL)
extend(lnLonH,  lonH)
extend(lnLonL,  lonL)
extend(lnNyH,   nyH)
extend(lnNyL,   nyL)
 
// ============================================================================
// DAILY FLIP LINE
// bullish daily -> red line at body bottom (open)
// bearish daily -> green line at body top (open)
// ============================================================================
dOpen  = request.security(syminfo.tickerid, "D", open,  barmerge.gaps_off, barmerge.lookahead_on)
dClose = request.security(syminfo.tickerid, "D", close, barmerge.gaps_off, barmerge.lookahead_on)
dBull  = dClose > dOpen
 
dFlip = dBull != dBull[1]
 
var line  lnFlip = na
var label txFlip = na
var color flipCol = na
 
if showFlip and dFlip
    flipCol := dBull ? colBearFlip : colBullFlip
    lnFlip  := line.new(bar_index, dOpen, rightEdge, dOpen, color=flipCol, width=flipWidth, style=flStyle)
    txFlip  := drawText(txFlip, dOpen, dBull ? lblBearFlip : lblBullFlip)
 
if not na(lnFlip)
    line.set_x2(lnFlip, rightEdge)
    line.set_y1(lnFlip, dOpen)
    line.set_y2(lnFlip, dOpen)
 
// keep the flip text tracking its label text even after the flip bar
txFlip := drawText(txFlip, dOpen, dBull ? lblBearFlip : lblBullFlip)
 
// ============================================================================
// ALERTS
// ============================================================================
alertcondition(isNewDay, "New Day", "New daily session started")
alertcondition(dFlip, "Daily Flip", "Daily candle flipped direction")

← 返回列表