Moving Averages
Exponential Moving Average
MarketTechnicals.ema
— Functionema(ta::TimeArray, n, wilder = false)
ema(A::Array, n, wilder = false)
Exponemtial Moving Average
A.k.a. exponentially weighted moving average (EWMA)
Formula
Let $k$ denote the degree of weighting decrease.
If parameter wilder
is true
, $k = \frac{1}{n}$, else $k = \frac{2}{n + 1}$.
\[\text{EMA}_t = k \times P_t + (1 - k) \times \text{EMA}_{t - 1}\]
julia> ema(cl, 10, wilder=true)
491×1 TimeArray{Float64,2,Date,Array{Float64,2}} 2000-01-14 to 2001-12-31
│ │ Close_ema_10 │
├────────────┼──────────────┤
│ 2000-01-14 │ 98.782 │
│ 2000-01-18 │ 99.2978 │
│ 2000-01-19 │ 100.024 │
│ 2000-01-20 │ 101.3716 │
│ 2000-01-21 │ 102.3655 │
│ 2000-01-24 │ 102.7539 │
│ 2000-01-25 │ 103.7035 │
│ 2000-01-26 │ 104.3522 │
│ ⋮ │ ⋮ │
│ 2001-12-20 │ 21.0683 │
│ 2001-12-21 │ 21.0615 │
│ 2001-12-24 │ 21.0913 │
│ 2001-12-26 │ 21.1312 │
│ 2001-12-27 │ 21.2251 │
│ 2001-12-28 │ 21.3456 │
│ 2001-12-31 │ 21.401 │
Kaufman's Adaptive Moving Average
MarketTechnicals.kama
— Functionkama(ta::TimeArray, n = 10, fn = 2, sn = 30)
Kaufman's Adaptive Moving Average
Arguments
n
: periodfn
: the fastest EMA constantsn
: the slowest EMA constant
Formula
\[\begin{align*} \text{KAMA}_t & = \text{KAMA}_{t-1} + \text{SC} \times (\text{Price} - \text{KAMA}_{t-1}) \\ \text{SC} & = (\text{ER} \times (\frac{2}{fn + 1} - \frac{2}{sn + 1}) + \frac{2}{sn + 1})^2 \\ \text{ER} & = \frac{Change}{Volatility} \\ \text{Change} & = | \text{Price} - \text{Price}_{t-n} | \\ \text{Volatility} & = \sum_{i}^{n} | \text{Price}_i - \text{Price}_{i - 1} | \end{align*}\]
julia> kama(cl)
490×1 TimeArray{Float64,1,Date,Array{Float64,1}} 2000-01-18 to 2001-12-31
│ │ kama │
├────────────┼──────────┤
│ 2000-01-18 │ 98.9052 │
│ 2000-01-19 │ 99.0098 │
│ 2000-01-20 │ 99.4499 │
│ 2000-01-21 │ 100.3882 │
│ 2000-01-24 │ 100.5256 │
│ 2000-01-25 │ 101.1938 │
│ 2000-01-26 │ 101.9353 │
│ 2000-01-27 │ 103.181 │
│ ⋮ │ ⋮ │
│ 2001-12-20 │ 21.0972 │
│ 2001-12-21 │ 21.0904 │
│ 2001-12-24 │ 21.1019 │
│ 2001-12-26 │ 21.1061 │
│ 2001-12-27 │ 21.1247 │
│ 2001-12-28 │ 21.2088 │
│ 2001-12-31 │ 21.2584 │
Moving Average Envelope
MarketTechnicals.env
— Functionenv(ta::TimeArray, n; e = 0.1)
env(A::AbstractArray, n; e = 0.1)
Moving Average Envelope
\[\begin{align*} \text{Upper Envelope} & = \text{SMA}(n) \times (1 + e) \\ \text{Lower Envelope} & = \text{SMA}(n) \times (1 - e) \end{align*}\]
Arguments
e
: the envelope,0.1
implies the10%
envelope.
References
julia> env(cl, 5)
496×2 TimeArray{Float64,2,Date,Array{Float64,2}} 2000-01-07 to 2001-12-31
│ │ Close_env_5_low │ Close_env_5_up │
├────────────┼─────────────────┼────────────────┤
│ 2000-01-07 │ 92.3292 │ 112.8468 │
│ 2000-01-10 │ 89.775 │ 109.725 │
│ 2000-01-11 │ 88.02 │ 107.58 │
│ 2000-01-12 │ 84.9942 │ 103.8818 │
│ 2000-01-13 │ 85.3092 │ 104.2668 │
│ 2000-01-14 │ 85.4784 │ 104.4736 │
│ 2000-01-18 │ 86.5926 │ 105.8354 │
│ 2000-01-19 │ 89.0784 │ 108.8736 │
│ ⋮ │ ⋮ │ ⋮ │
│ 2001-12-20 │ 18.7758 │ 22.9482 │
│ 2001-12-21 │ 18.8856 │ 23.0824 │
│ 2001-12-24 │ 19.0188 │ 23.2452 │
│ 2001-12-26 │ 19.1052 │ 23.3508 │
│ 2001-12-27 │ 19.1862 │ 23.4498 │
│ 2001-12-28 │ 19.503 │ 23.837 │
│ 2001-12-31 │ 19.665 │ 24.035 │
Simple Moving Average
MarketTechnicals.sma
— Functionsma(ta::TimeArray, n)
sma(A::Array, n)
Simple Moving Average
Formula
\[\text{SMA} = \frac{\sum_i^n{P_i}}{n}\]
julia> sma(cl, 5)
496×1 TimeArray{Float64,2,Date,Array{Float64,2}} 2000-01-07 to 2001-12-31
│ │ Close_sma_5 │
├────────────┼─────────────┤
│ 2000-01-07 │ 102.588 │
│ 2000-01-10 │ 99.75 │
│ 2000-01-11 │ 97.8 │
│ 2000-01-12 │ 94.438 │
│ 2000-01-13 │ 94.788 │
│ 2000-01-14 │ 94.976 │
│ 2000-01-18 │ 96.214 │
│ 2000-01-19 │ 98.976 │
│ ⋮ │ ⋮ │
│ 2001-12-20 │ 20.862 │
│ 2001-12-21 │ 20.984 │
│ 2001-12-24 │ 21.132 │
│ 2001-12-26 │ 21.228 │
│ 2001-12-27 │ 21.318 │
│ 2001-12-28 │ 21.67 │
│ 2001-12-31 │ 21.85 │