-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathforex_bar.py
More file actions
77 lines (58 loc) · 1.6 KB
/
forex_bar.py
File metadata and controls
77 lines (58 loc) · 1.6 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
import time
def curr_ms():
return time.time() * 1000.0
def elap(msg, start, end):
print "\n",msg, " elap=", end - start
def simpAvg(nptr, startNdx, endNdx):
numEle = (endNdx - startNdx) + 1
if numEle < 1:
return 0
total = 0.0
ndx = startNdx
while ndx <= endNdx:
total += nptr[ndx]
ndx += 1
return total / numEle
def sma(nptr, startNdx, endNdx, numPer, tout):
total = 0.0
ndx = startNdx
while ndx <= endNdx:
tbeg = max(1, ndx - numPer)
tout[ndx] = simpAvg(nptr, tbeg, ndx)
ndx += 1
class forexBars:
def __init__(self):
self.dtime = []
self.open = []
self.close = []
self.high = []
self.low = []
self.volume = []
startms = curr_ms()
f = open("2014.M1.csv")
#a = f.readlines()
#endreadms = curr_ms()
#elap("finished read lines", startms, endreadms)
begsplit = curr_ms()
header = f.readline()
tbars = forexBars()
for aline in f:
flds = aline.split(",")
tbars.dtime.append(flds[0])
tbars.open.append(float(flds[1]))
tbars.close.append(float(flds[2]))
tbars.high.append(float(flds[3]))
tbars.low.append(float(flds[4]))
tbars.volume.append(long(float(flds[5])))
endsplit = curr_ms()
numRec = len(tbars.open)
elap("finished split lines to flds" , begsplit, endsplit)
print "# rows=", numRec
startsmaclone = curr_ms()
smaopen = list(tbars.open)
smaclose = list(tbars.open)
elap("allocate sma memory", startsmaclone, curr_ms())
startsma = curr_ms()
sma(tbars.open, 0, numRec-1, 1000, smaopen)
sma(tbars.close, 0, numRec-1, 1000, smaclose)
elap("finished sma", startsma, curr_ms())