This script compares trend trading with a to "buy and hold".
Trades are taken with the trend, except when price exceeds a
forecast. The trend is defined by a moving average crossover. The forecast
is based on projecting future from .
The trend is defined by two parameters:
- long: the length of a long ("slow") moving average.
- short: the length of a short ("fast") moving average.
The trend is up when the short moving average is above the long. Otherwise
it is down.
The is defined by three parameters:
- window: determines the number of periods in the historical
calculation. More periods means a slower (smoother)
estimate of .
- stop forecast periods: the number of periods in the
forecast. For example, "7" on a means that the
will be forecasted with a one week lag.
- stop forecast stdev: the number of standard deviations in the stop
forecast. For example, "2" means two standard deviations.
The default parameters are:
- long: 50
- short: 20
- window: 30
- stop forecast periods: 7
- stop forecast standard deviations: 1
The trend will be up when the 20 period moving average is above the 50
period moving average. On each bar, the will be
calculated from the previous 30 bars. If the is 0.65
(65%), then a forecast will be drawn as a fuchsia line, subtracting
0.65 * sqrt(7 / 365) from the closing price. If price at any point falls
below the forecast, the is in place, and the trend is
- The trend is shown by painting the slow moving average green (up), red
(down), or black (none; ).
- The fast moving average is shown in faint blue
- The previous forecasts are shown in faint fuchsia
- The current forecast is shown as a fuchsia line, projecting
into the future as far as it is valid.
- The current is given in the top right corner, as a
whole number percentage.
- The performance table shows the mean, standard deviation, and sharpe
ratio of the trend strategy, as well as buy and hold.
If the trend is up, each period's return is added to the sample (the
strategy is long). If the trend is down, the inverse of each period's
return is added to the sample (the strategy is short). If there is no
trend (the is active), the period's return is excluded
from the sample. Every period is added to the buy-and-hold strategy's
sample. The total number of periods in each sample is also shown.
- changed the forecast plot style to be more pleasing
Nello spirito di condivisione promosso da TradingView, l'autore (al quale vanno i nostri ringraziamenti) ha deciso di pubblicare questo script in modalità open-source, così che chiunque possa comprenderlo e testarlo. Puoi utilizzarlo gratuitamente, ma il riutilizzo del codice è subordinato al rispetto del Regolamento. Per aggiungerlo al grafico, mettilo tra i preferiti.