本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator("Sunday Open Context | jjart", overlay = true, max_lines_count = 300, max_labels_count = 300, max_boxes_count = 100)
string timing = "Calendar"
string zoneMode = input.string("Exchange", "Time zone", options = ["Exchange", "UTC", "Custom"], group = timing)
string customZone = input.string("Europe/London", "Custom time zone", tooltip = "Use an IANA time zone, such as Europe/London or America/New_York.", group = timing)
int keep = input.int(5, "Sunday opens to keep", minval = 1, maxval = 100, group = timing)
string appearance = "Appearance"
color newestColor = input.color(color.yellow, "Latest level", group = appearance)
color olderColor = input.color(color.orange, "Previous levels", group = appearance)
string styleInput = input.string("Solid", "Line style", options = ["Solid", "Dashed", "Dotted"], group = appearance)
int width = input.int(2, "Line width", minval = 1, maxval = 5, group = appearance)
bool labelsOn = input.bool(true, "Show labels", group = appearance)
bool priceOn = input.bool(true, "Include price", group = appearance)
bool dateOn = input.bool(false, "Include date", group = appearance)
int labelOffset = input.int(5, "Label offset (bars)", minval = 0, maxval = 100, group = appearance)
string context = "Sunday context"
bool showRange = input.bool(true, "Show Sunday range", group = context)
bool showBands = input.bool(true, "Show ATR reaction bands", group = context)
int atrLength = input.int(14, "ATR length", minval = 1, group = context)
float bandSize = input.float(0.25, "Band width in ATR", minval = 0.05, maxval = 3.0, step = 0.05, group = context)
color rangeColor = input.color(color.aqua, "Range color", group = context)
color bandColor = input.color(color.new(color.yellow, 88), "Band color", group = context)
bool confirmedOnly = input.bool(true, "Confirm alerts at bar close", group = "Alerts")
string sessionGroup = "Session ranges and sweeps"
bool showSessions = input.bool(true, "Show session ranges", group = sessionGroup)
string sessionZone = input.string("Etc/UTC", "Session time zone", group = sessionGroup)
string asiaSession = input.session("0000-0800", "Asia", group = sessionGroup)
string londonSession = input.session("0800-1300", "London", group = sessionGroup)
string newYorkSession = input.session("1300-2100", "New York", group = sessionGroup)
bool showSweeps = input.bool(true, "Show first liquidity sweep", group = sessionGroup)
color asiaSessionColor = input.color(color.new(#8CB7FF, 88), "Asia range color", group = sessionGroup)
color londonSessionColor = input.color(color.new(#C9A7FF, 88), "London range color", group = sessionGroup)
color newYorkSessionColor = input.color(color.new(#FFB88C, 88), "New York range color", group = sessionGroup)
color sweepColor = input.color(color.new(#8A8D98, 15), "Sweep line color", group = sessionGroup)
if not (timeframe.isintraday or (timeframe.isdaily and timeframe.multiplier == 1))
runtime.error("Use an intraday or 1D chart. Higher timeframes cannot identify individual Sunday opens.")
string tz = zoneMode == "Exchange" ? syminfo.timezone : zoneMode == "UTC" ? "Etc/UTC" : customZone
string lineStyle = styleInput == "Dashed" ? line.style_dashed : styleInput == "Dotted" ? line.style_dotted : line.style_solid
// A Sunday open is the first available bar opening on Sunday in the selected calendar.
// Do not infer an opening from the first bar of a truncated data set.
int dateKey = year(time, tz) * 10000 + month(time, tz) * 100 + dayofmonth(time, tz)
bool newSunday = not na(dateKey[1]) and dateKey != dateKey[1] and dayofweek(time, tz) == dayofweek.sunday
var array<line> levels = array.new<line>()
var array<label> captions = array.new<label>()
var array<int> dates = array.new<int>()
var float latest = na
var float sundayHigh = na
var float sundayLow = na
var line highLine = na
var line lowLine = na
var box rangeBox = na
var box upperBand = na
var box lowerBand = na
var box asiaBox = na
var float asiaHigh = na
var float asiaLow = na
var int asiaEnd = na
var bool asiaHighTaken = false
var bool asiaLowTaken = false
var box londonBox = na
var float londonHigh = na
var float londonLow = na
var int londonEnd = na
var bool londonHighTaken = false
var bool londonLowTaken = false
var box newYorkBox = na
var float newYorkHigh = na
var float newYorkLow = na
var int newYorkEnd = na
var bool newYorkHighTaken = false
var bool newYorkLowTaken = false
bool inAsia = not na(time(timeframe.period, asiaSession, sessionZone))
bool asiaStart = inAsia and not inAsia[1]
bool inLondon = not na(time(timeframe.period, londonSession, sessionZone))
bool londonStart = inLondon and not inLondon[1]
bool inNewYork = not na(time(timeframe.period, newYorkSession, sessionZone))
bool newYorkStart = inNewYork and not inNewYork[1]
bool asiaHighSweep = showSweeps and not inAsia and not asiaHighTaken and not na(asiaHigh) and high > asiaHigh and close < asiaHigh
bool asiaLowSweep = showSweeps and not inAsia and not asiaLowTaken and not na(asiaLow) and low < asiaLow and close > asiaLow
bool londonHighSweep = showSweeps and not inLondon and not londonHighTaken and not na(londonHigh) and high > londonHigh and close < londonHigh
bool londonLowSweep = showSweeps and not inLondon and not londonLowTaken and not na(londonLow) and low < londonLow and close > londonLow
bool newYorkHighSweep = showSweeps and not inNewYork and not newYorkHighTaken and not na(newYorkHigh) and high > newYorkHigh and close < newYorkHigh
bool newYorkLowSweep = showSweeps and not inNewYork and not newYorkLowTaken and not na(newYorkLow) and low < newYorkLow and close > newYorkLow
if newSunday
latest := open
sundayHigh := high
sundayLow := low
array.unshift(levels, line.new(time, open, time + 1, open, xloc = xloc.bar_time, extend = extend.right, color = newestColor, style = lineStyle, width = width))
array.unshift(dates, time)
array.unshift(captions, label.new(bar_index, open, "", style = label.style_none))
if array.size(levels) > keep
line.delete(array.pop(levels))
label.delete(array.pop(captions))
array.pop(dates)
if dayofweek(time, tz) == dayofweek.sunday and not na(latest)
sundayHigh := math.max(sundayHigh, high)
sundayLow := math.min(sundayLow, low)
if showSessions
if asiaStart
asiaHigh := high
asiaLow := low
asiaHighTaken := false
asiaLowTaken := false
asiaBox := box.new(bar_index, high, bar_index, low, border_color = color.new(asiaSessionColor, 20), bgcolor = asiaSessionColor)
else if inAsia
asiaHigh := math.max(asiaHigh, high)
asiaLow := math.min(asiaLow, low)
box.set_rightbottom(asiaBox, bar_index, asiaLow)
box.set_top(asiaBox, asiaHigh)
if not inAsia and inAsia[1]
asiaEnd := bar_index - 1
if londonStart
londonHigh := high
londonLow := low
londonHighTaken := false
londonLowTaken := false
londonBox := box.new(bar_index, high, bar_index, low, border_color = color.new(londonSessionColor, 20), bgcolor = londonSessionColor)
else if inLondon
londonHigh := math.max(londonHigh, high)
londonLow := math.min(londonLow, low)
box.set_rightbottom(londonBox, bar_index, londonLow)
box.set_top(londonBox, londonHigh)
if not inLondon and inLondon[1]
londonEnd := bar_index - 1
if newYorkStart
newYorkHigh := high
newYorkLow := low
newYorkHighTaken := false
newYorkLowTaken := false
newYorkBox := box.new(bar_index, high, bar_index, low, border_color = color.new(newYorkSessionColor, 20), bgcolor = newYorkSessionColor)
else if inNewYork
newYorkHigh := math.max(newYorkHigh, high)
newYorkLow := math.min(newYorkLow, low)
box.set_rightbottom(newYorkBox, bar_index, newYorkLow)
box.set_top(newYorkBox, newYorkHigh)
if not inNewYork and inNewYork[1]
newYorkEnd := bar_index - 1
if asiaHighSweep
line.new(asiaEnd, asiaHigh, bar_index, asiaHigh, style = line.style_dashed, width = 1, color = sweepColor)
asiaHighTaken := true
if asiaLowSweep
line.new(asiaEnd, asiaLow, bar_index, asiaLow, style = line.style_dashed, width = 1, color = sweepColor)
asiaLowTaken := true
if londonHighSweep or newYorkHighSweep
float level = londonHighSweep ? londonHigh : newYorkHigh
int start = londonHighSweep ? londonEnd : newYorkEnd
line.new(start, level, bar_index, level, style = line.style_dashed, width = 1, color = sweepColor)
if londonHighSweep
londonHighTaken := true
else
newYorkHighTaken := true
if londonLowSweep or newYorkLowSweep
float level = londonLowSweep ? londonLow : newYorkLow
int start = londonLowSweep ? londonEnd : newYorkEnd
line.new(start, level, bar_index, level, style = line.style_dashed, width = 1, color = sweepColor)
if londonLowSweep
londonLowTaken := true
else
newYorkLowTaken := true
if barstate.islast and array.size(levels) > 0
if not na(highLine)
line.delete(highLine)
line.delete(lowLine)
box.delete(rangeBox)
box.delete(upperBand)
box.delete(lowerBand)
if showRange
highLine := line.new(time, sundayHigh, time + 1, sundayHigh, xloc = xloc.bar_time, extend = extend.right, color = rangeColor, style = line.style_dotted)
lowLine := line.new(time, sundayLow, time + 1, sundayLow, xloc = xloc.bar_time, extend = extend.right, color = rangeColor, style = line.style_dotted)
rangeBox := box.new(time, sundayHigh, time + 1, sundayLow, xloc = xloc.bar_time, extend = extend.right, border_color = na, bgcolor = color.new(rangeColor, 93))
if showBands and not na(ta.atr(atrLength))
float distance = ta.atr(atrLength) * bandSize
upperBand := box.new(time, latest + distance, time + 1, latest, xloc = xloc.bar_time, extend = extend.right, border_color = na, bgcolor = bandColor)
lowerBand := box.new(time, latest, time + 1, latest - distance, xloc = xloc.bar_time, extend = extend.right, border_color = na, bgcolor = bandColor)
for i = 0 to array.size(levels) - 1
line level = array.get(levels, i)
color shade = i == 0 ? newestColor : olderColor
line.set_color(level, shade)
label caption = array.get(captions, i)
float price = line.get_y1(level)
string labelText = "SO" + (priceOn ? " " + str.tostring(price, format.mintick) : "") + (dateOn ? " " + str.format_time(array.get(dates, i), "yyyy-MM-dd", tz) : "")
label.set_xy(caption, bar_index + labelOffset, price)
label.set_text(caption, labelsOn ? labelText : "")
label.set_textcolor(caption, shade)
// Compare both closes against the SAME level. Ignore the bar that creates a level.
bool ready = not newSunday and not na(latest) and (not confirmedOnly or barstate.isconfirmed)
bool crossedAbove = ready and close > latest and close[1] <= latest
bool crossedBelow = ready and close < latest and close[1] >= latest
bool brokeSundayHigh = ready and close > sundayHigh and close[1] <= sundayHigh
bool brokeSundayLow = ready and close < sundayLow and close[1] >= sundayLow
plot(latest, "Latest Sunday Open", display = display.data_window, color = newestColor)
plot(sundayHigh, "Sunday High", display = display.data_window, color = rangeColor)
plot(sundayLow, "Sunday Low", display = display.data_window, color = rangeColor)
alertcondition(crossedAbove, "Cross above Sunday Open", "{{ticker}} crossed above the latest Sunday Open on {{interval}}.")
alertcondition(crossedBelow, "Cross below Sunday Open", "{{ticker}} crossed below the latest Sunday Open on {{interval}}.")
alertcondition(crossedAbove or crossedBelow, "Cross Sunday Open", "{{ticker}} crossed the latest Sunday Open on {{interval}}.")
alertcondition(brokeSundayHigh, "Break Sunday High", "{{ticker}} broke above the current Sunday High.")
alertcondition(brokeSundayLow, "Break Sunday Low", "{{ticker}} broke below the current Sunday Low.")
alertcondition(asiaHighSweep, "Asia high sweep", "{{ticker}} swept the Asia session high.")
alertcondition(asiaLowSweep, "Asia low sweep", "{{ticker}} swept the Asia session low.")
alertcondition(londonHighSweep or newYorkHighSweep, "Session high sweep", "{{ticker}} swept a London or New York session high.")
alertcondition(londonLowSweep or newYorkLowSweep, "Session low sweep", "{{ticker}} swept a London or New York session low.")
alertcondition(newSunday and (not confirmedOnly or barstate.isconfirmed), "New Sunday Open", "A new Sunday Open was recorded for {{ticker}} on {{interval}}.")