Analyses for symbol XAUUSD
Geom moving average for last 20 days
#geom moving average for each year
m <- r[,.(day,mva=frollapply(rateOpen, WINDOW_SIZE, mean.geometric)), by=year]
ggplot(m, aes(x=day, y=mva, group=year, color=year)) + geom_line()
## Warning: Removed 399 row(s) containing missing values (geom_path).

Cluster it with size = 4
wide <- dcast(m, year ~ day, value.var = "mva")
wide_data <- wide[,-1:-20]
means_data <- kmeans(wide_data, CLUSTER_SIZE)
split(wide$year, means_data$cluster)
## $`1`
## [1] 2011 2013
##
## $`2`
## [1] 2000 2001 2002 2003 2004 2005 2007 2009 2010 2012 2014 2015 2016 2017 2018
## [16] 2019 2020
##
## $`3`
## [1] 2006
##
## $`4`
## [1] 2008
Plot cluster centers. Width of the line is power/size of cluster.
centers <- as.data.table(means_data$centers)
centers [,clusterIndex:= seq_len(.N)]
centers[,clusterPower:=means_data$size/sum(means_data$size)]
cTable <- melt(centers, id.vars = c("clusterIndex", "clusterPower"))
cTable[,day := as.numeric(variable) ]
cTable[,variable:=NULL]
ggplot(cTable, aes(x=day, y=value, group=clusterIndex, color=clusterIndex, size=clusterPower)) + geom_line()

Plot geom average for all years
m2 <- m[,.(mva=mean.geometric(mva)), by=day]
ggplot(m2, aes(x=day, y=mva)) + geom_line()
## Warning: Removed 19 row(s) containing missing values (geom_path).

Plot cummulative geom average
m2[is.na(mva), mva:=1]
m2[,cum_mva:= cumprod(mva)]
ggplot(m2, aes(x=day, y=cum_mva)) + geom_line()
