Certainly! I can help you create a custom indicator for TradingView that identifies and draws lines at Breaks of Structure (BoS). We'll use Pine Script v5, TradingView's scripting language, to code this indicator.
What is a Break of Structure?
A Break of Structure in technical analysis occurs when the price breaks beyond a previous significant high or low, indicating a potential shift in market sentiment or trend direction. detecting BoS can help traders identify potential entry or exit points.
Overview of the Indicator:
- Identify swing highs and swing lows.
- Detect when a swing high or low is broken by the price.
- Draw horizontal lines at the points of Break of Structure.
Pine Script Code:
Below is the code for the custom indicator. You can copy and paste this directly into the Pine Editor in TradingView.
//@version=5
indicator(title="Break of Structure Indicator", shorttitle="BoS Indicator", overlay=true)
// Input parameters
swingLookback = input.int(title="Swing Lookback Period", defval=5, minval=1, maxval=20)
lineLength = input.int(title="Line Length (Bars)", defval=20, minval=1)
showLabels = input.bool(title="Show BoS Labels", defval=true)
// Function to identify swing highs and lows
swingHigh(cond) => high[cond] == ta.highest(high, cond * 2 + 1)
swingLow(cond) => low[cond] == ta.lowest(low, cond * 2 + 1)
// Arrays to store swing points
var swingHighs = array.new_float()
var swingHighBars = array.new_int()
var swingLows = array.new_float()
var swingLowBars = array.new_int()
// Identify and store swing highs
if swingHigh(swingLookback)
array.unshift(swingHighs, high[swingLookback])
array.unshift(swingHighBars, bar_index - swingLookback)
// Identify and store swing lows
if swingLow(swingLookback)
array.unshift(swingLows, low[swingLookback])
array.unshift(swingLowBars, bar_index - swingLookback)
// Limit array sizes
if array.size(swingHighs) > 50
array.pop(swingHighs)
array.pop(swingHighBars)
if array.size(swingLows) > 50
array.pop(swingLows)
array.pop(swingLowBars)
// Check for Break of Structure to the upside
var line boLine = na
for i = 0 to array.size(swingHighs) - 1
sh = array.get(swingHighs, i)
shBar = array.get(swingHighBars, i)
if time >= time_from_bar_index(shBar)
if close > sh and close[1] <= sh
// Break of Structure detected
boLine := line.new(shBar, sh, bar_index, sh, extend=extend.right, color=color.green, width=2)
if showLabels
label.new(bar_index, high, "BoS High", color=color.green, style=label.style_label_down, textcolor=color.white, size=size.tiny)
break
// Check for Break of Structure to the downside
var line bosLine = na
for i = 0 to array.size(swingLows) - 1
sl = array.get(swingLows, i)
slBar = array.get(swingLowBars, i)
if time >= time_from_bar_index(slBar)
if close < sl and close[1] >= sl
// Break of Structure detected
bosLine := line.new(slBar, sl, bar_index, sl, extend=extend.right, color=color.red, width=2)
if showLabels
label.new(bar_index, low, "BoS Low", color=color.red, style=label.style_label_up, textcolor=color.white, size=size.tiny)
break
// Helper function to get time from bar index
time_from_bar_index(idx) =>
var int timezone = syminfo.timezone
time[bar_index - idx]
Explanation:
How to Use the Indicator:
-
Add the Indicator to TradingView:
- Open TradingView and navigate to a chart.
- Click on "Pine Editor" at the bottom of the page.
- Paste the code into the editor.
- Click on "Save" and give it a name like "Break of Structure Indicator".
- Click on "Add to Chart".
-
Customize Settings:
- After adding the indicator, you can customize the input parameters by clicking on the gear icon next to the indicator's name in the chart's "Indicators & Strategies" panel.
- Adjust the Swing Lookback Period to change sensitivity (higher values for longer-term structures).
- Modify Line Length if you want the BoS lines to be longer or shorter.
- Toggle Show BoS Labels on or off depending on your preference.
-
Interpreting the Indicator:
- Green Lines and Labels: Indicate a bullish Break of Structure where the price has broken above a previous swing high.
- Red Lines and Labels: Indicate a bearish Break of Structure where the price has broken below a previous swing low.
- Use these signals in conjunction with other analyses to make informed trading decisions.
Note: This indicator is a tool to aid in technical analysis and should not be used as a sole basis for trading decisions. Always consider additional factors and risk management practices.
Feel free to ask if you need further customization or have any questions!