本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator(" Auto Retracement Levels", overlay = true, max_bars_back = 5000, max_lines_count = 50, max_labels_count = 50)
// =====================================================
// INPUT SETTINGS — CANDLES ONLY
// =====================================================
FP = input.int(
defval = 25,
title = "Candles to Lookback",
minval = 1,
maxval = 5000)
Reverse = input.bool(
defval = false,
title = "Reverse Levels?")
// =====================================================
// STYLE INPUTS
// =====================================================
LineStyle = input.string(
"Solid",
options = ["Dotted", "Solid"],
title = "Level Line Style")
LineWidth = input.int(
1,
minval = 1,
maxval = 3,
title = "Level Line Width")
LabelOffsetBars = input.int(
10,
minval = 0,
title = "Label Horizontal Offset (Bars Right)")
LabelOffsetPrice = input.float(
0.1,
step = 0.1,
title = "Label Vertical Offset (Above Line)")
// =====================================================
// FIXED LEVELS & COLORS
// Level percentages cannot be changed in settings
// =====================================================
group_level = "Levels & Colors"
Level0 = 0.00
Level10 = 0.10
Level20 = 0.20
Level30 = 0.30
Level40 = 0.40
Level50 = 0.50
Level60 = 0.60
Level70 = 0.70
Level80 = 0.80
Level90 = 0.90
Level100 = 1.00
Level0Color = input.color(color.rgb(120, 123, 134), "Level 0 Color", group = group_level)
Level10Color = input.color(color.rgb(44, 122, 5), "Level 10 Color", group = group_level)
Level20Color = input.color(color.rgb(44, 122, 5), "Level 20 Color", group = group_level)
Level30Color = input.color(color.rgb(44, 122, 5), "Level 30 Color", group = group_level)
Level40Color = input.color(color.rgb(44, 122, 5), "Level 40 Color", group = group_level)
Level50Color = input.color(color.rgb(44, 122, 5), "Level 50 Color", group = group_level)
Level60Color = input.color(color.rgb(44, 122, 5), "Level 60 Color", group = group_level)
Level70Color = input.color(color.rgb(44, 122, 5), "Level 70 Color", group = group_level)
Level80Color = input.color(color.rgb(44, 122, 5), "Level 80 Color", group = group_level)
Level90Color = input.color(color.rgb(44, 122, 5), "Level 90 Color", group = group_level)
Level100Color = input.color(color.rgb(120, 123, 134), "Level 100 Color", group = group_level)
// =====================================================
// CALCULATE LOOKBACK — CANDLES ONLY
// =====================================================
int lookback = FP
// =====================================================
// GET HIGH / LOW
// =====================================================
Fhigh = ta.highest(high, lookback)
Flow = ta.lowest(low, lookback)
FH = ta.highestbars(high, lookback)
FL = ta.lowestbars(low, lookback)
revLevels = not Reverse ? FL > FH : FL < FH
// =====================================================
// LEVEL PRICE FUNCTION
// =====================================================
Level_x(n) =>
revLevels ? (Fhigh - Flow) * n + Flow : Fhigh - (Fhigh - Flow) * n
// =====================================================
// LINE STYLE SETUP
// =====================================================
EXTEND = extend.both
STYLE = LineStyle == "Solid" ? line.style_solid : line.style_dotted
WIDTH = LineWidth
BB = FL < FH ? bar_index - FL : bar_index - FH
// =====================================================
// DRAW LEVEL LINE
// =====================================================
Level_line(x, col) =>
var line ln = na
line.delete(ln)
ln := line.new(
BB, x,
bar_index, x,
color = col,
extend = EXTEND,
style = STYLE,
width = WIDTH)
ln
// =====================================================
// DRAW LEVEL LABEL
// =====================================================
Level_label(x, txt, col) =>
var label lbl = na
label.delete(lbl)
lbl := label.new(
bar_index + LabelOffsetBars,
x + LabelOffsetPrice,
txt,
textcolor = col,
color = color.new(#000000, 100),
style = label.style_label_left,
yloc = yloc.price)
lbl
// =====================================================
// LEVEL LINES
// =====================================================
L0 = Level_line(Level_x(Level0), Level0Color)
L10 = Level_line(Level_x(Level10), Level10Color)
L20 = Level_line(Level_x(Level20), Level20Color)
L30 = Level_line(Level_x(Level30), Level30Color)
L40 = Level_line(Level_x(Level40), Level40Color)
L50 = Level_line(Level_x(Level50), Level50Color)
L60 = Level_line(Level_x(Level60), Level60Color)
L70 = Level_line(Level_x(Level70), Level70Color)
L80 = Level_line(Level_x(Level80), Level80Color)
L90 = Level_line(Level_x(Level90), Level90Color)
L100 = Level_line(Level_x(Level100), Level100Color)
// =====================================================
// LEVEL LABELS
// =====================================================
LL0 = Level_label(Level_x(Level0), "0", Level0Color)
LL10 = Level_label(Level_x(Level10), "10", Level10Color)
LL20 = Level_label(Level_x(Level20), "20", Level20Color)
LL30 = Level_label(Level_x(Level30), "30", Level30Color)
LL40 = Level_label(Level_x(Level40), "40", Level40Color)
LL50 = Level_label(Level_x(Level50), "50", Level50Color)
LL60 = Level_label(Level_x(Level60), "60", Level60Color)
LL70 = Level_label(Level_x(Level70), "70", Level70Color)
LL80 = Level_label(Level_x(Level80), "80", Level80Color)
LL90 = Level_label(Level_x(Level90), "90", Level90Color)
LL100 = Level_label(Level_x(Level100), "100", Level100Color)
// =====================================================
// CANDLE GAP DETECTION & FILLING
// =====================================================
group_gap = "Candles Gap Detection & Filling"
enableGapFilling = input.bool(
false,
title = "Enable Gap Filling",
group = group_gap)
// Candle values
myOpen = open
myClose = close
myHigh = high
myLow = low
if enableGapFilling and not na(close[1])
myOpen := close[1]
myClose := close
myHigh := math.max(high, close[1])
myLow := math.min(low, close[1])
// =====================================================
// CANDLE COLORS
// =====================================================
candleColor = myOpen <= myClose ? color.rgb(9, 146, 132) : color.rgb(250, 77, 77)
// =====================================================
// PLOT CANDLES
// =====================================================
plotcandle(
myOpen,
myHigh,
myLow,
myClose,
color = candleColor,
bordercolor = candleColor,
wickcolor = candleColor)