-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmicro_live_agent.R
More file actions
447 lines (389 loc) · 12.1 KB
/
micro_live_agent.R
File metadata and controls
447 lines (389 loc) · 12.1 KB
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
# ==========================================================================#
#-- Project Name : Microstructure Trading Agent with Protector Initial Code
#-- Task : Write a Microstructure Live Trading Strategy
#-- version : 1.0
#-- Date : 07/MAY/2014
#-- Author : Maitreyi Mandal
#-- SVN Directory: \xxxx
# ==========================================================================#
#============================== Get the environment variables===============#
#==============================
#mripPath = Sys.getenv("MRIP_HOME")
#===================================
# Common variables across the agency
#===================================
assign("checPkgVec",0, pos=.GlobalEnv)
assign("firstCall",TRUE, pos=.GlobalEnv)
assign("symbolVec",c("JPM"), pos=.GlobalEnv)
#assign("activeMQIp","tcp://10.10.5.40:61616", pos=.GlobalEnv)
#=========================
# Check the logger package
#=========================
if(!is.element("futile.logger", installed.packages())){
install.packages("futile.logger",repos="http://lib.stat.cmu.edu/R/CRAN")
}
require("futile.logger")
#==================
# Setting up logger
#==================
flog.info("Sourcing micro_live_agent.R file - Begin!")
flog.appender(appender.file("C:/Users/Maitreyi.Mandal/Desktop/R/micro1.log"))
flog.info("Sourcing micro_live_agent.R file - Begin!")
#====================================
# Common Functions across the agency
#====================================
#---------------------------------------------------------------------
# A simple function to check if the required version of R is installed
#---------------------------------------------------------------------
CheckRversion<-function(reqRVersion = '2.15.1'){
if (getRversion() < reqRVersion){
flog.error("Upgrade your R!")
return(10)
} else {
return(0)
}
}
packInfo <- installed.packages ()[ ,c("Package", "Depends", "Version")]
#-----------------------------------------------------------------------------
# Check if a given package is installed and is >= required version
# Args:
# packName: Name of the package as a string
# reqVersion: Required version as a string
#Returns:
# 0 if the package is already installed/ or was succesfully installed by the
# function. Else returns 11.
#-----------------------------------------------------------------------------
CheckPackVer <- function(packName, reqVersion) {
flog.info("Checking package and version for package %s",paste(packName,reqVersion,sep=":"))
reqVersion <- unlist(strsplit(reqVersion, "[[:punct:][:space:]]+"))
count <- 1
tryCatch({
currVersion <- packInfo[packName,3]
currVersion <- unlist(strsplit(currVersion,"[[:punct:][:space:]]+"))
if(length(currVersion) > length(reqVersion) ){
reqVersion = c(reqVersion,rep("0",length(currVersion) - length(reqVersion)))
}
if(length(currVersion) < length(reqVersion) ){
currVersion = c(currVersion,rep("0",length(reqVersion) - length(currVersion)))
}
if(any(currVersion != reqVersion)){
chng <- which(currVersion != reqVersion)[1]
if(reqVersion[chng] > currVersion[chng]){
stop()
}
}
flog.info("%s is installed",packName)
return(0)
},
error = function(err){
flog.error("Error in the package %s",packName)
flog.error("Error is %s",err)
while(count<4){
flog.info("%s Installation attemp %d",packName,count)
install.packages(packName,dependencies=TRUE,repos="http://lib.stat.cmu.edu/R/CRAN")
packInfo <- installed.packages ()[ ,c("Package", "Depends", "Version")]
if(packName %in% packInfo[,1]) break
count<<-count+1
}
if (count == 4 && !(packName %in% packInfo[,1])){
return(11)
} else{
return(0)
}
},
finally={})
}
flog.info("Sourcing PairTradingModel.R file - End!")
#====================================
# Check R version and package version
#====================================
errorCode<-CheckRversion()
checPkgVec[1]<-CheckPackVer("Rjms",'0.0.5')
checPkgVec[2]<-CheckPackVer("WeightedPortTest",'1.0')
if (any(checPkgVec!=0)){
errorCode<<-11
}
#===========================
# Loading Required Libraries
#===========================
require("Rjms")
require("rjson")
flog.info("R libraries loaded succesfully")
#=====================
# Initialize Variables
#=====================
# Defining Global Variables#
#frameSize <- 100
#nSymbols <- length(symbolVec)
#waitTime <- 10000 # Time to wait (in millis) for the order to get filled
#dfMatrix <-NULL
#fillCount <-NULL
newbid<-0
newask<-0
prevna<-0
prevnb<-0
nac<-0
nbc<-0
askPrice<-0
ask_old<-0
bidPrice<-0
bid_old<-0
min<-0
min_old<-0
PL<-0
cnt<-0
position<-0
gamma<-0.01
firstCall <- TRUE
#===============================
# LatencyComputation Variables
#===============================
#latencyFile = paste(mripPath,"/MINTlogs/NewpairTradingProcessLatency12.csv",sep="")
#=========================================
# Function to Initialize dynamic Variables
#=========================================
initParams <- function(){
dfMatrix <<- matrix(-1,nrow=frameSize,ncol=nSymbols+1)
fillCount <<- 1
}
#==================================
# Function to generate trade signal
#==================================
generateTradeSignal <- function(tick){
tickList <- fromJSON(tick)
marketStatus <- tickList$marketStatus[1]
#tradeTime <- as.POSIXlt(tickList$timeStamp)
#flog.info(paste("TIMESTAMP is - ", tickList$timeStamp,sep=" "))
if (firstCall){
ask_old<<-tickList$askPrice
bid_old<<-tickList$bidPrice
tradeTime <- as.POSIXlt(tickList$timeStamp)
print(paste("First Ask old is:",ask_old,sep=","))
print(paste("First Ask Price is:",askPrice,sep=","))
print(paste("First bid old is:",bid_old,sep=","))
print(paste("First bid Price is:",bidPrice,sep=","))
min<<-as.POSIXct(tradeTime,origin = "1970-01-01")
print("99")
min<-(as.numeric(format(min,"%M")))
print(paste("min is:",min, sep=","))
print("199")
min_old<<-min
firstCall <<- FALSE
return(paste("ask_old is",ask_old,"bid_old is:",bid_old,sep=","))
}# End of First Call
tradeTime <- as.POSIXlt(tickList$timeStamp)
min<<-as.POSIXct(tradeTime,origin = "1970-01-01")
min<-(as.numeric(format(min,"%M")))
print(paste("Min is:",min,sep=","))
print(paste("Min_old is:",min_old,sep=","))
t<-as.difftime(min,min_old,unit="mins")
askPrice <<- tickList$askPrice
bidPrice<<-tickList$bidPrice
min<<-min
print("299")
print("499")
print(paste("t is:",t, sep=","))
#return(0)
if ((t<60)&&(min_old==min)){ # computing new ask & previous new ask counts
if(ask_old!=askPrice)
{
#prevna<-prevna
newask<<-newask+1
print(paste("prevna is:",prevna,sep=","))
print(paste("newask is:",newask,sep=","))
print(paste("BidPrice is:",bidPrice,sep=","))
print(paste("askPrice is:",askPrice,sep=","))
print(paste("ask_old is:",ask_old,sep=","))
print(paste("bid_old is:",bid_old,sep=","))
print("12")
#return(paste("newask is",newask,"prevna is:",prevna,sep=","))
}
else
{
#newask<-newask
#prevna<-prevna
print(paste("BidPrice is:",bidPrice,sep=","))
print(paste("askPrice is:",askPrice,sep=","))
print(paste("ask_old is:",ask_old,sep=","))
print(paste("bid_old is:",bid_old,sep=","))
print("13")
#return(paste("newask is",newask,"prevna is:",prevna,sep=","))
}
}
else
{ nac<<-prevna
prevna<<-newask
newask<<-0
cnt<<-cnt+1
print(paste("cnt is:",cnt,sep=","))
print(paste("prevna is:",prevna,sep=","))
print(paste("newask is:",newask,sep=","))
print(paste("BidPrice is:",bidPrice,sep=","))
print(paste("askPrice is:",askPrice,sep=","))
print(paste("ask_old is:",ask_old,sep=","))
print(paste("bid_old is:",bid_old,sep=","))
print("15")
#return(paste("newask is",newask,"prevna is:",prevna,sep=","))
}
if ((t<60) && (min_old==min)){
# computing new bid & previous new bid counts m
if (bid_old!=bidPrice)
{
prevnb<<-prevnb
newbid<<-newbid+1
print("16")
#return(paste("newbid is",newbid,"prevnb is:",prevnb,sep=","))
}
else
{
#newbid<-newbid
#prevnb<-prevnb
print("18")
#return(paste("newbid is",newbid,"prevnb is:",prevnb,sep=","))
}
}
# if previous min!=this min
else
{
nbc<<-prevnb
prevnb<<-newbid
newbid<<-0
print("30")
#return(paste("newbid is",newbid,"prevnb is:",prevnb,sep=","))
}
if (cnt>=2)
{
if (((newbid-prevnb)!=0) && ((bid_old)!=0) && ((bidPrice)!=0))
{
var1<-abs(1-gamma*(((newbid)*(prevnb))/(newbid-prevnb)))
var2<-log(var1)
var3<-(1/gamma)*var1
optbid <<-ifelse(is.nan(bidPrice-var3),0,(bidPrice-var3))
#return (paste("Optimal bid price is:",optbid,sep=","))
}
else
{
optbid<<-0
#return (paste("Optimal bid price is:",optbid,sep=","))
}
if (((newask-prevna)!=0) && ((ask_old)!=0) && ((askPrice)!=0))
{
var3<-abs(1-gamma*(((newask)*(prevna))/(newask-prevna)))
var4<-log(var3)
var5<-(1/gamma)*var4
optask <<-ifelse(is.nan(askPrice-var5),0,(askPrice-var5))
#return (paste("Optimal ask price is:",optask,sep=","))
}
else
{
optask<<-0
#return (paste("Optimal ask price is:",optask,sep=","))
}
{
if(optbid> askPrice)
{
if(position <= 1)
{
#action = 'B' or BUY
PL<<- PL-askPrice
position <<- (position +1)
print(PL)
print("3")
print(paste("position is:",position,sep=","))
#return (paste("PL is:",PL,sep=","))
}
else { #action = 'N'
#PL<<-PL
#position<<-position
print(PL)
print("4")
#return (paste("PL is:",PL,sep=","))
}
}
else if(optask < bidPrice)
{
if(position >= 1)
{
#action = 'A'
PL<<-PL+bidPrice
position<<-position-1
print(PL)
print(paste("position is:",position,sep=","))
print("5")
#return (paste("PL is:",PL,sep=","))
}
else {#action = 'N'
#PL <<-PL
#position<<-position
print(PL)
print("6")
#return (paste("PL is:",PL,sep=","))
}
}
else
{
#between bid and ask, if closer to bid, buy, else sell
if(abs(bidPrice-optbid) > abs(askPrice-optask))
{
#action = 'B'
PL <<-(PL-askPrice)
position <<-(position+1)
print(paste("position7 is:",position,sep=","))
print(PL)
print("7")
#return (paste("PL is:",PL,sep=","))
}
else (abs(bidPrice-optbid) > abs(askPrice-optask))
{
#action = 'A'
PL <<-(PL+bidPrice)
position <<-(position-1)
print(paste("position8 is:",position,sep=","))
print(PL)
print("8")
#return (paste("PL is:",PL,sep=","))
}
}
}
}
else {
#return (paste("PL is:",PL,sep=","))
}
ask_old<<-askPrice
bid_old<<-bidPrice
min_old<<-min
}
#=========================================
# Main function to push signals to a queue
#=========================================
MainSendSignal <- function(tick){
t1 <- as.character(format(Sys.time(),"%Y-%m-%d %H:%M:%OS6"))
if(firstCall){
initParams()
flog.info("Initialized Parameters for the first Call.")
firstCall <<- FALSE
}
predSignal <- generateTradeSignal(tick)
# Write the Latency values to a file
if(predSignal != "ignore")
{
output <- toJSON(predSignal)
#checkLogged <- to.logger(signalLogger,output,asString=T)
# Calculate Latency values of R computation
t2 <- as.character(format(Sys.time(),"%Y-%m-%d %H:%M:%OS6"))
latVal = as.double(difftime(t2,t1)[1])
latencyValues = c(t1,t2,latVal)
write.table(x=t(latencyValues), file = latencyFile, row.names=FALSE, col.names=FALSE, sep=",",append=TRUE)
flog.info("Sent a signal to the queue")
}
}
#===================
# Clean Up Function
#===================
cleanUp <- function(){
flog.info("The agent %s in agency %s has been killed. Clean up function evoked",
agent__,agency__, name=logNS)
#destroy.logger(signalLogger)
rm(list=ls())
flog.info("Clean up done.")
}