[AutoView] Trailing Stop Back Testing and alerts + TP and TS

7077 15 413
The number one request since the creation of autoview was to have alerts triggered for Take Profit, Stop Loss and Trailing Stops. Finally, we've figured it out, and as an additional bonus, this allows us to back test trailing stops in the strategy tester.

YouTube Video about this script and setup:

This script contains 2 things people have been asking for, multiple time frames and of course, a trailing stop that can be both back tested and setup for alerts.

This strategy itself seems to perform better without the use of the Trailing Stop, so play around and monitor it before actually trying to trade it live.
View all the trades being tracked in a Google             Sheets here:

To learn more about back testing strategies, automation and AutoView join our free slack group via

Referral Tax:
This requires 4 alerts, so you are going to have to upgrade to PRO. It'd be greatly appreciated if you signed up using our referral link.


Remove from Favorite Scripts Add to Favorite Scripts
strategy("Public TS - FX", shorttitle="Strategy", overlay=false, default_qty_value=100)

ts = input(5, "Trailing Stop") / 10000

//Heiken Ashi Candles
Factor = 3
Pd = 7
isHA = input(true, "HA Candles", bool)

data = isHA ? heikenashi(tickerid) : tickerid

r1 = input("3", "Resolution", resolution)
r2 = input("5", "Resolution", resolution)
r3 = input("15", "Resolution", resolution)
r4 = input("30", "Resolution", resolution)

o1 = security(data, r1, open[1])
c1= security(data, r1, close[1])
o2 = security(data, r2, open[1])
c2 = security(data, r2, close[1])
o3 = security(data, r3, open[1])
c3 = security(data, r3, close[1])
o4 = security(data, r4, open[1])
c4 = security(data, r4, close[1])

long = o4 < c4 and o3 < c3 and o2 < c2 and o1 < c1
short = o4 > c4 and o3 > c3 and o2 > c2 and o1 > c1

last_long = long ? time : nz(last_long[1])
last_short = short ? time : nz(last_short[1])

in_long = last_long > last_short
in_short = last_short > last_long

long_signal = crossover(last_long, last_short)
short_signal = crossover(last_short, last_long)

last_open_long = long ? open : nz(last_open_long[1])
last_open_short = short ? open : nz(last_open_short[1])

last_high = not in_long ? na : in_long and (na(last_high[1]) or high > nz(last_high[1])) ? high : nz(last_high[1])
last_low = not in_short ? na : in_short and (na(last_low[1]) or low < nz(last_low[1])) ? low : nz(last_low[1])

long_ts = not na(last_high) and close <= (last_high - ts)
short_ts = not na(last_low) and close >= (last_low + ts)

strategy.entry("Long", strategy.long, when=long_signal)
strategy.entry("Short", strategy.short, when=short_signal)

strategy.close("Long", when=long_ts)
strategy.close("Short", when=short_ts)

price  = close[1]
noleverage = price / 100
leverage = 10 // noleverage * 4

TP = input(0) * leverage
SL = input(42, maxval=200) * leverage
TS = input(0) * leverage
CQ = 100

TPP = (TP > 0) ? TP : na
SLP = (SL > 0) ? SL : na
TSP = (TS > 0) ? TS : na

strategy.exit("Close Long", "Long", qty_percent=CQ, profit=TPP, loss=SLP, trail_points=TSP)
strategy.exit("Close Short", "Short", qty_percent=CQ, profit=TPP, loss=SLP, trail_points=TSP)
Thanks @CryptoRox - When backtesting I am getting strange issues where TradingView miscalculates the trades.. sometimes it thinks a 2% gain is a 130% gain or something. It seems totally random showing even 1000% sometimes. When I use the TradingView Strategy TrailingStop results are amazing, but as soon as I use the Trailing Stop Implementation like you have here, the backtesting results go completely wrong. The stops are displaying on screen properly and plotting fine, but just the backtesting is full of bugs. Have you ever seen this issue before?
Your slack group domain is going to a parking site. You guys still active?
I don't see how to create alerts based on the Take Profit / Stop Loss. I watched the video but you didn't show how the alerts were created.
Hi CryptoRox. Love your work, also with Autoview and PINK!

I can't get this script to work properly. Sometimes the exit point are way off (more than a few candles too late). Also I can't get this script to work with crypto's. Any tips? Am I doing something wrong?
+3 Reply
Objredline romangeniuz
@romangeniuz, I'm in the same boat, the strategy.exit stop, loss, profit, trailing parameters doesn't seem to work properly with crypto. I've got stops that are hitting when there's a profit. If anyone knows of this issue, please confirm or let me know what I'm doing wrong
This strategy rely on future data (repaints) and can not be used in real life. Please vote up for fixing this issue:
CryptoRox zelibobla
This strategy does not repaint... Please learn the language and things that cause repainting before you go around spamming charts.

It relies on higher time frame candles yes, but it uses the LAST candle, therefore not relying on future data.
+2 Reply
CryptoRox CryptoRox
Don't get me wrong, I appreciate the attention you're bringing to this issue. But you really should understand the issue AND the work around hacks before posting on other peoples published charts.
zelibobla CryptoRox
Heikin Ashi you used in your example are the source of repaint in your case.

Here is the description: «yes its Heikin Ashi Close price which is calculatet by OHLC/4 of current bar. TradingView recalculate this price at close of the 180mn Heikin Ashi bar. Thats why its repainting».

Please pinpoint what language and thing should I learn more.
CryptoRox zelibobla
In line 18 - 25, when I call the Heiken Ashi candle, I call the previous candle that has already completed.

I am going to load this chart and run it while I'm at work, as it's been awhile since I've written anything with HA. But I'm pretty sure that this uses the previous HA candle avoiding all repainting.
EN English
EN English (UK)
EN English (IN)
DE Deutsch
FR Français
ES Español
IT Italiano
PL Polski
TR Türkçe
RU Русский
PT Português
ID Bahasa Indonesia
MS Bahasa Melayu
TH ภาษาไทย
VI Tiếng Việt
JA 日本語
KO 한국어
ZH 简体中文
ZH 繁體中文
AR العربية
Home Stock Screener Forex Signal Finder Cryptocurrency Signal Finder Economic Calendar How It Works Chart Features House Rules Moderators Website & Broker Solutions Widgets Stock Charting Library Feature Request Blog & News FAQ Help & Wiki Twitter
Profile Profile Settings Account and Billing My Support Tickets Contact Support Ideas Published Followers Following Private Messages Chat Sign Out