-
Notifications
You must be signed in to change notification settings - Fork 1
/
order.R
406 lines (347 loc) · 12.4 KB
/
order.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
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
# Load required libraries
library(RCurl)
library(jsonlite)
library(plyr)
library(dplyr)
library(stringr)
library(lubridate)
library(gbm)
library(gtools)
# Get script arguments
args<-commandArgs(TRUE)
arg1<-args[1]
if (invalid(arg1)) {
arg1=''
}
# Change working directory
if ( file.exists('/home/john') ) {
setwd("/home/john/Dropbox/pls")
} else if ( file.exists('/home/user') ) {
setwd("/home/user/pls")
} else {
setwd("C:/Users/john/Dropbox/pls")
}
# Load pre-built models
load('models/fitGbm.rda')
# Include milliseconds in log
options(digits.secs=4)
# Function to log status message
log <- function(msg) {
# now=paste(with_tz(now(),"America/Los_Angeles"),'PST')
now=paste(now(),'PST')
write(paste(now,msg,sep="|"), file="log/system.log",append=TRUE)
}
# Helper function to view structures in RStudio window
more <- function(x) {
file <- tempfile()
sink(file); on.exit(sink())
print(x)
file.show(file, delete.file = T)
}
# Currency formatter function
printCurrency <- function(value, currency.sym="$", digits=2, sep=",", decimal=".") {
paste(currency.sym,
formatC(value, format = "f", big.mark = sep, digits=digits, decimal.mark=decimal),sep="")
}
# Number formatter function
printNumber <- function(value, digits=0, sep=",", decimal=".") {
formatC(value, format = "f", big.mark = sep, digits=digits, decimal.mark=decimal)
}
log('-----------------------------------------------------------------------------------------------------')
log("Starting PLS")
# Load User(s) configuration
source('orderConfig.R')
# Load User(s)
load('config/filter.rda')
# Test API authentication
header=getURL(lc$urlCash,header = TRUE, httpheader = c('Authorization' = user$token,
'Accept' = "application/json",
'Content-type' = "application/json"))
if(grepl('Unauthorized',header)) {
msg='Lending Club Authentication failed. Check user configuration'
log(msg)
stop(msg)
}
# Get previously purchased note ids
if(file.exists('config/ids.rda')) {
load('config/ids.rda')
} else {
ids=c()
}
# Obtain initial cash
h = basicTextGatherer()
for (attempt in 1:5) {
lc$jsonCash <- getURL(lc$urlCash,httpheader = c('Authorization' = user$token,
'Accept' = "application/json",
'Content-type' = "application/json"))
if ( is.null(lc$jsonCash) | length(lc$jsonCash) == 0 ) {
log(paste('Unable to get initial available cash. Attempt: ',attempt,sep=""))
next
}
if ( ! grepl("availableCash",lc$jsonCash) ) {
log(paste('Unable to obtain initial available cash. Attempt: ',attempt,sep=""))
next
}
lc$cash <- fromJSON(lc$jsonCash)$availableCash
if ( ! is.numeric(lc$cash) ) {
log(paste('Unable to obtain initial available cash. Attempt: ',attempt,sep=""))
next
}
log(paste('Initial cash available: ',printCurrency(lc$cash),sep=""))
break
}
if ( ! is.numeric(lc$cash) ) {
msg='Unable to obtain initial available cash'
log(msg)
stop(msg)
}
# Verify have above minimum cash level + investment amount
if(lc$cash <= user$minCash + user$amountPerNote) {
msg <- paste('Available cash less than min cash plus amount per note: $',
user$minCash + user$amountPerNote,' (exit)',sep='')
log(msg)
stop(msg)
}
# Set maximum notes per cash parameters
lc$maxNotesPerCash <- floor(lc$cash / user$amountPerNote)
if(arg1=='nolist') {
log('Downloading notes from most recent listing period')
# Set default apiTime
apiTimeStart <- proc.time()[3]
for (cnt in 1:lc$maxNoteCount) {
# Loop to wait 1 second between API calls
while (TRUE) {
if(proc.time()[3] > apiTimeStart+1) {
apiTimeStart <- proc.time()[3]
newJson <- getURL(lc$urlLoanList,httpheader = c('Authorization' = user$token,
'Accept' = "application/json",
'Content-type' = "application/json"))
apiTimeElapse <- proc.time()[3] - apiTimeStart
break
}
}
if ( is.null(newJson) | length(newJson) == 0 ) {
log(paste("List detection (",cnt," of ",lc$maxNoteCount,") - Invalid API response",sep=''))
next
}
if ( ! grepl("pubRec",newJson) ) {
log(paste("List detection (",cnt," of ",lc$maxNoteCount,") - Invalid API response",sep=''))
next
}
loans = fromJSON(newJson)$loans
if ( ! nrow(loans) ) {
log(paste("List detection (",cnt," of ",lc$maxNoteCount,") - Invalid API result",sep=''))
next
}
newIds <- loans$id
newNoteCount <- length(newIds)
log(paste("Note count of most recent list: ",newNoteCount,sep=''))
list=TRUE
listTime=with_tz(now(),"America/Los_Angeles")
break
}
} else {
log("Starting loan list detection")
# Obtain starting note count
prevIds <- c()
for (attempt in 1:5) {
startJson <- getURL(lc$urlLoanList,httpheader = c('Authorization' = user$token,
'Accept' = "application/json",
'Content-type' = "application/json"))
if ( is.null(startJson) | length(startJson) == 0 ) {
log("Invalid API result from start note count. Attempting connection again...")
next
}
if ( ! grepl("pubRec",startJson) ) {
log("Invalid API response from start note count. Attempting connection again...")
next
}
prevIds <- fromJSON(startJson)$loans$id
if ( ! length(prevIds) ) {
log("Invalid API response from initial note count. Attempting connection again...")
next
}
log(paste("Initial note count of most recent loan list:",length(prevIds)))
break
}
if(length(prevIds)==0) {
msg <- 'Unable to obtain initial note count'
log(msg)
stop(msg)
}
# List detection
list=FALSE
# Set default apiTime
apiTimeStart <- proc.time()[3]
for (cnt in 1:lc$maxNoteCount) {
# Loop to wait 1 second between API calls
while (TRUE) {
if(proc.time()[3] > apiTimeStart+1) {
apiTimeStart <- proc.time()[3]
newJson <- getURL(lc$urlLoanList,httpheader = c('Authorization' = user$token,
'Accept' = "application/json",
'Content-type' = "application/json"))
apiTimeElapse <- proc.time()[3] - apiTimeStart
break
}
}
if ( is.null(newJson) | length(newJson) == 0 ) {
log(paste("List detection (",cnt," of ",lc$maxNoteCount,") - Invalid API response",sep=''))
next
}
if ( ! grepl("pubRec",newJson) ) {
log(paste("List detection (",cnt," of ",lc$maxNoteCount,") - Invalid API response",sep=''))
next
}
loans = fromJSON(newJson)$loans
if ( ! nrow(loans) ) {
log(paste("List detection (",cnt," of ",lc$maxNoteCount,") - Invalid API result",sep=''))
next
}
newIds <- loans$id
newNoteCount <- length(newIds)
# No previous notes in new notes and greater than threshold to detect list
if ( ! any(prevIds %in% newIds) & newNoteCount > lc$numNotesThresh ) {
list=TRUE
listTime=with_tz(now(),"America/Los_Angeles")
log(paste("List detected - Note count of most recent list: ",newNoteCount,sep=''))
break
} else {
log(paste("List detection (",cnt," of ",lc$maxNoteCount,")",sep=''))
}
}
# Used for testing
# save(loans,newIds,newNoteCount,list,listTime,file='data/loans.rda')
# Only continue if note list detected
if(!list) {
msg="New note listing not detected"
log(msg)
stop(msg)
}
}
# Add model probability to each loan
loans$earliestCrLine <- ymd(substring(loans$earliestCrLine,1,10))
loans$n=ymd(Sys.Date())
loans$earliestCrLineMonths=as.integer(round((loans$n - loans$earliestCrLine)/30.4375)-1)
loans$installmentIncomeRatio=round(loans$installment/(loans$annualInc/12)*100)
loans$revolBalAnnualIncRatio=round(loans$revolBal/loans$annualInc*100)
loans$grade <- factor(loans$grade)
loans$subGrade <- factor(loans$subGrade)
loans$homeOwnership <- factor(loans$homeOwnership)
loans$purpose <- factor(loans$purpose)
loans$addrState <- factor(loans$addrState)
loans$addrZip <- factor(loans$addrZip)
loans$model=predict(fitGbm,newdata=loans,n.trees=fitGbm$n.trees,type="response")
loans$model=round(loans$model*100-1)
# Remvoe any rows with missing model values
loans=subset(loans, !is.na(model))
newNoteCount=nrow(loans)
if(newNoteCount<=0) {
msg='Loan modelling error'
log(msg)
stop(msg)
}
# Remove uneeded fields
loans$n=NULL
loans$gbmProb=NULL
#loans$pctFunded = loans$fundedAmount / loans$loanAmount
#avgPctFunded <- round(mean(loans$pctFunded)*100)
#log(paste('Average notes funded: ',avgPctFunded,'%',sep=''))
timeStampFile = gsub(" ","_",gsub(":","-",listTime))
save(loans,file=paste('loans/',timeStampFile,'_new_loans.rda',sep=''))
# timeStampFile = gsub(" ","_",gsub(":","-",listTime))
# write.csv(loans,file=paste('data/loans/',timeStampFile,'_PST_new_notes.csv',sep=''),row.names=F)
# Simple loop to stop execution on error
for(one in 1) {
# Set maximum notes allowed per filter
lc$maxNotesPerFilterMax <- round(user$filterMaxPct/100 * newNoteCount)
# Obtain loan ids based on user provided criteria
lc$filteredLoansIds <- loans %>%
filter_(filterStrProd) %>%
arrange_(paste(user$sortOrder,'(',user$sortField,')',sep='')) %>%
select(id)
lc$totalFilteredLoans <- nrow(lc$filteredLoansIds)
if (lc$totalFilteredLoans < 1) {
log('No notes match filter criteria')
break
}
if (lc$totalFilteredLoans > lc$maxNotesPerFilterMax) {
log('Filtered notes exceed max percent notes per filter')
break
}
# Apply user maximums to filtered notes
lc$filteredLoansIds <- head(lc$filteredLoansIds,user$maxNotesPerOrder)
lc$filteredLoansIds <- head(lc$filteredLoansIds,lc$maxNotesPerCash)
lc$totalFilteredLoans <- nrow(lc$filteredLoansIds)
log(paste('Filtered notes after search criteria and user parameters:',lc$totalFilteredLoans))
# Do not purchase notes PLS already invested
skipNotes <- lc$filteredLoansIds[lc$filteredLoansIds$id %in% ids,]
numSkipNotes <- length(skipNotes)
if(numSkipNotes > 0) {
for(i in 1:numSkipNotes) {
log(paste('Previously invested in loan ID:',skipNotes[[i]] ))
}
lc$filteredLoansIds <- as.data.frame(lc$filteredLoansIds[!lc$filteredLoansIds$id %in% ids,])
}
# Total filtered loans given filter and user maximums
lc$totalFilteredLoans <- nrow(lc$filteredLoansIds)
# One final test to make sure there are at least one id left to order
if (lc$totalFilteredLoans < 1) {
log('No notes to order')
break
}
log(paste('Notes to order:',lc$totalFilteredLoans))
##################
### Order code ###
##################
if (user$submit=='Yes') {
# Create order JSON based on filtered Ids
lc$order$aid <- user$accNum
if (user$portfolioId) {
lc$order$orders <- data.frame(lc$filteredLoansIds,
user$amountPerNote,
user$portfolioId)
colnames(lc$order$orders) <- c('loanId','requestedAmount','portfolioId')
} else {
lc$order$orders <- data.frame(lc$filteredLoansIds,
user$amountPerNote)
colnames(lc$order$orders) <- c('loanId','requestedAmount')
}
lc$orderJSON <- toJSON(lc$order,auto_unbox=TRUE)
log('Sending API order request to Lending Club')
lc$resultOrderJSON <- postForm(lc$urlOrders,.opts=list(postfields = lc$orderJSON,
httpheader = c('Authorization' = user$token,
'Accept' = "application/json",
'Content-type' = "application/json")))
if ( is.null(lc$resultOrderJSON) | length(lc$resultOrderJSON) == 0 ) {
log('Order error')
break
}
if ( ! grep("orderInstructId",lc$resultOrderJSON) ) {
log('Order failed')
break
}
lc$resultOrder <- fromJSON(lc$resultOrderJSON)
# Log LC order response
res=lc$resultOrder$orderConfirmations
for(i in 1:nrow(res)) {
row <- res[i,]
log(paste('loanId:',row$loanId,' reqAmnt:$',row$requestedAmount,
' invAmnt:$',row$investedAmount,' status:',row$executionStatus,sep=''))
}
# Save ordered ids to avoid re-purchase
newIds=lc$resultOrder$orderConfirmations$loanId
ids=unique(c(ids,newIds))
save(ids,file='config/ids.rda')
# Log results
lc$resultOrder$requestedAmount <- sum(lc$resultOrder$orderConfirmation$requestedAmount)
log(paste('Total requested amount:',lc$resultOrder$requestedAmount))
lc$resultOrder$investedAmount <- sum(lc$resultOrder$orderConfirmation$investedAmount)
log(paste('Total invested amount:',lc$resultOrder$investedAmount))
lc$resultOrder$numOrderedNotes <- nrow(subset(lc$resultOrder$orderConfirmation, investedAmount>0))
log(paste('Total ordered notes:',lc$resultOrder$numOrderedNotes))
} else {
log('Order submission disabled')
}
}
log('PLS Service Finished')