Indicator SNR + ICT HTF Candles With FVG For TradingView
Indicator SNR + ICT HTF Candles With FVG For TradingView
0
at https://mozilla.org/MPL/2.0/
// Join us https://t.me/simpleforextools
// This Pine Script™ code is subject to the terms of the Mozilla Public License 2.0
at https://mozilla.org/MPL/2.0/
//@version=5
indicator("SnR + ICT HTF Candles with fvg", overlay = true, max_lines_count = 500,
max_labels_count = 500)
//------------------------------------------------------------------------------
//Settings
//-----------------------------------------------------------------------------{
length = input(11, 'Pivot Lookback')
//Style
showTop = input(true, 'Swing High', inline = 'top', group = 'Style')
topCss = input(color.rgb(0, 0, 0), '', inline = 'top', group = 'Style')
topAreaCss = input(color.rgb(197, 197, 197), 'Area', inline = 'top', group =
'Style')
//-----------------------------------------------------------------------------}
//Functions
//-----------------------------------------------------------------------------{
n = bar_index
if condition
count := 0
vol := 0.
else
if intraPrecision
if n > length
if array.size(v[length]) > 0
for [index, element] in v[length]
vol += array.get(l[length], index) < top and
array.get(h[length], index) > btm ? element : 0
else
vol += low[length] < top and high[length] > btm ? volume[length] : 0
[count, vol]
if ta.crossover(target, filterValue)
lbl := label.new(x, y, str.tostring(vol, format.volume)
, style = lbl_style
, size = label_size
, color = #00000000
, textcolor = css)
if condition
if target[1] < filterValue[1]
line.delete(lvl[1])
else if not crossed[1]
line.set_x2(lvl, n - length)
if not crossed[1]
line.set_x2(lvl, n+3)
//-----------------------------------------------------------------------------}
//Global Variables
//-----------------------------------------------------------------------------{
// Initialize variables for pivot high and low
var float ph_top = na
var float ph_btm = na
var float pl_top = na
var float pl_btm = na
var bool ph_crossed = na
var bool pl_crossed = na
var ph_x1 = 0
var pl_x1 = 0
//-----------------------------------------------------------------------------}
//Display pivot high levels
//-----------------------------------------------------------------------------{
ph = ta.pivothigh(length, length)
//Get ph counts
[ph_count, ph_vol] = get_counts(ph, ph_top, ph_btm)
ph_x1 := n - length
ph_crossed := false
else
ph_crossed := close > ph_top ? true : ph_crossed
if showTop
//Set ph level
set_level(ph, ph_crossed, ph_top, ph_count, ph_vol, topCss)
//Set ph label
set_label(ph_count, ph_vol, ph_x1, ph_top, topCss, label.style_label_down)
//-----------------------------------------------------------------------------}
//Display pivot low levels
//-----------------------------------------------------------------------------{
pl = ta.pivotlow(length, length)
//Get pl counts
[pl_count, pl_vol] = get_counts(pl, pl_top, pl_btm)
pl_x1 := n - length
pl_crossed := false
else
pl_crossed := close < pl_btm ? true : pl_crossed
if showBtm
//Set pl level
set_level(pl, pl_crossed, pl_btm, pl_count, pl_vol, btmCss)
//Set pl labels
set_label(pl_count, pl_vol, pl_x1, pl_btm, btmCss, label.style_label_up)
//-----------------------------------------------------------------------------}
// Functions
new_bar(res) =>
ta.change(time(res)) != 0
// candleColor =
iff(climax,iff(isBull,CUColor,CDColor),iff(aboveA,iff(isBull,AUColor,ADColor),iff(i
sBull,NUColor,NDColor)))
iff_5 = va == 2 ? AUColor : na
iff_6 = va == 1 ? CUColor : iff_5
iff_7 = va == 2 ? ADColor : na
iff_8 = va == 1 ? CDColor : iff_7
candleColor = isBull ? iff_6 : iff_8
barcolor(candleColor)
// @function get_slope()
get_slope(xA, yA, xB, yB) =>
(yB - yA) / (xB - xA)
// Solving for price at current x (bar_index), given two pairs of fractals with x
values < bar_index.
float m_dn = get_slope(i_recent_dn1, recent_dn1, i_recent_dn2, recent_dn2)
float y_dn = (m_dn * bar_index) + recent_dn1 - (m_dn * i_recent_dn1)
float m_up = get_slope(i_recent_up1, recent_up1, i_recent_up2, recent_up2)
float y_up = (m_up * bar_index) + recent_up1 - (m_up * i_recent_up1)
// Source input
source = input(defval=close, title="Source")
// Inputs
Periods = input.int(title="ATR Period", defval=14)
Multiplier = input.float(title="ATR Multiplier", step=0.1, defval=1.6)
Sensitivity = input.float(title="Sensitivity", step=0.1, defval=2.3)
changeATR = input.bool(title="Change ATR Calculation Method yes/no", defval=true)
showsignals = input.bool(title="Show Buy/Sell Signals", defval=true)
highlighting = input.bool(title="Highlighter On/Off", defval=true)
// ATR Calculation
atr2 = ta.sma(ta.tr, Periods)
atr = changeATR ? ta.atr(Periods) : atr2
// Trend Calculation
var int trend = na
trend := na(trend[1]) ? 1 : trend
trend := trend == -1 and haClose > dn1 ? 1 : trend == 1 and haClose < up1 ? -1 :
trend
// Plotting
buySignal = trend == 1 and trend[1] == -1
plotshape(series=buySignal ? up : na, title="buySignal",
location=location.belowbar, style=shape.labelup, size=size.tiny,
color=color.rgb(197,197,197), textcolor=color.rgb(0, 0, 0), text="BUY", offset=-1)
// Alerts
alertcondition(buySignal, title="Buy", message="Buy!")
alertcondition(sellSignal, title="Sell", message="Sell!")
changeCond = trend != trend[1]
alertcondition(changeCond, title="Direction Change", message="changed direction!")
// Script settings
emaLength = input.int(title="Length", defval=20, minval=2)
emaSource = input.source(title="Source", defval=close)
// Get EMA
ema = ta.ema(emaSource, emaLength)
type Candle
float o
float c
float h
float l
int o_idx
int c_idx
int h_idx
int l_idx
string dow
box body
line wick_up
line wick_down
label dow_label
type Trace
line o
line c
line h
line l
label o_l
label c_l
label h_l
label l_l
type Imbalance
box b
int idx
type CandleSettings
bool show
string htf
int max_display
type Settings
int max_sets
color bull_body
color bull_border
color bull_wick
color bear_body
color bear_border
color bear_wick
int offset
int buffer
int htf_buffer
int width
bool use_custom_daily
string custom_daily
bool daily_name
bool trace_show
color trace_o_color
string trace_o_style
int trace_o_size
color trace_c_color
string trace_c_style
int trace_c_size
color trace_h_color
string trace_h_style
int trace_h_size
color trace_l_color
string trace_l_style
int trace_l_size
string trace_anchor
bool label_show
color label_color
string label_size
string label_position
string label_alignment
bool fvg_show
color fvg_color
bool vi_show
color vi_color
bool htf_label_show
color htf_label_color
string htf_label_size
bool htf_timer_show
color htf_timer_color
string htf_timer_size
color dow_color
string dow_size
type CandleSet
array<Candle> candles
array<Imbalance> imbalances
CandleSettings settings
label tfNameTop
label tfNameBottom
label tfTimerTop
label tfTimerBottom
type Helper
string name = 'Helper'
//
+----------------------------------------------------------------------------------
--------------------------+//
//+--- Settings
---+//
//
+----------------------------------------------------------------------------------
--------------------------+//
//
+----------------------------------------------------------------------------------
--------------------------+//
//+--- Variables
---+//
//
+----------------------------------------------------------------------------------
--------------------------+//
//
+----------------------------------------------------------------------------------
--------------------------+//
//+--- Internal Functions
---+//
//
+----------------------------------------------------------------------------------
--------------------------+//
r = str.tostring(seconds, '00')
if minutes > 0 or hours > 0 or days > 0
r := str.tostring(minutes, '00') + ':' + r
r
if hours > 0 or days > 0
r := str.tostring(hours, '00') + ':' + r
r
if days > 0
r := str.tostring(days) + 'D ' + r
r
r
else
'n/a'
seconds = timeframe.in_seconds(HTF)
if seconds < 60
formatted := str.tostring(seconds) + 's'
formatted
else if seconds / 60 < 60
formatted := str.tostring(seconds / 60) + 'm'
formatted
else if seconds / 60 / 60 < 24
formatted := str.tostring(seconds / 60 / 60) + 'H'
formatted
formatted
last
if array.size(candles) > 0
for i = 0 to array.size(candles) - 1 by 1
Candle c = array.get(candles, i)
if c.h > h
h := c.h
h
if array.size(candles) > 0
for i = 0 to array.size(candles) - 1 by 1
Candle c = array.get(candles, i)
if c.l < l
l := c.l
l
if not na(candleSet.tfTimerTop)
candleSet.tfTimerTop.set_text(tmr)
if not na(candleSet.tfTimerBottom)
candleSet.tfTimerBottom.set_text(tmr)
candleSet
if size > 0
for i = size - 1 to 0 by 1
Candle candle = candleSet.candles.get(i)
t_buffer = offset + (settings.width + settings.buffer) * (size - i - 1)
box.set_left(candle.body, bar_index + t_buffer)
box.set_right(candle.body, bar_index + settings.width + t_buffer)
line.set_x1(candle.wick_up, bar_index + settings.width / 2 + t_buffer)
line.set_x2(candle.wick_up, bar_index + settings.width / 2 + t_buffer)
line.set_x1(candle.wick_down, bar_index + settings.width / 2 +
t_buffer)
line.set_x2(candle.wick_down, bar_index + settings.width / 2 +
t_buffer)
top = 0.0
bottom = 0.0
if settings.label_alignment == 'Align'
top := helper.CandlesHigh(candleSet.candles)
bottom := helper.CandlesLow(candleSet.candles, top)
if settings.label_alignment == 'Follow Candles'
top := helper.CandleSetHigh(candleSet.candles, 0)
bottom := helper.CandleSetLow(candleSet.candles, top)
if settings.htf_label_show
string lblt = helper.HTFName(candleSet.settings.htf)
string lbll = lblt
if settings.htf_timer_show
lblt := lblt + '\n'
lbll := '\n' + lbll
if not na(candleSet.tfNameTop)
candleSet.tfNameTop.set_xy(left, top)
else
candleSet.tfNameTop := label.new(left, top, lblt, color =
color_transparent, textcolor = settings.htf_label_color, style =
label.style_label_down, size = settings.htf_label_size)
if not na(candleSet.tfTimerTop)
candleSet.tfTimerTop.set_xy(left, top)
else
candleSet.tfTimerTop := label.new(left, top, tmr, color =
color_transparent, textcolor = settings.htf_timer_color, style =
label.style_label_down, size = settings.htf_timer_size)
if not na(candleSet.tfTimerBottom)
candleSet.tfTimerBottom.set_xy(left, bottom)
else
candleSet.tfTimerBottom := label.new(left, bottom, tmr, color =
color_transparent, textcolor = settings.htf_timer_color, style =
label.style_label_up, size = settings.htf_timer_size)
candleSet
if settings.use_custom_daily
int _830 = 0
if isNewHTFCandle
_830 := timestamp("America/New_York", year(time), month(time),
dayofmonth(time), 0, 0) + 30600000
if candleSet.settings.htf == '1D'
if settings.custom_daily == 'Midnight'
isNewHTFCandle := dayofweek(time, 'America/New_York') !=
dayofweek(time - (time - time[1]), 'America/New_York')
if settings.custom_daily == '8:30'
// Get 8:30 AM New York time for today
isNewHTFCandle := not na(time(timeframe.period, "0830-0831:123456",
'America/New_York')) and na(time(timeframe.period, "0830-0831:123456",
'America/New_York')[1])
if settings.custom_daily == '9:30'
// Get 9:30 AM New York time for today
isNewHTFCandle := not na(time(timeframe.period, "0930-0931:123456",
'America/New_York')) and na(time(timeframe.period, "0930-0931:123456",
'America/New_York')[1])
if isNewHTFCandle
Candle candle = Candle.new()
candle.o := open
candle.c := close
candle.h := high
candle.l := low
candle.o_idx := bar_index
candle.c_idx := bar_index
candle.h_idx := bar_index
candle.l_idx := bar_index
candle.dow := helper.DayofWeek(dayofweek(time_tradingday,
"America/New_York"))
candleSet.candles.unshift(candle)
candleSet
box.set_top(candle.body, candle.o)
box.set_bottom(candle.body, candle.c)
box.set_bgcolor(candle.body, bull ? settings.bull_body :
settings.bear_body)
box.set_border_color(candle.body, bull ? settings.bull_border :
settings.bear_border)
line.set_color(candle.wick_up, bull ? settings.bull_wick :
settings.bear_wick)
line.set_color(candle.wick_down, bull ? settings.bull_wick :
settings.bear_wick)
line.set_y1(candle.wick_up, candle.h)
line.set_y2(candle.wick_up, math.max(candle.o, candle.c))
line.set_y1(candle.wick_down, candle.l)
line.set_y2(candle.wick_down, math.min(candle.o, candle.c))
if barstate.isrealtime or barstate.islast
candleSet.Reorder(offset)
if settings.trace_show and showTrace
if bar_index - candle.o_idx < 5000
if na(trace.o)
trace.o := line.new(candle.o_idx, candle.o,
box.get_left(candle.body), candle.o, xloc = xloc.bar_index, color =
settings.trace_o_color, style = helper.LineStyle(settings.trace_o_style), width =
settings.trace_o_size)
trace.o
else
line.set_xy1(trace.o, candle.o_idx, candle.o)
line.set_xy2(trace.o, box.get_left(candle.body), candle.o)
if settings.label_show
if na(trace.o_l)
trace.o_l := label.new(box.get_right(candle.body),
candle.o, str.tostring(candle.o), textalign = text.align_center, style =
label.style_label_left, size = settings.label_size, color = color_transparent,
textcolor = settings.label_color)
trace.o_l
else
label.set_xy(trace.o_l, box.get_right(candle.body),
candle.o)
label.set_text(trace.o_l, str.tostring(candle.o))
if bar_index - candle.c_idx < 5000
if na(trace.c)
trace.c := line.new(candle.c_idx, candle.c,
box.get_left(candle.body), candle.c, xloc = xloc.bar_index, color =
settings.trace_c_color, style = helper.LineStyle(settings.trace_c_style), width =
settings.trace_c_size)
trace.c
else
line.set_xy1(trace.c, candle.c_idx, candle.c)
line.set_xy2(trace.c, box.get_left(candle.body), candle.c)
if settings.label_show
if na(trace.c_l)
trace.c_l := label.new(box.get_right(candle.body),
candle.c, str.tostring(candle.c), textalign = text.align_center, style =
label.style_label_left, size = settings.label_size, color = color_transparent,
textcolor = settings.label_color)
trace.c_l
else
label.set_xy(trace.c_l, box.get_right(candle.body),
candle.c)
label.set_text(trace.c_l, str.tostring(candle.c))
if settings.label_show
if na(trace.h_l)
trace.h_l := label.new(box.get_right(candle.body),
candle.h, str.tostring(candle.h), textalign = text.align_center, style =
label.style_label_left, size = settings.label_size, color = color_transparent,
textcolor = settings.label_color)
trace.h_l
else
label.set_xy(trace.h_l, box.get_right(candle.body),
candle.h)
label.set_text(trace.h_l, str.tostring(candle.h))
int cnt = 0
int last = helper.HTFEnabled()
//---------------------------------------------------------------------------------
------------------------------------
// Style
bullBCss = input(color.new(#089981, 40), 'Bullish BFVG', inline = 'bull', group =
'Style')
bullCss = input(color.new(#9598a1, 40), 'FVG' , inline = 'bull', group =
'Style')
p = bar_index
bull_fvg = low > high[2] and close[1] > high[2]
bear_fvg = high < low[2] and close[1] < low[2]
// Bullish BFVG
if low > upper[2] and bull_fvg
box.new(n-2, high[2], n, low, na, bgcolor = bullBCss)
// Bearish BFVG
if high < lower[2] and bear_fvg
box.new(n-2, low[2], n, high, na, bgcolor = bearBCss)
//
===================================================================================
=======