-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
executable file
·114 lines (97 loc) · 2.9 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
import csv
import sys
from datetime import *
filename = ''
SESSIONS = []
FROM = None
UNTIL = None
def transform(diffPeriod):
global SESSIONS, filename, FROM, UNTIL
timestamps = []
prices = []
differences = []
for i in range(len(SESSIONS)):
session = SESSIONS[i]
close = session['Close']
timestamps.append(session['Date'].strftime("%Y-%m-%d %H:%M:%S"))
prices.append(close)
if i == 0:
differences.append([''] * len(SESSIONS))
else:
diffs = [''] * len(SESSIONS)
if diffPeriod != None:
fromDiffIdx = i-diffPeriod-1 if i-diffPeriod-1 > 0 else 0
else:
fromDiffIdx = 0
prevSessionsToDiff = SESSIONS[fromDiffIdx:i]
c = 0
for idiff in reversed(range(len(prevSessionsToDiff))):
prevSession = prevSessionsToDiff[idiff]
diffs[c] = close / prevSession['Close']
c = c+1
differences.append(diffs)
print("Writing file now: {}".format(filename + '_converted.csv'))
timestampsWritten = False
closesWritten = False
with open(filename + '_converted.csv', mode='w') as csv_file:
writer = csv.writer(csv_file, delimiter=';', quotechar='"', quoting=csv.QUOTE_MINIMAL)
if not timestampsWritten:
writer.writerow(timestamps)
timestampsWritten = True
if not closesWritten:
writer.writerow(prices)
closesWritten = True
for x in range(len(prices)):
ls = []
for y in range(len(differences)):
ls.append(differences[y][x])
writer.writerow(ls)
def main():
global SESSIONS, filename, FROM, UNTIL
if len(sys.argv) < 2:
print("You must provide the input CSV file")
exit(0)
if len(sys.argv) >= 4:
try:
FROM = datetime.strptime(sys.argv[2], "%Y-%m-%d").date()
except ValueError as ve:
print("If you want to specify a FROM date, you should input the date in the format YYYY-MM-DD")
exit(-1)
try:
UNTIL = datetime.strptime(sys.argv[3], "%Y-%m-%d").date()
except ValueError as ve:
print("If you want to specify an UNTIL date, you should input the date in the format YYYY-MM-DD")
exit(-1)
try:
filename = sys.argv[1]
with open(filename, mode='r') as csv_file:
print("Reading...")
csv_reader = csv.DictReader(csv_file, delimiter=';')
for session in csv_reader:
date = session['Date']
dt = date.split(' ')
yr = dt[0][:4]
mo = dt[0][4:6]
da = dt[0][6:8]
hh = dt[1][:2]
mm = dt[1][2:4]
ss = dt[1][4:6]
d = datetime.strptime("{}-{}-{} {}:{}:{}".format(yr, mo, da, hh, mm, ss), "%Y-%m-%d %H:%M:%S")
if FROM != None and d.date() < FROM:
continue
if UNTIL != None and d.date() > UNTIL:
break
SESSIONS.append({
'Date': d,
'Open': float(session['Open']),
'High': float(session['High']),
'Low': float(session['Low']),
'Close': float(session['Close']),
'Volume': int(session['Volume']),
})
except IOError as e:
print("Error reading the file: {}".format(e))
exit(-1)
transform(sys.argv[4] if len(sys.argv) >= 5 else None)
if __name__ == "__main__":
main()