本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// Heikin Ashi Multi-Timeframe [Saikix]
// © Saikix
//@version=6
indicator("Heikin Ashi Multi-Timeframe [Saikix]", shorttitle="Saikix-HeikinAshi-Master", overlay=true, max_boxes_count=500, max_lines_count=500, behind_chart=false)
G_BASIC = "Basic settings"
autoTf = input.bool(true, "Auto timeframes", group=G_BASIC, display=display.none,
tooltip="The next two timeframes up from the chart: 5m and 15m on 1m, 15m and 1h on 5m, 1h and 4h on 15m, 4h and D on 30m and 1h, D and W on 4h, W and M on D.")
midIn = input.timeframe("240", "Middle timeframe", group=G_BASIC, display=display.none, active=not autoTf,
tooltip="Must be higher than the chart.")
longIn = input.timeframe("D", "Long timeframe", group=G_BASIC, display=display.none, active=not autoTf,
tooltip="Must be higher than the middle timeframe.")
haType = input.string("Standard", "Heikin Ashi type", options=["Standard", "Smoothed"], group=G_BASIC, display=display.none,
tooltip="Smoothed: an EMA of the Heikin Ashi values, counted in each timeframe's own candles. Used for all three timeframes.")
smoothLen = input.int(5, "Smoothing length", minval=1, maxval=100, group=G_BASIC, display=display.none, active=haType == "Smoothed")
G_DISP = "Chart display"
showLongC = input.bool(true, "Long timeframe candles", inline="L", group=G_DISP, display=display.none,
tooltip="The higher timeframes' Heikin Ashi candles drawn over the chart, one box per candle. Line style, line width and fill transparency (100 = no fill).")
longSty = input.string("Solid", "", options=["Solid", "Dashed", "Dotted"], inline="L", group=G_DISP, display=display.none)
longW = input.int(3, "", minval=1, maxval=5, inline="L", group=G_DISP, display=display.none)
longTr = input.int(80, "", minval=0, maxval=100, inline="L", group=G_DISP, display=display.none)
showMidC = input.bool(true, "Middle timeframe candles", inline="M", group=G_DISP, display=display.none,
tooltip="Line style, line width and fill transparency (100 = no fill).")
midSty = input.string("Dotted", "", options=["Solid", "Dashed", "Dotted"], inline="M", group=G_DISP, display=display.none)
midW = input.int(2, "", minval=1, maxval=5, inline="M", group=G_DISP, display=display.none)
midTr = input.int(100, "", minval=0, maxval=100, inline="M", group=G_DISP, display=display.none)
candleCnt = input.int(60, "Candles to draw", minval=5, maxval=150, group=G_DISP, display=display.none)
showRibbon = input.bool(true, "Show timeframe rows", group=G_DISP, display=display.none,
tooltip="Three small rows at the bottom of the chart. Top: long timeframe. Middle: middle timeframe. Bottom: chart timeframe. Each row has the color its Heikin Ashi candle had at that bar. Where all three rows have the same color, the timeframes agree.")
showBarCol = input.bool(false, "Color bars by chart Heikin Ashi", group=G_DISP, display=display.none,
tooltip="Colors the chart's candles by the chart timeframe's Heikin Ashi color.")
showTable = input.bool(true, "Show summary", group=G_DISP, display=display.none)
G_STYLE = "Colors and text"
upCol = input.color(#089981, "Bullish / bearish colors", inline="col", group=G_STYLE, display=display.none)
dnCol = input.color(#F23645, "", inline="col", group=G_STYLE, display=display.none)
txtSize = input.int(13, "Text size", minval=8, maxval=30, group=G_STYLE, display=display.none)
tblPos = input.string("Top right", "Summary position", options=["Top right", "Top left", "Bottom right", "Bottom left"], group=G_STYLE, display=display.none)
G_ALERT = "Alerts"
alertIn = input.string("All 3 agree", "Alert condition", options=["None", "All 3 agree", "All 3 agree or stop agreeing"], group=G_ALERT, display=display.none,
tooltip="Create the alert with \"Any alert() function call\". Sent at bar close.\nAll 3 agree: when the three timeframes become all bullish or all bearish.\nAll 3 agree or stop agreeing: also when they stop being all the same color.")
COL_GRAY = #787B86
lineStyle(string s) => s == "Dashed" ? line.style_dashed : s == "Dotted" ? line.style_dotted : line.style_solid
tfLabel(string tf) =>
int sec = timeframe.in_seconds(tf)
sec < 3600 ? str.tostring(math.round(sec / 60.0), "#") + "m" : sec < 86400 ? str.tostring(math.round(sec / 3600.0), "#") + "h" : sec < 604800 ? "D" : sec < 2419200 ? "W" : "M"
nextTf(string tf) =>
int s = timeframe.in_seconds(tf)
s * 2 < 300 ? "5" : s * 2 < 900 ? "15" : s * 2 < 3600 ? "60" : s * 2 < 14400 ? "240" : s * 2 < 86400 ? "D" : s * 2 < 604800 ? "W" : "M"
MID_TF = autoTf ? nextTf(timeframe.period) : midIn
LONG_TF = autoTf ? nextTf(MID_TF) : longIn
tfOk = timeframe.in_seconds(MID_TF) > timeframe.in_seconds(timeframe.period) and timeframe.in_seconds(LONG_TF) > timeframe.in_seconds(MID_TF)
fmtP(float p) => str.tostring(p, format.mintick)
type HA
float o
float h
float l
float c
float ho
float hc
float hh
float hl
bool bull
int consec
float prevHo
float prevHc
int prevDir
int prevConsec
int startBar
float eo
float eh
float el
float ec
float so
float sh
float sl
float sc
array<box> boxes
array<line> wicks
newHA() =>
HA.new(na, na, na, na, na, na, na, na, true, 0, na, na, 0, 0, 0, na, na, na, na, na, na, na, na, array.new_box(), array.new_line())
SMOOTH = haType == "Smoothed"
EMA_A = 2.0 / (smoothLen + 1)
emaNext(float prev, float x) => na(prev) ? x : prev + EMA_A * (x - prev)
method step(HA x, bool newP) =>
if newP and not na(x.o)
x.prevHo := x.ho
x.prevHc := x.hc
x.prevDir := x.bull ? 1 : -1
x.prevConsec := x.consec
x.eo := x.so
x.eh := x.sh
x.el := x.sl
x.ec := x.sc
if newP or na(x.o)
x.o := open
x.h := high
x.l := low
x.c := close
x.startBar := bar_index
else
x.h := math.max(x.h, high)
x.l := math.min(x.l, low)
x.c := close
x.hc := (x.o + x.h + x.l + x.c) / 4.0
x.ho := na(x.prevHo) ? (x.o + x.c) / 2.0 : (x.prevHo + x.prevHc) / 2.0
x.hh := math.max(x.h, math.max(x.ho, x.hc))
x.hl := math.min(x.l, math.min(x.ho, x.hc))
x.so := SMOOTH ? emaNext(x.eo, x.ho) : x.ho
x.sh := SMOOTH ? emaNext(x.eh, x.hh) : x.hh
x.sl := SMOOTH ? emaNext(x.el, x.hl) : x.hl
x.sc := SMOOTH ? emaNext(x.ec, x.hc) : x.hc
x.bull := x.sc >= x.so
x.consec := x.prevDir == 0 ? 1 : (x.bull ? 1 : -1) == x.prevDir ? x.prevConsec + 1 : 1
x
method drawCandle(HA x, bool newP, bool show, string sty, int w, int tr) =>
if show
color c = x.bull ? upCol : dnCol
float top = math.max(x.so, x.sc)
float bot = math.min(x.so, x.sc)
float wHi = math.max(x.sh, top)
float wLo = math.min(x.sl, bot)
int mid = int((x.startBar + bar_index) / 2)
if newP or x.boxes.size() == 0
x.boxes.push(box.new(x.startBar, top, bar_index, bot, border_color=c, border_width=w, border_style=lineStyle(sty), bgcolor=color.new(c, tr)))
x.wicks.push(line.new(mid, wHi, mid, top, color=c, width=w, style=lineStyle(sty)))
x.wicks.push(line.new(mid, bot, mid, wLo, color=c, width=w, style=lineStyle(sty)))
if x.boxes.size() > candleCnt
x.boxes.shift().delete()
x.wicks.shift().delete()
x.wicks.shift().delete()
else
box b = x.boxes.last()
b.set_right(bar_index)
b.set_top(top)
b.set_bottom(bot)
b.set_border_color(c)
b.set_bgcolor(color.new(c, tr))
int n = x.wicks.size()
line w1 = x.wicks.get(n - 2)
line w2 = x.wicks.get(n - 1)
w1.set_xy1(mid, wHi)
w1.set_xy2(mid, top)
w1.set_color(c)
w2.set_xy1(mid, bot)
w2.set_xy2(mid, wLo)
w2.set_color(c)
var HA haS = newHA()
var HA haM = newHA()
var HA haL = newHA()
bool newS = true
bool newM = timeframe.change(MID_TF)
bool newL = timeframe.change(LONG_TF)
haS.step(newS)
haM.step(newM)
haL.step(newL)
if tfOk
haL.drawCandle(newL, showLongC, longSty, longW, longTr)
haM.drawCandle(newM, showMidC, midSty, midW, midTr)
int dirS = barstate.isconfirmed ? (haS.bull ? 1 : -1) : haS.prevDir
int dirM = haM.prevDir
int dirL = haL.prevDir
int agree = dirS == 1 and dirM == 1 and dirL == 1 ? 1 : dirS == -1 and dirM == -1 and dirL == -1 ? -1 : 0
sendAlert(string body) =>
alert("[Saikix-HeikinAshi-Master] " + syminfo.ticker + " " + tfLabel(timeframe.period) + " " + body + ". Price: " + fmtP(close), alert.freq_once_per_bar_close)
ROWS = tfOk and showRibbon
rowCol(HA x) => x.bull ? upCol : dnCol
plotshape(ROWS, "Chart timeframe row", shape.square, location.bottom, color.new(color.black, 100), text="▬", textcolor=rowCol(haS), size=size.tiny, display=display.pane, editable=false)
plotshape(ROWS, "Middle timeframe row", shape.square, location.bottom, color.new(color.black, 100), text="▬\n", textcolor=rowCol(haM), size=size.tiny, display=display.pane, editable=false)
plotshape(ROWS, "Long timeframe row", shape.square, location.bottom, color.new(color.black, 100), text="▬\n\n", textcolor=rowCol(haL), size=size.tiny, display=display.pane, editable=false)
color barCol = haS.bull ? upCol : dnCol
plotcandle(showBarCol ? open : na, showBarCol ? high : na, showBarCol ? low : na, showBarCol ? close : na, "Chart Heikin Ashi color", color=barCol, wickcolor=barCol, bordercolor=barCol, editable=false, display=display.pane)
var int agreeStart = na
var int lastAgree = 0
if barstate.isconfirmed and tfOk and agree != lastAgree
agreeStart := bar_index
if barstate.isconfirmed and tfOk
if agree != lastAgree and alertIn != "None"
string tfs = tfLabel(timeframe.period) + ", " + tfLabel(MID_TF) + " and " + tfLabel(LONG_TF)
if agree == 1
sendAlert("All 3 bullish: " + tfs + " Heikin Ashi are bullish")
else if agree == -1
sendAlert("All 3 bearish: " + tfs + " Heikin Ashi are bearish")
else if alertIn == "All 3 agree or stop agreeing"
sendAlert((lastAgree == 1 ? "No longer all 3 bullish" : "No longer all 3 bearish") + ": " + tfs)
lastAgree := agree
var table tbl = table.new(tblPos == "Top left" ? position.top_left : tblPos == "Bottom right" ? position.bottom_right : tblPos == "Bottom left" ? position.bottom_left : position.top_right, 2, 4, bgcolor=chart.bg_color, border_color=chart.bg_color, border_width=3)
rowText(HA x, bool htf) =>
int d = htf ? x.prevDir : (barstate.isconfirmed ? (x.bull ? 1 : -1) : x.prevDir)
int n = htf ? x.prevConsec : (barstate.isconfirmed ? x.consec : x.prevConsec)
string t = d == 0 ? "–" : (d == 1 ? "Bullish" : "Bearish") + ", " + str.tostring(n) + (n == 1 ? " candle" : " candles")
htf ? t + " (forming " + (x.bull ? "bullish" : "bearish") + ")" : t
if barstate.islast and showTable
table.clear(tbl, 0, 0, 1, 3)
if not tfOk
table.cell(tbl, 0, 0, "Set the middle and long timeframes higher than the chart", text_color=color.white, bgcolor=COL_GRAY, text_size=txtSize)
else
int nBull = (dirS == 1 ? 1 : 0) + (dirM == 1 ? 1 : 0) + (dirL == 1 ? 1 : 0)
int since = na(agreeStart) ? 0 : bar_index - agreeStart + (barstate.isconfirmed ? 1 : 0)
string head = (nBull == 3 ? "All 3 bullish" : nBull == 0 ? "All 3 bearish" : nBull == 2 ? "2 of 3 bullish" : "2 of 3 bearish") + (nBull == 3 or nBull == 0 ? " · " + str.tostring(since) + (since == 1 ? " bar" : " bars") : "")
color hc = nBull == 3 ? upCol : nBull == 0 ? dnCol : COL_GRAY
table.cell(tbl, 0, 0, head, text_color=color.white, bgcolor=hc, text_size=txtSize)
table.merge_cells(tbl, 0, 0, 1, 0)
table.cell(tbl, 0, 1, tfLabel(timeframe.period), text_color=chart.fg_color, text_size=txtSize - 1, text_halign=text.align_left)
table.cell(tbl, 1, 1, rowText(haS, false), text_color=dirS == 1 ? upCol : dnCol, text_size=txtSize - 1, text_halign=text.align_left)
table.cell(tbl, 0, 2, tfLabel(MID_TF), text_color=chart.fg_color, text_size=txtSize - 1, text_halign=text.align_left)
table.cell(tbl, 1, 2, rowText(haM, true), text_color=dirM == 1 ? upCol : dnCol, text_size=txtSize - 1, text_halign=text.align_left)
table.cell(tbl, 0, 3, tfLabel(LONG_TF), text_color=chart.fg_color, text_size=txtSize - 1, text_halign=text.align_left)
table.cell(tbl, 1, 3, rowText(haL, true), text_color=dirL == 1 ? upCol : dnCol, text_size=txtSize - 1, text_halign=text.align_left)