The inputs define the trend and the .
The trend is defined by a moving average crossover. When the short
(or fast) moving average is above the long (slow) moving average, the
trend is up. Otherwise, the trend is down. The inputs are:
long: the number of periods in the long/slow moving average.
short: the number of periods in the short/fast moving average.
The slow moving average is shown in various colors (see explanation
below. The fast moving average is a faint blue.
The has two modes, percentage and rank. The percentage
stop is given in terms of annualized . The rank stop is given
in terms of percentile.
stop_pct and stop_rank are initialized with "-1". You need to set one of
these to the values you want after adding the indicator to your chart.
This is the only setting that requires your input.
mode: choose "rank" for a rank stop, "percentage" for a percentage stop.
vol_window: the number of periods in the
calculation. e.g. "30" means the will be a weighted
average of the previous 30 periods. applies to both types of stop.
stop_pct: the limit, annualized. for example, "50" means
that the trend will not be followed when rises
stop_rank: the trend will not be followed when the is in the
N-th percentile. for example, "75" means the trend will not be
followed when the current is greater than 75%
of previous volatilities.
rank_window: the number of periods in the rank percentile calculation.
for example, if rank_window is "252" and "stop_rank" is "80", the
trend will not be followed when current is
greater than 80% of the previous 252 historical volatilities.
The outputs include moving averages, to visually identify the trend,
a table, and a performance table.
The slow moving average is colored green in an uptrend, red in a
downtrend, and black when the is in place.
The table gives the current , annualized
and expressed as a whole number percentage. E.g. "65" means the
instrument's one standard deviation annual move is 65% of its price.
The current rank is expressed, also as a whole number percentage. E.g.
"15" means the current is greater than 15% of previous
volatilities. For convenience, the volatilities corresponding to the
0, 25, 50, 75, and 100th percentiles are also shown.
The performance table shows the current strategy's performance versus
buy-and-hold . If the trend is up, the instrument's return for that
period is added to the strategy's return, because the strategy is long.
If the trend is down, the negative return is added, because the strategy
is short. If the is in (the slow moving average is
black), that period's return is excluded from the strategy returns.
Every period's return is added to the buy-and-hold returns.
The table shows the average return, the standard deviation of returns,
and the sharpe ratio (average return / standard deviation of returns).
All figures are expressed as per-period, whole number percentages.
For exmaple, "0.1" in the mean column on a means a
0.1% daily return.
The number of periods (samples) for each strategy is also shown.
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.