TradingView
millerrh
Aug 9, 2020 2:39 PM

Breakout Trend Follower 

Bitcoin / United States DollarCoinbase

Description

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.

Release Notes

Cleaned up code a bit, added alert option for both buy and sell.
Comments
KLtrading
Thanks for sharing, great script and so educational.

In some cases, I see that it signals “long” or “exit long” without a coincident “buy”
millerrh
@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, 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, 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, thanks again for the education. All the best.
millerrh
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 = ["Using Stops", "On Candle Close"], 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.
behzadh666
@millerrh, hi i add this but not working it's give error
behzadh666
@millerrh, can you please do it and send code for me i try to follow your way but it's give compile error i try many time thanks
millerrh
@behzadh666, Sorry for the confusion, looks like I pasted the Strategy code in the Indicator. This message should have been for this one: tradingview.com/script/9uvZIPnH-Breakout-Trend-Follower-Strategy/
behzadh666
@millerrh, no problem bro can send code for body close for Breakout Trend Follower ?
More