Introduction
Highcharter is a R wrapper for Highcharts javascript library and its modules. Highcharts is a mature and flexible javascript charting library and it has a powerful API[http://www.highcharts.com/demo]. I wanted to show off the power of highcharter that is has with stocks and cryptocurrencies. Along with highcharter I used the quantmod and TTR package to create some powerful and interactive technical charts. First load in the libraries used to create the charts. Investopedia is a great resource where I found many of the definitions and formulas [https://www.investopedia.com]
Amazon with SMA and RSI over past 2 years
The base highchart graph is a candlestick chart with Amazon as the example from the past 3 years. Each candlestick represents all four important pieces of information for that day: open and close in the thick body; high and low in the “candle wick”. A Simple moving average of 50 and 200 days are added as well as Relative Strength Index(RSI).
AMZN <- getSymbols("AMZN", from = Sys.Date() - lubridate::years(3), auto.assign = FALSE)
AMZN.SMA.50 <- SMA(Cl(AMZN), n = 25)
AMZN.SMA.200 <- SMA(Cl(AMZN), n = 100)
AMZN.RSI.14 <- RSI(Cl(AMZN))
AMZN.RSI.SellLevel <- xts(rep(70, NROW(AMZN)), index(AMZN))
AMZN.RSI.BuyLevel <- xts(rep(30, NROW(AMZN)), index(AMZN))
highchart(type = "stock") %>%
# create axis for AMZN
hc_yAxis_multiples(
create_yaxis(3, height = c(2, 1, 1), turnopposite = TRUE)) %>%
# add the moving average, and RSI
hc_add_series(AMZN, yAxis = 0, name = "AMZN") %>%
hc_add_series(AMZN.SMA.50, yAxis = 0, name = "MA 50", color='purple') %>%
hc_add_series(AMZN.SMA.200, yAxis = 0, name = "MA 200",color='red') %>%
hc_add_series(AMZN$AMZN.Volume, color = "gray", yAxis = 1, name = "Volume", type = "column") %>%
hc_add_series(AMZN.RSI.14, yAxis = 2, name = "RSI", color = hex_to_rgba("blue", 0.7)) %>%
hc_add_series(AMZN.RSI.SellLevel, color = hex_to_rgba("red", 0.7),
yAxis = 2, name = "Sell level") %>%
hc_add_series(AMZN.RSI.BuyLevel, color = hex_to_rgba("green", 0.7),
yAxis = 2, name = "Buy level")
Formulas
A simple moving average (SMA) is an arithmetic moving average calculated by adding recent closing prices and then dividing that by the number of time periods in the calculation average.
\(SMA= \frac{A1+A2+...An}{n}\)
where:
\(An=\text{the price of an asset at period n}\) \(n=\text{the number of total periods}\)
The relative strength index (RSI) is a momentum indicator that measures the magnitude of recent price changes to evaluate overbought or oversold conditions in the price of a stock or other asset.Signals are considered overbought when the indicator is above 70% and oversold when the indicator is below 30%.
\(RSI\)step one = \(100−[\frac{100}{1+\frac{\text{Average gain}}{\text{Average loss}}}]\)
\(RSI\)step two = \(100−[\frac{100}{1+\frac{\text{Previous average gain} *13+\text{Current gain}}{\text{Average average loss}+\text{Current loss}}}]\)
Analysis
Using the SMA and RSI, look for SMA crossovers when the shorter term 50 crosses the 200 or vice versa such as September 2018 and recently December 2019. When the shorter term crosses the longer term that is a buy like December. When the longer term crosses the shorter that is a sell like September. The RSI at these times also hit the buy and sell levels.
Ethereum with EMA, TRIX, and CCI over past year
Ethereum the second largest cryptocurrency by marketcap can also be analzyed by looking at the currency pair to see the relative price. For this example USD will be used but many other world currincies can be used. Since ETH is more volatie asset exponetial moving average. Triple Exponetial Average(TRIX) and C
ETH <- getSymbols("ETH-USD", from = Sys.Date() - lubridate::years(1), auto.assign = FALSE, )
ETH.EMA.20 <- EMA(Cl(ETH), n = 10)
ETH.EMA.50 <- EMA(Cl(ETH), n = 25)
ETH.RSI.14 <- TRIX(Cl(ETH))
ETH.TRIX.ZeroLevel <- xts(rep(0, NROW(ETH)), index(ETH))
ETH.CCI <-CCI(Cl(ETH))
ETH.CCI.SellLevel <- xts(rep(100, NROW(ETH)), index(ETH))
ETH.CCI.BuyLevel <- xts(rep(-100, NROW(ETH)), index(ETH))
highchart(type = "stock") %>%
# create axis for ETH-USD and set theme
hc_add_theme(hc_theme_db())%>%
hc_yAxis_multiples(
create_yaxis(4, height = c(3, 1, 2,3), turnopposite = TRUE)) %>%
# add the moving averages, TRIX, and CCI
hc_add_series(ETH, yAxis = 0, name = "ETH",color='red') %>%
hc_add_series(ETH.EMA.20, yAxis = 0, name = "EMA 20", color = hex_to_rgba("blue", 0.5)) %>%
hc_add_series(ETH.EMA.50, yAxis = 0, name = "EMA 50", color = hex_to_rgba("green", 0.5)) %>%
hc_add_series(ETH$`ETH-USD.Volume`, color = "gray", yAxis = 1, name = "Volume", type = "column") %>%
hc_add_series(ETH.RSI.14, yAxis = 2, name = "TRIX", color = hex_to_rgba("purple", 0.7)) %>%
hc_add_series(ETH.CCI, yAxis = 3, name = "CCI", color = hex_to_rgba("blue", 0.7)) %>%
hc_add_series(ETH.TRIX.ZeroLevel, color = hex_to_rgba("blue", 0.7),
yAxis = 2, name = "ZERO level") %>%
hc_add_series(ETH.CCI.SellLevel, color = hex_to_rgba("red", 0.7),
yAxis = 3, name = "Sell level") %>%
hc_add_series(ETH.CCI.BuyLevel, color = hex_to_rgba("green", 0.7),
yAxis = 3, name = "Buy level")
Formulas
An exponential moving average (EMA) is a type of moving average (MA) that places a greater weight and significance on the most recent data points.
\(EMA Today =(\text{Value Today} ∗( 1+\frac{Days}{Smoothing} )) + \text{EMA Yesterday}∗(1−(1+\frac{Days}{Smoothing}))\)
where:
\(EMA = \text{Exponential moving average}\)
The Triple Exponential Average (TRIX) is a momentum indicator used by technical traders that shows the percentage change in a triple exponentially smoothed moving average.TRIX oscillates around a zero line. When it is used as an oscillator, a positive value indicates an overbought market while a negative value indicates an oversold market.
\(EMA1(i)=EMA(Price,N,1)\)
where:
\(Price(i)= \text{Current price}\)
\(EMA1(i)= \text{The current value of the Exponential Moving Average}\)
\(EMA2(i)=EMA(EMA1,N,i)\)
\(EMA2(i)=EMA(EMA1,N,i)\)
\(TRIX(i)= \frac{EMA3(i−1)}{(EMA3(i)−EMA3(i−1))}\)
Commodity Channel Index (CCI) is a momentum-based oscillator used to help determine when an investment vehicle is reaching a condition of being overbought or oversold.Going from negative or near-zero readings to +100 can be used as a signal to watch for an emerging uptrend.Going from positive or near-zero readings to -100 may indicate an emerging downtrend.
\(CCI\)= \(\frac{\text{Typical Price−MA}} {.015 * \text{Mean Deviation}}\)
where:
\(Typical Price =\sum_{i=1}^{P} ((High+Low+Close)÷3)\)
\(P=\text{Number of periods}\)
\(MA=\text{Moving Average}\)
\(Moving Average=\sum_{i=1}^{P} \text{Typical Price})÷P\)
\(Mean Deviation=\sum_{i=1}^{P}∣\text{Typical Price−MA}∣)÷P\)
Analysis
Using TRIX and CCI around mid June 2019 there is a sell signal with both TRIX and CCI trending down. Around Mid January there is a buy signal with both TRIX and CCI trending up. Following both those signals would of lead to profit in the short term.
Two symbols on one chart with VWAP over past 2 years
For this chart it demostrates the power of highcharts. There are two symbols ploted one is the S&P 500(SPY) an index next to Apple (AAPL). SPY is the candlestick chart that has been used and Apple an open-high-low-close chart (OHLC) Tick marks project from each side of the line indicating the opening price (e.g., for a daily bar chart this would be the starting price for that day) on the left, and the closing price for that time period on the right.
SPY <- getSymbols("SPY", from = Sys.Date() - lubridate::years(2), auto.assign = FALSE)
AAPL <- getSymbols("AAPL", from = Sys.Date() - lubridate::years(2), auto.assign = FALSE)
SPY.SMA.10 <- SMA(Cl(SPY), n = 5)
SPY.SMA.200 <- SMA(Cl(SPY), n = 100)
SPY.VWAP<- VWAP(Cl(SPY),SPY$SPY.Volume)
AAPL.VWAP<- VWAP(Cl(AAPL),AAPL$AAPL.Volume)
highchart(type = "stock") %>%
# create axis for both SPY and AAPL
hc_add_theme(hc_theme_538())%>% #For fans of 538 the stats website
hc_yAxis_multiples(
create_yaxis(3, height = c(4, 1, 1), turnopposite = TRUE)) %>%
# add both volumnes and VWAP to chart
hc_add_series(SPY, yAxis = 0, name = "SPY") %>%
hc_add_series(AAPL, yAxis = 0, name = "AAPL",type='ohlc') %>%
hc_add_series(SPY.VWAP, yAxis = 0, name = "VWAP SPY") %>%
hc_add_series(AAPL.VWAP, yAxis = 0, name = "VWAP AAPL") %>%
hc_add_series(SPY$SPY.Volume, color = "black", yAxis = 1, name = "SPY Volume", type = "column") %>%
hc_add_series(AAPL$AAPL.Volume, color = "gray", yAxis = 2, name = "AAPL Volume", type = "column")
Formulas
The volume weighted average price (VWAP) is a trading benchmark used by traders that gives the average price a security has traded at throughout the day, based on both volume and price. A rising VWAP, and/or the price above the VWAP line, means the price is likely in an uptrend.A declining VWAP, and/or the price below the VWAP line, means the price is likely in a downtrend.
\(VWAP= \frac{\sum_ \text{Price * Volume}}{ \sum_ \text{Volume}}\)
Analysis
These charts are more about comparing a stock to an index. However using a short term strategy a trader could profit any time the price dips below the VWAP and then sell any time the price rises above the VWAP.
Conclusion
This is just a demonstration of the power of highcharter with stocks. I hope you enjoyed seeing some different technicals and charts. I worked as a stock trader for several years so it was fun to make my own stock charts in R. Contact me on my website if you have any questions. Thanks for reading!