forked from ericscheier/systemtradeR
-
Notifications
You must be signed in to change notification settings - Fork 0
/
systemUpdate.R
143 lines (124 loc) · 4.57 KB
/
systemUpdate.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
# x <- 5
# source("systemConfig.R")
systemUpdate <- function(is.live=system.config$live){
system.config$live <- is.live
mock.time <- as.POSIXct("1992-04-25 07:40:00 UTC")
update.states <- data.frame(func.label=c("quarters","months","weeks","days","hours","minutes"),
unit=c( "months","months","weeks","days","hours","minutes"),
interval=c(3, 1, 1, 1, 1, 15.0),
last.updated=rep(mock.time, 6),
locked=rep(FALSE, 6))
if(file.exists("update_states.RDS")){
update.states <- readRDS("update_states.RDS")
}
for (i in 1:nrow(update.states)){
current.time <- Sys.time()
test.int <- as.interval(difftime(current.time, update.states[i,"last.updated"], units="mins"), start=update.states[i,"last.updated"])
is.due <- (as.period(test.int) %/% do.call(as.character(update.states[i, "unit"]), list(1))) >= update.states[i,"interval"]
is.locked <- update.states[i, "locked"]
if(is.due && !is.locked){
update.states[i, "locked"] <- TRUE
saveRDS(update.states, "update_states.RDS")
intervalFunc <- paste0(update.states[i, "func.label"], "Function")
running.alert <- paste0("Running ",intervalFunc)
actionNotify(running.alert)
func.successful <- try(runParallelFunc(parallel.func.name = intervalFunc))
actionNotify(func.successful)
if(!inherits(func.successful, "try-error")){update.states[i, "last.updated"] <- current.time}
update.states[i, "locked"] <- FALSE
saveRDS(update.states, "update_states.RDS")
}
}
}
runParallelFunc <- function(parallel.func.name, args=list()){
cl <- makeCluster(detectCores()) # -!system.config$live
registerDoParallel(cl)
clusterEvalQ(cl,source("sources.R"))
clusterExport(cl, c("system.config"))
func.successful <- try(do.call(parallel.func.name, args=args))
stopCluster(cl)
registerDoSEQ()
return(func.successful)
}
testFunction <- function(){return("Test Successful")}
minutesFunction <- function(){
# update & note account value
account.value <- recordAccountValue()
# check in on open orders and adjust accordingly
open.orders <- updateOpenOrders()
updateCurrentPositions()
return(list(account.value,
open.orders))
}
hoursFunction <- function(){
# cancel open orders
# refreshVolatility (not forecasts or anything else)
# determine trades to make
# make trades
canceling.orders <- NULL
if(system.config$live){canceling.orders <- cancelAllOrders()}
refreshed.pricing <- refreshPortfolioPricing()
updateCurrentPositions()
updateRefPrices()
updateInstrumentVolatilities()
updateSubsystemPositions()
updateOptimalPositions()
trades.to.make <- tradesToMake()
trades.made <- NULL
if(system.config$live){trades.made <- makeTrades()}
return(list(refreshed.pricing,
canceling.orders,
trades.to.make,
trades.made))
}
daysFunction <- function(){
# volatilityTargetChecking()
# reporting
# update pricing
# cancel open trades
canceling.orders <- NULL
if(system.config$live){canceling.orders <- cancelAllOrders()}
# refresh forecasts and volatility
refreshed.pricing <- refreshPortfolioPricing()
updateCurrentPositions()
updateRefPrices()
updateInstrumentVolatilities()
updateInstrumentForecasts()
updateSubsystemPositions()
updateOptimalPositions()
trades.to.make <- tradesToMake()
trades.made <- NULL
if(system.config$live){trades.made <- makeTrades()}
return(list(refreshed.pricing,
canceling.orders,
trades.to.make,
trades.made))
}
weeksFunction <- function(){
refreshed.pricing <- refreshAllPricing()
refreshed.pairs <- refreshPortfolioPairs()
simulated.forecasts <- simulateForecasts()
raw.forecast.weights <- rawForecastWeights()
smoothed.forecast.weights <- smoothedForecastWeights()
simulated.subsystems <- simulateSubsystems()
raw.instrument.weights <- rawInstrumentWeights()
smoothed.instrument.weights <- smoothedInstrumentWeights()
updateInstrumentWeights()
return(list(refreshed.pricing,
refreshed.pairs,
simulated.forecasts,
raw.forecast.weights,
smoothed.forecast.weights,
simulated.subsystems,
raw.instrument.weights,
smoothed.instrument.weights))
}
monthsFunction <- function(){
return.temp <- "Nothing in this function yet"
return(return.temp)
}
quartersFunction <- function(){
# distributions
return.temp <- "Nothing in this function yet"
return(list(return.temp))
}