-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathKelly_Eng1213.Rmd
191 lines (165 loc) · 7.91 KB
/
Kelly_Eng1213.Rmd
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
---
title: "Kelly-Criterion"
author: "englianhu"
website: <https://jp.linkedin.com/in/englianhu>
date: "Thursday, December 25, 2014"
output: html_document
---
I tried to scrap the odds price from 7M and NowGoal website, and apply Kelly-Criterion Model, from the simulatioin we can know that the EM model is profitable. However real-time trading platform is the future project which need to start soon... <https://github.com/englianhu/Kelly-Criterion>.
Load and manipulate the dataset.
```{r}
suppressMessages(library('turner'))
load('./data/dataset.RData')
COModds <- subset(COM_7M1213$AH, names(COM_7M1213$AH) %in% eng1213$MatchID_7M)
# initial odds price
ini.odds <- lapply(COModds, function(x) na.omit(x)[1,-1])
ini.odds <- lapply(ini.odds, function(x){ df_to_blocks(x,blocks=c(rep(3,ncol(x)/3)),byrow=FALSE)})
ini.odds2 <- lapply(seq(ini.odds), function(i) {
lapply(seq(ini.odds[[i]]),function(j) {
COM=substr(names(ini.odds[[i]][[j]]),1,nchar(names(ini.odds[[i]][[j]]))-3)[1]
names(ini.odds[[i]][[j]])=c('AH','HM','AW')
data.frame(MatchID=names(ini.odds)[[i]], COM, ini.odds[[i]][[j]])}) })
iniOdds <- Reduce(function(x, y) merge(x, y, all = T), unlist(ini.odds2, recursive=F), accumulate = F)
iniOdds <- iniOdds[iniOdds$MatchID %in% EM_7M1213$FT$FTAH$MatchID,]
iniOdds$Spreads <- iniOdds$HM + iniOdds$AW
rm(ini.odds, ini.odds2)
```
Manipulate and reshape the dataset
```{r}
suppressMessages(library('reshape2'))
# EM_7M1213$FT$FTAH[,1:5]
# filter the soccer matches which the teams has selected to calculated
eng1213b <- subset(eng1213, MatchID_7M %in% EM_7M1213$FT$FTAH$MatchID)
nrow(EM_7M1213$FT$FTAH)
# filter the initial odds data from 7M by MatchID
dat <- EM_7M1213$FT$FTAH[EM_7M1213$FT$FTAH$MatchID %in% iniOdds$MatchID,]
# nrow(EM_7M1213$FT$FTAH[EM_7M1213$FT$FTAH$MatchID %in% iniOdds$MatchID,])
ah <- ifelse(nchar(abs(iniOdds$AH*100))<3, paste0('0',abs(iniOdds$AH*100)),iniOdds$AH*100)
ah <- ifelse(substr(ah,1,1)=='-', paste0('HN',ah), paste0('HP',ah))
ah <- gsub('-', '', ah); iniOdds$AHCode <- gsub('HP00', 'H000', ah); rm(ah)
iniOdds$AHCode <- as.factor(iniOdds$AHCode)
iniHM <- iniOdds[c("MatchID","COM","AH","HM","AHCode","Spreads")]; iniHM$AH <- -iniHM$AH
iniAW <- iniOdds[c("MatchID","COM","AH","AW","AHCode","Spreads")]
iniAW$AHCode <- gsub('HN','AP',iniAW$AHCode); iniAW$AHCode <- gsub('HP','AN',iniAW$AHCode)
iniAW$AHCode <- gsub('H000','A000',iniAW$AHCode)
iniOdds <- merge(iniHM, iniAW, all=T)
iniOdds$AH <- ifelse(substr(iniOdds$AHCode,2,2)=='N', -abs(iniOdds$AH), abs(iniOdds$AH))
iniOdds$COMOdds <- ifelse(is.na(iniOdds$HM) & !is.na(iniOdds$AW), iniOdds$AW,
ifelse(!is.na(iniOdds$HM) & is.na(iniOdds$AW), iniOdds$HM, NA))
iniOdds <- iniOdds[c('MatchID','COM','AH','AHCode','COMOdds','Spreads')]
iniOdds$RProb <- iniOdds$COMOdds * 2/iniOdds$Spreads
rm(iniHM, iniAW)
```
Merge the dataset
```{r}
# Filter the MatchID of EM_7M and iniOdds
EMOdds <- melt(EM_7M1213$FT$FTAH,id=c('MatchID','Round','KODate','Home',
'Away','FTHG','FTAG','HTHG','HTAG'),
variable.name='AHCode', value.name='EMprob')
names(EMOdds)[names(EMOdds) %in% c('variable', 'value')] = c('AHCode', 'EMprob')
length(unique(EMOdds$MatchID))
length(unique(iniOdds$MatchID))
# merge iniOdds and EMOdds
mbase <- merge(iniOdds, EMOdds, by=c("MatchID","AHCode"))
mbase$AH <- ifelse(substr(mbase$AHCode,2,2)=='N', -abs(mbase$AH), abs(mbase$AH))
```
AH Calculator and Kelly-Betting
```{r}
# -----------------------------------------------------------------------------
# Kelly criterion
# Advantages = (prob of win * decimal odds) + (prob of lose * -1)
# Optimal Kelly wager % = Advantages / decimal odds
mbase$Adv <- (mbase$EMprob * mbase$COMOdds) + ((1-mbase$EMprob) * -1)
mbase$Staking <- mbase$Adv / mbase$COMOdds
mbase$Staking <- ifelse(mbase$Staking < 0, 0, mbase$Staking)
# Measure the advantage (pure odds without overround/margin)
mbase$Adv2 <- (mbase$EMprob * mbase$RProb) + ((1-mbase$EMprob) * -1)
mbase$Staking2 <- mbase$Adv2 / mbase$COMOdds
mbase$Staking2 <- ifelse(mbase$Staking2 < 0, 0, mbase$Staking2)
# AH & OU Calculator
mbase$Output <- 0
for(i in seq(mbase$AH)){
if(substr(mbase$AHCode[i], 1, 1)=='H'){
if((mbase$FTHG[i] + mbase$AH[i] - mbase$FTAG[i]) > 0.25){
mbase$Output[i] = "W"
} else if((mbase$FTHG[i] + mbase$AH[i] - mbase$FTAG[i]) == -0.25){
mbase$Output[i] = "LH"
} else if((mbase$FTHG[i] + mbase$AH[i] - mbase$FTAG[i]) == 0){
mbase$Output[i] = "VOID"
} else if((mbase$FTHG[i] + mbase$AH[i] - mbase$FTAG[i]) == 0.25){
mbase$Output[i] = "WH"
} else if((mbase$FTHG[i] + mbase$AH[i] - mbase$FTAG[i]) < -0.25){
mbase$Output[i] = "L"
}
} else if(substr(mbase$AHCode[i],1,1)=='A'){
if((mbase$FTAG[i] + mbase$AH[i] - mbase$FTHG[i]) > 0.25){
mbase$Output[i] = "W"
} else if((mbase$FTAG[i] + mbase$AH[i] - mbase$FTHG[i]) == -0.25){
mbase$Output[i] = "LH"
} else if((mbase$FTAG[i] + mbase$AH[i] - mbase$FTHG[i]) == 0){
mbase$Output[i] = "VOID"
} else if((mbase$FTAG[i] + mbase$AH[i] - mbase$FTHG[i]) == 0.25){
mbase$Output[i] = "WH"
} else if((mbase$FTAG[i] + mbase$AH[i] - mbase$FTHG[i]) < -0.25){
mbase$Output[i] = "L"
}
}
}; rm(i)
# Calculate the Net Profit and Lose (AH and OU odds without capital)
mbase$PL <- 0
for(i in seq(mbase$Output)){
if(mbase$Output[i]=='W'){
mbase$PL[i] <- mbase$COMOdds[i] * mbase$Staking[i]
} else if(mbase$Output[i]=='WH'){
mbase$PL[i] <- mbase$COMOdds[i] * mbase$Staking[i] * 0.5
} else if(mbase$Output[i]=='VOID'){
mbase$PL[i] <- 0
} else if(mbase$Output[i]=='LH'){
mbase$PL[i] <- mbase$COMOdds[i] * mbase$Staking[i] * -0.5
} else if(mbase$Output[i]=='L'){
mbase$PL[i] <- mbase$COMOdds[i] * -mbase$Staking[i]
} else {
mbase$PL[i] <- NA
}
}; rm(i)
# Calculate the Net Profit and Lose (AH and OU odds without capital and without overround/margin)
mbase$PL2 <- 0
for(i in seq(mbase$Output)){
if(mbase$Output[i]=='W'){
mbase$PL2[i] <- mbase$COMOdds[i] * mbase$Staking2[i]
} else if(mbase$Output[i]=='WH'){
mbase$PL2[i] <- mbase$COMOdds[i] * mbase$Staking2[i] * 0.5
} else if(mbase$Output[i]=='VOID'){
mbase$PL2[i] <- 0
} else if(mbase$Output[i]=='LH'){
mbase$PL2[i] <- mbase$COMOdds[i] * mbase$Staking2[i] * -0.5
} else if(mbase$Output[i]=='L'){
mbase$PL2[i] <- mbase$COMOdds[i] * -mbase$Staking2[i]
} else {
mbase$PL2[i] <- NA
}
}; rm(i)
mbase[1:100,-c(6:9,12:13)]
```
Summary of the betting
```{r}
suppressMessages(library('formattable'))
suppressMessages(library('tidyverse'))
Summary <- data.frame(
Category = c('No of Matches','Total PL','No of Bets','No of Won Bets','No of Voided Bets','No of Lose Bets',
'Staking','Won Bets Stakes','Voided Stakes','Lose Bets Stakes'),
Bets_Wf_Overround = c(nrow(mbase), sum(mbase$PL), length(mbase[mbase$Staking>0,]$Staking), length(mbase[mbase$PL>0,]$PL),
length(mbase[mbase$PL==0 & mbase$Staking>0,]$PL), length(mbase[mbase$PL<0,]$PL),
sum(mbase[mbase$Staking>0,]$Staking), sum(mbase[mbase$PL>0,]$PL),
sum(mbase[mbase$PL==0 & mbase$Staking>0,]$Staking), sum(mbase[mbase$PL<0,]$PL)),
Bets_No_Overround = c(nrow(mbase), sum(mbase$PL2), length(mbase[mbase$Staking2>0,]$Staking2), length(mbase[mbase$PL2>0,]$PL2),
length(mbase[mbase$PL2==0 & mbase$Staking2>0,]$PL2), length(mbase[mbase$PL2<0,]$PL2),
sum(mbase[mbase$Staking2>0,]$Staking2), sum(mbase[mbase$PL2>0,]$PL2),
sum(mbase[mbase$PL2==0 & mbase$Staking2>0,]$Staking2), sum(mbase[mbase$PL2<0,]$PL2)))
# Total Profit & Lose on different companies
COMPL <- data.frame(PL=sapply(split(mbase,mbase$COM), function(x) sum(x$PL)), PL2=sapply(split(mbase,mbase$COM), function(x) sum(x$PL2)))
Summary %>% formattable %>% as.htmlwidget
```
```{r}
COMPL %>% tbl_df %>% mutate(PL = currency(PL), PL.R = percent(PL/sum(PL)), PL2 = currency(PL2), PL2.R = percent(PL2/sum(PL2))) %>% formattable %>% as.htmlwidget
```