-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.R
231 lines (194 loc) · 7.31 KB
/
server.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
options(stringsAsFactors = FALSE)
library(shiny)
library(xts)
library(quantmod)
Sys.setenv(TZ='UTC')
source('functions.R')
dat <- readRDS('data/EURUSD_H1_Ask.rds')
n_candles <- 20
max_tries <- 10
risk <- .01
# Define server logic required to draw a histogram
shinyServer(function(input, output) {
# Reactive Values
#______________________________________________________________________
values <- reactiveValues(iter=0,
pips=0,
current_position='None',
index=1:n_candles,
points=data.frame(x=numeric(0),
y=numeric(0),
pch=integer(0),
col=character(0)),
this.week=getRandomWeek(dat['2009/']),
allDone='How much can you make in one week?',
quantile=NULL
)
# Candlestick chart
#______________________________________________________________________
output$Chart <- renderPlot({
if(values$allDone != 'GAME OVER'){
plot(chart_Series(values$this.week[values$index], name='EUR/USD Hourly'))
points(x=values$points$x, y=values$points$y, pch=values$points$pch,
col='black', bg=values$points$col, cex=1)
graphics::box(lwd=4, which='figure')
} else {
rand <- randomTrading(values$this.week, n_candles)
values$quantile <- length(rand[rand <= values$pips])/length(rand)*100
hist(rand, breaks=50,
main='How you compared to random decisions (1000 permutations):')
abline(v=values$pips, col='red', lwd=3)
graphics::box(lwd=4, which='figure')
}
})
# Daily Chart
output$DailyChart <- renderPlot({
week_start <- first(index(values$this.week))
daily_start <- week_start - as.difftime(100, unit = "days")
plot(chart_Series(dat[paste0(daily_start, '/', week_start),],
name='Last 100 days with SMA 200',
type='line', TA='add_SMA(n=200)'))
graphics::box(lwd=4, which='figure')
})
# On New Game Start
#______________________________________________________________________
observeEvent(input$Start, {
# Reset all values
values$iter <- 0
values$pips <- 0
values$current_position <- 'None'
values$index <- 1:n_candles
values$this.week <- getRandomWeek(dat['2009/'])
values$points <- data.frame(x=numeric(0),
y=numeric(0),
pch=integer(0),
col=character(0))
values$allDone <- 'How much can you make in one week?'
})
# On Trade decisions
#_______________________________________________________________________
observeEvent(input$Buy, {
# Iterate and change position
values$current_position <- 'Buy'
values$index <- values$index + 1
if(values$index[n_candles] >= nrow(values$this.week)){
values$allDone <- 'GAME OVER'
} else {
# Calulate Pips
last <- Cl(last(values$this.week[values$index], n=2))
values$pips <- values$pips + (as.numeric(last[2]) - as.numeric(last[1]))*1e4
# Move the current points back one, then add a new point
values$points$x <- values$points$x - 1
values$points <- rbind(values$points,
data.frame(x=n_candles + .2,
y=as.numeric(last[1]),
pch=24, col='green'))
}
})
observeEvent(input$Sell, {
#Iterate and change position
values$current_position <- 'Sell'
values$index <- values$index + 1
if(values$index[n_candles] >= nrow(values$this.week)){
values$allDone <- 'GAME OVER'
} else {
values$points$x <- values$points$x - 1
# Calculate pips
last <- Cl(last(values$this.week[values$index], n=2))
values$pips <- values$pips - (as.numeric(last[2]) - as.numeric(last[1]))*1e4
# Add a sell marker that moves with plot
values$points <- rbind(values$points,
data.frame(x=n_candles + .2,
y=as.numeric(last[1]),
pch=25, col='red'))
}
})
observeEvent(input$Close, {
values$current_position <- 'None'
})
observeEvent(input$Hold, {
# Iterate
values$index <- values$index + 1
if(values$index[n_candles] >= nrow(values$this.week)){
values$allDone <- 'GAME OVER'
} else {
values$points$x <- values$points$x - 1
if(values$current_position == 'None'){
} else if(values$current_position == 'Buy'){
last <- Cl(last(values$this.week[values$index], n=2))
values$pips <- values$pips + (as.numeric(last[2]) - as.numeric(last[1]))*1e4
} else if(values$current_position == 'Sell') {
last <- Cl(last(values$this.week[values$index], n=2))
values$pips <- values$pips - (as.numeric(last[2]) - as.numeric(last[1]))*1e4
}
}
})
# Value Boxes
#______________________________________
# Current position
output$Position <- renderValueBox({
if(values$current_position=='Buy'){
icon <- icon("circle-arrow-up", lib = "glyphicon")
} else if(values$current_position=='Sell'){
icon <- icon('circle-arrow-down', lib='glyphicon')
} else {
icon <- icon('option-horizontal', lib='glyphicon')
}
valueBox(values$current_position, subtitle='Current Position', icon=icon,
color = 'aqua')
})
# Pips
output$Pips <- renderValueBox({
if(values$pips == 0){
color <- 'navy'
icon <- icon("meditation", lib = "glyphicon")
} else if(values$pips > 0){
color <- 'green'
icon <- icon("thumbs-up", lib = "glyphicon")
} else if(values$pips < 0){
color <- 'red'
icon <- icon("thumbs-down", lib = "glyphicon")
}
valueBox(
format(values$pips, big.mark=",", scientific=FALSE, nsmall=1),
subtitle="Pips P/L", icon = icon,
color = color
)
})
# Day of Week
output$Time <- renderValueBox({
time <- index(last(values$this.week[values$index]))
time <- format(time, format='%a %H:%M')
valueBox(time, subtitle='Ends on Friday', icon=icon('time', lib='glyphicon'))
})
# UI
#___________________________________________________________________
output$endScreen <- renderUI({
if(values$allDone != 'GAME OVER') {
box(title=values$allDone,
background='aqua', width=NULL,
actionButton('Start', label='Start New Game'),
hr(),
plotOutput('Chart'),
hr(),
div(align='center',
actionButton('Sell', label='Sell'),
actionButton('Hold', label='Hold'),
actionButton('Buy', label='Buy'),
'|',
actionButton('Close', label='Close All Trades')
)
)
} else {
box(title=values$allDone,
background='aqua', width=NULL,
h1(paste('Score:', values$quantile)),
h5('(percentile compared to random trading choices)'),
actionButton('Start', label='Start New Game'),
hr(),
plotOutput('Chart'),
hr()
)
}
})
})