本页源码来自 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/
// Renko Overlay [Saikix]
// © Saikix
//@version=6
indicator("Renko Overlay [Saikix]", shorttitle="Saikix-Renko", overlay=true, max_boxes_count=500, max_lines_count=50, max_labels_count=50, behind_chart=false)
G_BASIC = "Basic settings"
sizeMode = input.string("Auto", "Brick size", options=["Auto", "Fixed"], group=G_BASIC, display=display.none,
tooltip="Auto: the average true range of the last 500 bars, rounded to a round number (pips on forex). It is decided bar by bar from past bars only, so history and real time match. Fixed: the size below.")
fixedSize = input.float(10.0, "Fixed size", minval=0.0, step=1.0, group=G_BASIC, display=display.none, active=sizeMode == "Fixed",
tooltip="Pips on forex, price units on other markets.")
bigMult = input.int(4, "Large size (× brick)", minval=2, maxval=20, group=G_BASIC, display=display.none,
tooltip="The large Renko uses bricks this many times the brick size.")
useMid = input.bool(false, "Middle Renko", inline="mid", group=G_BASIC, display=display.none,
tooltip="A third Renko between the two, with bricks this many times the brick size. Shown as dotted outlines and included in the summary, rows and alerts.")
midMult = input.int(2, "", minval=2, maxval=10, inline="mid", group=G_BASIC, display=display.none)
G_DISP = "Chart display"
showB1 = input.bool(true, "Bricks", group=G_DISP, display=display.none,
tooltip="Each brick is drawn over the bars it took to form. Bricks made on the same bar are stacked on that bar.")
showB3 = input.bool(true, "Large bricks", group=G_DISP, display=display.none, tooltip="Thick outlines.")
lookback = input.int(1500, "Lookback (bars)", minval=100, maxval=5000, group=G_DISP, display=display.none,
tooltip="Bricks are drawn on this many recent bars.")
showL1 = input.bool(false, "Renko line", group=G_DISP, display=display.none,
tooltip="A step line at the close of the last brick, in the color of the Renko direction. Each step is one brick.")
showL2 = input.bool(false, "Middle Renko line", group=G_DISP, display=display.none, active=useMid)
showL3 = input.bool(false, "Large Renko line", group=G_DISP, display=display.none)
showLv = input.bool(true, "Show next brick and reversal levels", group=G_DISP, display=display.none,
tooltip="Dashed lines at the price where the next brick forms and where the Renko reverses.")
showRows = input.bool(true, "Show Renko rows", group=G_DISP, display=display.none,
tooltip="Small rows at the bottom of the chart, one per Renko. Top: large. Bottom: brick size (the middle Renko in between when turned on). Each row has the direction of that Renko at that bar. Where all rows have the same color, they agree.")
showBarCol = input.bool(false, "Color bars by Renko direction", group=G_DISP, display=display.none)
showTable = input.bool(true, "Show summary", group=G_DISP, display=display.none)
G_STYLE = "Colors and text"
upCol = input.color(#089981, "Up / down colors", inline="col", group=G_STYLE, display=display.none)
dnCol = input.color(#F23645, "", inline="col", group=G_STYLE, display=display.none)
fillTr = input.int(65, "Brick fill transparency", minval=0, maxval=100, group=G_STYLE, display=display.none)
lineW = input.int(2, "Line width", minval=1, maxval=5, group=G_STYLE, display=display.none,
tooltip="Width of the Renko line. The middle and large lines are one step thinner.")
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"
alRev = input.bool(true, "Reversal", group=G_ALERT, display=display.none,
tooltip="Create the alert with \"Any alert() function call\". Sent at bar close.\nReversal: the brick-size Renko changes direction.")
alAgree = input.bool(true, "All agree", group=G_ALERT, display=display.none, tooltip="The Renko shown (brick size and large, plus middle when turned on) turn the same direction.")
alNew = input.bool(false, "Every new brick", group=G_ALERT, display=display.none, tooltip="Each new brick of the brick-size Renko.")
COL_GRAY = #787B86
IS_FX = syminfo.type == "forex"
PIP = syminfo.mintick * (syminfo.mintick == 0.001 or syminfo.mintick == 0.00001 ? 10 : 1)
fmtP(float p) => str.tostring(p, format.mintick)
sizeText(float s) => IS_FX ? str.tostring(s / PIP, "#.#") + " pips" : fmtP(s)
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"
nice(float x) =>
float e = math.pow(10, math.floor(math.log10(x)))
float m = x / e
(m < 1.5 ? 1.0 : m < 2.25 ? 2.0 : m < 3.5 ? 2.5 : m < 7.5 ? 5.0 : 10.0) * e
float trNow = ta.tr(true)
float cumAvg = ta.cum(trNow) / (bar_index + 1)
float sma500 = ta.sma(trNow, 500)
float avgTr = bar_index < 500 ? cumAvg : sma500
float autoSz = IS_FX ? nice(math.max(avgTr / PIP, 0.1)) * PIP : nice(math.max(avgTr, syminfo.mintick))
float BASE = math.max(syminfo.mintick, math.round((sizeMode == "Fixed" ? (IS_FX ? fixedSize * PIP : fixedSize) : autoSz) / syminfo.mintick) * syminfo.mintick)
type RK
int mult
float size = na
float top = na
float bot = na
int dir = 0
int cnt = 0
int lastBar = 0
int made = 0
bool turned = false
int lastT = 0
drawBrick(RK r, float b, float t, bool up, int left) =>
bool want = r.mult == 1 ? showB1 : r.mult == bigMult ? showB3 : useMid
if want and bar_index >= last_bar_index - lookback
color c = up ? upCol : dnCol
if r.mult == 1
box.new(left, t, time_close, b, xloc=xloc.bar_time, border_color=c, border_width=1, bgcolor=color.new(c, fillTr))
else if r.mult != bigMult
box.new(left, t, time_close, b, xloc=xloc.bar_time, border_color=c, border_width=1, border_style=line.style_dotted, bgcolor=color.new(c, 100))
else
box.new(left, t, time_close, b, xloc=xloc.bar_time, border_color=c, border_width=2, bgcolor=color.new(c, 100))
want
method feed(RK r, float c, float base) =>
float sz = base * r.mult
r.made := 0
r.turned := false
if na(r.size)
r.size := sz
r.bot := math.floor(c / sz) * sz
r.top := r.bot + sz
r.lastBar := bar_index
r.lastT := time_close
else if sz != r.size
float edge = r.dir >= 0 ? r.top : r.bot
r.size := sz
r.top := r.dir >= 0 ? edge : edge + sz
r.bot := r.dir >= 0 ? edge - sz : edge
int left = r.lastT
int guard = 0
while c >= r.top + r.size and guard < 200
r.bot := r.top
r.top := r.top + r.size
r.turned := r.turned or r.dir == -1
r.cnt := r.dir == 1 ? r.cnt + 1 : 1
r.dir := 1
r.made += 1
drawBrick(r, r.bot, r.top, true, left)
guard += 1
while c <= r.bot - r.size and guard < 200
r.top := r.bot
r.bot := r.bot - r.size
r.turned := r.turned or r.dir == 1
r.cnt := r.dir == -1 ? r.cnt + 1 : 1
r.dir := -1
r.made += 1
drawBrick(r, r.bot, r.top, false, left)
guard += 1
if r.made > 0
r.lastBar := bar_index
r.lastT := time_close
r.made
nextLevel(RK r) => r.dir >= 0 ? r.top + r.size : r.bot - r.size
revLevel(RK r) => r.dir >= 0 ? r.bot - r.size : r.top + r.size
var RK r1 = RK.new(1)
var RK r2 = RK.new(midMult)
var RK r3 = RK.new(bigMult)
var int lastAgree = 0
var int agreeStart = na
int agree = 0
if barstate.isconfirmed
r1.feed(close, BASE)
r2.feed(close, BASE)
r3.feed(close, BASE)
agree := r1.dir != 0 and r1.dir == r3.dir and (not useMid or r2.dir == r1.dir) ? r1.dir : 0
if agree != lastAgree
agreeStart := bar_index
string head = "[Saikix-Renko] " + syminfo.ticker + " " + tfLabel(timeframe.period) + " "
if alRev and r1.turned
alert(head + "Reversal: Renko turned " + (r1.dir == 1 ? "up" : "down") + " (" + sizeText(r1.size) + "). Price: " + fmtP(close), alert.freq_once_per_bar_close)
if alAgree and agree != lastAgree and agree != 0
alert(head + (useMid ? "All 3 agree: " : "Both agree: ") + (agree == 1 ? "up" : "down") + ". Price: " + fmtP(close), alert.freq_once_per_bar_close)
if alNew and r1.made > 0
alert(head + "New brick: " + (r1.dir == 1 ? "up" : "down") + ", " + str.tostring(r1.cnt) + (r1.cnt == 1 ? " brick" : " bricks") + ". Price: " + fmtP(close), alert.freq_once_per_bar_close)
lastAgree := agree
else
agree := lastAgree
lvl(RK r) => r.dir == 1 ? r.top : r.dir == -1 ? r.bot : na
dirCol(RK r, int tr) => color.new(r.dir == 1 ? upCol : dnCol, tr)
plot(showL3 ? lvl(r3) : na, "Large Renko line", color=dirCol(r3, 40), linewidth=math.max(1, lineW - 1), style=plot.style_stepline, display=display.pane + display.price_scale)
plot(showL2 and useMid ? lvl(r2) : na, "Middle Renko line", color=dirCol(r2, 40), linewidth=math.max(1, lineW - 1), style=plot.style_stepline, display=display.pane + display.price_scale)
plot(showL1 ? lvl(r1) : na, "Renko line", color=dirCol(r1, 0), linewidth=lineW, style=plot.style_stepline, display=display.pane + display.price_scale)
rowCol(RK r) => r.dir == 1 ? upCol : r.dir == -1 ? dnCol : color.new(COL_GRAY, 50)
plotshape(showRows, "Brick-size row", shape.square, location.bottom, color.new(color.black, 100), text="▬", textcolor=rowCol(r1), size=size.tiny, display=display.pane, editable=false)
plotshape(showRows and useMid, "Middle row", shape.square, location.bottom, color.new(color.black, 100), text="▬\n", textcolor=rowCol(r2), size=size.tiny, display=display.pane, editable=false)
plotshape(showRows and useMid, "Large row", shape.square, location.bottom, color.new(color.black, 100), text="▬\n\n", textcolor=rowCol(r3), size=size.tiny, display=display.pane, editable=false)
plotshape(showRows and not useMid, "Large row (2 rows)", shape.square, location.bottom, color.new(color.black, 100), text="▬\n", textcolor=rowCol(r3), size=size.tiny, display=display.pane, editable=false)
color bc = r1.dir == 1 ? upCol : r1.dir == -1 ? dnCol : COL_GRAY
plotcandle(showBarCol ? open : na, showBarCol ? high : na, showBarCol ? low : na, showBarCol ? close : na, "Renko color", color=bc, wickcolor=bc, bordercolor=bc, editable=false, display=display.pane)
var line lnNext = na
var line lnRev = na
var label lbNext = na
var label lbRev = na
if barstate.islast and showLv and r1.dir != 0
float nx = nextLevel(r1)
float rv = revLevel(r1)
color cn = r1.dir == 1 ? upCol : dnCol
color cr = r1.dir == 1 ? dnCol : upCol
int x0 = math.max(0, bar_index - 30)
if na(lnNext)
lnNext := line.new(x0, nx, bar_index, nx, style=line.style_dashed, color=cn, width=1)
lnRev := line.new(x0, rv, bar_index, rv, style=line.style_dashed, color=cr, width=1)
lbNext := label.new(bar_index, nx, "", color=cn, textcolor=color.white, size=math.max(8, txtSize - 1))
lbRev := label.new(bar_index, rv, "", color=cr, textcolor=color.white, size=math.max(8, txtSize - 1))
lnNext.set_xy1(x0, nx)
lnNext.set_xy2(bar_index, nx)
lnNext.set_color(cn)
lnRev.set_xy1(x0, rv)
lnRev.set_xy2(bar_index, rv)
lnRev.set_color(cr)
lbNext.set_xy(bar_index, nx)
lbNext.set_style(nx > rv ? label.style_label_lower_right : label.style_label_upper_right)
lbNext.set_text("Next brick " + fmtP(nx))
lbNext.set_color(cn)
lbRev.set_xy(bar_index, rv)
lbRev.set_style(rv > nx ? label.style_label_lower_right : label.style_label_upper_right)
lbRev.set_text("Reversal " + fmtP(rv))
lbRev.set_color(cr)
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, 5, bgcolor=chart.bg_color, border_color=chart.bg_color, border_width=3)
rowText(RK r) =>
r.dir == 0 ? "–" : (r.dir == 1 ? "Up" : "Down") + ", " + str.tostring(r.cnt) + (r.cnt == 1 ? " brick" : " bricks")
if barstate.islast and showTable
table.clear(tbl, 0, 0, 1, 4)
if r1.dir == 0
table.cell(tbl, 0, 0, "Renko: no brick yet", text_color=color.white, bgcolor=COL_GRAY, text_size=txtSize)
else
string head = (r1.dir == 1 ? "Renko up" : "Renko down") + " · " + str.tostring(r1.cnt) + (r1.cnt == 1 ? " brick" : " bricks")
string agreeTxt = useMid ? " · all 3 " : " · both "
head += agree == 1 ? agreeTxt + "up" : agree == -1 ? agreeTxt + "down" : ""
table.cell(tbl, 0, 0, head, text_color=color.white, bgcolor=r1.dir == 1 ? upCol : dnCol, text_size=txtSize)
table.merge_cells(tbl, 0, 0, 1, 0)
table.cell(tbl, 0, 1, r1.dir == 1 ? "Turns down below" : "Turns up above", text_color=chart.fg_color, text_size=txtSize - 1, text_halign=text.align_left)
table.cell(tbl, 1, 1, fmtP(revLevel(r1)), text_color=r1.dir == 1 ? dnCol : upCol, text_size=txtSize - 1, text_halign=text.align_left)
table.cell(tbl, 0, 2, sizeText(r1.size), text_color=chart.fg_color, text_size=txtSize - 1, text_halign=text.align_left)
table.cell(tbl, 1, 2, rowText(r1), text_color=r1.dir == 1 ? upCol : r1.dir == -1 ? dnCol : chart.fg_color, text_size=txtSize - 1, text_halign=text.align_left)
int rowL = useMid ? 4 : 3
if useMid
table.cell(tbl, 0, 3, sizeText(r2.size), text_color=chart.fg_color, text_size=txtSize - 1, text_halign=text.align_left)
table.cell(tbl, 1, 3, rowText(r2), text_color=r2.dir == 1 ? upCol : r2.dir == -1 ? dnCol : chart.fg_color, text_size=txtSize - 1, text_halign=text.align_left)
table.cell(tbl, 0, rowL, sizeText(r3.size), text_color=chart.fg_color, text_size=txtSize - 1, text_halign=text.align_left)
table.cell(tbl, 1, rowL, rowText(r3), text_color=r3.dir == 1 ? upCol : r3.dir == -1 ? dnCol : chart.fg_color, text_size=txtSize - 1, text_halign=text.align_left)