-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrypto-currency-stats.r
34 lines (29 loc) · 1.24 KB
/
crypto-currency-stats.r
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
library(data.table)
library(quantmod)
expandPath <- function(path) {
normalizePath(paste(getwd(), path, sep=''))
}
# Load symbol historical data in data.table structure with fast/slow moving averages.
getSymbolHistoricalDataTable <- function(dataFile, maFast=20, maSlow=50, dateFormat="%Y-%m-%d", firstDayDate='1970-01-01') {
filename <- expandPath(paste("/data/", dataFile, ".csv", sep=''))
symbol <- fread(filename)
symbol[, Date := as.Date(as.character(Date), format=dateFormat)]
symbol[, Year := as.character(format(Date, "%Y"))]
# Sort by Date and create an index.
setkey(symbol, 'Date')
symbol <- symbol[Date >= firstDayDate,]
# Calculate moving averages from average (Mid) candle price.
symbol[, Mid := (High + Low + Close + Open) / 4]
symbol[, MidMAF := SMA(Mid, n=maFast)]
symbol[, MidMAS := SMA(Mid, n=maSlow)]
symbol[, priceDiff:= MidMAF-MidMAS]
if (all(symbol$priceDiff== 0)) {
stop("Undetermined symbol price direction")
}
# Label price direction based on price difference.
symbol <- symbol[!is.na(priceDiff)]
symbol[, Eff := cut(priceDiff, c(-50000, 0, 50000), labels=c('loss', 'profit'), right=FALSE)]
return(symbol)
}
btcusdDataTable <- getSymbolHistoricalDataTable("BTC-USD")
summary(btcusdDataTable$priceDiff)