本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
// This work is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International
// https://creativecommons.org/licenses/by-nc-sa/4.0/
// © BigBeluga
//@version=5
indicator("Half Trend HeikinAshi [BigBeluga]", "HalfTrend - [BigBeluga]", overlay=true, max_labels_count = 500)
// INPUTS ――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――{
int amp = input.int(5, "Amplitude", minval=1) // Length for indicator calculations
bool useHeikin = input.bool(false, "Heikin Ashi") // Toggle Heikin Ashi mode
string signalSize = input.string("Large", "Signals Size", ["Large", "Small"]) // Size of signals
color up_col = input.color(#26e47b, "Up", group="Trend Colors", inline="1") // Color for upward trend
color dn_col = input.color(#2c75c9, "Down", group="Trend Colors", inline="1") // Color for downward trend
bool trend_candl = input.bool(false, "Candles Color") // Toggle candles color based on trend
// }
// 𝚄𝙳𝚃𝚜 ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――{
type HeikinAshi
float open
float high
float low
float close
var HeikinAshi ha = HeikinAshi.new(na, na, na, na) // Initialize Heikin Ashi UDT
// }
// Variable Initialization
var bool up_signal = na // Signal Up
var bool dn_signal = na // Signal Down
var color trend_col = na // Trend color
var float hl_t = 0. // Half trend value
var float trend_st = 0. // Trend strength
var float oc2 = na // Midpoint for Heikin Ashi calculations
// CALCULATIONS――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――{
series float closeMA = ta.sma(close, amp) // Moving average of close prices
series float highestHigh = ta.highest(amp) // Highest high over the period
series float lowestLow = ta.lowest(amp) // Lowest low over the period
// Initialize half trend on the first bar
if barstate.isfirst
hl_t := close
// Update half trend value based on conditions
switch
closeMA < hl_t and highestHigh < hl_t => hl_t := highestHigh
closeMA > hl_t and lowestLow > hl_t => hl_t := lowestLow
=> hl_t := hl_t
// Calculate Heikin Ashi values based on Half Trend
if useHeikin
ha.close := (nz(ha.open) + nz(ha.high) + nz(ha.low) + hl_t) / 4
ha.open := na(ha.open[1]) ? (hl_t + hl_t[1]) / 2 : (nz(ha.open[1]) + nz(ha.close[1])) / 2
ha.high := math.max(hl_t, math.max(ha.open, ha.close))
ha.low := math.min(hl_t, math.min(ha.open, ha.close))
oc2 := (ha.close + ha.open) / 2
// Calculate trend strength
trend_st := (ha.high - ha.low) / ta.stdev((ha.high - ha.low), 200)
trend_st := (trend_st * 20) > 100 ? 100 : (trend_st * 20)
// Determine trend color
if ta.crossover(hl_t, hl_t[1])
trend_col := up_col
if ta.crossunder(hl_t, hl_t[1])
trend_col := dn_col
// Signal conditions for trend direction
float trend_change = (ha.close - ha.open) / ha.open
up_signal := useHeikin ? ta.crossover (trend_change, 0) : ta.crossover (hl_t, hl_t[1]) and ta.change(trend_col == up_col)
dn_signal := useHeikin ? ta.crossunder(trend_change, 0) : ta.crossunder(hl_t, hl_t[1]) and ta.change(trend_col == dn_col)
// }
// PLOT ――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――{
// Heikin Ashi colors
color haColor = ha.close > ha.open
? color.from_gradient(trend_st, 0, 100, color.new(up_col, 70), up_col)
: color.from_gradient(trend_st, 0, 100, color.new(dn_col, 70), dn_col)
color ha_border_col = ha.close > ha.open
? color.from_gradient(trend_st, 0, 70, color.new(up_col, 70), up_col)
: color.from_gradient(trend_st, 0, 70, color.new(dn_col, 70), dn_col)
// Set candle color based on trend
color candle_col = trend_candl ? (useHeikin ? (ha.close > ha.open ? up_col : dn_col) : trend_col) : na
// Plot half trend line
p1 = plot(useHeikin ? na : hl_t, color = trend_col, linewidth = 2, editable = false)
p2 = plot(hl2, display = display.none, editable = false)
// Fill between lines
fill(p1, p2, hl2, useHeikin ? na : hl_t, na, color.new(trend_col, 80))
// Plot upward signals
plotshape(
up_signal ? (useHeikin ? oc2 : hl_t[1]) : na,
title = "Up",
location = location.absolute, color=up_col,
style = shape.circle,
size = size.tiny,
offset =-1
)
// Plot downward signals
plotshape(
dn_signal ? (useHeikin ? oc2 : hl_t[1]) : na,
title = "Down",
location = location.absolute,
color = dn_col,
style = shape.circle,
size = size.tiny,
offset =-1
)
switch
up_signal =>
label.new(
bar_index-1, (useHeikin ? oc2 : hl_t[1]),
textcolor = up_col,
color = color(na),
text = "⮙",
size = signalSize == "Large" ? size.large : size.normal,
style = label.style_label_up
)
dn_signal =>
label.new(
bar_index-1, (useHeikin ? oc2 : hl_t[1]),
textcolor = dn_col,
color = color(na),
text = "⮛",
size = signalSize == "Large" ? size.large : size.normal,
style = label.style_label_down
)
// Plot Heikin Ashi candles
plotcandle(
ha.open, ha.open, ha.close, ha.close,
title = "Half Trend Heikin Ashi",
color = haColor,
wickcolor = haColor,
bordercolor = ha_border_col,
editable = false
)
// Plot regular candles colored by trend
plotcandle(
open, high, low, close,
title = 'Candles Trend',
color = candle_col,
wickcolor = candle_col,
bordercolor = candle_col,
editable = false
)
// Dashboard
if barstate.islast
var dashboard = table.new(position.top_right, 10, 10, na, color.new(chart.fg_color, 50), 1)
dashboard.cell(0, 0, "Ticker: ", text_color=chart.fg_color, text_halign=text.align_left)
dashboard.cell(1, 0, syminfo.ticker, text_color=up_col, text_halign=text.align_center)
dashboard.cell(0, 1, "TimeFrame: ", text_color=chart.fg_color, text_halign=text.align_left)
dashboard.cell(1, 1, timeframe.period,
text_color = useHeikin ? (ha.close > ha.open ? up_col : dn_col) : trend_col,
text_halign = text.align_center)
dashboard.cell(0, 2, useHeikin ? "Trend Strength: " : "Trend: ",
text_color = chart.fg_color,
text_halign = text.align_left)
dashboard.cell(1, 2, useHeikin ? str.tostring(trend_st, format.percent) : (trend_col == up_col ? "Up" : "Dn"),
text_color = useHeikin ? (ha.close > ha.open ? up_col : dn_col) : trend_col,
text_halign = text.align_center)
// }