-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtrading_engine_buy_testing.R
155 lines (135 loc) · 4.69 KB
/
trading_engine_buy_testing.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
# We will run throughout 21,24,31,35,39
library(binhf)
library(parallel)
library(foreach)
library(doMC)
library(pbapply)
library(caret)
library(Metrics)
library(methods)
library(plyr)
load("~/Dev/ORF 474/hw2/combined/amzn/20150301_AMZN.RData")
Trades <- Trades[,-c(9:13)] # Removes any junk info we don't need
allDays <- unique(Trades$Date)
first_buy_val_day <- readRDS("allTrain_Day_9_Buy_fefs")
glmnetFitBuy <- readRDS("glmnetFitBuy")
# INITIATE
# Naive Algo follows asks, trades 100 shares if there was a trade at the ask
naiveAlgo <- function(currentPrice_p, currentAsk_p)
{
if(currentPrice_p >= currentAsk_p)
{
return(100)
} else {
return(-1)
}
}
allFinalVWAP_ME <- c()
allFinalVWAP_Market <- c()
allFinalObjectives <- c()
allFinalPovRates <- c()
runSimulation <- function(tradingDay)
{
miniTrades_1 <- Trades[which(Trades$Date == allDays[tradingDay]),]
miniTrades_1$Exchange <- NULL
miniTrades_1$Sym <- NULL
miniTrades_1$SYM_SUFFIX <- NULL
miniTrades_1$Cond <- NULL
miniTrades_1$QU_COND <- NULL
currMarketVWAP <- 0
currMarketVWAP_top <- 0
currMarketVWAP_bottom <- 0
currMyVWAP <- 0
currMyVWAP_top <- 0
currMyVWAP_bottom <- 0
currPOV <- 0
totalBuyVolObs <- 0
currObjective <- 0
currentState <- 0
numSharesToTrade <- 0
dfOfMyTransactions <- data.frame(matrix(ncol = 2))
colnames(dfOfMyTransactions) <- c("Price", "Volume")
dfOfMyTransactions_counter = 1;
for (i in c(1:nrow(miniTrades_1))) {
currentRow <- miniTrades_1[i,]
currentTime <- miniTrades_1[i,2]
currentShares <- miniTrades_1[i,3]
currentPrice <- miniTrades_1[i,4]
currentAsk <- miniTrades_1[i,7]
currentSign <- miniTrades_1[i,9]
if(currentState == 1)
{
dfOfMyTransactions[dfOfMyTransactions_counter,1] = currentAsk
dfOfMyTransactions[dfOfMyTransactions_counter,2] = numSharesToTrade
dfOfMyTransactions_counter = dfOfMyTransactions_counter + 1
currMyVWAP_top = currMyVWAP_top + currentAsk*numSharesToTrade
currMyVWAP_bottom = currMyVWAP_bottom + numSharesToTrade
currMyVWAP = currMyVWAP_top/currMyVWAP_bottom
currPOV = currPOV + numSharesToTrade
currentState = 0;
}
currMarketVWAP_top = currMarketVWAP_top + currentPrice*currentShares
currMarketVWAP_bottom = currMarketVWAP_bottom + currentShares
currMarketVWAP = currMarketVWAP_top/currMarketVWAP_bottom
currObjective = max(currMyVWAP - currMarketVWAP, 0)
if (currentSign == 1)
{
totalBuyVolObs = totalBuyVolObs + currentShares
}
# Check if over-participating
if (currPOV - .1*totalBuyVolObs < 800)
{
result = naiveAlgo(currentPrice_p = currentPrice, currentAsk_p = currentAsk)
if (result > 0)
{
currentState = 1
numSharesToTrade = result
} else {
currentState = 0
}
}
if (.1*totalBuyVolObs - currPOV > 800){
currentState = 1
print("BREAKER EXECUTED")
numSharesToTrade = 300
}
}
# Final Reconcilliation if overbought or oversold
if(currPOV > .1*totalBuyVolObs)
{
dfOfMyTransactions[dfOfMyTransactions_counter,1] = miniTrades_1[i,5]
dfOfMyTransactions[dfOfMyTransactions_counter,2] = currPOV - .1*totalBuyVolObs
currMyVWAP_top = currMyVWAP_top + miniTrades_1[i,5]*(currPOV - .1*totalBuyVolObs)
currMyVWAP_bottom = currMyVWAP_bottom + (currPOV - .1*totalBuyVolObs)
currMyVWAP = currMyVWAP_top/currMyVWAP_bottom
currPOV = currPOV - (currPOV - .1*totalBuyVolObs)
currObjective = max(currMyVWAP - currMarketVWAP, 0)
} else if (currPOV < .1*totalBuyVolObs) {
dfOfMyTransactions[dfOfMyTransactions_counter,1] = currentAsk
dfOfMyTransactions[dfOfMyTransactions_counter,2] = .1*totalBuyVolObs - currPOV
currMyVWAP_top = currMyVWAP_top + currentAsk*(.1*totalBuyVolObs - currPOV)
currMyVWAP_bottom = currMyVWAP_bottom + (.1*totalBuyVolObs - currPOV)
currMyVWAP = currMyVWAP_top/currMyVWAP_bottom
currPOV = currPOV + (.1*totalBuyVolObs - currPOV)
currObjective = max(currMyVWAP - currMarketVWAP, 0)
}
print(paste0("Our Final VWAP: ", currMyVWAP))
print(paste0("Market's Final VWAP: ", currMarketVWAP))
print(paste0("Objective Function: ", currObjective))
print(paste0("Our Final POV rate: ", currPOV/totalBuyVolObs))
return(c(currMyVWAP, currMarketVWAP, currObjective, currPOV/totalBuyVolObs))
}
listOfDays <- c(21, 24, 31, 35)
for(d in listOfDays)
{
print(paste0("On day ", d))
res <- runSimulation(d)
allFinalVWAP_ME <- c(allFinalVWAP_ME, res[1])
allFinalVWAP_Market <- c(allFinalVWAP_Market, res[2])
allFinalObjectives <- c(allFinalObjectives, res[3])
allFinalPovRates <- c(allFinalPovRates, res[4])
}
print("Results of Simulation: ")
print(paste0("Our Final Simulation Average VWAP: ", mean(allFinalVWAP_ME)))
print(paste0("Market's Final Simulation Average VWAP: ", mean(allFinalVWAP_Market)))
print(paste0("Simulation Average Objective Function: ", mean(allFinalObjectives)))