All-In-One Sessions, Weekly, Monday, Previous Highs/Lows

Texmoonbeam · study · 277 行 · 点赞 5,090 · 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/
// // © Texmoonbeam
 
//@version=5
indicator("All-In-One Sessions, Weekly, Monday, Previous Highs/Lows", overlay = true, max_lines_count = 500, max_labels_count = 500, max_bars_back=150)
//timeframe = input.timeframe(defval = '240')
 
ShowMonday = input.bool(defval = true, title="Show Monday High/Low", group="Mondays & Weekly", inline="1")
ExtendMonday = input.bool(defval = false, title="Extend?", group="Mondays & Weekly", inline="1a")
LabelMonday = input.bool(defval = true, title="Label?", group="Mondays & Weekly", inline="1b")
MonHighCol = input.color(color.green, "High", group="Mondays & Weekly", inline="1c")
MonHighWidth = input.int(defval=1, title="Width", minval=1, maxval = 4, group="Mondays & Weekly", inline="1c")
MonLowCol = input.color(color.red, "Low", group = "Mondays & Weekly", inline="1c")
MonLowWidth = input.int(defval=1, title="Width", minval=1, maxval = 4, group="Mondays & Weekly", inline="1c")
 
ShowPrevDay = input.bool(defval = true, title="Show Prev Day High/Low", group="Mondays & Weekly", inline="2")
ExtendPrevDay = input.bool(defval = false, title="Extend?", group="Mondays & Weekly", inline="2a")
LabelPrevDay = input.bool(defval = true, title="Label?", group="Mondays & Weekly", inline="2b")
PrevDayHighCol = input.color(color.green, "High", group="Mondays & Weekly", inline="2c")
PrevDayHighWidth = input.int(defval=1, title="Width", minval=1, maxval = 4, group="Mondays & Weekly", inline="2c")
PrevDayLowCol = input.color(color.red, "Low", group = "Mondays & Weekly", inline="2c")
PrevDayLowWidth = input.int(defval=1, title="Width", minval=1, maxval = 4, group="Mondays & Weekly", inline="2c")
 
ShowCurWeek = input.bool(defval = false, title="Show Current Week High/Low", group="Mondays & Weekly", inline="3")
ExtendCurWeek = input.bool(defval = false, title="Extend?", group="Mondays & Weekly", inline="3a")
LabelCurWeek = input.bool(defval = false, title="Label?", group="Mondays & Weekly", inline="3b")
CurWeekHighCol = input.color(color.green, "High", group="Mondays & Weekly", inline="3c")
CurWeekHighWidth = input.int(defval=1, title="Width", minval=1, maxval = 4, group="Mondays & Weekly", inline="3c")
CurWeekLowCol = input.color(color.red, "Low", group = "Mondays & Weekly", inline="3c")
CurWeekLowWidth = input.int(defval=1, title="Width", minval=1, maxval = 4, group="Mondays & Weekly", inline="3c")
 
ShowPrevWeek = input.bool(defval = true, title="Show Previous Week High/Low", group="Mondays & Weekly", inline="4")
ExtendPrevWeek = input.bool(defval = false, title="Extend?", group="Mondays & Weekly", inline="4a")
LabelPrevWeek = input.bool(defval = true, title="Label?", group="Mondays & Weekly", inline="4b")
PrevWeekHighCol = input.color(color.green, "High", group="Mondays & Weekly", inline="4c")
PrevWeekHighWidth = input.int(defval=1, title="Width", minval=1, maxval = 4, group="Mondays & Weekly", inline="4c")
PrevWeekLowCol = input.color(color.red, "Low", group = "Mondays & Weekly", inline="4c")
PrevWeekLowWidth = input.int(defval=1, title="Width", minval=1, maxval = 4, group="Mondays & Weekly", inline="4c")
 
zone = input.string('GMT+1', title='Timezone', options=['GMT-11', 'GMT-10', 'GMT-9', 'GMT-8', 'GMT-7', 'GMT-6', 'GMT-5', 'GMT-4', 'GMT-3', 'GMT-2', 'GMT-1', 'GMT', 'GMT+1', 'GMT+2', 'GMT+3', 'GMT+330', 'GMT+4', 'GMT+430', 'GMT+5', 'GMT+530', 'GMT+6', 'GMT+7', 'GMT+8', 'GMT+9', 'GMT+10', 'GMT+11', 'GMT+12'], tooltip='e.g. \'America/New_York\', \'Asia/Tokyo\', \'GMT-4\', \'GMT+9\'...', group="Sessions")
 
ShowAsia = input.bool(defval = true, title="Show Asia Session", group="Sessions", inline="1")
ExtendAsia = input.bool(defval = false, title="Extend?", group="Sessions", inline="1")
AsiaSession = input.session("2200-0700:1234567", "Asia Session", group="Sessions", inline="2")
AsiaCol = input.color(color.new(color.green,90), "Colour", group="Sessions", inline="2")
 
ShowLon = input.bool(defval = true, title="Show London Session", group="Sessions", inline="4")
ExtendLon = input.bool(defval = false, title="Extend?", group="Sessions", inline="4")
LonSession = input.session("0800-1700:1234567", "London Session", group="Sessions", inline="5")
LonCol = input.color(color.new(color.red,90), "Colour", group="Sessions", inline="5")
 
ShowNY = input.bool(defval = true, title="Show NY Session", group="Sessions", inline="7")
ExtendNY = input.bool(defval = false, title="Extend?", group="Sessions", inline="7")
NYSession = input.session("1300-2100:1234567", "NY Session", group="Sessions", inline="8")
NYCol = input.color(color.new(color.aqua,90), "Colour", group="Sessions", inline="8")
 
i_show_history = input.bool(defval=false, title='History?', group="Sessions", inline="A")
i_sess_border_style = input.string(line.style_solid, 'Line Style', options=[line.style_solid, line.style_dotted, line.style_dashed], group="Sessions", inline="B")
i_sess_border_width = input.int(1, 'Line Width', minval=0, group="Sessions", inline="B")
i_sess_bgopacity = input.int(80, 'Background Transparency', minval=0, maxval=100, step=1, group="Sessions", tooltip='100 = No Background', inline="C")
 
ExtendMon = (ExtendMonday == true ? extend.right : extend.none)
ExtendPD = (ExtendPrevDay == true ? extend.right : extend.none)
ExtendPrevW = (ExtendPrevWeek == true ? extend.right : extend.none)
ExtendCurW = (ExtendCurWeek == true ? extend.right : extend.none)
 
ResolutionToSec(res)=>
    mins = res == "1" ? 1 :
           res == "3" ? 3 :
           res == "5" ? 5 :
           res == "10" ? 10 :
           res == "15" ? 15 :
           res == "30" ? 30 :
           res == "45" ? 45 :
           res == "60" ? 60 :
           res == "120" ? 120 :
           res == "180" ? 180 :
           res == "240" ? 240 :
           res == "D" or res == "1D" ? 1440 :
           res == "W" or res == "1W" ? 10080 :
           res == "M" or res == "1M" ? 43200 : 0
    ms = mins * 60 * 1000
 
bar = ResolutionToSec(timeframe.period) 
 
get_Monday()=>
    highs= array.new_float(0)
    lows= array.new_float(0)
    starttimes= array.new_int(0)
    for n = 0 to 6
        array.push(highs, high[n])
        array.push(lows, low[n])
        array.push(starttimes, time[n])
    [highs, lows, starttimes]
    
   
get_Weekly()=>
    highs= array.new_float(0)
    lows= array.new_float(0)
    starttimes= array.new_int(0)
    for n = 0 to 1
        array.push(highs, high[n])
        array.push(lows, low[n])
        array.push(starttimes, time[n])
    [highs, lows, starttimes]
    
   
get_Sessions()=>
    highs= array.new_float(0)
    lows= array.new_float(0)
    starttimes= array.new_int(0)
    for n = 0 to 96
        array.push(highs, high[n])
        array.push(lows, low[n])
        array.push(starttimes, time[n])
    [highs, lows, starttimes]
    
[shighs, slows, sstarttimes] = request.security(syminfo.tickerid, "15", get_Sessions(), lookahead = barmerge.lookahead_off)
[mhighs, mlows, mstarttimes] = request.security(syminfo.tickerid, "1D", get_Monday(), lookahead = barmerge.lookahead_off)
[whighs, wlows, wstarttimes] = request.security(syminfo.tickerid, "1W", get_Weekly(), lookahead = barmerge.lookahead_off)
 
 
if (bar_index == last_bar_index)  
    for x = 0 to array.size(mhighs)-1
        if (dayofweek(array.get(mstarttimes, x)) == 2 and ShowMonday == true)
            MH = line.new(x1 = array.get(mstarttimes, x), y1 = array.get(mhighs, x), x2 = array.get(mstarttimes, x)+(86400000*(dayofweek(timenow, syminfo.timezone)-1)), y2 = array.get(mhighs, x), xloc= xloc.bar_time, extend=ExtendMon, color=MonHighCol, style= line.style_solid, width=MonHighWidth)
            ML = line.new(x1 = array.get(mstarttimes, x), y1 = array.get(mlows, x), x2 = array.get(mstarttimes, x)+(86400000*(dayofweek(timenow, syminfo.timezone)-1)), y2 = array.get(mlows, x), xloc= xloc.bar_time, extend=ExtendMon, color= MonLowCol, style= line.style_solid, width= MonLowWidth)
            line.delete(MH[1])
            line.delete(ML[1])
            if (LabelMonday == true)   
                MHL = label.new(x=array.get(mstarttimes, x)+(86400000*(dayofweek(timenow, syminfo.timezone)-1)), y=array.get(mhighs, x)*1.0001, text="Mon High", xloc=xloc.bar_time, color=color.white, style=label.style_none, textcolor=MonHighCol, size=size.normal, textalign=text.align_right)
                MLL = label.new(x=array.get(mstarttimes, x)+(86400000*(dayofweek(timenow, syminfo.timezone)-1)), y=array.get(mlows, x)*1.0001, text="Mon Low", xloc=xloc.bar_time, color=color.white, style=label.style_none, textcolor=MonLowCol, size=size.normal, textalign=text.align_right)
                label.delete(MHL[1])
                label.delete(MLL[1])
                
    if (ShowPrevDay == true)
        PDH = line.new(x1 = array.get(mstarttimes, 1), y1 = array.get(mhighs, 1), x2 = array.get(mstarttimes, 1)+(86400000*2), y2 = array.get(mhighs, 1), xloc= xloc.bar_time, extend=ExtendPD, color=PrevDayHighCol, style= line.style_solid, width=PrevDayHighWidth)
        PDL = line.new(x1 = array.get(mstarttimes, 1), y1 = array.get(mlows, 1), x2 = array.get(mstarttimes, 1)+(86400000*2), y2 = array.get(mlows, 1), xloc= xloc.bar_time, extend=ExtendPD, color= PrevDayLowCol, style= line.style_solid, width= PrevDayLowWidth)
        line.delete(PDH[1])
        line.delete(PDL[1])
        if (LabelMonday == true)   
            PDHL = label.new(x=array.get(mstarttimes, 1)+(86400000*2), y=array.get(mhighs, 1)*1.0001, text="Prev Day High", xloc=xloc.bar_time, color=color.white, style=label.style_none, textcolor=PrevDayHighCol, size=size.normal, textalign=text.align_right)
            PDLL = label.new(x=array.get(mstarttimes, 1)+(86400000*2), y=array.get(mlows, 1)*1.0001, text="Prev Day Low", xloc=xloc.bar_time, color=color.white, style=label.style_none, textcolor=PrevDayLowCol, size=size.normal, textalign=text.align_right)
            label.delete(PDHL[1])
            label.delete(PDLL[1])
                
    if (ShowPrevWeek == true)
        PWH = line.new(x1 = array.get(wstarttimes, 1), y1 = array.get(whighs, 1), x2 = array.get(wstarttimes, 1)+(86400000*(dayofweek(timenow, syminfo.timezone)+6)), y2 = array.get(whighs, 1), xloc= xloc.bar_time, extend=ExtendPrevW, color=PrevWeekHighCol, style= line.style_solid, width=PrevWeekHighWidth)
        PWL = line.new(x1 = array.get(wstarttimes, 1), y1 = array.get(wlows, 1), x2 = array.get(wstarttimes, 1)+(86400000*(dayofweek(timenow, syminfo.timezone)+6)), y2 = array.get(wlows, 1), xloc= xloc.bar_time, extend=ExtendPrevW, color= PrevWeekLowCol, style= line.style_solid, width= PrevWeekLowWidth)
        line.delete(PWH[1])
        line.delete(PWL[1])
        if (LabelPrevWeek == true)   
            PWHL = label.new(x=array.get(wstarttimes, 1)+(86400000*(dayofweek(timenow, syminfo.timezone)+6)), y=array.get(whighs, 1)*1.0001, text="Prev Week High", xloc=xloc.bar_time, color=color.white, style=label.style_none, textcolor=PrevWeekHighCol, size=size.normal, textalign=text.align_right)
            PWLL = label.new(x=array.get(wstarttimes, 1)+(86400000*(dayofweek(timenow, syminfo.timezone)+6)), y=array.get(wlows, 1)*1.0001, text="Prev Week Low", xloc=xloc.bar_time, color=color.white, style=label.style_none, textcolor=PrevWeekLowCol, size=size.normal, textalign=text.align_right)
            label.delete(PWHL[1])
            label.delete(PWLL[1])
            
    if (ShowCurWeek == true)
        CWH = line.new(x1 = array.get(wstarttimes, 0), y1 = array.get(whighs, 0), x2 = array.get(wstarttimes, 0)+(86400000*(dayofweek(timenow, syminfo.timezone)-1)), y2 = array.get(whighs, 0), xloc= xloc.bar_time, extend=ExtendCurW, color=CurWeekHighCol, style= line.style_solid, width=CurWeekHighWidth)
        CWL = line.new(x1 = array.get(wstarttimes, 0), y1 = array.get(wlows, 0), x2 = array.get(wstarttimes, 0)+(86400000*(dayofweek(timenow, syminfo.timezone)-1)), y2 = array.get(wlows, 0), xloc= xloc.bar_time, extend=ExtendCurW, color= CurWeekLowCol, style= line.style_solid, width= CurWeekLowWidth)
        line.delete(CWH[1])
        line.delete(CWL[1])
        if (LabelCurWeek == true)   
            CWHL = label.new(x=array.get(wstarttimes, 0)+(86400000*(dayofweek(timenow, syminfo.timezone)-1)), y=array.get(whighs, 0)*1.0001, text="Weekly High", xloc=xloc.bar_time, color=color.white, style=label.style_none, textcolor=CurWeekHighCol, size=size.normal, textalign=text.align_right)
            CWLL = label.new(x=array.get(wstarttimes, 0)+(86400000*(dayofweek(timenow, syminfo.timezone)-1)), y=array.get(wlows, 0)*1.0001, text="Weekly Low", xloc=xloc.bar_time, color=color.white, style=label.style_none, textcolor=CurWeekLowCol, size=size.normal, textalign=text.align_right)
            label.delete(CWHL[1])
            label.delete(CWLL[1])
    
///////////////
// Defined
///////////////
show = true
pips = syminfo.mintick * 10
max_bars = 500
 
fmt_price = '{0,number,#.#####}'
fmt_pips  = '{0,number,#.#}'
 
icon_separator = ' • '
 
c_none = color.new(color.black, 100)
 
is_weekends = dayofweek == 7 or dayofweek == 1
 
f_get_time_by_bar(bar_count) => timeframe.multiplier * bar_count * 60 * 1000
 
f_get_period (_session, _start, _lookback) =>
    result = math.max(_start, 1)
    for i = result to _lookback
        if na(_session[i+1]) and _session[i]
            result := i+1
            break
    result
 
f_get_label_position (_y, _side) =>
    switch _y
        'top'    => _side == 'outside' ? label.style_label_lower_left : label.style_label_upper_left
        'bottom' => _side == 'outside' ? label.style_label_upper_left : label.style_label_lower_left
 
f_get_day (n) =>
    switch n
        1 => 'Sun'
        2 => 'Mon'
        3 => 'Tue'
        4 => 'Wed'
        5 => 'Thu'
        6 => 'Fri'
        7 => 'Sat'
 
f_get_started (_session) => na(_session[1]) and _session
 
f_get_ended (_session) => na(_session) and _session[1]
 
i_lookback = 12 * 60
 
f_render_session (_show, _session, _is_started, _is_ended, _color, _top, _bottom, _is_extend, _delete_history) =>
    var box my_box = na
 
    x0_1 = ta.valuewhen(na(_session[1]) and _session, bar_index, 1)
    x0_2 = ta.valuewhen(na(_session) and _session[1], bar_index, 0)
    var x1 = 0
    var x2 = 0
    var session_open  = 0.0
    var session_high  = 0.0
    var session_low   = 0.0
 
    if _show
        if _is_started
            diff = math.abs(x0_2 - x0_1)
            x1 := bar_index
            x2 := bar_index + (math.min(diff, max_bars))
            my_box := box.new(x1, _top, x2, _bottom, color.new(_color, i_sess_bgopacity/1.1), i_sess_border_width, i_sess_border_style, bgcolor=color.new(_color, i_sess_bgopacity))
 
            session_open := open
            session_high := _top
            session_low  := _bottom
 
            if _is_extend
                box.set_extend(my_box, extend.right)
 
            if _delete_history
                box.delete(my_box[1])
 
        else if _session
            box.set_top(my_box, _top)
            box.set_bottom(my_box, _bottom)
 
            session_high := _top
            session_low  := _bottom
        
        else if _is_ended
            session_open := na
            box.set_right(my_box, bar_index)
 
    [x1, x2, session_open, session_high, session_low]
 
draw (_show, _session, _color, is_extend, _lookback) =>
    max = f_get_period(_session, 1, _lookback)
    top = ta.highest(high, max)
    bottom = ta.lowest(low, max)
    
    is_started = f_get_started(_session)
    is_ended = f_get_ended(_session)
    
    delete_history = (not i_show_history) or is_extend
 
    [x1, x2, _open, _high, _low] = f_render_session(_show, _session, is_started, is_ended, _color, top, bottom, is_extend, delete_history)
 
    [_session, _open, _high, _low]
 
int sess1 = time(timeframe.period, AsiaSession, zone)
int sess2 = time(timeframe.period, LonSession, zone)
int sess3 = time(timeframe.period, NYSession, zone)
 
draw(ShowAsia, sess1, AsiaCol, ExtendAsia, i_lookback)
draw(ShowLon, sess2, LonCol, ExtendLon, i_lookback)
draw(ShowNY, sess3, NYCol, ExtendNY, i_lookback)

← 返回列表