Linear Regression Candles With OB and Target by LEO
Linear Regression Candles With OB and Target by LEO
0 at
https://mozilla.org/MPL/2.0/
// © Llopezf
//@version=5
indicator("Linear Regression Candles with OB and Target by LEO",
format=format.price, precision=4, overlay = true, max_bars_back =
4000,max_lines_count=500,max_labels_count=500, max_boxes_count=500)
ob_period = periods + 1 //
Identify location of relevant Order Block candle
absmove = ((math.abs(close[ob_period] - close[1]))/close[ob_period]) * 100 //
Calculate absolute percent move from potential OB to last candle of subsequent
candles
relmove = absmove >= threshold //
Identify "Relevant move" by comparing the absolute move to the threshold
// Color Scheme
bullcolor = colors == "DARK"? color.white : color.green
bearcolor = colors == "DARK"? color.blue : color.red
int upcandles = 0
for i = 1 to periods
upcandles := upcandles + (close[i] > open[i]? 1 : 0) //
Determine color of subsequent candles (must all be green to identify a valid
Bearish OB)
int downcandles = 0
for i = 1 to periods
downcandles := downcandles + (close[i] < open[i]? 1 : 0) //
Determine color of subsequent candles (must all be red to identify a valid Bearish
OB)
// Plotting
line.delete(linebull2)
linebull2 := line.new(x1 = bar_index, y1 = OB_bull_high, x2 = bar_index - 1, y2
= OB_bull_high, extend = extend.left, color = bullcolor, style = line.style_dashed,
width = 1)
line.delete(linebull3)
linebull3 := line.new(x1 = bar_index, y1 = OB_bull_low, x2 = bar_index - 1, y2
= OB_bull_low, extend = extend.left, color = bullcolor, style = line.style_dashed,
width = 1)
line.delete(linebear2)
linebear2 := line.new(x1 = bar_index, y1 = OB_bear_high, x2 = bar_index - 1, y2
= OB_bear_high, extend = extend.left, color = bearcolor, style =
line.style_dashed, width = 1)
line.delete(linebear3)
linebear3 := line.new(x1 = bar_index, y1 = OB_bear_low, x2 = bar_index - 1, y2
= OB_bear_low, extend = extend.left, color = bearcolor, style = line.style_dashed,
width = 1)
if OB_bull_avg > 0
latest_bull_avg := OB_bull_avg
if OB_bull_low > 0
latest_bull_low := OB_bull_low
if OB_bear_high > 0
latest_bear_high := OB_bear_high
if OB_bear_avg > 0
latest_bear_avg := OB_bear_avg
if OB_bear_low > 0
latest_bear_low := OB_bear_low
// Plot invisible characters to be able to show the values in the Data Window
plotchar(latest_bull_high, char = ' ', location = location.abovebar, color =
#777777, transp = 100, size = size.tiny, title = "Latest Bull High")
plotchar(latest_bull_avg, char = ' ', location = location.abovebar, color =
#777777, transp = 100, size = size.tiny, title = "Latest Bull Avg")
plotchar(latest_bull_low, char = ' ', location = location.abovebar, color =
#777777, transp = 100, size = size.tiny, title = "Latest Bull Low")
plotchar(latest_bear_high, char = ' ', location = location.abovebar, color =
#777777, transp = 100, size = size.tiny, title = "Latest Bear High")
plotchar(latest_bear_avg, char = ' ', location = location.abovebar, color =
#777777, transp = 100, size = size.tiny, title = "Latest Bear Avg")
plotchar(latest_bear_low, char = ' ', location = location.abovebar, color =
#777777, transp = 100, size = size.tiny, title = "Latest Bear Low")
panel_text = '\n' + title + '\n' + row0 + '\n' + row1 + '\n' + row2 + '\n' + row3 +
'\n' + row4 + '\n\n' + row5 + '\n' + row6 + '\n' + row7 + '\n'
if info_pan
draw_InfoPanel(panel_text, info_panel_x, info_panel_y, size.normal)
if showdocu
l_docu := label.new(x = time + chper * 35, y = close, text = "DOCU OB",
color=color.gray, textcolor=color.white, style=label.style_label_center, xloc =
xloc.bar_time, yloc=yloc.price, size=size.tiny, textalign = text.align_left,
tooltip = vartooltip)
plot(signal, color=color.blue,linewidth = 2)
//------------------------------------------------------------------------------
// === Trend Line ===
//------------------------------------------------------------------------------
if ph
add_to_array(tval, tpos, ph)
if pl
add_to_array(bval, bpos, pl)
// line definitions
maxline = 3
var bln = array.new_line(maxline, na)
var tln = array.new_line(maxline, na)
if valid
uv1 := hline - diff
uv2 := val2
up1 := lloc
up2 := pos2
break
dv1 = 0.0
dv2 = 0.0
dp1 = 0
dp2 = 0
if countlinehi <= maxline
for p2 = PPnum - 1 to p1 + 1
val1 = array.get(tval, p1)
val2 = array.get(tval, p2)
pos1 = array.get(tpos, p1)
pos2 = array.get(tpos, p2)
if val1 < val2
diff = (val2 - val1) / float(pos1 - pos2)
hline = val2 - diff
lloc = bar_index
lval = high
valid = true
for x = pos2 + 1 - prd to bar_index
if close[bar_index - x] > hline
valid := false
break
lloc := x
lval := hline
hline := hline - diff
if valid
dv1 := hline + diff
dv2 := val2
dp1 := lloc
dp2 := pos2
break
//------------------------------------------------------------------------------
// === Hull Suite ===
//------------------------------------------------------------------------------
//INPUT
src = input(close, title="Source")
modeSwitch = input.string("Hma", title="Hull Variation", options=["Hma", "Thma",
"Ehma"])
length = input(55, title="Length(180-200 for floating S/R , 55 for swing entry)")
lengthMult = input(1.0, title="Length multiplier (Used to view higher timeframes
with straight band)")
//FUNCTIONS
//HMA
HMA(_src, _length) => ta.wma(2 * ta.wma(_src, _length / 2) - ta.wma(_src,
_length), math.round(math.sqrt(_length)))
//EHMA
EHMA(_src, _length) => ta.ema(2 * ta.ema(_src, _length / 2) - ta.ema(_src,
_length), math.round(math.sqrt(_length)))
//THMA
THMA(_src, _length) => ta.wma(ta.wma(_src,_length / 3) * 3 - ta.wma(_src,
_length / 2) - ta.wma(_src, _length), _length)
//SWITCH
Mode(modeSwitch, src, len) =>
modeSwitch == "Hma" ? HMA(src, len) :
modeSwitch == "Ehma" ? EHMA(src, len) :
modeSwitch == "Thma" ? THMA(src, len/2) : na
//OUT
_hull = Mode(modeSwitch, src, int(length * lengthMult))
HULL = useHtf ? request.security(syminfo.ticker, htf, _hull) : _hull
MHULL = HULL[0]
SHULL = HULL[2]
//COLOR
hullColor = switchColor ? (HULL > HULL[2] ? #00ff0070 : #ff000070) : #ff9800
//PLOT
///< Frame
Fi1 = plot(MHULL, title="MHULL", color=hullColor, linewidth=thicknesSwitch,
transp=50)
Fi2 = plot(visualSwitch ? SHULL : na, title="SHULL", color=hullColor,
linewidth=thicknesSwitch, transp=50)
alertcondition(MHULL > SHULL, title="Hull trending up.", message="Hull trending
up.")
alertcondition(SHULL > MHULL, title="Hull trending down.", message="Hull trending
down.")
///< Ending Filler
fill(Fi1, Fi2, title="Band Filler", color=hullColor, transp=transpSwitch)
///BARCOLOR
barcolor(color = candleCol ? (switchColor ? hullColor : na) : na)
//------------------------------------------------------------------------------
// ABCD Patterns
//------------------------------------------------------------------------------
length2 = input(5)
up1 = ta.pivothigh(high,length2,length2)
dn1 = ta.pivotlow(low,length2,length2)
upcount = ta.barssince(not na(up1))
dncount = ta.barssince(not na(dn1))
ppp = (not na(dn1) and dncount[1]>upcount[1]) ? dn1 : (not na(up1) and
dncount[1]<upcount[1]) ? up1 : na
up = (not na(up1) and dncount[1]<upcount[1]) ? up1 : na
dn = (not na(dn1) and dncount[1]>upcount[1]) ? dn1 : na
n1 = bar_index
plot(up,title="pivot high",color = #77777700,offset=-length2)
plot(dn,title="pivot low",color = #77777700,offset=-length2)
plot(ppp,title="pivot line",color = #77777700,offset=-length2)
a = ta.valuewhen(not na(ppp),n1,2)
b = ta.valuewhen(not na(ppp),n1,1)
c = ta.valuewhen(not na(ppp),n1,0)
av = ta.valuewhen(not na(ppp),ppp,2)
bv = ta.valuewhen(not na(ppp),ppp,1)
cv = ta.valuewhen(not na(ppp),ppp,0)
//------------------------------------------------------------------------------
// === Zig Zag Channels ===
//------------------------------------------------------------------------------
length1 = input(100)
extend = input(true,'Extend To Last Bar')
show_ext = input(true,'Show Extremities')
show_labels = input(true,'Show Labels')
//Style
//------------------------------------------------------------------------------
upper = ta.highest(src1,length1)
lower = ta.lowest(src1,length1)
os1 := src1[length1] > upper ? 0 : src1[length1] < lower ? 1 : os1[1]
//------------------------------------------------------------------------------
btm_n = ta.valuewhen(btm,n,0)
top_n = ta.valuewhen(top,n,0)
len = math.abs(btm_n - top_n)
if btm
max_diff_up = 0.
max_diff_dn = 0.
valbtm := low[length1]
for i = 0 to len-1
point = low[length1] + i/(len-1)*(valtop - low[length1])
max_diff_up := math.max(math.max(src1[length1+i],open[length1+i]) -
point,max_diff_up)
max_diff_dn := math.max(point -
math.min(src1[length1+i],open[length1+i]),max_diff_dn)
line.new(n[len+length1],valtop,n[length1],low[length1],color=midcol)
if show_ext
line.new(n[len+length1],valtop+max_diff_up,n[length1],low[length1]+max_diff_up
,color=upcol,style=line.style_dotted)
line.new(n[len+length1],valtop-max_diff_dn,n[length1],low[length1]-
max_diff_dn
,color=dncol,style=line.style_dotted)
if show_labels
label.new(n[length1],low[length1],str.tostring(low[length1],'#.####'),color=#000000
00
,style=label.style_label_up,textcolor=dncol,textalign=text.align_left,siz
e=size.small)
if top
max_diff_up = 0.
max_diff_dn = 0.
valtop := high[length1]
for i = 0 to len-1
point = high[length1] + i/(len-1)*(valbtm - high[length1])
max_diff_up := math.max(math.max(src1[length1+i],open[length1+i]) -
point,max_diff_up)
max_diff_dn := math.max(point -
math.min(src1[length1+i],open[length1+i]),max_diff_dn)
line.new(n[len+length1],valbtm,n[length1],high[length1],color=midcol)
if show_ext
line.new(n[len+length1],valbtm+max_diff_up,n[length1],high[length1]+max_diff_up
,color=upcol,style=line.style_dotted)
line.new(n[len+length1],valbtm-max_diff_dn,n[length1],high[length1]-
max_diff_dn
,color=dncol,style=line.style_dotted)
if show_labels
label.new(n[length1],high[length1],str.tostring(high[length1],'#.####'),color=#0000
0000
,style=label.style_label_down,textcolor=upcol,textalign=text.align_left,s
ize=size.small)
if os1 == 1
x1 := btm_n-length1
y1 := valbtm
for i = 0 to n-btm_n+length1-1
point = src1 + i/(n-btm_n+length1-1)*(valbtm - src1)
max_diff_up := math.max(math.max(src1[i],open[i]) - point,max_diff_up)
max_diff_dn := math.max(point - math.min(src1[i],open[i]),max_diff_dn)
else
x1 := top_n-length1
y1 := valtop
for i = 0 to n-top_n+length1-1
point = src1 + i/(n-top_n+length1-1)*(valtop - src1)
max_diff_up := math.max(math.max(src1[i],open[i]) - point,max_diff_up)
max_diff_dn := math.max(point - math.min(src1[i],open[i]),max_diff_dn)
line.delete(line.new(x1,y1,n,src1,color=midcol,extend=extend.right)[1])
if show_ext
line.delete(line.new(x1,y1+max_diff_up,n,src1+max_diff_up
,color=upcol,style=line.style_dotted,extend=extend.right)[1])
line.delete(line.new(x1,y1-max_diff_dn,n,src1-max_diff_dn
,color=dncol,style=line.style_dotted,extend=extend.right)[1])
//------------------------------------------------------------------------------
plot(btm ? low[length1] : top ? high[length1] : na,'Circles'
,color = btm ? dncol : upcol
,style=plot.style_circles
,offset=-length1)
//------------------------------------------------------------------------------
// Gann Square of 9
//------------------------------------------------------------------------------
var bool RedGan = input.bool(true, title="On/Off Red line")
var bool BlueGan = input.bool(true, title="On/Off Blue line")
var bool labelOn = input.bool(true, title="On/Off Label")
var color LabelColor = input.color(title="Label color", defval=color.black)
var color BlueLineColor = input.color(title="Blue Line color", defval=color.blue)
var color RedLineColor = input.color(title="Red Line color", defval=color.red)
var string Extend = input.string(defval="true", title="Extend line")
var int PriceLevel = input.int(3, title="Price Level", options=[3, 5])
// Variables
var label rl1 = na
var label rl2 = na
var label sl1 = na
var label bl1 = na
var label bl2 = na
label.delete(rl1)
label.delete(rl2)
label.delete(sl1)
label.delete(bl1)
label.delete(bl2)
// Variables y cálculos
var int max = 20
var int rangeMin = 0
var float[] GannNum = array.new_float(0)
dt = time - time[1]
labelPosition = time + 3 * dt
if RedGan
resistance1 = drawLine(resistance, startLine, endLine, Extend == "true",
RedLineColor)
support1 = drawLine(support, startLine, endLine, Extend == "true",
RedLineColor)
if labelOn
rl1 := drawLabel(labelPosition, resistance, "R1 = " +
str.tostring(resistance))
sl1 := drawLabel(labelPosition, support, "S1 = " + str.tostring(support))
label.set_textcolor(sl1, LabelColor)
if PriceLevel == 5
resistancePrice2 = array.get(GannNum, GannPos+GannWeightPosition) /
denomenator
resistance2 = drawLine(resistancePrice2, startLine, endLine, Extend ==
"true", RedLineColor)
if labelOn
resistanceText2 = GannWeightPosition > 0 ? "R2 = " : "S2 = "
rl2 := drawLabel(labelPosition, resistancePrice2, resistanceText2 +
str.tostring(resistancePrice2))
label.set_textcolor(rl2, LabelColor)
if BlueGan
var line blueGann1 = drawLine(blueGannPrice1, startLine, endLine, Extend ==
"true", BlueLineColor)
if labelOn
BlueText1 = close[0] >= blueGannPrice1 ? "S1 = " : "R1 = "
bl1 := drawLabel(labelPosition, blueGannPrice1, BlueText1 +
str.tostring(blueGannPrice1))
label.set_textcolor(bl1, LabelColor)
if PriceLevel == 5
var float BlueGannPrice2 = GannWeightPosition > 0 ? (array.get(GannNum,
GannPos+1) + array.get(GannNum, GannPos+2)) / 2 : (array.get(GannNum, GannPos-1) +
array.get(GannNum, GannPos)) / 2
BlueGannPrice2 := BlueGannPrice2 / denomenator
var line blueGann2 = drawLine(BlueGannPrice2, startLine, endLine,
Extend == "true", BlueLineColor)
if labelOn
BlueText2 = GannWeightPosition > 0 ? "R1 = " : "S1 = "
bl2 := drawLabel(labelPosition, BlueGannPrice2, BlueText2 +
str.tostring(BlueGannPrice2))
label.set_textcolor(bl2, LabelColor)
//-----------------------------------------------------------------------------}
//Targets with Market Structure Break & Order Block
//-----------------------------------------------------------------------------{
showLabels = input(true, 'Show Target Labels', inline = 'style')
candleColoring = input(true, 'Candle Coloring', inline = 'style')
//Condition Rule
enableTarget1 = input(true, 'Enable Target'
, inline = 'enable'
, group = 'Target 1')
//Source A
targetSource1A = input.string('External', 'Source A'
, options = ['External', 'ACCDIST', 'ATR', 'BB Middle', 'BB Upper', 'BB Lower',
'CCI', 'CMO', 'COG', 'DC High', 'DC Mid', 'DC Low'
, 'DEMA', 'EMA', 'HMA', 'III', 'KC Middle', 'KC Upper', 'KC Lower',
'LINREG', 'MACD', 'MACD-signal', 'MACD-histogram', 'MEDIAN'
, 'MFI', 'MODE', 'MOM', 'NVI', 'OBV', 'PVI', 'PVT', 'RMA', 'ROC',
'RSI', 'SMA', 'STOCH', 'Supertrend'
, 'TEMA', 'VWAP', 'VWMA', 'WAD', 'WMA', 'WVAD', '%R']
, inline = 'A'
, group = 'Target 1')
//Source B
targetSource1B = input.string('Supertrend', 'Source B'
, options = ['External', 'Value', 'ACCDIST', 'ATR', 'BB Middle', 'BB Upper',
'BB Lower', 'CCI', 'CMO', 'COG', 'DC High', 'DC Mid', 'DC Low'
, 'DEMA', 'EMA', 'HMA', 'III', 'KC Middle', 'KC Upper', 'KC Lower',
'LINREG', 'MACD', 'MACD-signal', 'MACD-histogram', 'MEDIAN'
, 'MFI', 'MODE', 'MOM', 'NVI', 'OBV', 'PVI', 'PVT', 'RMA', 'ROC',
'RSI', 'SMA', 'STOCH', 'Supertrend'
, 'TEMA', 'VWAP', 'VWMA', 'WAD', 'WMA', 'WVAD', '%R']
, inline = 'B'
, group = 'Target 1')
//-----------------------------------------------------------------------------}
//Target 1 Logic
//-----------------------------------------------------------------------------{
waitTarget1 = input(false, 'Wait Until Reached'
, group = 'Target 1 Logic')
//-----------------------------------------------------------------------------}
//Target 2
//-----------------------------------------------------------------------------{
//Condition Rule
enableTarget2 = input(true, 'Enable Target'
, inline = 'enable'
, group = 'Target 2')
//Source A
targetSource2A = input.string('External', 'Source A'
, options = ['External', 'ACCDIST', 'ATR', 'BB Middle', 'BB Upper', 'BB Lower',
'CCI', 'CMO', 'COG', 'DC High', 'DC Mid', 'DC Low'
, 'DEMA', 'EMA', 'HMA', 'III', 'KC Middle', 'KC Upper', 'KC Lower',
'LINREG', 'MACD', 'MACD-signal', 'MACD-histogram', 'MEDIAN'
, 'MFI', 'MODE', 'MOM', 'NVI', 'OBV', 'PVI', 'PVT', 'RMA', 'ROC',
'RSI', 'SMA', 'STOCH', 'Supertrend'
, 'TEMA', 'VWAP', 'VWMA', 'WAD', 'WMA', 'WVAD', '%R']
, inline = 'A'
, group = 'Target 2')
//Source B
targetSource2B = input.string('Supertrend', 'Source B'
, options = ['External', 'Value', 'ACCDIST', 'ATR', 'BB Middle', 'BB Upper',
'BB Lower', 'CCI', 'CMO', 'COG', 'DC High', 'DC Mid', 'DC Low'
, 'DEMA', 'EMA', 'HMA', 'III', 'KC Middle', 'KC Upper', 'KC Lower',
'LINREG', 'MACD', 'MACD-signal', 'MACD-histogram', 'MEDIAN'
, 'MFI', 'MODE', 'MOM', 'NVI', 'OBV', 'PVI', 'PVT', 'RMA', 'ROC',
'RSI', 'SMA', 'STOCH', 'Supertrend'
, 'TEMA', 'VWAP', 'VWMA', 'WAD', 'WMA', 'WVAD', '%R']
, inline = 'B'
, group = 'Target 2')
//-----------------------------------------------------------------------------}
//Target 2 Logic
//-----------------------------------------------------------------------------{
waitTarget2 = input(false, 'Wait Until Reached'
, group = 'Target 2 Logic')
//-----------------------------------------------------------------------------}
//Target 2 Logic
//-----------------------------------------------------------------------------{
showDash = input.bool ( false , 'Show Dashboard'
, group= 'Dashboard')
dashLoc = input.string ( 'Top Right' , 'Location' , options = ['Top
Right', 'Bottom Right', 'Bottom Left'] , group= 'Dashboard')
textSize = input.string ( 'Normal' , 'Size' , options =
['Tiny', 'Small', 'Normal'] , group= 'Dashboard')
//-----------------------------------------------------------------------------}
//UDT
//-----------------------------------------------------------------------------{
type lshape
line v
line h
type target
float value
int loc
bool reached
bool islong
bool active
lshape lines
label lbl
//-----------------------------------------------------------------------------}
//Functions
//-----------------------------------------------------------------------------{
if sZ > 0
for i = setting.size ( ) -1 to 0
get = setting.get (i)
if get == '' or get == ' '
or get == ' ' or get == ' '
setting.remove (i)
isS = setting.isString()
txt = switch
isS != '' => isS
a_1Val.includes(choice) =>
if setting.size() != 1
str.format ( 'Please enter 1 value for {0}: {1}' ,
choice, a_1ValValues.get(a_1Val.indexof(choice)))
else
if setting.getSetting(0) - setting.getStFloat(0) != 0
str.format('{0} ({1}) -> Length must be an Integer value',
choice, a_1ValValues.get(a_1Val.indexof(choice)))
a_2Val.includes(choice) =>
if setting.size() != 2
str.format ( 'Please enter 2 values for {0}: {1}' ,
choice, a_2ValValues.get(a_2Val.indexof(choice)))
else
if setting.getSetting(0) - setting.getStFloat(0) != 0
str.format('{0} ({1}) -> Length must be an Integer value',
choice, a_2ValValues.get(a_2Val.indexof(choice)))
a_3Val.includes(choice) =>
if setting.size() != 3
str.format ( 'Please enter 3 values for {0}: {1}' ,
choice, a_3ValValues.get(a_3Val.indexof(choice)))
else
if setting.getSetting(0) - setting.getStFloat(0) != 0
or setting.getSetting(1) - setting.getStFloat(1) != 0
or setting.getSetting(2) - setting.getStFloat(2) != 0
'Each Number must be an Integer value'
float shortEMA = na, float longEMA = na, float macd = na, float signal2 =
na, float hist = na
float BBmiddle = na, float BBupper = na, float BBlower = na
float KCmiddle = na, float KCupper = na, float KClower = na
if txt == ''
if str.contains(choice, 'BB')
len = setting.getSetting(0)
BBmiddle := ta.sma(close, len)
float dev = setting.getStFloat(1) * ta.stdev(close, len)
BBupper := BBmiddle + dev
BBlower := BBmiddle - dev
if str.contains(choice, 'KC')
KCmiddle := setting.getSetting(0).ema(close)
KCrange = setting.getSetting(0).ema(ta.tr)
KCupper := KCmiddle + KCrange * setting.getStFloat(1)
KClower := KCmiddle - KCrange * setting.getStFloat(1)
if str.contains(choice, 'MACD')
shortEMA := setting.getSetting(0).ema(close)
longEMA := setting.getSetting(1).ema(close)
macd := shortEMA - longEMA
signal2 := setting.getSetting(2).ema(macd)
hist := macd - signal2
'HMA' =>
len = setting.getSetting(0)
ta.wma(2 * ta.wma(close, math.floor(len / 2)) - ta.wma(close, len),
math.floor(math.sqrt(len)))
'LINREG' =>
len = setting.getSetting(0), off = setting.getSetting(1)
float sX = 0, float sY = 0, float sXSqr = 0, float sXY = 0
for i = 1 to len
val = close[len-i], per = i+1, sX += per, sY += val
sXSqr += math.pow(per, 2), sXY += val * per
slope = (sXY * len - sX * sY) / (sXSqr * len - math.pow(sX,
2))
intcp = ta.sma(close, len) - slope * sX / len + slope
intcp + slope * (len - off)
'Supertrend' =>
len = setting.getSetting(0), factor =
setting.getStFloat(1)
var atr = 0. , var upper = high, var lower = low, var float
trend = na
atr += (nz(ta.tr) - atr) / len
up = hl2 + atr * factor , dn = hl2 - atr * factor
upper := close[1] < upper ? math.min(up, upper) : up
lower := close[1] > lower ? math.max(dn, lower) : dn
trend := close > upper ? 1 : close < lower ? 0 : trend
trend == 1 ? lower : upper
'TEMA' =>
len = setting.getSetting(0)
ema1 = len.ema(close)
ema2 = len.ema(ema1 )
ema3 = len.ema(ema2 )
(3 * ema1) - (3 * ema2) + ema3
[txt, value]
//-----------------------------------------------------------------------------}
//Set target 1
//-----------------------------------------------------------------------------{
var color css = na
bool isNewTarget1 = false
bool isTgReached1 = false
//Distance
dist1 = switch distOptions1
'Currencies' => distTarget1
'%' => close + distTarget1 / 100 * close
'ATR' => nz(ta.atr(50)) * distTarget1
'Ticks' => syminfo.mintick * distTarget1
'External Value' => externalDist1 * distTarget1
if enableTarget1 and
(
(target1_condition and (waitTarget1 ? target1_object.reached : true))
or
(newTarget1 and target1_object.reached)
)
target_value = close + (isLong1 ? dist1 : -dist1)
if showLabels
target1_object.lbl := label.new(n, target_value, 'Target'
, color = color.new(target1Css, 50)
, textcolor = color.white
, size = size.tiny
, style = label.style_label_left
, tooltip = str.tostring(target_value, format.mintick))
css := target1Css
target1_object.set_target(target1Css, target1Style)
isNewTarget1 := true
countTargets1 += 1
//-----------------------------------------------------------------------------}
//Set target 2
//-----------------------------------------------------------------------------{
bool isNewTarget2 = false
bool isTgReached2 = false
//Distance
dist2 = switch distOptions2
'Currencies' => distTarget2
'%' => close + distTarget2 / 100 * close
'ATR' => nz(ta.atr(50)) * distTarget2
'Ticks' => syminfo.mintick * distTarget2
'External Value' => externalDist2 * distTarget2
if enableTarget2 and
(
(target2_condition and (waitTarget2 ? target2_object.reached : true))
or
(newTarget2 and target2_object.reached)
)
target_value = close + (isLong2 ? dist2 : -dist2)
if showLabels
target2_object.lbl := label.new(n, target_value, 'Target'
, color = color.new(target2Css, 50)
, textcolor = color.white
, size = size.tiny
, style = label.style_label_left
, tooltip = str.tostring(target_value, format.mintick))
css := target2Css
target2_object.set_target(target2Css, target2Style)
isNewTarget2 := true
countTargets2 += 1
//-----------------------------------------------------------------------------}
//Plots
//-----------------------------------------------------------------------------{
noVisuals1 = array.includes(aNoVisuals, targetSource1A) or
array.includes(aNoVisuals, targetSource1B)
noVisuals2 = array.includes(aNoVisuals, targetSource2A) or
array.includes(aNoVisuals, targetSource2B)
//-----------------------------------------------------------------------------}
//Alerts
//-----------------------------------------------------------------------------{
alertcondition(isNewTarget1, "Target 1 New" , "Target 1 New" )
alertcondition(isTgReached1, 'Target 1 Reached', 'Target 1 Reached')
//-----------------------------------------------------------------------------}
//Market Structure Break & Order Block
//-----------------------------------------------------------------------------{
settings = "Settings"
zigzag_len = input.int(9, "ZigZag Length", group=settings)
show_zigzag = input.bool(false, "Show Zigzag", group=settings)
fib_factor = input.float(0.273, "Fib Factor for breakout confirmation", 0, 1, 0.01,
group=settings)
text_size = input.string(size.tiny, "Text Size", [size.tiny, size.small,
size.normal, size.large, size.huge], group=settings)
trend = 1
trend := nz(trend[1], 1)
trend := trend == 1 and to_down ? -1 : trend == -1 and to_up ? 1 : trend
last_trend_up_since = ta.barssince(to_up[1])
low_val = ta.lowest(nz(last_trend_up_since > 0 ? last_trend_up_since : 1, 1))
low_index = bar_index - ta.barssince(low_val == low)
last_trend_down_since = ta.barssince(to_down[1])
high_val = ta.highest(nz(last_trend_down_since > 0 ? last_trend_down_since : 1, 1))
high_index = bar_index - ta.barssince(high_val == high)
if ta.change(trend) != 0
if trend == 1
array.push(low_points_arr, low_val)
array.push(low_index_arr, low_index)
if trend == -1
array.push(high_points_arr, high_val)
array.push(high_index_arr, high_index)
f_get_high(ind) =>
[array.get(high_points_arr, array.size(high_points_arr) - 1 - ind),
array.get(high_index_arr, array.size(high_index_arr) - 1 - ind)]
f_get_low(ind) =>
[array.get(low_points_arr, array.size(low_points_arr) - 1 - ind),
array.get(low_index_arr, array.size(low_index_arr) - 1 - ind)]
f_delete_box(box_arr) =>
if delete_boxes
box.delete(array.shift(box_arr))
else
array.shift(box_arr)
0
market = 1
market := nz(market[1], 1)
// market := market == 1 and close < l0 and low < l0 - math.abs(h0 - l0) *
fib_factor ? -1 : market == -1 and close > h0 and high > h0 + math.abs(h0 - l0) *
fib_factor ? 1 : market
last_l0 = ta.valuewhen(ta.change(market) != 0, l0, 0)
last_h0 = ta.valuewhen(ta.change(market) != 0, h0, 0)
market := last_l0 == l0 or last_h0 == h0 ? market : market == 1 and l0 < l1 and l0
< l1 - math.abs(h0 - l1) * fib_factor ? -1 : market == -1 and h0 > h1 and h0 > h1 +
math.abs(h1 - l0) * fib_factor ? 1 : market
bu_ob_index = bar_index
bu_ob_index := nz(bu_ob_index[1], bar_index)
for i=h1i to l0i[zigzag_len]
index = bar_index - i
if open[index] > close[index]
bu_ob_index := bar_index[index]
be_ob_index = bar_index
be_ob_index := nz(be_ob_index[1], bar_index)
for i=l1i to h0i[zigzag_len]
index = bar_index - i
if open[index] < close[index]
be_ob_index := bar_index[index]
be_bb_index = bar_index
be_bb_index := nz(be_bb_index[1], bar_index)
for i=h1i - zigzag_len to l1i
index = bar_index - i
if open[index] > close[index]
be_bb_index := bar_index[index]
bu_bb_index = bar_index
bu_bb_index := nz(bu_bb_index[1], bar_index)
for i=l1i - zigzag_len to h1i
index = bar_index - i
if open[index] < close[index]
bu_bb_index := bar_index[index]
if ta.change(market) != 0
if market == 1
line.new(h1i, h1, h0i, h1, color=color.rgb(76, 175, 79, 100), width=1)
label.new(int(math.avg(h1i, l0i)), h1, "MSB", color=color.new(color.black,
100), style=label.style_label_down, textcolor=color.rgb(76, 175, 79, 100),
size=size.small)
bu_ob = box.new(bu_ob_index, high[bu_ob_since], bar_index + 10,
low[bu_ob_since], bgcolor=bu_ob_color, border_color=bu_ob_border_color, text="Bu-
OB", text_color=bu_ob_text_color, text_halign=text.align_right,
text_size=text_size)
bu_bb = box.new(bu_bb_index, high[bu_bb_since], bar_index + 10,
low[bu_bb_since], bgcolor=bu_bb_color, border_color=bu_bb_border_color, text=l0 <
l1 ? "Bu-BB" : "Bu-MB", text_color=bu_bb_text_color, text_halign=text.align_right,
text_size=text_size)
array.push(bu_ob_boxes, bu_ob)
array.push(bu_bb_boxes, bu_bb)
if market == -1
line.new(l1i, l1, l0i, l1, color=color.rgb(255, 82, 82, 100), width=1)
label.new(int(math.avg(l1i, h0i)), l1, "MSB", color=color.new(color.black,
100), style=label.style_label_up, textcolor=color.rgb(255, 82, 82, 100),
size=size.small)
be_ob = box.new(be_ob_index, high[be_ob_since], bar_index + 10,
low[be_ob_since], bgcolor=be_ob_color, border_color=be_ob_border_color, text="Be-
OB", text_color=be_ob_text_color, text_halign=text.align_right,
text_size=text_size)
be_bb = box.new(be_bb_index, high[be_bb_since], bar_index + 10,
low[be_bb_since], bgcolor=be_bb_color, border_color=be_bb_border_color, text=h0 >
h1 ? "Be-BB" : "Be-MB", text_color=be_bb_text_color, text_halign=text.align_right,
text_size=text_size)
array.push(be_ob_boxes, be_ob)
array.push(be_bb_boxes, be_bb)
//-----------------------------------------------------------------------------}
//SUPPLY AND DEMAND
//-----------------------------------------------------------------------------{
//------------------------------------------------------------------------------
//Settings
//-----------------------------------------------------------------------------{
per = input.float(10., 'Threshold %', minval = 0, maxval = 100)
div = input.int(50, 'Resolution' , minval = 2, maxval = 500)
tf = input.timeframe('', 'Intrabar TF')
//Colors
showSupply = input(true ,'Supply ', inline = 'supply', group = 'Style')
supplyCss = input(#2156f321, '' , inline = 'supply', group = 'Style')
supplyArea = input(true ,'Area' , inline = 'supply', group = 'Style')
supplyAvg = input(true ,'Average' , inline = 'supply', group = 'Style')
supplyWavg = input(true ,'Weighted' , inline = 'supply', group = 'Style')
//-----------------------------------------------------------------------------}
//UDT's
//-----------------------------------------------------------------------------{
type bin
float lvl
float prev
float sum
float prev_sum
float csum
float avg
bool isreached
type area
box bx
line avg
line wavg
//-----------------------------------------------------------------------------}
//Functions
//-----------------------------------------------------------------------------{
method set_area(area id, x1, top, btm, avg, wavg, showArea, showAvg, showWavg)=>
if showArea
id.bx.set_lefttop(x1, top)
id.bx.set_rightbottom(n, btm)
if showAvg
id.avg.set_xy1(x1, avg)
id.avg.set_xy2(n, avg)
if showWavg
id.wavg.set_xy1(x1, wavg)
id.wavg.set_xy2(n, wavg)
//-----------------------------------------------------------------------------}
//Main variables
//-----------------------------------------------------------------------------{
var max1 = 0.
var min1 = 0.
var x1 = 0
var csum = 0.
//Intrabar data
[h, l, v] = request.security_lower_tf(syminfo.tickerid, tf, get_hlv())
//-----------------------------------------------------------------------------}
//Set zones
//-----------------------------------------------------------------------------{
var supply_area = area.new(
box.new(na, na, na, na, na, bgcolor = color.new(supplyCss, 90))
, line.new(na, na, na, na, color = supplyCss)
, line.new(na, na, na, na, color = supplyCss, style = line.style_dashed))
if time == chart.right_visible_bar_time
r = (max1 - min1) / div
supply = bin.new(max1, max1, 0, 0, 0, 0, false)
demand = bin.new(min1, min1, 0, 0, 0, 0, false)
supply.isreached := true
demand.isreached := true
supply.prev := supply.lvl
demand.prev := demand.lvl