-
Notifications
You must be signed in to change notification settings - Fork 0
/
15PointsMovingAverage.py
135 lines (102 loc) · 3.61 KB
/
15PointsMovingAverage.py
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
"""
Created on Fri Mar 12 05:53:35 2021
@author: davsu428
"""
import datetime
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
dflist=[]
seasonst=[]
for year in range(5,21,1):
if year<9:
yeartext='0'+str(year)+'0'+str(year+1)
yeartext2='0'+str(year)+'-0'+str(year+1)
elif year==9:
yeartext='0910'
yeartext2='09-10'
else:
yeartext=str(year)+str(year+1)
yeartext2=str(year)+'-'+str(year+1)
seasonst=seasonst+[yeartext2]
#For England
performance_year = pd.read_csv("https://www.football-data.co.uk/mmz4281/"+yeartext+"/E0.csv",delimiter=',')
#For Spain
#performance_year = pd.read_csv("https://www.football-data.co.uk/mmz4281/"+yeartext+"/SP1.csv",delimiter=',')
dflist=dflist+[performance_year]
performance= pd.concat(dflist)
teams=list(set(performance['AwayTeam']))
ma=38
teamdf=dict()
for team in teams:
game=0
rows=[]
matches=performance[((performance['AwayTeam']==team) | (performance['HomeTeam']==team))]
for i,match in matches.iterrows():
game=game+1
#matchdate = datetime.datetime.strptime(match['Date'],"%d/%m/%y")
if match['AwayTeam']==team:
goalsfor=match['FTAG']
goalsagainst=match['FTHG']
oddsfor=match['PSA']
if match['FTR']=='A':
points=3
profit=oddsfor-1
elif match['FTR']=='D':
points=1
profit=-1
else:
points=0
profit=-1
if match['HomeTeam']==team:
goalsfor=match['FTHG']
goalsagainst=match['FTAG']
oddsfor=match['PSH']
if match['FTR']=='H':
points=3
profit=oddsfor-1
elif match['FTR']=='D':
points=1
profit=-1
else:
points=0
profit=-1
goaldiff=goalsfor-goalsagainst
rows.append([matchdate,goalsfor,goalsagainst,goaldiff,points,profit,season,game])
ma_goaldiff=np.convolve(goaldiff, np.ones(10)/10, mode='valid')
df = pd.DataFrame(rows, columns=["Date", "For","Against","Difference","Points","Profit","Season","Game"])
#df = df.sort_values('Date', ascending=True)
df['PointsRA'] = df['Points'].rolling(window=ma, win_type='triang').mean()
teamdf[team]=df
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
fig,ax=plt.subplots(num=1)
comparison1='Man City'
comp_color1='lightblue'
comparison3='Liverpool'
comp_color3='red'
comparison2='Man United'
comp_color2='darkred'
#
#comparison1='Real Madrid'
#comp_color1='blue'
#comparison2='Barcelona'
#comp_color2='darkred'
#comparison3='Ath Madrid'
#comp_color3='red'
ax.plot(teamdf[comparison1]['Game'], teamdf[comparison1]['PointsRA'], linewidth=2, linestyle='-',color=comp_color1)
ax.plot(teamdf[comparison2]['Game'], teamdf[comparison2]['PointsRA'], linewidth=2 , linestyle='-',color=comp_color2)
ax.plot(teamdf[comparison3]['Game'], teamdf[comparison3]['PointsRA'], linewidth=2 , linestyle='-',color=comp_color3)
ax.set_title(str(ma) + ' game moving average')
plt.gcf().autofmt_xdate()
ax.legend([comparison1,comparison2,comparison3])
ax.set_ylim(1,3.2)
ax.set_xticks(np.arange(0,max(teamdf[comparison2]['Game']),38))
ax.set_xticklabels(seasonst)
ax.set_xlim(0,max(teamdf[comparison2]['Game'])+40)
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
fig.show()
ax.set_ylabel('Rolling Average Points Per Game')
ax.set_xlabel('Season')
fig.savefig('dip.png', dpi=None, bbox_inches="tight")