Strategy · Pine Script v6 · On the chart

Traffic Light Strategy

by Pa1-Tiwari

The Traffic Light Strategy is a breakout trading system overlaid on the main price chart.

View on GitHub

GitHub stars3on the repository
Pine versionv6current
Lines of code121
Settings7inputs
LicenceMIT
Last update10 months ago5 Dec 2025

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

121 lines of Pine Script v6 from source-code/Traffic-light-strategy--shubhasis-pani.txt, published under MIT. Keep the author's credit and licence lines when you reuse it.

Traffic-light-strategy--shubhasis-pani.txt
// This Pine Script® code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/

//@version=6
strategy("Traffic light strategy" , overlay = true , process_orders_on_close = true ,initial_capital = 1000000, margin_short = 0 ,margin_long = 0,default_qty_type = strategy.cash, default_qty_value = 1000000 )

tString = input.string("15:00", "Intraday exit time")
parts = str.split(tString, ":")
iHour = str.tonumber(parts.get(0))
iMinute = str.tonumber(parts.get(1))

// Current bar time
h = hour(time)
m = minute(time)


// Condition
intraday_con = (h <= iHour and ((m < iMinute and h >= iHour) or h < iHour ))
intraday_bool = input.bool(false , 'Intraday')
show_trades = input.bool(true , 'Show trades')
show_tp_sl  = input.bool(true , 'Show Target/SL')

tp_sl_type = input.string('Percent' , 'Target/SL type' , ['Percent' , 'Point'] , group = 'Target and SL settings')
input_tgt  = input.float(1 , 'Taget value' , group = 'Target and SL settings')
input_sl   = input.float(0.5 , 'Stoploss value' , group = 'Target and SL settings')

var float lvl1 = na 
var float lvl2 = na 
var float tgt = na 
var float sl  = na
var float ent = na
var draw_lvl   = false
var pos = 0

if (h == iHour and m == iMinute) and intraday_bool
    strategy.cancel_all()
    strategy.close_all()

signal = ((close > open and close[1] < open[1]) or (close < open and close[1] > open[1])) and (time-time[1])/1000 == timeframe.in_seconds()

if signal and pos == 0 and na(lvl1) and (intraday_con or not intraday_bool)
    lvl1 := math.max(high , high[1])
    lvl2 := math.min(low , low[1])
    strategy.entry('long' , strategy.long , stop = lvl1,limit = lvl1 ,oca_name = 'limit' , oca_type = strategy.oca.cancel)
    strategy.entry('Short' , strategy.short , stop = lvl2,limit = lvl2,oca_name = 'limit' , oca_type = strategy.oca.cancel)
    strategy.exit('long exit' , 'long' , limit =  (lvl1 + (tp_sl_type == 'Percent' ? (input_tgt/100)*close : input_tgt)) , stop =  (lvl1 - (tp_sl_type == 'Percent' ? (input_sl/100)*close : input_sl)))
    strategy.exit('short exit' , 'Short' , limit =  (lvl2 - (tp_sl_type == 'Percent' ? (input_tgt/100)*close : input_tgt)) , stop = (lvl2 + (tp_sl_type == 'Percent' ? (input_sl/100)*close : input_sl)))
    draw_lvl := true

buy = high > lvl1 and pos == 0 and (intraday_con or not intraday_bool)
sell = low < lvl2 and pos == 0 and (intraday_con or not intraday_bool)

bgcolor((draw_lvl and not draw_lvl[1]) or(draw_lvl[1] and not draw_lvl[2])  ? color.new(color.orange,80) : color.new(color.orange,100),1)

if buy and sell
    if open-low < high-open
        buy := false
    else
        sell := false
// buy
if buy
    pos := 1
    draw_lvl := false
    tgt := lvl1 + (tp_sl_type == 'Percent' ? (input_tgt/100)*close : input_tgt)
    sl  := lvl1 - (tp_sl_type == 'Percent' ? (input_sl/100)*close : input_sl)
    ent := lvl1
    lvl1 := na
    lvl2 := na

//sell
if sell
    pos := -1
    draw_lvl := false
    tgt := lvl2 - (tp_sl_type == 'Percent' ? (input_tgt/100)*close : input_tgt)
    sl  := lvl2 + (tp_sl_type == 'Percent' ? (input_sl/100)*close : input_sl)
    ent := lvl2
    lvl1 := na
    lvl2 := na


// exit long
if pos == 1 and (high >= tgt or low <= sl)
    pos := 0
    tgt := na 
    sl := na
    ent := na
    lvl1 := na
    lvl2 := na

// exit short
if pos == -1 and (high >= sl or low <= tgt)
    pos := 0
    tgt := na 
    sl := na
    ent := na
    lvl1 := na
    lvl2 := na

// intraday exit   
if (h == iHour and m == iMinute) and intraday_bool
    pos := 0
    lvl1 := na 
    lvl2 := na
    tgt := na 
    sl := na
    ent := na
    strategy.cancel_all()
    strategy.close_all()

// plots
plot(lvl1[1], 'entry level' , draw_lvl ? color.gray : na , style = plot.style_linebr)
plot(lvl2[1], 'entry level' , draw_lvl ? color.gray : na , style = plot.style_linebr)
tgt_line = plot(tgt[1], 'Target level' , pos != 0 and show_tp_sl? color.teal : na , style = plot.style_linebr)
sl_line = plot(sl[1], 'SL level' , pos != 0 and show_tp_sl ? color.maroon : na , style = plot.style_linebr)
ent_line = plot(ent[1], 'entry level' , pos != 0 and show_tp_sl ? color.maroon : na , style = plot.style_linebr)
fill(ent_line , tgt_line ,pos != 0 and show_tp_sl ? color.new(color.teal,80) : na , 'target fill')
fill(ent_line , sl_line ,pos != 0  and show_tp_sl? color.new(color.maroon,80) : na , 'Stoploss fill')

plotshape(show_trades ?  buy : false , 'long' , shape.triangleup , location.belowbar , color.teal , text = 'Long' , textcolor= color.teal )
plotshape(show_trades ?  sell : false , 'short', shape.triangledown , location.abovebar , color.maroon , text = 'Short' , textcolor= color.maroon )
plotshape(show_trades ?  pos[1] == 1 and pos == 0 : false , 'exit', shape.triangledown , location.abovebar , color.gray , text = 'Exit' , textcolor= color.gray )
plotshape(show_trades ?  pos[1] == -1 and pos == 0 : false , 'exit', shape.triangleup , location.belowbar , color.gray , text = 'Exit' , textcolor= color.gray )

Overview

Traffic Light Strategy detects momentum transitions whenever two consecutive candles have opposing colors (bullish followed by bearish, or vice versa). Upon detection, it marks the combined high and low range of those two candles as potential breakout levels. When price crosses either level, the strategy triggers a long or short entry while canceling the opposite pending order. Trades exit using percentage-based or point-based profit targets and stop losses, or at a user-defined intraday time.

Details

Published by

Pa1-Tiwari on GitHub

Type

Strategy, Pine Script v6

Draws

On the price chart

Category

Breakout, Candlestick patterns

Markets

Any symbol

Timeframes

M15-H1

Risk Notes

No repainting, lookahead or stacked entries were found in its code or README. That says nothing about how well it works: test it first.

Settings

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

SettingTypeDefaultVariable
Intraday exit timestring15:00tString
Intradayboolfalseintraday_bool
Show tradesbooltrueshow_trades
Show Target/SLbooltrueshow_tp_sl
Target/SL typestringPercenttp_sl_type
Taget valuefloat1input_tgt
Stoploss valuefloat0.5input_sl
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

BarUpDn Long Strategy

by hasnocool
106 stars

BarUpDn Long Strategy executes long trades based on price bar directional movements and previous bar closes. It places a long stop order above the current high when a bullish candle opens above a prior close. Because short entries are restricted to exiting lon

Strategy v4 Candlestick patternsBreakout
Updated 2 years ago · GPL-3.0 GitHub repository of BarUpDn Long Strategy

OutSide Bar Strategy

by TWODS-CAPITAL
37 stars

The OutSide Bar Strategy identifies outside bar candlestick patterns directly on the main price chart. It triggers a long entry when a bar's high exceeds the previous high and its low drops below the previous low, provided the bar closes higher than its open.

Strategy v5 Candlestick patternsBreakout
Updated 3 years ago · MIT GitHub repository of OutSide Bar Strategy
3 stars

This strategy plots Bollinger Bands on the price chart and enters long only when flat. It buys on a strong bullish close above the middle band or on a small-body pullback that tags the middle band with a long lower wick. The stop sits below the signal bar by a

Strategy v5 Volatility & bandsBreakout
Updated yesterday · None stated GitHub repository of H4 布林ATR型態多單策略
3 stars

This strategy detects Opening Range Breakouts in a specified session. It calculates the high and low of the opening range bar then plots these levels on the price chart and fills the area between them. Candles are colored based on their relation to the ORB hig

Strategy v5 BreakoutSupport & resistance
Updated 5 months ago · None stated GitHub repository of ORB Hooplah Strategy

Inside Bar Breakout

by Basant1Saini
1 stars

Inside Bar Breakout identifies standard and double inside bar candlestick patterns to trade breakout momentum. It sets trigger levels using reference bar extremes plus an adjustable percentage buffer. Once a pattern forms, the strategy waits up to five bars fo

Strategy v5 Candlestick patternsBreakout
Updated 7 months ago · None stated GitHub repository of Inside Bar Breakout

More strategys

Trade Your Charts With a Regulated Broker

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

Compare brokers