-
Notifications
You must be signed in to change notification settings - Fork 40
/
natalChartTransitAspectsPrepare.R
211 lines (179 loc) · 7.44 KB
/
natalChartTransitAspectsPrepare.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# Title : Prepare transits aspects to asset natal chart.
# Objective : Support asset natal chart transits aspects data preparation for statistical analysis.
# Created by: pablocc
# Created on: 16/02/2021
library(data.table)
library(magrittr)
library(stringr)
source("./configUtils.R")
source("./dataLoadUtils.R")
source("./fileSystemUtilities.R")
source("./planetAspectsDataPrepare.R")
calculatePointsPlanetsAspects <- function(longitudePoints) {
aspectsSet <- pabloCerdaAspectSet()
columnNames <- colnames(longitudePoints)
lonColumnNames <- columnNames[grep('^..LON$', columnNames)]
# Build column names for distance, aspect and orbs.
planetsLonDisCols <- str_replace(lonColumnNames, 'LON', 'DIS')
planetsLonAspCols <- str_replace(lonColumnNames, 'LON', 'ASP')
planetsLonOrbCols <- str_replace(lonColumnNames, 'LON', 'ORB')
# Calculate lon / planets distance
for (curcol in planetsLonDisCols) {
planetcol <- paste0(substr(curcol, 1, 2), 'LON')
longitudePoints[, c(curcol) := lon - get(planetcol)]
}
# Normalize to 180 degrees range
longitudePoints[,
c(planetsLonDisCols) := lapply(.SD, degreesDistanceNormalize), .SDcols = planetsLonDisCols
]
# Aspects orbs matrix.
orbsMatrix <- matrix(
aspectsSet$orbs,
nrow = 1,
ncol = length(aspectsSet$orbs),
byrow = TRUE,
dimnames = list('orbs', aspectsSet$aspects)
)
longitudePoints[,
c(planetsLonAspCols) :=
lapply(.SD, longitudeDistanceAspectCategorize, orbs = orbsMatrix), .SDcols = planetsLonDisCols
]
longitudePoints[,
c(planetsLonOrbCols) :=
lapply(.SD, longitudeDistanceAspectOrbCalculate, orbs = orbsMatrix), .SDcols = planetsLonDisCols
]
# Format wide all the significant point points.planets
longitudePoints[, variable := substr(variable, 1, 2)]
natalPointsAspects <- dcast(
longitudePoints,
Date + Hour ~ variable,
value.var = c(planetsLonAspCols, planetsLonOrbCols),
fill = NA
)
# Normalize aspect names with same format as mundane aspects.
columnNames <- colnames(natalPointsAspects)
aspectColumnNames <- columnNames[grep("_", columnNames)]
columnNamesParts <- tstrsplit(aspectColumnNames, "_", fixed = T)
normalAspectColumnNames <- paste0(
substr(columnNamesParts[[1]], 1, 2),
columnNamesParts[[2]],
substr(columnNamesParts[[1]], 3, 6)
)
setnames(natalPointsAspects, c('Date', 'Hour', normalAspectColumnNames))
return(natalPointsAspects)
}
#' Prepare the natal positions table for a given symbol.
buildNatalLongitudes <- function(symbol) {
# open the stocks incorporation date planets positions
natalfile <- paste0(astroDataDestinationPath(), 'assets_natal_charts.tsv')
natal <- fread(natalfile, sep = "\t", na.strings = "", verbose = F)
loncols <- colnames(natal)
loncols <- loncols[grep('^..LON$', loncols)]
natal.long <- melt(natal, id.var = c('Symbol'), measure.var = loncols)
natal.symbol <- natal.long[Symbol == symbol,]
setnames(natal.symbol, c('Symbol', 'variable', 'lon'))
return(natal.symbol)
}
#' Asset natal chart data.
natalChartDateGet <- function(symbol) {
# open the stocks incorporation date planets positions
natalfile <- paste0(astroDataDestinationPath(), 'assets_natal_charts.tsv')
natalCharts <- fread(natalfile, sep = "\t", na.strings = "", verbose = F)
natalChart <- natalCharts[Symbol == symbol]
as.Date(natalChart$Date)
}
# Calculate transits to natal position (symbol incorporation chart) aspects.
buildNatalLongitudeAspects <- function(symbolID, dailyPlanetsPositions) {
bornDate <- natalChartDateGet(symbolID)
if (length(bornDate) == 0) {
return(NULL)
}
cat("Calculating", symbolID, "natal transits aspects\n", sep = " ")
# Prepare natal positions data table.
natalPlanetPositions <- buildNatalLongitudes(symbolID)
columnNames <- colnames(dailyPlanetsPositions)
selectColumnNames <- columnNames[grep('..LON', columnNames)]
# extract only the planets longitudes
dailyPlanetsPositions <- dailyPlanetsPositions[, c('Date', 'Hour', selectColumnNames), with = F] %>%
dataTableDateGreaterFilter(bornDate)
# Cartesian join of natal and mundane positions.
natalMundanePositions <- setkey(dailyPlanetsPositions[, c(k = 1, .SD)], k)[
natalPlanetPositions[, c(k = 1, .SD)],
allow.cartesian = T
]
# Helper dummy index cleanup.
natalMundanePositions[, k := NULL]
natalMundanePositions <- dailyPlanetsPositions[,
as.list(natalPlanetPositions),
by = dailyPlanetsPositions
]
# Calculate natal chart positions transits aspects.
calculatePointsPlanetsAspects(natalMundanePositions)
}
assetsNatalChartTransitsPrepare <- function() {
watchList <- assetsWatchList()
for (symbolID in watchList$SymbolID) {
symbolIdParts <- unlist(strsplit(symbolID, "-"))
isCurrencyPair <- length(symbolIdParts) == 2
natalSymbolId <- ifelse(isCurrencyPair, symbolIdParts[1], symbolID)
natalAspectsWide <- loadPlanetsPositionTable() %>%
buildNatalLongitudeAspects(natalSymbolId, .)
# Process only when natal aspects ara available for this symbol.
if (!is.null(natalAspectsWide)) {
natalAspectsLong <- hourlyAspectsWideToLongTransform(natalAspectsWide)
dailyNatalAspectsLong <- hourlyAspectsDateAggregate(natalAspectsLong)
fwrite(
dailyNatalAspectsLong,
paste0(astroDataDestinationPath(), natalSymbolId, '_natal_transits.csv')
)
}
}
}
natalChartFixedStarPositionsGet <- function(natalSymbolID) {
bornDate <- natalChartDateGet(natalSymbolID)
chineseZodiacFixedStarPositions <- chineseZodiacFixedStarPositionsLoad()
natalFixedStarPositions <- chineseZodiacFixedStarPositions[Date == bornDate]
if (nrow(natalFixedStarPositions) == 0) {
return(NULL);
}
natalFixedStarPositions[, -c('Date')] %>% as.vector() %>% sort()
}
#' Prepare transit planets natal chart chinese mansions positions.
natalChartTransitsChineseMansionsPrepare <- function(symbolID) {
planetPositionsTable <- loadPlanetsPositionTable()
symbolIdParts <- unlist(strsplit(symbolID, "-"))
isCurrencyPair <- length(symbolIdParts) == 2
natalSymbolID <- ifelse(isCurrencyPair, symbolIdParts[1], symbolID)
bornDate <- natalChartDateGet(natalSymbolID)
natalFixedStarPositions <- natalChartFixedStarPositionsGet(natalSymbolID)
colNames <- colnames(planetPositionsTable)
longitudeColNames <- colNames[grep("^..LON", colNames)]
planetLongitudeTableLong <- melt(
planetPositionsTable,
id.var = "Date",
measure.var = longitudeColNames
)
# Aggregate horly to daily with average longitude.
planetLongitudeTableLong <- planetLongitudeTableLong[, mean(value), by = c('Date', 'variable')]
setnames(planetLongitudeTableLong, c('Date', 'pID', 'Lon'))
planetLongitudeTableLong[, Lon := round(Lon, 2)]
setkey(planetLongitudeTableLong, Date)
if (!is.null(natalFixedStarPositions)) {
zodiacMansionsCut <- c(0, as.numeric(natalFixedStarPositions), 360)
zodiacMansionsNames <- names(natalFixedStarPositions)
zodiacMansionsCutNames <- c(
last(zodiacMansionsNames),
zodiacMansionsNames
)
planetLongitudeTableLong[, CM := cut(Lon, zodiacMansionsCut, zodiacMansionsCutNames)]
planetLongitudeTableLong[, pID := substr(pID, 1, 2)]
targetPathFileName <- paste0(astroDataDestinationPath(), natalSymbolID, '_transits_positions.csv')
fwrite(
planetLongitudeTableLong[Date >= as.Date(bornDate)],
targetPathFileName
)
cat("Exported transits positions to: ", targetPathFileName, "\n")
}
}
assetsNatalChartTransitsPrepare()
natalChartTransitsChineseMansionsPrepare("BTC-USD")