Voici le code EMA standard sur 20 périodes ; il utilise une SMA pour les 20 premières périodes, puis applique la formule. Ainsi, sur 500 ou plus de 5 000 périodes, le calcul reste identique.
Probuilder précharge toujours 500 barres.
Par conséquent, pour l’EMA, il n’utilise que les 20 dernières barres (fenêtre glissante étroite), conformément à la formule.
// EMA - Exponential Moving Average
//
//https://school.stockcharts.com/doku.php?id=technical_indicators:moving_averages
//https://sciencing.com/calculate-exponential-moving-averages-8221813.html
//https://corporatefinanceinstitute.com/resources/knowledge/trading-investing/exponential-moving-average-ema/
//https://www.investopedia.com/terms/e/ema.asp
//
//Periods = 20
ONCE Periods = max(1,min(999,Periods))
ONCE Multip = 2 / (Periods + 1) //weighting multiplier or smoothing constant
src = CustomClose
IF BarIndex > (Periods + 1) THEN
Ema = (close - Ema[1]) * Multip + Ema[1]
ELSE
Ema = average[Periods,0](src) //First EMA is the SMA of the same periods
ENDIF
return Ema AS "Ema"
Voici la formule RSI :
// (https://en.wikipedia.org/wiki/Relative_strength_index)
//
// Calculation
//
// For each trading period an upward change U or downward change D is calculated. Up periods are characterized by the close being higher than the previous close:
//
// U = close now − close previous {\displaystyle U={\text{close}}_{\text{now}}-{\text{close}}_{\text{previous}}}
// U = {\text{close}}_{{\text{now}}}-{\text{close}}_{{\text{previous}}}
//
// D = 0 {\displaystyle D=0} D=0
//
// Conversely, a down period is characterized by the close being lower than the previous period’s close (note that D is nonetheless a positive number),
//
// U = 0 {\displaystyle U=0} U=0
// D = close previous − close now {\displaystyle D={\text{close}}_{\text{previous}}-{\text{close}}_{\text{now}}}
//
// D = 0 {\text{close}}_{{\text{previous}}}-{\text{close}}_{{\text{now}}}
//
// If the last close is the same as the previous, both U and D are zero. The average U and D are calculated using an n-period smoothed or modified moving average // (SMMA or MMA) which is an exponentially smoothed Moving Average with α = 1/period. Some commercial packages, like AIQ, use a standard exponential moving
// average (EMA) as the average instead of Wilder’s SMMA.
//
// Wilder originally formulated the calculation of the moving average as: newval = (prevval * (period – 1) + newdata) / period. This is fully equivalent to the
// aforementioned exponential smoothing. New data is simply divided by period which is equal to the alpha calculated value of 1/period. Previous average values
// are modified by (period -1)/period which in effect is period/period – 1/period and finally 1 – 1/period which is 1 – alpha.
//
// The ratio of these averages is the relative strength or relative strength factor:
//
// RS = SMMA(U,n)/SMMA(D,n) {\displaystyle RS={\frac {{\text{SMMA}}(U,n)}{{\text{SMMA}}(D,n)}}} RS={\frac {{\text{SMMA}}(U,n)}{{\text{SMMA}}(D,n)}}
//
// If the average of D values is zero, then according to the equation, the RS value will approach infinity, so that the resulting RSI, as computed below, will
// approach 100.
//
//The relative strength factor is then converted to a relative strength index between 0 and 100:[1]
//
// RSI = 100 − (100 / (1 + RS)) {\displaystyle RSI=100-{100 \over {1+RS}}} RSI=100-{100 \over {1+RS}}
//
// The smoothed moving averages should be appropriately initialized with a simple moving average using the first n values in the price series.
//
// Interpretation
//
// Basic configuration: Relative strength index 14-period
//
// The RSI is presented on a graph above or below the price chart. The indicator has an upper line, typically at 70, a lower line at 30, and a dashed mid-line
// at 50. Wilder recommended a smoothing period of 14 (see exponential smoothing, i.e. α = 1/14 or N = 14).
// Stochastic RSI (prt)
//
// https://www.prorealcode.com/topic/errore-calcolo-rsi/#post-114040
//
Periods = 14 //14
Ob = 70 //70 - 30
Ob = max(0,min(100,Ob)) //0 - 100
Os = 100 - Ob
Periods = max(2,min(999,Periods)) //2 - 999
MyRsi = RSI[Periods](close)
MinRSI = lowest[Periods](MyRsi)
MaxRSI = highest[Periods](MyRsi)
StochRSI = (MyRsi-MinRSI) / (MaxRSI-MinRSI) * 100
RETURN StochRSI AS "Stochastic RSI",Ob AS "Ob",Os AS "Os"
Pour un nombre donné de barres, une EMA a besoin d’environ deux fois plus de barres que de périodes.