Various events in the financial markets does, undoubtedly, have an impact on the prices. For traders relying on technical analysis one important event is when the short simple moving average (SMA), crosses the long SMA.
## Calculate moving averages
## SMA(7) and SMA(10)
ATOMUSDT$short_SMA <- TTR::SMA(
x = ATOMUSDT$Close,
n = 7,
)
ATOMUSDT$long_SMA <- TTR::SMA(
x = ATOMUSDT$Close,
n = 21,
)
## Determine the cross
## from below
ATOMUSDT$cross <- as.numeric(
lag(ATOMUSDT$short_SMA) < ATOMUSDT$long_SMA &
ATOMUSDT$short_SMA > ATOMUSDT$long_SMA
)
Based on the cross
indicator, the ATOMUSDT
pair can now be subset, and processed accordingly.
NOTE: The library supports all kinds of data.frames, including data.table and tibbles.
## create event data
## and store it as a data.rfrae
event_data <- subset(
ATOMUSDT,
cross == 1
)
# 1.1) Extract the index
# from the event data
index <- zoo::index(
event_data
)
# 1.2) Convert the coredata
# into a data.frame
event_data <- as.data.frame(
zoo::coredata(event_data)
)
# 1.3) Add the index into the data.frame
# case insensitive
event_data$index <- index
# 1.4) add events to the data.
# here we use Buys and Sells.
event_data$event <- 'Bull Cross'
event_data$color <- 'darkgray'
Finally, the crosses can be plotted using the chart
and
addEvents
functions, like below,