-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
244 lines (224 loc) · 10.4 KB
/
main.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
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
##
# csvToExcel
# AUTHOR: MARCO SELVA
# DATE CREATION 02/12/2022
# VERSION: 2.0
# v2.0: 01/12/2023
# v1.0: 03/06/2022
#
# TODO
#
# ##
from openpyxl import Workbook
from datetime import datetime, timedelta
from itertools import zip_longest
import os, csv, sys
PATH = "./MarketReports/"
EXPPATH = "./ExportReport/"
# functions
def getField(field, content, index=0):
try:
try:
line = content.split(f'{field}:', index + 1)
value = line[index + 1].split(';')[0].split(' - Match Odds')[0].split('\n')[0].strip()
if 'marketName' == field:
dateString = line[index].split('\n')[-1].split(' ')[0]
return value, dateString
else:
return value
except:
return content.split(f'{field} = ', index + 1)[index + 1].split('\n')[0].strip()
except:
return None
def removeOneHour(time):
temp = datetime.strptime(time, "%H:%M:%S")
temp -= timedelta(hours=1)
modified_time_str = temp.strftime("%H:%M:%S")
return modified_time_str
# main
if not os.path.exists(EXPPATH):
try:
os.mkdir(EXPPATH)
except:
print("Export Path not exists and unable to Create")
sys.exit(0)
book = Workbook()
sheet = book.active
sheet.title = 'report'
# columns
sheet.append(["DATE", "TIME", "MARKET", "ACCOUNT", "EXECUTOR", "STRATEGY", "EVENT", "RUNNERS", "", "", 'RUNNER SET',
'RUNNER SET', "NOTE", "S1 A", "S1 B", "S2 A", "S2 B", "S3 A", "S3 B", "S4 A", "S4 B", "S5 A", "S5 B",
"GAME A", "GAME B", "SER", "ID", "TIME", "TYPE", "RUN", "BET", "ODDS", "STAKE", "TOTAL RISK", "COMM",
"NET PL", None, "aParams"] + [None] * 9 + ["bParams"])
# selections params
sheet.append([None] * 7 + ["A", "B", "WINNER", 'A', 'B'] + [None] * 25 + \
[f'aParams{i+1}' for i in range(10)] + [f'bParams{i+1}' for i in range(10)])
for date in os.listdir(PATH):
# files name
datePath = os.path.abspath(os.path.join(PATH, date))
logsPath = os.path.join(datePath, 'Logs')
matchedBetsPath = os.path.join(datePath, 'MatchedBets')
profitAndLossPath = os.path.join(datePath, 'ProfitAndLoss')
logsFiles = []
matchedBetsFiles = []
profitAndLossFiles = []
# iterate over directories looking for files
for logFile in os.listdir(logsPath):
if not logFile.startswith('Log'):
continue
file = logFile.split('_', 1)[1]
if 'Market Closed' in file:
continue
logsFiles.append(os.path.join(logsPath, f'Log_{file}'))
matchedBetsFiles.append(os.path.join(matchedBetsPath, f'MatchedBetsReport_{file}'))
profitAndLossFiles.append(os.path.join(profitAndLossPath, f'ProfitLossReport_{file}'))
# iterate over found market
for logFile, matchedBetFile, profitAndLossFile in zip(logsFiles, matchedBetsFiles, profitAndLossFiles):
with open(logFile) as logger, open(matchedBetFile) as matcher, open(profitAndLossFile) as profiter:
mContent = matcher.read()
# check if have at least 1 bets
#if len(mContent.splitlines()) < 2:
# continue
content = logger.read()
print("File: ", logFile, end='')
try:
marketName, marketDate = getField('marketName', content)
except:
print(" (Skipped)")
continue
print()
# get market info
market = {}
market['runnerA'] = getField('runnerA', content)
market['runnerB'] = getField('runnerB', content)
market['aId'] = getField('aId', content)
market['bId'] = getField('bId', content)
market['aBsp'] = getField('aBsp', content)
market['bBsp'] = getField('bBsp', content)
market['volume'] = getField('volume', content)
market['profit'] = []
# result reader
pReader = csv.reader(profiter.read().splitlines()[1:])
for row in pReader:
market['profit'].append(row[-2:])
totalPoints = content.count('totalStake =')
# set odds and time
market['sets'] = []
d1 = content.split('aFirstSetPrice')[0].split('\n')[-1].split(': ')[0].split(' ')[-1]
d2 = content.split('aSecondSetPrice')[0].split('\n')[-1].split(': ')[0].split(' ')[-1]
# if time is not correct check here
#if d1: d1 = removeOneHour(d1)
#if d2 :d2 = removeOneHour(d2)
market['setPriceDates'] = [None, d1, d2]
market['runnerSets'] = []
market['runnerSets'].append([getField('aFirstSetPrice', content), getField('bFirstSetPrice', content)])
market['runnerSets'].append([getField('aSecondSetPrice', content), getField('bSecondSetPrice', content)])
# selections params
market['aParams'] = [[getField(f'(Shared) for {market["runnerA"]}: aParams{i}', content) for i in range(1,11)]]
market['bParams'] = [[getField(f'(Shared) for {market["runnerB"]}: bParams{i}', content) for i in range(1,11)]]
pointings = {}
i = 0
# iterate over bets point and serve
for points in range(totalPoints):
totalStake = getField('totalStake', content, points)
if totalStake == getField('totalStake', content, points + 1):
continue
gameContent = content.split(f'(Shared) for {market["runnerA"]}: aPoints', points + 1)[-1]
aPoint = getField('aPoints = point', content, points)
pointTime = gameContent.split(': [')[0].split('\n')[-1].split(' ')[-1]
pointings[pointTime] = i
aServing = getField('aServing = serving', content, points)
aGame = getField(f'games', gameContent)
aSet = getField(f'sets', gameContent)
bPoint = getField('bPoints = point', content, points)
bGame = getField(f'games', gameContent, 1)
bSet = getField(f'sets', gameContent, 1)
bServing = getField('bServing = serving', content, points)
#serv = ['A', 'B'][0 if aServing else 1]
serv = ['A', 'B'][0 if int(aServing) else 1]
sets = [[None, None]] * 5
if aSet == '1' and bSet == '1':
sets[0] = ['1', '1']
sets[1] = ['1', '1']
sets[2] = [aGame, bGame]
elif aSet == '1' and bSet == '0':
sets[0] = ['1', '0']
sets[1] = [aGame, bGame]
elif aSet == '0' and bSet == '1':
sets[0] = ['0', '1']
sets[1] = [aGame, bGame]
else:
sets[0] = [aGame, bGame]
aPoint = 'AD' if aPoint == '99' else aPoint
bPoint = 'AD' if bPoint == '99' else bPoint
if aGame == '6' and bGame == '6':
sets.append(['0', '0'])
else:
sets.append([aPoint, bPoint])
sets.append([serv])
market['sets'].append(sets)
i += 1
# sort bets by time
mList = sorted([m for m in csv.reader(mContent.splitlines()[1:])], key=lambda x: x[3])
mList = sorted([m for m in mList], key=lambda x: x[0].split(' ')[1])
# iterate over bets odds and stake
market['stakes'] = {}
i = 0
betsInfo = []
for row in mList:
date = row[0].split(' ')[-1]
flag = row[1][0]
runner = ['A', 'B'][row[2].strip().lower() == market['runnerB'].lower()]
odds = row[3]
stake = row[4]
if market['stakes'].get(date + "__" + odds):
market['stakes'][date + "__" + odds][-1] += float(stake)
continue
i += 1
entry = None
betsInfo.append(date)
if i == 1:
entry = 'OPEN'
market['stakes'][date + "__" + odds] = [i, date, entry, runner, flag, odds, float(stake)]
inserted = []
i = 0
# reorder bets
for stake in market['stakes'].values():
if betsInfo.count(stake[1]) > 1 and stake[1] not in inserted:
[market['sets'].insert(i + x + 1, market['sets'][i]) for x in range(betsInfo.count(stake[1]) - 1)]
inserted.append(stake[1])
i += 1
final = False
# save data in excel
for data in zip_longest([marketDate], [marketName, market['volume']],
[market['runnerA'], market['aBsp'], market['aId']],
[market['runnerB'], market['bBsp'], market['bId']], market['sets'],
market['stakes'].values(), market['profit'], market['aParams'], market['bParams'] , market['setPriceDates'], market['runnerSets']):
setPointList = []
resultList = data[6] or []
pointList = data[4]
if pointList:
[setPointList.extend(set_) for set_ in pointList]
setPointList = setPointList or [None] * 13
betsInfo = data[5]
if not betsInfo and not pointList and not resultList and not final:
betsInfo = ([None] * 2) + ['FINAL'] + [None] * 6
final = True
if not betsInfo:
betsInfo = [None] * 7
row = [data[0], data[-2] or None, data[1], None, None, None, None, data[2], data[3], None] + (
data[-1] or [None, None])
row += [None] + setPointList + betsInfo + [None] + resultList + [None]
row += (data[7] or ([None] * 10)) + (data[8] or ([None] * 10))
sheet.append(row)
if not final:
sheet.append(([None] * 28) + ['FINAL'])
# save file
dateOfRun = datetime.now().strftime('%d_%m_%Y_%H_%M')
outFile = os.path.join(os.path.abspath(EXPPATH), f'REPORT_{dateOfRun}.xlsx')
print("\nSaved to:", outFile)
book.save(outFile)
print("\n\n -- STATS -- \n")
print("\nLog market:", len(logsFiles))
print("Matched market:", len(matchedBetsFiles))
print("Traded market:", len(profitAndLossFiles))