Indicator Algo Market Structure For TradingView
Indicator Algo Market Structure For TradingView
// https://t.me/Tradingviewpremiumvault
// Disclaimer: This indicator belongs to the original author and is
provided for educational purposes only. Distributed under the Mozilla
Public License 2.0 without any warranty. No affiliation with TradingView.
//@version=5
indicator('Algo Market Structure', overlay=true, max_bars_back=800,
max_labels_count=300, max_lines_count=300)
// -----------------------------------------
// ---------------- INPUTS -----------------
var GRP_TF1 = "•••••••• Timeframe 1 Market Structure ••••••••"
tf1_useStructure = input.bool(true, "", inline="1", group=GRP_TF1)
tf1_timeframe = input.timeframe("", "Timeframe 1", inline="1",
group=GRP_TF1)
tf1_min = input.timeframe("", "Min", inline="1", group=GRP_TF1)
// -----------------------------------------
// --------------- FUNCTIONS ---------------
// Candles
isNeutral(i=0) =>
open[i] == close[i]
isBearish(i=0) =>
_n = isNeutral(i)
_temp = _n ? close[i+1] < open[i+1] : close[i] < open[i]
_temp
isBullish(i=0) =>
_n = isNeutral(i)
_temp = _n ? close[i+1] > open[i+1] : close[i] > open[i]
_temp
// Styles
getLineStyle(_style) =>
_linestyle = _style == "Solid" ? line.style_solid : _style ==
"Dashed" ? line.style_dashed : line.style_dotted
_linestyle
getlabelSize(_labelSize) =>
_size = _labelSize == "auto" ? size.auto : _labelSize == "Tiny" ?
size.tiny : _labelSize == "Small" ? size.small : _labelSize == "Normal" ?
size.normal : size.huge
_size
// Arrays
addToArray(_array, _val) => array.unshift(_array, _val)
getLastElementInArray(_arr) =>
_size = array.size(_arr)
_el = _size > 0 ? array.get(_arr, _size-1) : na
_el
get5ArrayValues(_array) =>
_0 = array.get(_array, 0)
_1 = array.get(_array, 1)
_2 = array.get(_array, 2)
_3 = array.get(_array, 3)
_4 = array.get(_array, 4)
// Box
_bottom(_id) => box.get_bottom(_id)
_top(_id) => box.get_top(_id)
_left(_id) => box.get_left(_id)
_right(_id) => box.get_right(_id)
// Fractals
isRegularFractal(mode, n) =>
ret = mode == 'Buy' ? high[n - 1] < high[n] and high[n + 1] < high[n]
: mode == 'Sell' ? low[n - 1] > low[n] and low[n + 1] > low[n] : false
ret
isBWFractal(mode, n) =>
ret = mode == 'Buy' ? high[n - 2] < high[n] and high[n - 1] < high[n]
and high[n + 1] < high[n] and high[n + 2] < high[n] : mode == 'Sell' ?
low[n - 2] > low[n] and low[n - 1] > low[n] and low[n + 1] > low[n] and
low[n + 2] > low[n] : false
ret
// Helpers
delete_label(_l) =>
if not na(_l)
label.delete(_l)
delete_line(_l) =>
if not na(_l)
line.delete(_l)
delete_box(_l) =>
if not na(_l)
box.delete(_l)
timeToBarIndex(_from) =>
__cond = math.round((time - _from) / (1000 *
timeframe.in_seconds(timeframe.period)))
_cond = __cond <= 5000 ? math.abs(math.round(__cond)) : 5000
_cond
[_highest, _highestTime]
[_lowest, _lowestTime]
get_pivot_ohlc(i=0) =>
[open, high, low, close, time, open[1], high[1], low[1], close[1],
time[1], open[2], high[2], low[2], close[2], time[2]]
_TimeTo = f_timeFrom(7)
GetPipSize() =>
syminfo.mintick * (syminfo.type == "forex" ? 10 : 1)
// --------------- FUNCTIONS ---------------
// -----------------------------------------
// ------------------------------------------------------
// --------------- TIMEFRAME1 STRUCTURE ----------------
[tf1_open, tf1_high, tf1_low, tf1_close, tf1_time, tf1_open1, tf1_high1,
tf1_low1, tf1_close1, tf1_time1, tf1_open2, tf1_high2, tf1_low2,
tf1_close2, tf1_time2] = request.security(syminfo.tickerid,
tf1_timeframe, get_pivot_ohlc(), lookahead=barmerge.lookahead_on)
isUseTimeframe1 = not isHigherTimeframe(tf1_timeframe) and
isHigherTimeframe(tf1_min, true) and tf1_useStructure and time >
indicator_start_time
_tf1_countBull = ta.barssince(tf1_lastBullCandle)
tf1_countSinceBullCandle = request.security(syminfo.tickerid,
tf1_timeframe, _tf1_countBull, lookahead = barmerge.lookahead_on)
_tf1_countBear = ta.barssince(tf1_lastBearCandle)
tf1_countSinceBearCandle = request.security(syminfo.tickerid,
tf1_timeframe, _tf1_countBear, lookahead = barmerge.lookahead_on)
// Initialize arrays
var tf1_pivotHighs = array.new_float(5, MIN)
var tf1_pivotHighsTime = array.new_int(5)
var tf1_pivotLows = array.new_float(5, MAX)
var tf1_pivotLowsTime = array.new_int(5)
tf1_isStructureHigh = false
tf1_isStructureLow = false
if tf1_canPlot
// --- STRUCTURE HIGH ---
if tf1_countSinceBullCandle >= 1
if tf1_countSinceBullCandle == 1
tf1_mostRecentBullCount1 := __time
if tf1_countSinceBearCandle == 1
tf1_mostRecentBearCount1 := __time
if tf1_showIlq
if tf1_canPlot
if tf1_isValidHH or tf1_isValidLL
delete_line(tf1_bottomIlqLine)
delete_line(tf1_topIlqLine)
delete_label(tf1_bottomIlqLabel)
delete_label(tf1_topIlqLabel)
// Bottom ILQ
if tf1_isValidHH
tf1_bottomIlqPrice := tf1_pl0
tf1_topIlqPrice := na
// Top ILQ
if tf1_isValidLL
tf1_topIlqPrice := tf1_ph0
tf1_bottomIlqPrice := na
if tf1_showTlq
// Bottom TLQ Delete
if not na(tf1_bottomTlqLine) and ((low < tf1_bottomTlqPrice) or
(tf1_isValidHH[1] and low[1] < tf1_bottomTlqPrice) or (low[2] < tf1_pl1
and low[2] < tf1_pl2 and low[2] < tf1_bottomTlqPrice)) and not
array.includes(ex_usedBosTimes, line.get_x1(tf1_bottomTlqLine))
tf1_structureDirection := "bearish"
tf1_bottomTlqPrice := na
addToArray(ex_usedBosTimes, line.get_x1(tf1_bottomTlqLine))
if tf1_showBos
line.new(line.get_x1(tf1_bottomTlqLine),
line.get_y1(tf1_bottomTlqLine), time, line.get_y1(tf1_bottomTlqLine),
xloc=xloc.bar_time, color=color.red)
label.new(time, line.get_y1(tf1_bottomTlqLine), "BOS" +
LABEL_SPACE, xloc=xloc.bar_time, style=label.style_none,
size=getlabelSize("Normal"), textcolor=color.red)
line.delete(tf1_bottomTlqLine)
if not na(tf1_bottomTlqLabel)
label.delete(tf1_bottomTlqLabel)
if tf1_showBos
line.new(line.get_x1(tf1_topTlqLine),
line.get_y1(tf1_topTlqLine), time, line.get_y1(tf1_topTlqLine),
xloc=xloc.bar_time, color=color.green)
label.new(time, line.get_y1(tf1_topTlqLine), "BOS" +
LABEL_SPACE, xloc=xloc.bar_time, style=label.style_none,
size=getlabelSize("Normal"), textcolor=color.green)
line.delete(tf1_topTlqLine)
if not na(tf1_topTlqLabel)
label.delete(tf1_topTlqLabel)
// Bottom TLQ
if tf1_isValidHH
tf1_bottomTlqPrice := tf1_pl1 < tf1_pl0 ? tf1_pl1 : tf1_pl0
tf1_bottomTlqTime = tf1_pl1 < tf1_pl0 ? tf1_pl1Time :
tf1_pl0Time
tf1_topTlqPrice := na
// Top TLQ
if tf1_isValidLL
tf1_topTlqPrice := tf1_ph1 > tf1_ph0 ? tf1_ph1 : tf1_ph0
tf1_topTlqTime = tf1_ph1 > tf1_ph0 ? tf1_ph1Time :
tf1_ph0Time
tf1_bottomTlqPrice := na
if tf1_showExtreme
if tf1_canPlot
if tf1_isValidHH or tf1_isValidLL
delete_box(tf1_bottomExtremeBox)
delete_box(tf1_topExtremeBox)
// Bottom Extreme
if tf1_isValidHH
tf1_bottomExtremePrice = tf1_pl1 < tf1_pl0 ? tf1_pl1 :
tf1_pl0
tf1_bottomExtremeTime = tf1_pl1 < tf1_pl0 ? tf1_pl1Time :
tf1_pl0Time
_biL = barIndexAtTime(timesArray, tf1_bottomExtremeTime)
lowestBody = open[_biL] > close[_biL] ? close[_biL] :
open[_biL]
if _biL > 1
tf1_bottomExtremeBox := box.new(tf1_bottomExtremeTime,
lowestBody, _TimeTo, tf1_bottomExtremePrice, xloc=xloc.bar_time,
border_color=TRANSPARENT, bgcolor=tf1_extremeCol, text=tf1_extremeText,
text_color=color.new(tf1_extremeCol, 0), text_halign=text.align_right,
text_valign=text.align_top, text_size=getlabelSize("Small"))
delete_box(tf1_bottomExtremeBox[1])
// Top Extreme
if tf1_isValidLL
tf1_topExtremePrice = tf1_ph1 > tf1_ph0 ? tf1_ph1 : tf1_ph0
tf1_topExtremeTime = tf1_ph1 > tf1_ph0 ? tf1_ph1Time :
tf1_ph0Time
_biH = barIndexAtTime(timesArray, tf1_topExtremeTime)
highestBody = open[_biH] < close[_biH] ? close[_biH] :
open[_biH]
if _biH > 1
tf1_topExtremeBox := box.new(tf1_topExtremeTime,
tf1_topExtremePrice, _TimeTo, highestBody, xloc=xloc.bar_time,
border_color=TRANSPARENT, bgcolor=tf1_extremeCol, text=tf1_extremeText,
text_color=color.new(tf1_extremeCol, 0), text_halign=text.align_right,
text_valign=text.align_bottom, text_size=getlabelSize("Small"))
delete_box(tf1_topExtremeBox[1])
if tf1_showEpa
if tf1_canPlot
if tf1_isValidHH or tf1_isValidLL
tf1_isPriceEfficient := false
if tf1_epaDelete
delete_line(tf1_bearEpaLine)
delete_line(tf1_bullEpaLine)
delete_label(tf1_bearEpaLabel)
delete_label(tf1_bullEpaLabel)
// Bear EPA
if tf1_isValidLL
tf1_bearEpaPrice := tf1_pl1
tf1_bullEpaPrice := na
// Bull EPA
if tf1_isValidHH
tf1_bullEpaPrice := tf1_ph1
tf1_bearEpaPrice := na
// Plot VTA
if tf1_showVta
// Delete boxes
if tf1_deleteVta == true
// Top
if array.size(tf1_topVtaBoxes) > 0
for i = array.size(tf1_topVtaBoxes)-1 to 0
_box = array.get(tf1_topVtaBoxes, i)
// Delete top
if (not na(_box) and tf1_close > _top(_box))
removeFromArray(tf1_topVtaBoxes, i)
box.delete(_box)
// Bottom
if array.size(tf1_bottomVtaBoxes) > 0
for i = array.size(tf1_bottomVtaBoxes)-1 to 0
_box = array.get(tf1_bottomVtaBoxes, i)
// Delete bottom
if (not na(_box) and tf1_close < _bottom(_box))
removeFromArray(tf1_bottomVtaBoxes, i)
box.delete(_box)
// --------------- TIMEFRAME1 VTR ---------------
// HH HH
LH LL LL
tf1_isBearishPattern := (tf1_ph2 > tf1_ph3) and (tf1_ph1 > tf1_ph2) and
(tf1_ph0 < tf1_ph1) and (tf1_pl1 < tf1_pl2) and (tf1_pl0 < tf1_pl1) and
(close > tf1_pl0)
tf1_isBullishPattern := (tf1_pl2 < tf1_pl3) and (tf1_pl1 < tf1_pl2) and
(tf1_pl0 > tf1_pl1) and (tf1_ph1 > tf1_ph2) and (tf1_ph0 > tf1_ph1) and
(close < tf1_ph0)
// if barstate.islast
// label.new(bar_index, high, str.tostring(tf1_ph2) + "\n" + )
// --------------- PATTERN ---------------
// ----------------------------------------------
// --------------- DETAILS TABLE ----------------
if barstate.islast and structure_show_table
table _table = table.new(structure_table_vertical + "_" +
structure_table_horizontal, columns = 6, rows = 5, border_width=3)
if isUseTimeframe1
_timeframeString = tf1_timeframe == "" ? timeframe.period :
tf1_timeframe
table.cell(_table, column = 1, row = 0, text =
str.tostring(_timeframeString), text_color=color.green,
bgcolor=color.new(color.green, 90))
table.cell(_table, column = 1, row = 1, text = isUseTimeframe1 ?
tf1_structureDirection == "bullish" ? "Bullish ▲" : "Bearish ▼" : "NA",
text_color=tf1_structureDirection == "bullish" ? color.lime : color.red)
table.cell(_table, column = 1, row = 2, text = isUseTimeframe1 ?
tf1_isPriceEfficient ? "Efficient ✓" : "Inefficient 𐄂" : "NA",
text_color=tf1_isPriceEfficient ? color.lime : color.red)
table.cell(_table, column = 1, row = 3, text = isUseTimeframe1 ?
tf1_isBullishMsu ? "MSU ▲" : tf1_isBearishMsu ? "MSU ▼" : "" : "NA",
text_color=tf1_isBullishMsu ? color.lime : color.red)
table.cell(_table, column = 1, row = 4, text = isUseTimeframe1 ?
tf1_isBullishVta ? "VTA ▲" : tf1_isBearishVta ? "VTA ▼" : "" : "NA",
text_color=tf1_isBullishVta ? color.lime : color.red)
/////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////
//Water Mark
Watermark = table.new(position.bottom_right, 1, 2, border_width=3)
table.cell(Watermark, 0, 0, text='@ Premium Vault ',
text_color=color.lime, text_size=size.small)
table.cell(Watermark, 0, 1, text='https://t.me/Tradingviewpremiumvault',
text_color=color.lime, text_size=size.small)