本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
// This Pine Script® code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © rathattrader
//@version=6
indicator("First Strike ATR Compass", overlay=true)
//=========================
// Inputs
//=========================
atrLength = input.int(14, "Daily ATR Length", minval=1)
currentAtrLen = input.int(15, "Current Timeframe ATR Length", minval=1)
avgMinutes = input.int(60, "Average ATR Lookback Minutes", minval=1)
vixSymbol = input.symbol("CBOE:VIX", "VIX Symbol")
nqSymbol = input.symbol("CME_MINI:NQ1!", "NQ Symbol")
tablePosition = input.string("Top Right", "Table Position", options=[
"Top Left", "Top Center", "Top Right",
"Bottom Left", "Bottom Center", "Bottom Right"
])
textSizeInput = input.string("Small", "Text Size",
options=["Tiny", "Small", "Normal", "Large", "Huge"])
bgTransparency = input.int(15, "Shading Weight / Transparency",
minval=0, maxval=100)
// Row shading
currentAtrBgColor = input.color(color.gray, "Current TF ATR Row Shading Color")
marketDataBgColor = input.color(color.silver, "VIX / NQ Row Shading Color")
// Daily range utilization thresholds
lowDtrThreshold = input.float(50.0, "Green Starts at DTR %", step=1.0)
highDtrThreshold = input.float(95.0, "Red Starts Above DTR %", step=1.0)
// Colors
lowDtrColor = input.color(color.yellow, "Low DTR Shading")
normalDtrColor = input.color(color.rgb(132, 216, 135), "Normal DTR Shading")
highDtrColor = input.color(color.red, "High DTR Shading")
//=========================
// Helpers
//=========================
f_getPosition(pos) =>
switch pos
"Top Left" => position.top_left
"Top Center" => position.top_center
"Top Right" => position.top_right
"Bottom Left" => position.bottom_left
"Bottom Center" => position.bottom_center
"Bottom Right" => position.bottom_right
f_getTextSize(sz) =>
switch sz
"Tiny" => size.tiny
"Small" => size.small
"Normal" => size.normal
"Large" => size.large
"Huge" => size.huge
//=========================
// Daily ATR and DTR
//=========================
dailyATR = request.security(
syminfo.tickerid, "D", ta.atr(atrLength),
barmerge.gaps_off, barmerge.lookahead_off)
dayHigh = request.security(
syminfo.tickerid, "D", high,
barmerge.gaps_off, barmerge.lookahead_off)
dayLow = request.security(
syminfo.tickerid, "D", low,
barmerge.gaps_off, barmerge.lookahead_off)
dtr = dayHigh - dayLow
pctOfATR = dailyATR > 0 ? (dtr / dailyATR) * 100.0 : na
//=========================
// Current Timeframe ATR
//=========================
currentTfATR = ta.atr(currentAtrLen)
tfSeconds = timeframe.in_seconds(timeframe.period)
tfMinutes = tfSeconds / 60.0
avgBars = math.max(1, int(math.round(avgMinutes / tfMinutes)))
avgAtrLastMinutes = ta.sma(currentTfATR, avgBars)
//=========================
// VIX Current Trading Price
//=========================
vixPrice = request.security(
vixSymbol, timeframe.period, close,
barmerge.gaps_off, barmerge.lookahead_off,
ignore_invalid_symbol=true)
//=========================
// NQ % Change vs Previous
// Trading Day Close
//=========================
// Use the selected NQ contract's previous
// daily close as the reference.
nqPrevClose = request.security(
nqSymbol, "D", close[1],
barmerge.gaps_off, barmerge.lookahead_on,
ignore_invalid_symbol=true)
// Current NQ price on chart timeframe
nqCurrentPrice = request.security(
nqSymbol, timeframe.period, close,
barmerge.gaps_off, barmerge.lookahead_off,
ignore_invalid_symbol=true)
nqPctChange = not na(nqPrevClose) and nqPrevClose != 0 and not na(nqCurrentPrice) ?
((nqCurrentPrice - nqPrevClose) / nqPrevClose) * 100.0 : na
//=========================
// DTR Color Logic
//=========================
color baseColor = color.gray
if na(pctOfATR)
baseColor := color.gray
else if pctOfATR < lowDtrThreshold
baseColor := lowDtrColor
else if pctOfATR <= highDtrThreshold
baseColor := normalDtrColor
else
baseColor := highDtrColor
cellBg = color.new(baseColor, bgTransparency)
atrRowBg = color.new(currentAtrBgColor, bgTransparency)
marketRowBg = color.new(marketDataBgColor, bgTransparency)
txtColor = color.black
//=========================
// Display Text
//=========================
dtrText = str.tostring(dtr, format.mintick)
atrText = str.tostring(dailyATR, format.mintick)
pctText = na(pctOfATR) ? "N/A" :
str.tostring(math.round(pctOfATR)) + "%"
currentAtrText = str.tostring(currentTfATR, format.mintick)
avgAtrText = str.tostring(avgAtrLastMinutes, format.mintick)
vixText = na(vixPrice) ? "N/A" :
str.tostring(vixPrice, "#.00")
nqChangeText = na(nqPctChange) ? "N/A" :
(nqPctChange > 0 ? "+" : "") +
str.tostring(nqPctChange, "#.00") + "%"
displayTextTop = "DTR " + dtrText +
" vs ATR " + atrText + " " + pctText
displayTextMid = "Current TF ATR (" +
str.tostring(currentAtrLen) + ") " +
currentAtrText + " | " +
str.tostring(avgMinutes) + "m Avg " + avgAtrText
displayTextBot = "VIX " + vixText +
" | NQ " + nqChangeText + " vs Prev Close"
//=========================
// Table
//=========================
var table atrTable = table.new(
f_getPosition(tablePosition),
1, 3, border_width=1)
if barstate.islast
table.cell(
atrTable, 0, 0, displayTextTop,
text_color=txtColor,
text_size=f_getTextSize(textSizeInput),
bgcolor=cellBg)
table.cell(
atrTable, 0, 1, displayTextMid,
text_color=txtColor,
text_size=f_getTextSize(textSizeInput),
bgcolor=atrRowBg)
table.cell(
atrTable, 0, 2, displayTextBot,
text_color=txtColor,
text_size=f_getTextSize(textSizeInput),
bgcolor=marketRowBg)