本页源码来自 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/
// © tvenn
//@version=5
indicator(title="RF+ Replay for Heikin Ashi", shorttitle="RF+ Replay", overlay = true, max_bars_back = 1000, max_lines_count = 400, max_labels_count = 400)
//=Constants
green = #95BD5F
red = #EA1889
mocha = #C0A392
transp = color.new(#000000, 100)
grp_main = "Heikin Ashi candles"
haTicker = ticker.heikinashi(syminfo.tickerid)
[haO, haH, haL, haC] = request.security(haTicker, timeframe.period, [open, high, low, close])
useHAformula = input(true, title="Use Heikin Ashi formula", tooltip="Range Filters and moving averages will be calculated using Heikin Ashi OHLC values when this is enabled.")
showHACandles = input(true, title="Show Heikin Ashi candles", tooltip="This will print HA candles onto your standard chart, to help backtest using the Tradingview replay mode using Heikin Ashi type candles while using realprice data.\n\nYou will need to hide your actual chart candles by making the candle bodies, borders and wicks transparent within the Tradingviews Chart Settings, so you don't see two sets of candles.\n\nThese settings can be found by clicking the settings Cog icon, or right-clicking on your chart and selecting 'Chart Settings'.", group=grp_main)
candleUpColor = input.color(#FFFFFF, title="", inline="1", group=grp_main)
candleDownColor = input.color(color.rgb(155, 155, 155), title="", inline="1", group=grp_main)
candleBodyEnabled = input(true, title="Body", inline="1", group=grp_main)
candleUpBorderColor = input.color(color.rgb(110, 110, 110), title="", inline="2", group=grp_main)
candleDownBorderColor = input.color(color.rgb(110, 110, 110), title="", inline="2", group=grp_main)
candleBorderEnabled = input(true, title="Border", inline="2", group=grp_main)
wickUpColor = input.color(color.rgb(110, 110, 110), title="", inline="3", group=grp_main)
wickDownColor = input.color(color.rgb(110, 110, 110), title="", inline="3", group=grp_main)
candleWickEnabled = input(true, title="Wick", inline="3", group=grp_main)
candleColor = haC >= haO and showHACandles and candleBodyEnabled ? candleUpColor : showHACandles and candleBodyEnabled ? candleDownColor : na
candleBorderColor = haC >= haO and showHACandles and candleBorderEnabled ? candleUpBorderColor : showHACandles and candleBorderEnabled ? candleDownBorderColor : na
candleWickColor = haC >= haO and showHACandles and candleWickEnabled ? wickUpColor : showHACandles and candleWickEnabled ? wickDownColor : na
//=Range Filter general settings
//==================================================================================================================================
grp_RF = "Range Filters"
showRangeFilters = input(true, title="Show Range Filters", group=grp_RF)
rfUpColor = input.color(#ffffff, title="", group=grp_RF, inline="1")
rfDownColor = input.color(color.blue, title="", group=grp_RF, inline="1")
rfLineWidth = input.int(1, minval=1, maxval=3, title="", group=grp_RF, inline="1")
//=Real price line & real price close dots
//==================================================================================================================================
//Real price line and dot settings
grp_HA = "Real price for Heikin Ashi candles"
realclosedotscolor = input(color.new(color.blue, 50), title="", group=grp_HA, inline="1")
plotrealclosedots = input(true, title="Show real price close dots", group=grp_HA, inline="1")
showrealpricelinecolor = input(color.new(#949494, 20), title="", group=grp_HA, inline="2")
showrealprice = input(false, title="show real price line (for HA candles)", group=grp_HA, inline="2")
realpricewidth = input.int(1, options=[1, 2], title="", group=grp_HA, inline="3")
realpricestyle = input.string("Dotted", options=["Dotted", "Solid", "Dashed"], title="", group=grp_HA, inline="3")
real_price = ticker.new(prefix=syminfo.prefix, ticker=syminfo.ticker)
real_close = request.security(symbol=real_price, timeframe='', expression=close, gaps=barmerge.gaps_off, lookahead=barmerge.lookahead_off)
if(showrealprice and realpricestyle == "Dotted")
real_price_line = line.new(bar_index[1], real_close, bar_index, real_close, xloc.bar_index, extend.both, showrealpricelinecolor, line.style_dotted, realpricewidth)
line.delete(real_price_line[1])
if(showrealprice and realpricestyle == "Solid")
real_price_line = line.new(bar_index[1], real_close, bar_index, real_close, xloc.bar_index, extend.both, showrealpricelinecolor, line.style_solid, realpricewidth)
line.delete(real_price_line[1])
if(showrealprice and realpricestyle == "Dashed")
real_price_line = line.new(bar_index[1], real_close, bar_index, real_close, xloc.bar_index, extend.both, showrealpricelinecolor, line.style_dashed, realpricewidth)
line.delete(real_price_line[1])
//=Sessions
//==================================================================================================================================
london_color = color.new(color.green, 90)
newyork_color = color.new(color.blue, 90)
tokyo_color = color.new(color.red, 90)
sydney_color = color.new(color.orange, 90)
grp_SESS = "Sessions"
enableSessions = input(true, title="Show sessions", group=grp_SESS, tooltip="This simply toggles your sessions so you don't have to reselect all the options to turn your prefered sessions settings on and off.")
utcsetting = input.string(title="Select UTC timezone", defval="UTC+1", options=["UTC-11","UTC-10","UTC-9","UTC-8","UTC-7","UTC-6","UTC-5","UTC-4","UTC-3","UTC-2:30","UTC-2","UTC-1","UTC","UTC+1","UTC+2","UTC+3","UTC+4","UTC+4:30","UTC+5","UTC+6","UTC+7","UTC+8","UTC+9","UTC+10","UTC+11","UTC+12","UTC+13"], group=grp_SESS)
daylightsavings = input(false, title="Add 1hr for daylight savings time (DST)", group=grp_SESS)
//Show sessions
timelondon = time(timeframe.period, daylightsavings ? "0800-1700:23456" : "0700-1600:23456", utcsetting)
timenewyork = time(timeframe.period, daylightsavings ? "1300-2200:23456" : "1200-2100:23456", utcsetting)
timetokyo = time(timeframe.period, daylightsavings ? "0000-0900:23456" : "2300-0800:23456", utcsetting)
timesydney = time(timeframe.period, daylightsavings ? "2200-0700:23456" : "2100-0600:23456", utcsetting)
//Show Trading Sessions
//Inverted Sessions Logic
invertedsessioncolor= input.color(color.new(color.blue, 90), title="", group=grp_SESS, inline="0")
invertsessioncolour = input(false, title="Enable inverted session(s) color", tooltip="This will color the background of the entire chart - except the trading session(s) you select, to give you a clear chart, free of a distracting background colors. 👌", group=grp_SESS, inline="0")
//London Session
londonsessioncolor = input.color(london_color, title="", group=grp_SESS, inline="1")
showlondonsession = input(false, title="Show London session", group=grp_SESS, inline="1")
bgcolor((enableSessions and timelondon and showlondonsession and not invertsessioncolour) ? londonsessioncolor : na)
//New York Session
newyorksessioncolor = input.color(newyork_color, title="", group=grp_SESS, inline="2")
shownewyorksession = input(true, title="Show New York session", group=grp_SESS, inline="2")
bgcolor((enableSessions and timenewyork and shownewyorksession and not invertsessioncolour) ? newyorksessioncolor : na)
//Tokyo Session
tokyosessioncolor = input.color(tokyo_color, title="", group=grp_SESS, inline="3")
showtokyosession = input(false, title="Show Tokyo session", group=grp_SESS, inline="3")
bgcolor((enableSessions and timetokyo and showtokyosession and not invertsessioncolour) ? tokyosessioncolor : na)
//Sydney Session
sydneysessioncolor = input.color(sydney_color, title="", group=grp_SESS, inline="4")
showsydneysession = input(false, title="Show Sydney session", group=grp_SESS, inline="4")
bgcolor((enableSessions and timesydney and showsydneysession and not invertsessioncolour) ? sydneysessioncolor : na)
bgcolor((enableSessions and invertsessioncolour and not (showlondonsession ? timelondon : na) and not (shownewyorksession ? timenewyork : na) and not (showtokyosession ? timetokyo : na) and not (showsydneysession ? timesydney : na)) ? invertedsessioncolor : na)
//=Moving averages
//==================================================================================================================================
ma(length, type) =>
if(useHAformula)
switch type
"SMA" => ta.sma(haC, length)
"EMA" => ta.ema(haC, length)
"SMMA (RMA)" => ta.rma(haC, length)
"WMA" => ta.wma(haC, length)
"VWMA" => ta.vwma(haC, length)
else
switch type
"SMA" => ta.sma(close, length)
"EMA" => ta.ema(close, length)
"SMMA (RMA)" => ta.rma(close, length)
"WMA" => ta.wma(close, length)
"VWMA" => ta.vwma(close, length)
grp_MA = "Moving averages"
ma1color = input(color.blue, title="", group=grp_MA, inline="1")
lenMA1 = input(144, title="", group=grp_MA, inline="1")
typeMA1 = input.string(title = "", defval = "EMA", options=["EMA", "SMA", "SMMA (RMA)", "WMA", "VWMA"], group=grp_MA, inline="1")
showma1 = input(true, title="Show", group=grp_MA, inline="1")
ma2color = input(green, title="", group=grp_MA, inline="2")
lenMA2 = input(50, title="", group=grp_MA, inline="2")
typeMA2 = input.string(title = "", defval = "EMA", options=["EMA", "SMA", "SMMA (RMA)", "WMA", "VWMA"], group=grp_MA, inline="2")
showma2 = input(false, title="Show", group=grp_MA, inline="2")
ma3color = input(red, title="", group=grp_MA, inline="3")
lenMA3 = input(21, title="", group=grp_MA, inline="3")
typeMA3 = input.string(title = "", defval = "EMA", options=["EMA", "SMA", "SMMA (RMA)", "WMA", "VWMA"], group=grp_MA, inline="3")
showma3 = input(false, title="Show", group=grp_MA, inline="3")
plot(showma1 ? ma(lenMA1, typeMA1) : na, title="MA 1", color = ma1color, linewidth=2)
plot(showma2 ? ma(lenMA2, typeMA2) : na, title="MA 2", color = ma2color, linewidth=2)
plot(showma3 ? ma(lenMA3, typeMA3) : na, title="MA 3", color = ma3color, linewidth=2)
priceCrossingMA1 = ta.crossover(ma(lenMA1, typeMA1), close)
alertcondition(priceCrossingMA1, title="Price crossing moving average 1", message="Price crossing moving average 1")
//=Plot candles above MA and Below Range Filters
//==================================================================================================================================
plotcandle(haO, haH, haL, haC, color=candleColor, bordercolor=candleBorderColor, wickcolor=candleWickColor)
//Show real price close dots above candles
plotshape(series=real_close, title="Real price close dots", location=location.absolute, color=realclosedotscolor, editable=false, style=shape.circle, size=size.auto, display=(plotrealclosedots and showHACandles ? display.all : display.none))
//=Range Filter settings
//==================================================================================================================================
//Range Filter #1 Settings
grp_RF1 = "Range Filter #1 settings"
src1 = input(defval=hl2, title="Source", group=grp_RF1)
per1 = input(defval=30, title="Sampling Period", group=grp_RF1)
mult1 = input(defval=2.6, title="Range Multiplier", group=grp_RF1)
//Range Filter #2 Settings
grp_RF2 = "Range Filter #2 settings"
src2 = input(defval=ohlc4, title="Source", group=grp_RF2)
per2 = input(defval=48, title="Sampling Period", group=grp_RF2)
mult2 = input(defval=3.4, title="Range Multiplier", group=grp_RF2)
sourceType(type, showHA) =>
if(showHA)
switch type
open => haO
high => high
low => low
close => haC
hl2 => ((haH + haL) / 2)
hlc3 => (high + low + haC / 3)
ohlc4 => ((haO + haH + haL + haC) / 4)
hlcc4 => ((high + low + haC + haC) / 4)
else if(showHA==false)
switch type
open => open
high => high
low => low
close => close
hl2 => hl2
hlc3 => hlc3
ohlc4 => ohlc4
hlcc4 => hlcc4
// filterSrc1 = (src1 == hl2) and showHACandles ? ((haH + haL) / 2) : hl2
// FilterSrc2 = (src2 == ohlc4) and showHACandles ? ((haO + haH + haL + haC) / 4) : ohlc4
filterSrc1 = sourceType(src1, useHAformula)
filterSrc2 = sourceType(src2, useHAformula)
// Smooth average
smoothrng(x, t, m) =>
wper = t * 2 - 1
avrng = ta.ema(math.abs(x - x[1]), t)
smoothrng = ta.ema(avrng, wper) * m
smoothrng
smrng1 = smoothrng(filterSrc1, per1, mult1)
smrng2 = smoothrng(filterSrc2, per2, mult2)
// Range Filter calculation
rngfilt(x, r) =>
rngfilt = x
rngfilt := x > nz(rngfilt[1]) ? x - r < nz(rngfilt[1]) ? nz(rngfilt[1]) : x - r :
x + r > nz(rngfilt[1]) ? nz(rngfilt[1]) : x + r
rngfilt
filt1 = rngfilt(filterSrc1, smrng1)
filt2 = rngfilt(filterSrc2, smrng2)
//RF Filter direction
upward1 = 0.0
downward1 = 0.0
upward2 = 0.0
downward2 = 0.0
upward1 := filt1 > filt1[1] ? nz(upward1[1]) + 1 : filt1 < filt1[1] ? 0 : nz(upward1[1])
downward1 := filt1 < filt1[1] ? nz(downward1[1]) + 1 : filt1 > filt1[1] ? 0 : nz(downward1[1])
upward2 := filt2 > filt2[1] ? nz(upward2[1]) + 1 : filt2 < filt2[1] ? 0 : nz(upward2[1])
downward2 := filt2 < filt2[1] ? nz(downward2[1]) + 1 : filt2 > filt2[1] ? 0 : nz(downward2[1])
//RF Target bands
hband1 = filt1 + smrng1
lband1 = filt1 - smrng1
hband2 = filt2 + smrng2
lband2 = filt2 - smrng2
//RF Colors
filtcolor1 = upward1 > 0 ? rfUpColor : rfDownColor
filtcolor2 = upward2 > 0 ? rfUpColor : rfDownColor
filtplot1 = plot((showRangeFilters ? filt1 : na), color=filtcolor1, linewidth=rfLineWidth, title="Range Filter #1")
filtplot2 = plot((showRangeFilters ? filt2 : na), color=filtcolor2, linewidth=rfLineWidth, title="Range Filter #2")
//RF range bands upper / lower
hbandplot1 = plot(hband1, color=transp, title="RF #1 high", linewidth=1, display=display.none)
lbandplot1 = plot(lband1, color=transp, title="RF #1 low", linewidth=1, display=display.none)
hbandplot2 = plot(hband2, color=color.new(#CCCCCC, 70), title="RF #2 high", linewidth=1, display=display.none)
lbandplot2 = plot(lband2, color=color.new(#CCCCCC, 70), title="RF #2 low", linewidth=1, display=display.none)
//RF Fills
fill(hbandplot1, filtplot1, color=color.new(#5B9CF6, 100), title="RF #1 area high")
fill(lbandplot1, filtplot1, color=color.new(#5B9CF6, 100), title="RF #1 area low")
fill(hbandplot2, filtplot2, color=color.new(#cccccc, 100), title="RF #2 area high")
fill(lbandplot2, filtplot2, color=color.new(#cccccc, 100), title="RF #2 area low")
//=Watermark
//==================================================================================================================================
textVPosition = input.string("top", "", options = ["top", "middle", "bottom"], group = "Watermark text", inline="w0")
showText = input(true, title="Show text", group = "Watermark text", inline="w0")
s_title = input.string("normal", "", options = ["small", "normal", "large", "huge", "auto"], group = "Watermark text", inline="w1")
c_title = input.color(color.new(color.gray, 0), "", group = "Watermark text", inline="w1")
title = input("BACKTEST | PATIENCE | DISCIPLINE | PROFIT", "", group = "Watermark text", inline="w1")
s_subtitle = input.string("normal", "", options = ["small", "normal", "large", "huge", "auto"], group = "Watermark text", inline="w2")
c_subtitle = input(color.new(color.gray, 0), "", group = "Watermark text", inline="w2")
subtitle = input("", "", group = "Watermark text", inline="w2")
symVPosition = input.string("bottom", "", options = ["top", "middle", "bottom"], group = "Watermark symbol", inline="w3")
showSymText = input(false, title="Show symbol", group = "Watermark symbol", inline="w3")
symInfo = syminfo.ticker + " | " + timeframe.period + (timeframe.isminutes ? "M" : na)
date = str.tostring(dayofmonth(time_close)) + "/" + str.tostring(month(time_close)) + "/" + str.tostring(year(time_close))
s_symInfo = input.string("normal", "", options = ["small", "normal", "large", "huge", "auto"], group = "Watermark symbol", inline="w4")
c_symInfo = input(color.new(color.gray, 30), "", group = "Watermark symbol", inline="w4")
c_bg = color.new(color.blue, 100)
//text watermark creation
textWatermark = table.new(textVPosition + "_" + "center", 1, 3)
if showText == true
table.cell(textWatermark, 0, 0, title, 0, 0, c_title, "center", text_size = s_title, bgcolor = c_bg)
table.cell(textWatermark, 0, 1, subtitle, 0, 0, c_subtitle, "center", text_size = s_subtitle, bgcolor = c_bg)
//symbol info watermark creation
symWatermark = table.new(symVPosition + "_" + "center", 5, 5)
if showSymText == true
table.cell(symWatermark, 0, 1, symInfo + " | " + date, 0, 0, c_symInfo, "center", text_size = s_symInfo, bgcolor = c_bg)
//table.cell(symWatermark,1, 1, date, 0, 0, c_symInfo, "center", text_size = s_symInfo, bgcolor = c_bg)