-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathserver.R
280 lines (224 loc) · 10.4 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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
# (C) 2018 Vladimir Zhbanko
# Shiny app to monitor statistics of the trading systems
# Course Lazy Trading Part 3: Set up your automated Trading Journal
library(shinydashboard)
library(readr)
library(lazytrade)
library(lubridate)
library(dplyr)
library(ggplot2)
library(DT)
#=============================================================
#========= FUNCTIONS AND VARIABLES============================
#=============================================================
# specifying the path to the 4x terminals used into the dataframe
Terminals <- data.frame(id = 1:5, TermPath = c("C:/Program Files (x86)/FxPro - Terminal1/MQL4/Files/",
"C:/Program Files (x86)/FxPro - Terminal2/MQL4/Files/",
"C:/Program Files (x86)/FxPro - Terminal3/MQL4/Files/",
"C:/Program Files (x86)/FxPro - Terminal4/MQL4/Files/",
"C:/Program Files (x86)/FxPro - Terminal5/MQL4/Files/"),
stringsAsFactors = F)
# -------------------------------
# load prices of 28 currencies
# if file is not found in the terminal sandbox, retrieve it from working directory
if(!file.exists(file.path(Terminals[2,2], "AI_CP15.csv"))){
# retrieve the price data from working directory
prices <- read_csv("AI_CP15.csv", col_names = F)
# otherwise get the fresh copy from the terminal sandbox
} else { prices <- read_csv(file.path(Terminals[2,2], "AI_CP15.csv"), col_names = F)}
# make the price having proper format
prices$X1 <- ymd_hms(prices$X1)
# Vector of currency pairs
Pairs = c("Date", "EURUSD", "GBPUSD", "AUDUSD", "NZDUSD", "USDCAD", "USDCHF", "USDJPY",
"EURGBP", "EURJPY", "EURCHF", "EURNZD", "EURCAD", "EURAUD", "GBPAUD",
"GBPCAD", "GBPCHF", "GBPJPY", "GBPNZD", "AUDCAD", "AUDCHF", "AUDJPY",
"AUDNZD", "CADJPY", "CHFJPY", "NZDJPY", "NZDCAD", "NZDCHF", "CADCHF")
# Rename the column?
names(prices) <- Pairs
# -------------------------------
# Load tables with trading strategies
Strategies <- read_csv("Strategies.csv")
Strategies$ID <- as.factor(Strategies$ID)
# -------------------------------
# function that write data to csv file
storeData <- function(data, fileName) {
# store only unique records
# non duplicates
nonDuplicate <- data[!duplicated(data), ]
# Write the file to the local system
write.csv(
x = nonDuplicate,
file = fileName,
row.names = FALSE, quote = FALSE, append = TRUE, col.names = FALSE
)
}
# ============================================================
shinyServer(function(input, output, session) {
#=============================================================
#========= REACTIVE VALUES ===================================
#=============================================================
#---------------------
# have a reactive value of terminal number selected
file_path <- reactive({ file_path <- paste0(Terminals[input$TermNum, 2], "OrdersResultsT", input$TermNum,".csv") })
#Debugging: file_path <- paste0(Terminals[1, 2], "OrdersResultsT", 1,".csv")
# # No DSS? Uncomment and use this variable instead:
# file_path <- reactive({ file_path <- paste0("OrdersResultsT", input$TermNum,".csv") })
#---------------------
# have a reactive value of the magic system selected
system_analysed <- reactive({ system_analysed <- input$MagicNum })
#---------------------
# have a reactive value of the strategy type
strategy_analysed <- reactive({ system_analysed() %>% substr(3,4) })
#---------------------
# cleaning data and creating relevant statistics
DF_Stats <- reactive({
DF_Stats <- read_csv(file = file_path(), col_names = F)
#DF_Stats <- read_csv(file = file_path, col_names = F)
DF_Stats$X3 <- ymd_hms(DF_Stats$X3)
DF_Stats$X4 <- ymd_hms(DF_Stats$X4)
DF_Stats <- DF_Stats %>%
filter(X3 > as.POSIXct(input$filterDate)) %>%
group_by(X1) %>%
summarise(PnL = sum(X5),
NumTrades = n()) %>%
arrange(X1) %>%
filter(NumTrades > input$nTrades[1], NumTrades < input$nTrades[2]) %>%
filter(PnL > input$filter[1], PnL < input$filter[2])
})
#---------------------
# make summary statistics of all systems PnL
DF_Stats_PnL <- reactive({
DF_Stats_PnL <- read_csv(file = file_path(), col_names = F)
#DF_Stats_PnL <- read_csv(file = file_path, col_names = F)
DF_Stats_PnL$X3 <- ymd_hms(DF_Stats_PnL$X3)
DF_Stats_PnL$X4 <- ymd_hms(DF_Stats_PnL$X4)
DF_Stats_PnL <- DF_Stats_PnL %>%
filter(X3 > as.POSIXct(input$filterDate)) %>%
group_by(X1) %>%
summarise(PnL = sum(X5),
NumTrades = n()) %>%
arrange(X1) %>%
filter(NumTrades > input$nTrades[1], NumTrades < input$nTrades[2]) %>%
filter(PnL > input$filter[1], PnL < input$filter[2]) %>%
summarise(TotPnL = sum(PnL),
NumTrades = sum(NumTrades))
})
#---------------------
# make strategy table (to derive it from magic number)
Strategy <- reactive({ Strategies %>% filter(ID == strategy_analysed()) })
#---------------------
# store record as reactive value
DF <- reactive({
DF <- data.frame(ID = strategy_analysed(), Date = as.character(Sys.Date()), Log = as.character(input$caption))
})
#=============================================================
#========= REACTIVE EVENTS ===================================
#=============================================================
# import the summary statistics on the beginning of the app, call the statistics on refresh button call
observeEvent(input$Refresh, {
# update the magic numbers selection
updateSelectInput(session, inputId = "MagicNum", label = NULL, choices = unique(DF_Stats()$X1), selected = NULL)
#try to read from file responses.csv first for the information that is already available
DF <- try(read_csv(file = "responses.csv", col_types = "ccc"),silent = T)
if (class(DF)[3] == "data.frame") { # get data from file to the responses
responses <<- DF
}
})
# add record to the log file and write that to the file back, delete content from the input text
observeEvent(input$subm_rec, {
#add record to log object
# function that write data to global directory called "responses"
saveDataGlobal <- function(data) {
if (exists("responses")) { # get data from global environment is it's exist there
responses <<- rbind(responses, data)
} else {
responses <<- data # <<- this saves to the global environment
}
}
# save data to global directory
saveDataGlobal(DF())
#write to file (append)
storeData(responses, "responses.csv")
#eraze what was written
updateTextAreaInput(session, inputId = "caption", label = NULL, value = "")
})
#=============================================================
#========= OUTPUTS ===========================================
#=============================================================
# -------------------------------------------
# generating plot 1 statistics of the terminal
output$plot1 <- renderPlot({
DF_Stats() %>%
#DF_Stats %>% #debugging
ggplot(aes(x = PnL, y = as.factor(X1), size = NumTrades)) + geom_point()+
ggtitle(label = "Plot indicating which systems are profitable",
subtitle = "Size of the point represent number of trades completed") +
geom_vline(xintercept=0, linetype="dashed", color = "green") +
geom_vline(xintercept = DF_Stats_PnL()$TotPnL, color = "red")
})
# -------------------------------------------
# table with statistic of the system, P/L and Number of trades
output$statistics <- renderTable({ DF_Stats() %>% filter(X1 == system_analysed()) })
# -------------------------------------------
# table with statistic of the system, Sum PnL and N trades
output$summary <- renderTable({ DF_Stats_PnL() })
# -------------------------------------------
# generating plot 2 statistics of the system
output$plot2 <- renderPlot({
DF <- read_csv(file = file_path(), col_names = F)
DF$X3 <- ymd_hms(DF$X3)
DF$X4 <- ymd_hms(DF$X4)
DF %>%
# only show one system
filter(X1 == system_analysed()) %>%
# filter by date, this allows to see trends better!!!
filter(X4 > as.POSIXct(input$filterDate)) %>%
# bring the plot...
ggplot(aes(x = X4, y = X5, col = as.factor(X7), shape = as.factor(X6))) + geom_point()+
# this is just a line separating profit and loss :)
geom_hline(yintercept=0, linetype="dashed", color = "red")+
# adding a simple line summarising points, user can select if apply stat.error filter
geom_smooth(method = "lm", se = input$StatErr)
})
# # -------------------------------------------
# generating plot 3 price chart of pairs
output$plot3 <- renderPlot({
DF <- read_csv(file = file_path(), col_names = F)
DF$X3 <- ymd_hms(DF$X3)
DF$X4 <- ymd_hms(DF$X4)
# find the oldest trade done
DF1 <- DF %>%
# only show one system
filter(X1 == system_analysed()) %>%
select(X4) %>% arrange() %>% head(1)
FirstTrade <- DF1$X4
# find the currency which is in trade
DF2 <- DF %>%
# only show one system
filter(X1 == system_analysed()) %>%
select(X6) %>% head(1)
Currency <- DF2$X6
# extract relevant price information...
DF_Date <- subset(prices, select = Date)
DF_Price <- subset(prices, select = Currency) %>% bind_cols(DF_Date)
# rename otherwise ggplot did not work
names(DF_Price) <- c("X1", "Date")
# bring the plot...
DF_Price %>% filter(Date > as.POSIXct(FirstTrade)) %>%
select(Date, X1) %>%
ggplot(aes(Date, X1, col = "red")) + geom_line()
})
# generating strategy output
output$strategy_text <- renderTable({ Strategy() })
# function that visualizes the current table results if it's stored in GLobal Environment
loadData <- function() {
if (exists("responses")) {
responses
}
}
# writing logs of the records
output$mytable <- DT::renderDataTable({
input$subm_rec
loadData()
})
})