How to detect pre-market breakouts in Pine Script

This lesson demonstrates how to analyze pre-market price action to detect overnight gaps higher in stocks. It can be adapted to post-market, too.

Check out my other free lessons!

Source Code

// This Pine Script™ code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © ZenAndTheArtOfTrading / www.PineScriptMastery.com
// @version=5
indicator("Premarket Breakout", overlay=true)

// Get highs and lows
float previousDailyHigh = request.security(syminfo.tickerid, "D", high[barstate.isrealtime ? 1 : 0])[barstate.isrealtime ? 0 : 1]
float previousDailyLow  = request.security(syminfo.tickerid, "D", low [barstate.isrealtime ? 1 : 0])[barstate.isrealtime ? 0 : 1]

// Prepare breakout vars
bool bullishPremarketBreakout = session.isfirstbar and session.ispremarket and open > previousDailyHigh
bool bearishPremarketBreakout = session.isfirstbar and session.ispremarket and open < previousDailyLow

// Generate alerts 
alertcondition(bullishPremarketBreakout or bearishPremarketBreakout, "Premarket Breakout", "Breakout detected for {{ticker}} @ {{close}}")
alertcondition(bullishPremarketBreakout, "Bullish Premarket Breakout", "Bullish Breakout detected for {{ticker}} @ {{close}}")
alertcondition(bearishPremarketBreakout, "Bearish Premarket Breakout", "Bearish Breakout detected for {{ticker}} @ {{close}}")

// Draw htf high/lows 
plot(previousDailyHigh, color=color.red, linewidth=2)
plot(previousDailyLow, color=color.green, linewidth=2)

// Draw signals
plotshape(bullishPremarketBreakout, "Bullish Premarket Breakout", shape.triangleup, location.belowbar, color.lime, size=size.small)
plotshape(bearishPremarketBreakout, "Bearish Premarket Breakout", shape.triangledown, location.abovebar, color.red, size=size.small)