本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © kurtsmock
// Highest and Lowest Functions: Credit to @ricardosantos
//@version=4
study("Session HiLo", overlay=true, max_bars_back = 5000)
f_highest(_source, _length) =>
float _return = _source
for _i = 0 to max(0, _length-1)
_return := max(_source[_i], _return)
_return
f_lowest(_source, _length) =>
float _return = _source
for _i = 0 to max(0, _length-1)
_return := min(_source[_i], _return)
_return
c01 = "Aqua", c02 = "Black", c03 = "Blue", c04 = "Fuchsia", c05 = "Gray", c06 = "Green", c07 = "Lime", c08 = "Maroon", c09 = "Navy", c10 = "Olive", c11 = "Orange", c12 = "Purple", c13 = "Red", c14 = "Silver", c15 = "Teal", c16 = "White", c17 = "Yellow"
_color(_var) =>
if _var == c01
color.aqua
else if _var == c02
color.black
else if _var == c03
color.blue
else if _var == c04
color.fuchsia
else if _var == c05
color.gray
else if _var == c06
color.green
else if _var == c07
color.lime
else if _var == c08
color.maroon
else if _var == c09
color.navy
else if _var == c10
color.olive
else if _var == c11
color.orange
else if _var == c12
color.purple
else if _var == c13
color.red
else if _var == c14
color.silver
else if _var == c15
color.teal
else if _var == c16
color.white
else if _var == c17
color.yellow
header_1 = input(false, "[===== Display Options =====]", input.bool)
s_lines = input(false, "Show Lines, Not Plots")
s_bg = input(true, "Show Background Color")
c_bg = input(c13, "Choose Background Color", options = [c01, c02, c03, c04, c05, c06, c07, c08, c09, c10, c11, c12, c13, c14, c15, c16, c17])
c_transp = input(90, "Background Transparency", input.integer, minval = 1, maxval = 100)
c_highsCol = input(c13, "Choose Highs Color", options = [c01, c02, c03, c04, c05, c06, c07, c08, c09, c10, c11, c12, c13, c14, c15, c16, c17])
c_lowsCol = input(c06, "Choose Lows Color", options = [c01, c02, c03, c04, c05, c06, c07, c08, c09, c10, c11, c12, c13, c14, c15, c16, c17])
c_lw = input(2, "Choose Line Width", input.integer)
i_sessions = input(5, "Sessions Back (Lines)", input.integer, minval = 1, maxval = 21)
header_2 = input(false, "[===== Date Range =====]", input.bool)
i_startMonth = input(10, "Starting Month", input.integer, minval = 1, maxval = 12)
i_startDay = input(1, "Starting Day", input.integer, minval = 1, maxval = 31)
i_startYear = input(2020, "Starting Year", input.integer, minval = 1970)
i_endMonth = input(1, "End Month", input.integer, minval = 1, maxval = 12)
i_endDay = input(1, "End Day", input.integer, minval = 1, maxval = 31)
i_endYear = input(2021, "End Year", input.integer, minval = 1970)
v_startBacktest = timestamp(i_startYear, i_startMonth, i_startDay, 0, 0)
v_endBacktest = timestamp(i_endYear, i_endMonth, i_endDay, 23, 59)
header_3 = input(false, "[==== Session Range ====]", input.bool)
s_useSession = input(false, "Use Session Constraint", input.bool)
sessionPeriod = input("0000-2359", "Session Definition", input.session)
s_weekends_inc = input(true, "Include Weekends", input.bool)
s_weekends = s_weekends_inc ? ":1234567" : ":23456"
_inSession(sess) => na(time(timeframe.period, sess)) == false
signal_Session = s_useSession ? _inSession(sessionPeriod+s_weekends) : true
signal_backtest() => time >= v_startBacktest and time <= v_endBacktest and signal_Session ? true : false
o_startSession = barssince(not(signal_backtest()))
_hh = float(na)
_hh := o_startSession >= 1 ? f_highest(high, o_startSession) : nz(_hh[1])
_ll = float(na)
_ll := o_startSession >= 1 ? f_lowest(low, o_startSession) : nz(_ll[1])
plotHighColors = o_startSession[1] == 0 and o_startSession[0] != 0 ? #FF000000 : _color(c_highsCol)
plotLowColors = o_startSession[1] == 0 and o_startSession[0] != 0 ? #FF000000 : _color(c_lowsCol)
plot(not s_lines and _hh != 0 ? _hh : na, "Highest High of Session", color = plotHighColors, linewidth = c_lw, transp = 30)
plot(not s_lines and _ll != 0 ? _ll : na, "Lowest Low of Session", color = plotLowColors, linewidth = c_lw, transp = 30)
///////////////
//// Lines ////
///////////////
_bar_index1 = bar_index - o_startSession[1]
_bar_index2 = bar_index - o_startSession[1] + 1
lineVarHighs = line(na)
if s_lines and o_startSession[1] > 0 and o_startSession[0] == 0
lineVarHighs := line.new(_bar_index1, _hh[1], _bar_index2, _hh[1], extend = extend.right, color = color.red, width = c_lw)
highsCount = 0
for i = 1 to 1000
if not na(lineVarHighs[i])
highsCount := highsCount + 1
if highsCount > (i_sessions)
line.delete(lineVarHighs[i])
lineVarLows = line(na)
if s_lines and o_startSession[1] > 0 and o_startSession[0] == 0
lineVarLows := line.new(_bar_index1, _ll[1], _bar_index2, _ll[1], extend = extend.right, color = color.green, width = c_lw)
lowsCount = 0
for i = 1 to 1000
if not na(lineVarLows[i])
lowsCount := lowsCount + 1
if lowsCount > (i_sessions)
line.delete(lineVarLows[i])
//////////////////
//// BG Color ////
//////////////////
bgcolor(color = signal_backtest() and s_bg ? _color(c_bg) : na, title = "Background", transp = c_transp)