本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=5
indicator("Heikin Ashi & Swing Highs/Lows", overlay=true, shorttitle="HA & SHL")
// Heikin Ashi Parameters
tf = input.string("Same as current", "Timeframe period", ["Same as current", "1", "3", "5", "15", "30", "60", "120", "180", "240", "1D", "3D", "1W", "1M"])
vo_short = input.float(1.00, "Short period vertical offset", step=0.005, tooltip="Offset for 1M, 3M, 5M")
vo_mid = input.float(1.05, "Medium period vertical offset", step=0.005, tooltip="Offset for 15M, 30M")
vo_long = input.float(1.0, "Long period vertical offset", step=0.005, tooltip="Offset for 1H, 2H, 3H")
vo_slong = input.float(1.0, "Superlong period vertical offset", step=0.005, tooltip="Offset for 4H, 1D, 3D, W, MO")
// Determine the offset based on the selected timeframe
var float offset = 1.0
var string p = timeframe.period
var string[] p_short = array.from("1", "3", "5")
var string[] p_mid = array.from("15", "30")
var string[] p_long = array.from("60", "120", "180")
var string[] p_slong = array.from("240", "D", "3D", "W", "M")
if array.includes(p_short, p)
offset := vo_short
if array.includes(p_mid, p)
offset := vo_mid
if array.includes(p_long, p)
offset := vo_long
if array.includes(p_slong, p)
offset := vo_slong
// Calculate Heikin Ashi Values
ha_close = (open + high + low + close) / 4
var float ha_open = na
ha_open := na(ha_open[1]) ? (open + close) / 2 : (ha_open[1] + ha_close[1]) / 2
ha_high = math.max(high, math.max(ha_open, ha_close))
ha_low = math.min(low, math.min(ha_open, ha_close))
// Determine the color for Heikin Ashi candles
ha_col = ha_open < ha_close ? color.new(#22ab94, 60) : color.new(#eb4d5c, 60)
// Plot Heikin Ashi Candles
plotcandle(ha_open, ha_high, ha_low, ha_close, color=ha_col, wickcolor=ha_col, bordercolor=ha_col)
// Swing Highs/Lows & Candle Patterns Parameters
length = input.int(21)
swinghCss = input.color(color.red, 'Swing High', group='Style')
swinglCss = input.color(color.teal, 'Swing Low', group='Style')
// Descriptions for Candle Patterns
hammer_ = "The hammer candlestick pattern is formed of a short body with a long lower wick, and is found at the bottom of a downward trend."
+ "\n" + "\n A hammer shows that although there were selling pressures during the day, ultimately a strong buying pressure drove the price back up."
ihammer_ = "The inverted hammer is a similar pattern than the hammer pattern. The only difference being that the upper wick is long, while the lower wick is short."
+ "\n" + "\n It indicates a buying pressure, followed by a selling pressure that was not strong enough to drive the market price down. The inverse hammer suggests that buyers will soon have control of the market."
bulleng_ = "The bullish engulfing pattern is formed of two candlesticks. The first candle is a short red body that is completely engulfed by a larger green candle"
+ "\n" + "\n Though the second day opens lower than the first, the bullish market pushes the price up, culminating in an obvious win for buyers."
hanging_ = "The hanging man is the bearish equivalent of a hammer; it has the same shape but forms at the end of an uptrend."
+ "\n" + "It indicates that there was a significant sell-off during the day, but that buyers were able to push the price up again. The large sell-off is often seen as an indication that the bulls are losing control of the market."
shooting_ = "The shooting star is the same shape as the inverted hammer, but is formed in an uptrend: it has a small lower body, and a long upper wick."
+ "\n" + "Usually, the market will gap slightly higher on opening and rally to an intra-day high before closing at a price just above the open – like a star falling to the ground."
beareng_ = "A bearish engulfing pattern occurs at the end of an uptrend. The first candle has a small green body that is engulfed by a subsequent long red candle."
+ "\n" + "It signifies a peak or slowdown of price movement, and is a sign of an impending market downturn. The lower the second candle goes, the more significant the trend is likely to be."
// Calculate Swing Highs/Lows
var float phy = na
var float ply = na
o = open[length]
h = high[length]
l = low[length]
c = close[length]
d = math.abs(c - o)
ph = ta.pivothigh(length, length)
pl = ta.pivotlow(length, length)
// Patterns Detection
hammer = (pl and math.min(o,c) - l > d and h - math.max(c,o) < d) ? 'Hammer' : na
ihammer = (pl and h - math.max(c,o) > d and math.min(c,o) - l < d) ? 'Inverted Hammer' : na
bulleng = (c > o and c[1] < o[1] and c > o[1] and o < c[1]) ? 'Bullish Engulfing' : na
hanging = (ph and math.min(c,o) - l > d and h - math.max(o,c) < d) ? 'Hanging Man' : na
shooting = (ph and h - math.max(o,c) > d and math.min(c,o) - l < d) ? 'Shooting Star' : na
beareng = (c > o and c[1] < o[1] and c > o[1] and o < c[1]) ? 'Bearish Engulfing' : na
// Set Labels for Patterns
n = bar_index
var string pattern_title = na
var string pattern_description = ""
if ph
H = ph > phy ? 'HH' : 'LH'
pattern_title := na(hammer) ? pattern_title : hammer
pattern_description := na(hammer) ? pattern_description : hammer_
pattern_title := na(ihammer) ? pattern_title : ihammer
pattern_description := na(ihammer) ? pattern_description : ihammer_
pattern_title := na(bulleng) ? pattern_title : bulleng
pattern_description := na(bulleng) ? pattern_description : bulleng_
pattern_title := na(hanging) ? pattern_title : hanging
pattern_description := na(hanging) ? pattern_description : hanging_
pattern_title := na(shooting) ? pattern_title : shooting
pattern_description := na(shooting) ? pattern_description : shooting_
pattern_title := na(beareng) ? pattern_title : beareng
pattern_description := na(beareng) ? pattern_description : beareng_
label.new(n[length], ph, H + "\n" + (na(pattern_title) ? 'None' : pattern_title), color=color.new(color.white, 0), style=label.style_label_down, textcolor=swinghCss, tooltip=pattern_description)
phy := ph
else if pl
L = pl < ply ? 'LL' : 'HL'
pattern_title := na(hammer) ? pattern_title : hammer
pattern_description := na(hammer) ? pattern_description : hammer_
pattern_title := na(ihammer) ? pattern_title : ihammer
pattern_description := na(ihammer) ? pattern_description : ihammer_
pattern_title := na(bulleng) ? pattern_title : bulleng
pattern_description := na(bulleng) ? pattern_description : bulleng_
pattern_title := na(hanging) ? pattern_title : hanging
pattern_description := na(hanging) ? pattern_description : hanging_
pattern_title := na(shooting) ? pattern_title : shooting
pattern_description := na(shooting) ? pattern_description : shooting_
pattern_title := na(beareng) ? pattern_title : beareng
pattern_description := na(beareng) ? pattern_description : beareng_
label.new(n[length], pl, L + "\n" + (na(pattern_title) ? 'None' : pattern_title), color=color.new(color.white, 0), style=label.style_label_up, textcolor=swinglCss, tooltip=pattern_description)
ply := pl