Dimilet
Pine Script Rookie
Pine Script Rookie
Posts: 1
Joined: October 16th, 2023
Contact: TradingView Profile

A friend wrote me a script about supply and demand levels and liquidity. But it won’t be possible to make it multi-timef

//@version=5
indicator(shorttitle='My SDL', title='My SDL', overlay=true)

// functions
get(arr, index) =>
index < array.size(arr) ? array.get(arr, index) : na

busted(highs, lows, times, bounces) =>
array.shift(highs)
array.shift(lows)
array.shift(times)
array.shift(bounces) or true

bounced(bounces) =>
status = array.get(bounces, 0)
array.set(bounces, 0, true)
status

// A. CORE
ceiling = math.max(high, close[1], open[1])
floor = math.min(low, close[1], open[1])

buying = close >= open and high != low
selling = close <= open and low != high

green = close > open and close > close[1]
red = close < open and close < close[1]

gapup = open > close[1]
gapdown = open < close[1]

higher = high > high[1]
lower = low < low[1]

bullish = green and higher
bearish = red and lower

// notable price actions
bullishEngulf = selling[1] and (gapdown or lower) and bullish and close > open[1]
bearishEngulf = buying[1] and (gapup or higher) and bearish and close < open[1]

breakHigh = (selling[2] or selling[1]) and buying and close > ceiling[1]
breakLow = (buying[2] or buying[1]) and selling and close < floor[1]

whiteSoldiers = bearish[3] and buying[2] and bullish[1] and bullish and close > high[3]
blackCrows = bullish[3] and selling[2] and bearish[1] and bearish and close < low[3]

// pivot setups
soaring = bullishEngulf or breakHigh or whiteSoldiers
tumbling = bearishEngulf or breakLow or blackCrows

reversal = switch
whiteSoldiers => not soaring[1] and not soaring[2]
blackCrows => not tumbling[1] and not tumbling[2]
breakHigh => not soaring[1] and (bearish[1] or bearish[2])
breakLow => not tumbling[1] and (bullish[1] or bullish[2])

continuation = switch
breakHigh => bullish[2] and close > high[2] and not bearish[1]
breakLow => bearish[2] and close < low[2] and not bullish[1]

engulfing = (bullishEngulf or bearishEngulf) and (higher[1] or lower[1])

// B. PIVOT ZONES
var buyzoneHigh = array.new_float(0)
var buyzoneLow = array.new_float(0)
var buyzoneTime = array.new_int(0)
var bounceUp = array.new_bool(0)

var sellzoneHigh = array.new_float(0)
var sellzoneLow = array.new_float(0)
var sellzoneTime = array.new_int(0)
var bounceDown = array.new_bool(0)

// 1. Broken Pivot Zones
brokenHigh = while get(sellzoneHigh, 0) < high
busted(sellzoneHigh, sellzoneLow, sellzoneTime, bounceDown)

brokenLow = while get(buyzoneLow, 0) > low
busted(buyzoneHigh, buyzoneLow, buyzoneTime, bounceUp)

// 2. Distribution/Accumulation Bar and Pivot Bar
upturn = soaring and (reversal or continuation or engulfing)
downturn = tumbling and (reversal or continuation or engulfing)

dacbar = switch
upturn => whiteSoldiers ? 3 : (breakHigh and selling[2] ? 2 : 1)
downturn => blackCrows ? 3 : (breakLow and buying[2] ? 2 : 1)

pivotbar = switch
upturn => whiteSoldiers ? 2 : (green[1] ? 1 : 0)
downturn => blackCrows ? 2 : (red[1] ? 1 : 0)

// 3. Pivot Zone Values
pzHigh = float(na)
pzLow = float(na)

switch
upturn =>
// low at wick
pzLow := math.min(low[dacbar], low[pivotbar], low[1], low)
// high at wick or open
pzHigh := switch
close[pivotbar] > high[dacbar] => high[dacbar]
open[pivotbar] > open[dacbar] => open[pivotbar]
=> open[dacbar]

downturn =>
// high at wick
pzHigh := math.max(high[dacbar], high[pivotbar], high[1], high)
// low at wick or open
pzLow := switch
close[pivotbar] < low[dacbar] => low[dacbar]
open[pivotbar] < open[dacbar] => open[pivotbar]
=> open[dacbar]

// 4. Overlapping Pivot Zones
overlap = switch
upturn => get(buyzoneHigh, 0) >= pzLow
downturn => get(sellzoneLow, 0) <= pzHigh

replace = switch
overlap and upturn => bounced(bounceUp)
overlap and downturn => bounced(bounceDown)

// remove replaced zone or adjust overlapped zone
switch
replace and upturn => busted(buyzoneHigh, buyzoneLow, buyzoneTime, bounceUp)
replace and downturn => busted(sellzoneHigh, sellzoneLow, sellzoneTime, bounceDown)
overlap and upturn => array.set(buyzoneHigh, 0, pzLow)
overlap and downturn => array.set(sellzoneLow, 0, pzHigh)

// 5. Pivot Zones Queue
switch
upturn =>
array.unshift(buyzoneHigh, pzHigh)
array.unshift(buyzoneLow, pzLow)
array.unshift(buyzoneTime, time[dacbar])
array.unshift(bounceUp, false)

downturn =>
array.unshift(sellzoneHigh, pzHigh)
array.unshift(sellzoneLow, pzLow)
array.unshift(sellzoneTime, time[dacbar])
array.unshift(bounceDown, false)

// 6. Pivot Zones Markup
maxbox(redraw) => redraw ? 22 : na

newbox(bg) =>
box.new(0, 0, 0, 0, xloc=xloc.bar_time, border_color=na, bgcolor=bg, extend=extend.right)

render(boxes, index, highs, lows, times) =>
ibox = get(boxes, index)
top = get(highs, index)
bottom = get(lows, index)
left = get(times, index)
overlapped = if index > 0
lastbox = index - 1
top == get(lows, lastbox) or bottom == get(highs, lastbox)
box.set_lefttop(ibox, left, overlapped ? na : top)
box.set_rightbottom(ibox, time, overlapped ? na : bottom)

var supply = input.color(#F2364514, 'Supply Zones')
var demand = input.color(#08998114, 'Demand Zones')

var buyBox = array.new_box(0)
var sellBox = array.new_box(0)

for i = 0 to maxbox(na(close[1]))
array.push(buyBox, newbox(demand))
array.push(sellBox, newbox(supply))

for i = 0 to maxbox(upturn or brokenLow)
render(buyBox, i, buyzoneHigh, buyzoneLow, buyzoneTime)

for i = 0 to maxbox(downturn or brokenHigh)
render(sellBox, i, sellzoneHigh, sellzoneLow, sellzoneTime)

// C. ALERTS
if brokenHigh
alert('Breakout', alert.freq_once_per_bar)

if brokenLow
alert('Breakdown', alert.freq_once_per_bar)

if upturn or downturn
setup = switch
whiteSoldiers => 'White Soldiers'
blackCrows => 'Black Crows'
breakHigh => bullishEngulf ? 'Engulf & Break High' : 'Break High'
breakLow => bearishEngulf ? 'Engulf & Break Low' : 'Break Low'
bullishEngulf => 'Bullish Engulf'
bearishEngulf => 'Bearish Engulf'
occurence = replace ? 'Replace' : (overlap ? 'Bounce' : 'Fresh')
message = setup + ' (' + occurence + ')'
alert(message, alert.freq_once_per_bar_close)

plotLiq = input.bool(defval=true, title='Liquidity Highs/Lows', group='Enable/Disable Section---------------------------------------------', inline = '01')

/////////////////////////////////////////////////////////////////////////Liquidity Equal Highs
pvtTopColor = input.color(defval=color.new(color.black,85), title='Top Color', group='Liquidity-------------------------------------------------', inline='1')
pvtBtmColor = input.color(defval=color.new(color.black,85), title='Bottom Color', group='Liquidity-------------------------------------------------',inline = '1')
pvtStyle = line.style_solid
pvtMax = input.int(defval=30, title='Maximum Liquidity Displayed', minval=1, maxval=500, group='Liquidity-------------------------------------------------', tooltip='Minimum = 1, Maximum = 500')
delline = input.bool(defval=true, title='Delete After X Bars Through Line', group='Liquidity-------------------------------------------------')
mitdel = input.int(defval=15, title='Mitigated+Line Delete (X Bars)', minval=1, maxval=100, group='Liquidity-------------------------------------------------', tooltip='Line will remain on chart until this many bars after first broken through. Minimum = 1, Maximum = 500')
linebrk = input.bool(defval=true, title='Color After Close Through Line', group='Liquidity-------------------------------------------------')
mitdelcol = input.color(defval=(color.new(color.black,85)), title='Mitigated Color', group='Liquidity-------------------------------------------------', tooltip = 'Once broken, line will change to this colour until deleted. Line deletion is controlled by (Mitigated+Line Delete) input control')
//del_pc = input.bool(defval=false, title='', group='Liquidity-------------------------------------------------', inline="m")
//mitdel_pc = input.int(defval=300, title='Only show lines within x% of current price', minval=1, maxval=500, step=50, group='Liquidity-------------------------------------------------', inline="m")
//
brkstyle = line.style_dotted
var line[] _lowLiqLines = array.new_line()
var line[] _highLiqLines = array.new_line()

//Functions
isPvtHigh(_index, __high) =>
__high[_index+2] < __high[_index+1] and __high[_index+1] > __high[_index]

// | <-- pivot high
// ||| <-- candles
// 210 <-- candle index

isPvtLow(_index, __low) =>
__low[_index+2] > __low[_index+1] and __low[_index+1] < __low[_index]

// ||| <-- candles
// | <-- pivot low
// 210 <-- candle index

//Function to Calculte Line Length
_controlLine(_lines, __high, __low) =>
if array.size(_lines) > 0
for i = array.size(_lines) - 1 to 0 by 1
_line = array.get(_lines, i)
_lineLow = line.get_y1(_line)
_lineHigh = line.get_y2(_line)
_lineRight = line.get_x2(_line)
if na or (bar_index == _lineRight and not((__high > _lineLow and __low < _lineLow) or (__high > _lineHigh and __low < _lineHigh)))
line.set_x2(_line, bar_index + 1)
//deletes line if not within X% of current last price
//pch = (mitdel_pc/100) + 1
//pcl = 1 - (mitdel_pc/100)
//cprice = close
//highpc = (cprice * pch)
//lowpc = (cprice * pcl)
//if del_pc
//if _lineLow < close + mitdel_pc //) and > (close*0.98)) //if Y2 < close*1.05
//line.set_color(_line, color.new(color.white,50))
//line.delete(_line)
///deletes line if more than X bars pass through
if _lineRight > bar_index[mitdel] and _lineRight < bar_index[0] and linebrk
line.set_color(_line,mitdelcol)
line.set_style(_line, brkstyle)
if _lineRight < bar_index[mitdel] and delline
line.delete(_line)

//Pivot Low Line Plotting
if isPvtLow(0, low) and plotLiq
_lowPVT = line.new(x1=bar_index - 1, y1=low[1], x2=bar_index, y2=low[1], extend=extend.none, color=pvtBtmColor, style=pvtStyle)
if array.size(_lowLiqLines) >= pvtMax
line.delete(array.shift(_lowLiqLines))
array.push(_lowLiqLines, _lowPVT)

//Pivot High Line Plotting
if isPvtHigh(0, high) and plotLiq
_highPVT = line.new(x1=bar_index - 1, y1=high[1], x2=bar_index, y2=high[1], extend=extend.none, color=pvtTopColor, style=pvtStyle)
if array.size(_highLiqLines) >= pvtMax
line.delete(array.shift(_highLiqLines))
array.push(_highLiqLines, _highPVT)

if plotLiq
_controlLine(_lowLiqLines, high, low)
_controlLine(_highLiqLines, high, low)


Return to “Share Your Scripts”