本页源码来自 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/
// © AstrideUnicorn
//@version=5
indicator("Intraday Spark Chart [AstrideUnicorn]", "ISC", overlay=false, scale=scale.right, max_lines_count=500, max_labels_count=500)
// Input for asset selection. This allows user to display other asset intraday price action to perform intermarket analysis.
targetSymbol = input.symbol("", "Asset", tooltip="Leave empty for current symbol")
// Timeframe check. If timeframe is higher than 4 hours display a warning instead of the indicator.
var table tfWarning = table.new(position.middle_center, 1, 1, bgcolor=color.new(color.blue, 90))
isIntraday = timeframe.in_seconds() <= 4 * 60 * 60
showWarning = not isIntraday
if showWarning
table.cell(tfWarning, 0, 0, "This is an intraday indicator. Please, switch to a lower timeframe.", text_color=color.white)
// Get pricing data for selected asset.
assetClose = request.security(targetSymbol == "" ? syminfo.tickerid : targetSymbol,
timeframe.period, close, lookahead=barmerge.lookahead_off)
dailyOpen = isIntraday ? request.security(targetSymbol == "" ? syminfo.tickerid : targetSymbol, "D", open,
lookahead=barmerge.lookahead_on) : na
// lookahead_on is needed to align daily open prices with the corresponding intraday timeframe pricing data.
// Calculate percentage changes using selected asset's data
percentChange = isIntraday ? (assetClose - dailyOpen) / dailyOpen * 100 : na
// Day identification
isNewDay = isIntraday ? ta.change(time("D")) : false
// Colors
positiveColor = #4CAF50
negativeColor = #F44336
neutralColor = #787B86
// Vertical lines for day separators
if isIntraday and isNewDay
line.new(bar_index, -3, bar_index, 3, color=color.new(#787B86, 0), width=1, style=line.style_dotted, xloc=xloc.bar_index)
// Baseline
hline(0, "Zero Line", color=neutralColor, linestyle=hline.style_solid)
// Main plots
plotColor = not isIntraday ? color.new(color.white, 100) :
isNewDay ? color.new(color.black, 100) :
percentChange > 0 ? color.new(positiveColor, 0) :
color.new(negativeColor, 0)
areaColor = not isIntraday ? color.new(color.white, 100) :
isNewDay ? color.new(color.black, 100) :
percentChange > 0 ? color.new(positiveColor, 80) :
color.new(negativeColor, 80)
plot(percentChange, "Outline", color=plotColor, linewidth=2, style=plot.style_line)
plot(percentChange, "Surface", color=areaColor, linewidth=1, style=plot.style_area)
// Daily open price label
if isIntraday and isNewDay
label.new(
bar_index, 2.2,
text="Op:"+str.tostring(dailyOpen, format.mintick),
color=color.new(color.gray, 100),
textcolor=color.rgb(129, 132, 144),
style=label.style_label_left,
size=size.small,
yloc=yloc.price
)
// Daily percentage change label system
var label[] dayLabels = array.new_label(0)
var int currentDayLabelIndex = -1
if isIntraday
isPositive = percentChange >= 0
if isNewDay
if array.size(dayLabels) > 0 and currentDayLabelIndex >= 0
prevDayPct = percentChange[1]
prevIsPositive = prevDayPct >= 0
str_arrow = prevIsPositive ? "▲" : "▼"
label.set_text(array.get(dayLabels, currentDayLabelIndex),str_arrow + str.format("{0, number, +#.##%;-#.##%}", prevDayPct/100))
label.set_textcolor(array.get(dayLabels, currentDayLabelIndex), prevIsPositive ? positiveColor : negativeColor)
label.set_color(array.get(dayLabels, currentDayLabelIndex), prevIsPositive ? color.new(positiveColor,100) : color.new(negativeColor,100))
lbl = label.new(bar_index, 1.4, text = str.format("{0, number, +#.##%;-#.##%}", percentChange/100),
color=color.new(isPositive ? positiveColor : negativeColor, 100),
textcolor=isPositive ? positiveColor : negativeColor, style=label.style_label_left, size=size.small, yloc=yloc.price)
array.push(dayLabels, lbl)
currentDayLabelIndex := array.size(dayLabels) - 1
if barstate.islast and array.size(dayLabels) > 0 and currentDayLabelIndex >= 0
str_arrow_current = isPositive ? "▲" : "▼"
label.set_text(array.get(dayLabels, currentDayLabelIndex), str_arrow_current + str.format("{0, number, +#.##%;-#.##%}", percentChange/100))
label.set_textcolor(array.get(dayLabels, currentDayLabelIndex), isPositive ? positiveColor : negativeColor)