Strategy · Pine Script v5 · On the chart

Breakout Strategy

by Web3Degenerate

This strategy identifies breakouts above or below a higher-timeframe reference level (daily by default) and executes trades with automated position sizing based on risk percentage.

View on GitHub

Repaints

GitHub stars2on the repository
Pine versionv5runs as is
Lines of code105
Settings5inputs
LicenceNone statedfile: MPL-2.0
Last update2 years ago13 Sep 2024

Add It to Your TradingView Chart

  1. 1Copy the scriptPress Copy script above, or the copy button on the code below.
  2. 2Open the Pine EditorOpen a TradingView chart and choose Pine Editor at the bottom of the screen.
  3. 3Paste over the templateCreate a new strategy, select all of the template text and paste. Press Save and give it a name.
  4. 4Add it to the chartPress Add to chart. The trades and results appear in the Strategy Tester tab; adjust the settings with the gear icon.

Source Code

105 lines of Pine Script v5 from advanced-pine-script/9.Strategy-Scripts/7.Calculating-Position-Size.pine, published under MPL-2.0 (file header). Keep the author's credit and licence lines when you reuse it.

7.Calculating-Position-Size.pine
// Calculation
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// @version=5
strategy(title="Breakout Strategy", overlay=true, currency="USD")

// Import ZenLibrary
import ZenAndTheArtOfTrading/ZenLibrary/3 as zen

// Get user input
res             = input.timeframe(title="Timeframe", defval="D")
rr              = input.float(title="Risk:Reward", defval=1.0)
stopMultiplier  = input.float(title="Stop Multiplier", defval=1.0)
lookback        = input.int(title="Stop Lookback", defval=1)
riskPerTrade    = input.float(title="Risk Per Trade %", defval=1.0)

// Get highs and lows
dailyHigh = request.security(syminfo.tickerid, res, high[barstate.isrealtime ? 1 : 0])
dailyLow  = request.security(syminfo.tickerid, res, low[barstate.isrealtime ? 1 : 0])

// Detect unique breakout for today
var bullishBreakoutToday = false
var bearishBreakoutToday = false
newDay = ta.change(time("D"))
if newDay
    bullishBreakoutToday := false
    bearishBreakoutToday := false

// Get ATR
atr = ta.atr(14)

// Check for breakouts
bullishBO = close > dailyHigh and close[1] <= dailyHigh and not bullishBreakoutToday and not na(atr) and strategy.position_size == 0
bearishBO = close < dailyLow and close[1] >= dailyLow and not bearishBreakoutToday and not na(atr) and strategy.position_size == 0

// Get stops & targets
longStop  = ta.lowest(low, lookback) - (atr * stopMultiplier)
shortStop = ta.highest(high, lookback) + (atr * stopMultiplier)
longStopDist  = close - longStop
shortStopDist = shortStop - close
longTarget  = close + (longStopDist * rr)
shortTarget = close - (shortStopDist * rr)

//------------- DETERMINE CURRENCY CONVERSION RATE -------------//
// Check if our account currency is the same as the base or quote currency or neither (for risk $ conversion purposes)
accountSameAsCounterCurrency = strategy.account_currency == syminfo.currency
accountSameAsBaseCurrency = strategy.account_currency == syminfo.basecurrency
accountNeitherCurrency = not accountSameAsCounterCurrency and not accountSameAsBaseCurrency
// Get currency conversion rates if applicable
conversionCurrencyPair = accountSameAsCounterCurrency ? syminfo.tickerid : strategy.account_currency + syminfo.currency
conversionCurrencyRate = accountSameAsBaseCurrency or accountNeitherCurrency ? request.security(conversionCurrencyPair, "D", close, ignore_invalid_symbol=true) : 1.0
// Display the current conversion currency ticker (for debug purposes)
if barstate.islastconfirmedhistory
    table t = table.new(position.top_right, 1, 1, color.black)
    table.cell(t, 0, 0, "Conversion: " + conversionCurrencyPair + " (" + str.tostring(conversionCurrencyRate) + ")", text_color=color.white, text_size=size.small)
//------------- END CURRENCY CONVERSION RATE CODE -------------//

// Save stop & target
var float t_stop = na
var float t_target = na

// If bullish breakout detected today, disable any future signals for today
if bullishBO
    bullishBreakoutToday := true
    t_stop := longStop
    t_target := longTarget
    positionSize = zen.av_getPositionSize(strategy.equity, riskPerTrade, zen.toWhole(longStopDist) * 10, conversionCurrencyRate)
    stopLabelText = str.tostring(longStopDist / syminfo.mintick / 10, "#.#")
    strategy.entry(id="Long", direction=strategy.long, qty=positionSize, comment="Long (SL=" + stopLabelText + " pips)")

// If bearish breakout detected today, disable any future signals for today
if bearishBO
    bearishBreakoutToday := true
    t_stop := shortStop
    t_target := shortTarget
    positionSize = zen.av_getPositionSize(strategy.equity, riskPerTrade, zen.toWhole(shortStopDist) * 10, conversionCurrencyRate)
    stopLabelText = str.tostring(shortStopDist / syminfo.mintick / 10, "#.#")
    strategy.entry(id="Short", direction=strategy.short, qty=positionSize, comment="Short (SL=" + stopLabelText + " pips)")

// Check long stops & targets being hit
strategy.exit(id="Long Exit",
     from_entry="Long",
     limit=t_target,
     stop=t_stop,
     when=strategy.position_size > 0,
     comment="Long Exit (Equity: " + str.tostring(strategy.equity, "###,###,###") + ")")

// Check short stops & targets being hit
strategy.exit(id="Short Exit",
     from_entry="Short",
     limit=t_target,
     stop=t_stop,
     when=strategy.position_size < 0,
     comment="Short Exit (Equity: " + str.tostring(strategy.equity, "###,###,###") + ")")

// Draw highs and lows to the chart
plot(dailyHigh, color=color.red, linewidth=2)
plot(dailyLow, color=color.green, linewidth=2)

// Draw signals
plotshape(bearishBO, title="Bearish Breakout", style=shape.triangledown, location=location.abovebar, color=color.red)
plotshape(bullishBO, title="Bullish Breakout", style=shape.triangleup, location=location.belowbar, color=color.green)

// Draw stops & targets
plot(strategy.position_size != 0 ? t_stop : na, color=color.red, style=plot.style_linebr, title="Trade Stop")
plot(strategy.position_size != 0 ? t_target : na, color=color.green, style=plot.style_linebr, title="Trade Target")

Overview

Entries occur when the close crosses the reference high or low for the first time in a session. Stop losses are calculated using ATR and recent swing lookbacks, while take-profit targets use a fixed risk-to-reward ratio. Note that the higher-timeframe data request behaves differently between historical and real-time bars, which causes historical discrepancies.

Details

Published by

Web3Degenerate on GitHub

Draws

On the price chart

Markets

Any symbol

Timeframes

M15-H1

Risk Notes

  • Signals can change on the live bar or after it closes (repainting)

Settings

The inputs you can change from the gear icon on the chart. Defaults are the author's.

SettingTypeDefaultVariable
TimeframetimeframeDres
Risk:Rewardfloat1.0rr
Stop Multiplierfloat1.0stopMultiplier
Stop Lookbackint1lookback
Risk Per Trade %float1.0riskPerTrade
Third-party code. ForexR did not write, test or endorse this script, and the summary above was generated automatically from its code and README. Indicators describe the past, and a strategy's backtest can look better than live trading, especially when a script repaints. Check it in the Strategy Tester and on a demo account before you rely on it.

Similar Pine Scripts

This trend-following strategy executes a classic Donchian channel breakout system designed to cross-validate backtest results against external framework benchmarks. It triggers a long entry when the closing price breaks above the highest high of the prior entr

Strategy v6 BreakoutTrend
Updated 2 days ago · AGPL-3.0 GitHub repository of Forven S01509 - Simple Donchian 20/10 Breakout
257 stars

This strategy plots running pivot highs as red R circles and pivot lows as blue S circles directly on the price chart. It computes an EMA 3 and a Cto trend line from close prices, then generates long signals on upward EMA-Cto crossovers combined with price pos

Strategy v6 TrendMoving averages Repaints
Updated 7 weeks ago · None stated GitHub repository of Best Chart 1m Strategy Candle Bar

STRG-HOLP

by harryguiacorn
220 stars

STRG-HOLP is a trading strategy that identifies High of Low Period (HOLP) and Low of High Period (LOHP) breakout setups. It tracks the lowest low or highest high over a user-defined lookback window. A long trade opens when the close breaks above the high of th

Strategy v5 BreakoutSupport & resistance
Updated 1 year ago · None stated GitHub repository of STRG-HOLP
106 stars

This script monitors opening gap conditions and breakout behavior relative to the first candle's range across up to twenty configurable stock tickers. It detects whether following bars remain inside the opening range before breaking out, displaying the status

Strategy v4 Screeners & dashboardsBreakout Repaints
Updated 2 years ago · GPL-3.0 GitHub repository of 1St Bar GAP+BkOut Screener v2 by RM

This trend breakout strategy plots Donchian Channels consisting of upper, lower, and middle bands directly on the main price chart. Long positions enter when price touches the upper channel and pulls back to or below the middle band, placing a stop loss at the

Strategy v6 TrendBreakout
Updated 2 months ago · MPL-2.0 GitHub repository of Donchian Strategy - Dynamic TP, Fixed SL, 1% Risk, Daily Target

More Breakout scripts

Related on ForexR

Trade Your Charts With a Regulated Broker

Compare brokers that connect to TradingView, with spreads, minimum deposits and leverage side by side.

Compare brokers