Breakout Trend Follower

This is a Study mirroring the Breakout Trend Follower Strategy I made. I use this one during live trading and the other for backtesting. It will also give alerts when buy and sell signals are hit.
Open-source script

In true TradingView spirit, the author of this script has published it open-source, so traders can understand and verify it. Cheers to the author! You may use it for free, but reuse of this code in a publication is governed by House Rules. You can favorite it to use it on a chart.

Want to use this script on a chart?


Thanks for sharing, great script and so educational.

In some cases, I see that it signals “long” or “exit long” without a coincident “buy”
350 coins
millerrh KLtrading
@KLtrading, Thanks! So the "buy" and "sell" labels are actually meant to be for the Study whereas the Strategy has the default "long" and "exit long" labels built in. But I have recently noticed the same thing myself going through some live trading. What is happening is that the Strategy is taking a long position on ANY previous high above the SMA filter that gets broken, but the study version (that says "buy" and "sell") is only looking at the most recent swing high. If the most recent swing high is below the SMA filter, but the one before that was above the filter, the study logic (that says "buy" and "sell") won't show a buy signal, but the strategy will indicate a long when that previous high is breached. So in that sense the study and strategy are slightly different. I didn't even realize this was happening until recently, so I might see if I can somehow adjust it so they have better parity between them.
KLtrading millerrh
@millerrh, thanks for the prompt response. I think I understand. To clarify, the “breakout trend follower strategy” is the one to trade with using the long/exit long indicators?

I applaud inclusion of the option to filter out buy signals with the configurable MA. Again, great work.
millerrh KLtrading
@KLtrading, In TradingView, they have two things:
1) Studies - these are indicators of anything you want them to be.
2) Strategies - these are scripts that have buy/sell rules that you can use to backtest trading ideas. The strategies have built-in logic that gives you those "long" and "exit long" labels by default (just part of how Strategies work).

What I did was built a Study I can use for live trading based on the Strategy I made. Studies give you the ability to set alerts (can't do that with a Strategy) and you can make them look a bit cleaner, so I typically like to have Studies on my charts during day to day use and only use Strategies when backtesting something. So for the Study, I had to try to mimic the buy/sell rules that are just built into the Strategy by default. Looks like this is one area that my Study it didn't quite match up with the Strategy's logic.

So essentially I have two scripts that I published - one for use during live trading and that you can add alerts to if you want (the study) and one you can use to backtest and figure out what parameters you like (the strategy). But they are both essentially the same thing.
KLtrading millerrh
@millerrh, thanks again for the education. All the best.
For those interested in testing this out using the close of a candle instead of executing immediately when the price crosses the swing high/low as a stop order, I just coded a version up to do this and the results are horrible. If you want to experiment, feel free to save a copy and do yourself, but I am not going to add this to the script and clutter it up.

Add this under Inputs:
tradeType = input(defval="Using Stops", options = , title = "Execute Trades")
That puts a selector the user can select for whether you want to use stops to execute trades or wait for the candle to close.

Then change the entry/exit criteria from what was there to this:
strategy.entry("StopLong", strategy.long, stop = buyLevel2, when = time > Start and time < Finish and buyLevel2 > maFilterCheck and tradeType == "Using Stops")
strategy.exit("Exit StopLong", from_entry = "StopLong", stop=stopLevel2)
strategy.entry("CloseLong", strategy.long, when = close > buyLevel2 and time > Start and time < Finish and buyLevel2 > maFilterCheck and tradeType == "On Candle Close")
strategy.close("CloseLong", when = close < stopLevel2)

As an example of how horrible this is, on BTC 2-hr chart from 2019, using close values gives you a 134% return but using stops gets you a 525% return.
+1 Reply
Hi this script is wonderful! GOOD WORK!

Just I'm thinking if there's a stock that I wanted to buy but already missed out the 1st Breakout buy.
The next breakout seems won't be triggered as it haven't complete a sell.
Any ways that I can code it as such every breakout I can get alerted?

Thanks in advance!
millerrh Ant886688
@Ant886688, Not sure you'd want to get alerted to EVERY breakout - the ideal entry is getting the breakout from consolidation and riding the trend. If you miss that one (as I sometimes do), I'll put my stop order above the next high and a manual alert there.

Actually, that reminds me...this strategy doesn't work well using alerts at all. Breakouts can happen rather violently and by the time you are alerted you've missed the ideal entry spot. The way I actually play this strategy is to use stop orders at these breakout levels and let them sit there. I then use a horizontal line with an alert attached to it that I drag around the screen based on where my stop is. When the alert goes off, I know my stop-buy order was hit, I then submit a stop-loss order, and move that line down to where my stop loss is. So the line is always where my orders are placed (since that is manually). If i notice my line isn't where a new swing high/swing low is according to the strategy, that lets me know to go into my brokerage or crypto account and update my stop levels, then I drag the line to my new stop levels. So my alerts are manual (by dragging an alert line around) but I know they are always in sync with the orders on any exchanges.
+1 Reply
HI Apologies but do the signals appear dynamically?