-
Notifications
You must be signed in to change notification settings - Fork 40
/
planetAspectsAssetsPriceDataPrepare.R
158 lines (137 loc) · 7.09 KB
/
planetAspectsAssetsPriceDataPrepare.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# Title : Compose daily planetary aspects / assets price data table.
# Objective : Prepare daily assets prices with planetary aspects data table for statistical research.
# Created by: pablocc
# Created on: 10/01/2021
library(data.table)
library(quantmod)
library(stringr)
source("./configUtils.R")
source("./fileSystemUtilities.R")
#' Fetch asset price data from a given data provider source.
#' @param symbolID Data source asset symbol ID.
#' @param source Data provider source ID supported by quantmod.
#' @return Boolean vector with TRUE for success symbol data download and FALSE for failed.
assetPriceDataFetch <- function(symbolID, source = "yahoo") {
# Date to retrieve asset price since if available, oldest data source records are around this year.
startDate = as.Date("1970-01-01")
maxRetry <- 5
for (t in 1:maxRetry) {
tryCatch({
cat("Downloading", symbolID, "- attempt: #", t, "of", maxRetry, "\n")
symbolDataFrame <- getSymbols(
symbolID,
src = "yahoo",
from = startDate,
env = NULL,
return.class = 'data.frame'
)
symbolDataFrame <- cbind(rownames(symbolDataFrame), symbolDataFrame)
# Adjust the prices for splits / dividends
symbolDataFrame[, 2:7] <- adjustOHLC(symbolDataFrame[, 2:7], use.Adjusted = T)
names(symbolDataFrame) <- c('Date', 'Open', 'High', 'Low', 'Close', 'Volume', 'Adj Close')
setDT(symbolDataFrame)
# Store to tmp data directory.
targetFileName <- paste0(assetsDataDestinationPath(), symbolID, ".csv")
write.csv(symbolDataFrame, file = targetFileName, row.names = F)
cat("Exported asset price data to:", targetFileName, "\n")
return(TRUE)
}, error = function(e) {
print(e)
return(FALSE)
})
}
}
#' Process watch list assets price data retrieval.
watchListPriceDataFetch <- function() {
symbolsList <- assetsWatchList()
result <- lapply(symbolsList$SymbolID, assetPriceDataFetch)
}
#' Process asset price data load and price derivations calculation.
#' @param symbolID Asset symbol ID as identified in data source.
#' @return Boolean vector with TRUE for success data augmentation and FALSE for failed.
assetPriceDataPriceAugment <- function(symbolID) {
dateFormat = "%Y-%m-%d"
mapricefs <- 2
mapricesl <- 4
assetPriceTable <- fread(
paste0(assetsDataDestinationPath(), symbolID, ".csv")
)
# Filter observations with missing open price.
assetPriceTable <- assetPriceTable[!is.na(Open)]
assetPriceTable[, Date := str_replace(Date, '^X', '')]
assetPriceTable[, Date := str_replace_all(Date, '\\.', '-')]
assetPriceTable[, Date := as.Date(Date, format = dateFormat)]
setkey(assetPriceTable, 'Date')
# Calculate daily mid prices based on OHLC combinations.
assetPriceTable[, OHLCMid := (Open + High + Low + Close) / 4]
assetPriceTable[, HLCMid := (High + Low + Close) / 3]
assetPriceTable[, HLMid := (High + Low) / 2]
# Calculate fast / slow MAs for the different mid prices.
assetPriceTable[, OHLCMAF := SMA(OHLCMid, n = mapricefs)]
assetPriceTable[, OHLCMAS := SMA(OHLCMid, n = mapricesl)]
assetPriceTable[, HLCMAF := SMA(HLCMid, n = mapricefs)]
assetPriceTable[, HLCMAS := SMA(HLCMid, n = mapricesl)]
assetPriceTable[, HLMAF := SMA(HLMid, n = mapricefs)]
assetPriceTable[, HLMAS := SMA(HLMid, n = mapricesl)]
# Calculate moving average momentum.
assetPriceTable[, OHLCMom := OHLCMAF - OHLCMAS]
assetPriceTable[, HLCMom := HLCMAF - HLCMAS]
assetPriceTable[, HLMom := HLMAF - HLMAS]
# Calculate mid price differences.
assetPriceTable[, diffOHLC := Delt(OHLCMid, k = 1)]
assetPriceTable[, difflogOHLC := Delt(OHLCMid, k = 1, type = "log")]
assetPriceTable[, diffOxHL := Delt(Open, HLMid, k = 0)]
assetPriceTable[, difflogOxHL := Delt(Open, HLMid, k = 0, type = "log")]
assetPriceTable[, diffOxHLC := Delt(Open, HLCMid, k = 0)]
assetPriceTable[, difflogOxHLC := Delt(Open, HLCMid, k = 0, type = "log")]
assetPriceTable[, difflogHLMASxF := Delt(HLMAS, HLMAF, k = 0, type = "log")]
assetPriceTable[, difflogHLCMASxF := Delt(HLCMAS, HLCMAF, k = 0, type = "log")]
# Normalize price daily change to Z scores and center data.
assetPriceTable[, zdiffOHLC := scale(diffOHLC, center = T)]
assetPriceTable[, zdifflogOHLC := scale(difflogOHLC, center = T)]
assetPriceTable[, zdifflogHLMASxF := scale(difflogHLMASxF, center = T)]
assetPriceTable[, zdifflogHLCMASxF := scale(difflogHLCMASxF, center = T)]
assetPriceTable[, zdiffOxHL := scale(diffOxHL, center = T)]
assetPriceTable[, zdifflogOxHL := scale(difflogOxHL, center = T)]
assetPriceTable[, zdiffOxHLC := scale(diffOxHLC, center = T)]
assetPriceTable[, zdifflogOxHLC := scale(difflogOxHLC, center = T)]
# Filter the initial days needed by MAs max period where MA value cannot be determined.
assetPriceTable <- assetPriceTable[!is.na(OHLCMAF) & !is.na(difflogHLMASxF)]
labelCuts <- c(-1000000, 0, 1000000)
labels <- c('Sell', 'Buy')
assetPriceTable[, OHLCEff := cut(diffOHLC, labelCuts, labels = labels, right = FALSE)]
assetPriceTable[, OHLCMomEff := cut(OHLCMom, labelCuts, labels = labels, right = FALSE)]
assetPriceTable[, HLCMomEff := cut(HLCMom, labelCuts, labels = labels, right = FALSE)]
assetPriceTable[, HLMomEff := cut(HLMom, labelCuts, labels = labels, right = FALSE)]
assetPriceTable[, OxHLEff := cut(diffOxHL, labelCuts, labels = labels, right = FALSE)]
assetPriceTable[, OxHLCEff := cut(diffOxHLC, labelCuts, labels = labels, right = FALSE)]
# Store augmented asset price table into temporal data directory.
targetFileName <- paste0(assetsDataDestinationPath(), symbolID, "--augmented.csv")
fwrite(assetPriceTable, targetFileName)
cat("Exported augmented asset price data to:", targetFileName, "\n")
# TODO: Implement error handling on write and return false on failure.
return(TRUE)
}
#' Merge daily planet aspects and asset prices augmented tables and export to CSV.
#' @param symbolID Symbol ID of the asset data to process.
#' @return Boolean vector with TRUE for success data augmentation and FALSE for failed.
planetAspectsAssetPricesDataMerge <- function(symbolID) {
# TODO: Separate merge from export logic.
assetPriceAugmentedFileName <- paste0(assetsDataDestinationPath(), symbolID, "--augmented.csv")
assetPriceAugmentedTable <- fread(assetPriceAugmentedFileName)
aspectFileName <- "aspects_all_planets_pablo_aspects_set_long"
planetAspectLong <- fread(paste0("./data/", aspectFileName, ".csv"))
planetAspectsAssetPricesTable <- merge(planetAspectLong, assetPriceAugmentedTable, by = c('Date'))
targetFileName <- paste0(assetsDataDestinationPath(), symbolID, "--", aspectFileName, ".csv")
fwrite(planetAspectsAssetPricesTable, targetFileName)
cat("Exported aspects / asset price merged data to:", targetFileName, "\n")
# TODO: Implement error handling on write and return false on failure.
return(TRUE)
}
#' Process planet aspects / assets price data preparation.
planetsAspectsAssetsPriceDataPrepare <- function() {
watchListPriceDataFetch()
symbolsList <- assetsWatchList()
augmentResult <- lapply(symbolsList$SymbolID, assetPriceDataPriceAugment)
mergeResult <- lapply(symbolsList$SymbolID, planetAspectsAssetPricesDataMerge)
}