本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator("Trend Hunter Line", overlay=true, max_labels_count=500)
//==== Settings ====
atrLength = input.int(10, title="ATR Period")
multiplier = input.float(3.0, title="SuperTrend Multiplier", step=0.1)
//==== SuperTrend Calculation ====
[stLine, stDir] = ta.supertrend(multiplier, atrLength)
//Official reversal signals
buySignal = ta.crossunder(stDir, 0)
sellSignal = ta.crossover(stDir, 0)
//==== Record entry price and trade type ====
var float entryPrice = na
var string tradeType = ""
if buySignal
entryPrice := close
tradeType := "LONG"
if sellSignal
entryPrice := close
tradeType := "SHORT"
//Close trade trigger conditions
closeLongTrade = sellSignal
closeShortTrade = buySignal
//==== Drawing ====
//Main trend line
lineColor = stDir < 0 ? color.new(color.teal, 0) : color.new(color.maroon, 0)
plot(stLine, color=lineColor, linewidth=3, title="SuperTrend Line")
// BUY label, slight shift below candle
if buySignal
label.new(bar_index, low * 0.99, "BUY",
color=color.new(color.green, 15),
textcolor=color.white,
style=label.style_label_up,
size=size.normal)
// SELL label, slight shift above candle
if sellSignal
label.new(bar_index, high * 1.01, "SELL",
color=color.new(color.red, 15),
textcolor=color.white,
style=label.style_label_down,
size=size.normal)
// Profit percentage labels
if closeLongTrade and tradeType == "LONG" and not na(entryPrice)
rate = (close / entryPrice - 1) * 100
textColor = rate >= 0 ? color.green : color.red
label.new(bar_index - ta.barssince(buySignal), low * 0.96, str.format("{0:.1f}%", rate),
color=color.gray, textcolor=textColor, size=size.tiny)
if closeShortTrade and tradeType == "SHORT" and not na(entryPrice)
rate = (entryPrice / close - 1) * 100
textColor = rate >= 0 ? color.green : color.red
label.new(bar_index - ta.barssince(sellSignal), high * 1.04, str.format("{0:.1f}%", rate),
color=color.gray, textcolor=textColor, size=size.tiny)
// Profit point labels
if sellSignal and tradeType == "LONG" and not na(entryPrice)
profitPoint = close - entryPrice
label.new(bar_index, high * 1.015, str.format("{0:.1f} Pts", profitPoint),
color=color.new(color.green,15), textcolor=color.white, size=size.tiny)
if buySignal and tradeType == "SHORT" and not na(entryPrice)
profitPoint = entryPrice - close
label.new(bar_index, low * 0.985, str.format("-{0:.1f} Pts", profitPoint),
color=color.new(color.red,15), textcolor=color.white, size=size.tiny)