-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathmovingAverage.py
More file actions
128 lines (116 loc) · 3.54 KB
/
movingAverage.py
File metadata and controls
128 lines (116 loc) · 3.54 KB
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
# mvayg.py : moving average calculation
# by: Albert Lam
# assume conditions
import os
import myfunctions as mf
def mvavg(cc,ddir,adir,range):
# CALCUOLATING MOVING AVERAGES
#
# Set to calulate the moving average starting with 5 days data and wihh
# increment of 5 days till the tartget maxium date average is reached.
malow = range['lower']
mvd = malow # lower limits of moving data points
maup = range['upper'] # upper limit of moving average datapoints
inc = range['skip'] # increment
din = ddir + cc + '.csv'
inp = adir + cc + 'mvg.csv'
oup = adir + cc + 'tmp.csv'
# Collect the infromation and write it out to the output files
# from the trade data collected from the .csv
inf = open(din, 'r')
ouf = open(inp, 'w')
l = inf.readline()
l = l[l.index('date'):l.index(',ask')]+'\n'
ouf.write(l)
t = []
for l in inf:
t = l.split(',')
l = '{},{},{},{},{}\n'.format(t[1],t[2],t[3],t[4],t[5])
ouf.write(l)
inf.close()
ouf.close()
while mvd < maup:
inf = open(inp, 'r')
l = inf.readline() # read header
ouf = open(oup, 'w')
smvd = str(mvd)
l = l.strip() + ',mavgh'+smvd + ',mavgl'+smvd + ',mavgc'+smvd+'\n'
ouf.write(l) # write header
valh = []
vall = []
valc = []
c = 0
normal = True
while c < (mvd - 1):
l = inf.readline()
if len(l) > 1:
ouf.write(l)
t = l.split(',')
valh.append(float(t[2]))
vall.append(float(t[3]))
valc.append(float(t[4]))
c = c + 1
else:
normal = False
break
if normal:
for l in inf:
t = l.split(',')
valh.append(float(t[2]))
vall.append(float(t[3]))
valc.append(float(t[4]))
avgh = mf.avg(valh)
avgl = mf.avg(vall)
avgc = mf.avg(valc)
l = l.strip() + ',{0:.3f},{1:.3f},{2:.3f}\n'.format(avgh,avgl,avgc)
ouf.write(l)
valh.pop(0)
vall.pop(0)
valc.pop(0)
ouf.close()
inf.close()
mvd = mvd + inc
os.remove(inp)
while os.path.isfile(inp): continue
os.rename(oup, inp)
else:
maup = mvd
ouf.close()
inf.close()
os.remove(oup)
#
# EVALUATE AND ANALIZING THE CALCULATED MOVING AVERAGE
#
oup = adir + cc + 'mvana.csv'
inf = open(inp, 'r')
l = inf.readline()
l = 'date,change'+ l[l.index(',mavg'):]
ouf = open(oup, 'w')
ouf.write(l)
mv1 = []
mv2 = []
mv3 = []
l = inf.readline()
mv1 = l.strip().split(',')
l = inf.readline()
mv2 = l.strip().split(',')
for l in inf:
mv3 = l.strip().split(',')
ct = (len(mv2) - 5) // 3
lo = ''
rn = 0
rt = (float(mv3[4]) - float(mv2[4]))
while (rn <= ct) and (len(mv1) > (3 * rn + 6) ):
po = 2 + rn * 3
ph = float(mv2[po]) - float(mv1[po])
pl = float(mv2[po+1]) - float(mv1[po+1])
pc = float(mv2[po+2]) - float(mv1[po+2])
lo = lo + ',{0:.3f},{1:.3f},{2:.3f}'.format(ph,pl,pc)
rn = rn + 1
if len(lo) > 0:
lo = '{0},{1:.3f}'.format(mv3[0],rt)+lo+'\n'
ouf.write(lo)
mv1 = mv2
mv2 = mv3
ouf.close()
inf.close()