Dynamic Structure Indicator


This is one of the first indicators I created when I was learning how to code in PineScript. The purpose of this indicator was to learn how to detect structure using code.

I never managed to get the indicator to be quite as accurate as I would like it to be, and it is still a work in progress. But it can be helpful for new traders to train their eyes to spot these types of zones, and some traders may find the code itself useful for their own purposes.

How It Works

For resistance zones, the script will search the previous 50 candles to find the highest high and the highest close. Until price breaks and closes above this zone, the zone will be painted red. When this zone is broken, it will turn green as past resistance often becomes support.

For support zones, the script will search the previous 50 candles to find the lowest low and the lowest close. Until price breaks and closes below this zone, the zone will be painted green. When this zone is broken, it will turn red as past support often becomes resistance.

You can also change it to draw higher timeframe zones in the settings menu.

Settings

Lookback Period (In Bars):
This sets how far the script will look back for detecting swing high/lows.

Reference Timeframe:
This can change what timeframe the script gets its high/low close data from.

Draw Resistance Zones:
If this is disabled, resistance zones will not be drawn.

Draw Support Zones:
If this is disabled, support zones will not be drawn.

Enable Max Zone Size:
This is a filter for removing abnormally large zones when they occur. These huge zones can be the result of a large wick spike or the script miscalculating the zone (human eyes will always be better at detecting these types of zones).

Max Zone Size (Pips):
This tells the indicator what maximum zone size should be allowed.

Min Zone Size (Pips):
This tells the indicator what minimum zone size should be allowed.

Draw Zone Size Values:
This is for debugging. If it is enabled, it will tell you what the zone size is when you hover the mouse over a zone.

Offset:
This tells PineScript how many bars to ‘shift’ the drawing of the zone.



Source Code

// Created by Matthew J. Slabosz
// www.zenandtheartoftrading.com
// @version=3
study("MJS - Dynamic Structure Indicator", shorttitle="DSI", overlay=true)

// User user input
lookback = input(title="Lookback Period (In Bars):", type=integer, defval=10)
higherRes = input(title="Reference Timeframe:", defval="240", type=resolution)
drawResistance = input(title="Draw Resistance Zones?", type=bool, defval=true)
drawSupport = input(title="Draw Support Zones?", type=bool, defval=true)
enableMaxZoneSize = input(title="Enable Max Zone Size?", type=bool, defval=true)
maxZoneSize = input(title="Max Zone Size (Pips):", type=float, defval=50, minval=1)
minZoneSize = input(title="Min Zone Size (Pips):", type=float, defval=1, minval=1)
drawZoneSize = input(title="Draw Zone Size Values?", type=bool, defval=false)
offset = input(title="Offset:", type=integer, defval=2)

// Get HTF data
htfClose = security(tickerid, higherRes, close[1])
htfHigh = security(tickerid, higherRes, high[1])
htfLow = security(tickerid, higherRes, low[1])

// Gather previous major swing high/lows and then grab their wick and close data to map future potential reversal/support zones
resTop = valuewhen(htfHigh >= highest(htfHigh, lookback) and htfHigh[1] < htfHigh and htfHigh[2] < htfHigh, htfHigh, 0)
resBot = valuewhen(htfClose >= highest(htfClose, lookback) and htfHigh[1] < htfHigh and htfHigh[2] < htfHigh, htfClose, 0)
supTop = valuewhen(htfClose <= lowest(htfClose, lookback) and htfLow[1] > htfLow and htfLow[2] > htfLow, htfClose, 0)
supBot = valuewhen(htfLow <= lowest(htfLow, lookback) and htfLow[1] > htfLow and htfLow[2] > htfLow, htfLow, 0)

// Calculate zone sizes in pips
resistanceSize = abs(resTop - resBot)
supportSize = abs(supTop - supBot)

// Zone size validation / pip conversion
atr=atr(14)
maxZonePips = atr < 0.1 ? maxZoneSize / 10000 : (atr > 1.0 ? maxZoneSize : maxZoneSize / 100)
minZonePips = atr < 0.1 ? minZoneSize / 10000 : (atr > 1.0 ? minZoneSize : minZoneSize / 100)
plot(drawZoneSize ? resistanceSize : na, color=black, transp=0, title="Resistance Zone Size (pips)")
plot(drawZoneSize ? supportSize : na, color=black, transp=0, title="Support Zone Size (pips)")
validResistance = (not enableMaxZoneSize or (resistanceSize <= maxZonePips and resistanceSize >= minZonePips)) and resTop == resTop[1] and resBot == resBot[1] and drawResistance
validSupport = (not enableMaxZoneSize or (supportSize <= maxZonePips and supportSize >= minZonePips)) and supTop == supTop[1] and supBot == supBot[1] and drawSupport

// Draw invisible plot lines required for zone color fill
r1 = plot(resTop == resTop[1] and validResistance ? resTop : na, color = na, transp=100, offset=offset)
r2 = plot(resBot == resBot[1] and validResistance ? resBot : na, color = na, transp=100, offset=offset)
s1 = plot(supTop == supTop[1] and validSupport ? supTop : na, color = na, transp=100, offset=offset)
s2 = plot(supBot == supBot[1] and validSupport ? supBot : na, color = na, transp=100, offset=offset)

// Fill zones
fill(r1, r2, color=resTop != resTop[1] or resBot != resBot[1] or not validResistance ? white : (close > resTop ? green : red), transp=50)
fill(s1, s2, color=supTop != supTop[1] or supBot != supBot[1] or not validSupport ? white : (close < supBot ? red : green), transp=50)

Last Updated: 21st March, 2019


TradingView Charting Platform

Leave a Comment, Question or Suggestion