Moving Averages
Simple Moving Average
MarketTechnicals.sma
— Function.sma(arr, n)
Simple Moving Average
\[SMA = \frac{\sum_i^n{P_i}}{n}\]
julia> using MarketData
julia> using MarketTechnicals
julia> sma(cl, 5)
496x1 TimeSeries.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 │
│ 2000-01-20 │ 104.238 │
⋮
│ 2001-12-19 │ 20.928 │
│ 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 │
Exponential Moving Average
MarketTechnicals.ema
— Function.ema(arr, n, wilder=false)
Exponemtial Moving Average
A.k.a. exponentially weighted moving average (EWMA)
\[ \text{Let } k \text{denote the degree of weighting decrease}\]
If parameter wilder
is true
, $k = \frac{1}{n}$, else $k = \frac{2}{n + 1}$.
\[ EMA_t = k \times P_t + (1 - k) \times EMA_{t - 1}\]
julia> using MarketData
julia> using MarketTechnicals
julia> ema(cl, 10, wilder=true)
491x1 TimeSeries.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 │
│ 2000-01-27 │ 104.917 │
⋮
│ 2001-12-19 │ 21.1125 │
│ 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
— Function.Kaufman's Adaptive Moving Average
Arguments:
n
: periodfn
: the fastest EMA constantsn
: the slowest EMA constant
Formula:
\[ \begin{align*}
KAMA_t & = KAMA_{t-1} + SC \times (Price - KAMA_{t-1}) \\
SC & =
(ER \times (\frac{2}{fn + 1} - \frac{2}{sn + 1}) + \frac{2}{sn + 1})^2 \\
ER & = \frac{Change}{Volatility} \\
Change & = | Price - Price_{t-n} | \\
Volatility & = \sum_{i}^{n} | Price_i - Price_{i-1} |
\end{align*}\]
julia> using MarketData
julia> using MarketTechnicals
julia> kama(cl)
490x1 TimeSeries.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 │
│ 2000-01-28 │ 103.1519 │
⋮
│ 2001-12-19 │ 21.1539 │
│ 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
— Function.env(arr, n; e = 0.1)
Moving Average Envelope
\[ \begin{align*}
\text{Upper Envelope} & = \text{n period SMA } \times (1 + e) \\
\text{Lower Envelope} & = \text{n period SMA } \times (1 - e)
\end{align*}\]
Arguments
e
: the envelope,0.1
implies the10%
envelope.
Reference
julia> using MarketData
julia> using MarketTechnicals
julia> env(cl, 5)
496x2 TimeSeries.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 │
│ 2000-01-20 │ 93.8142 │ 114.6618 │
⋮
│ 2001-12-19 │ 18.8352 │ 23.0208 │
│ 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 │