-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathex_csv_bar_parse.js
155 lines (130 loc) · 4.15 KB
/
ex_csv_bar_parse.js
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
var readline = require('readline');
var fs = require('fs');
var util = require('util');
function curr_ms()
{
return (new Date()).getMilliseconds();
}
var fs = require('fs');
var LastTime = new Date().getTime();
function Elapsed()
{
var tStamp = new Date().getTime()
var tElap = tStamp - LastTime;
LastTime = tStamp;
return tElap;
}
// Convert the simple array of lines into a array
// of pre-parsed float and int values to allow rapid
// multiple ieteration with indicators that need fast
// access to values as numbers.
function ParseStockCSV(DataLines)
{
// inputFormat = DateTime,day, open, close, high, low, volume
return dataOut;
}
function CalcAverage(DataLines, columnNum)
{
var vTotal = 0.0;
for (aLineNum in DataLines)
{
vTotal = vTotal + DataLines[aLineNum][columnNum];
}
var vAvg = vTotal / DataLines.length;
return vAvg;
}
function CalcAverageOpen(DataLines)
{
return CalcAverage(DataLines, 2);
}
function calcAverageX(tarr)
{
var vtotal = 0.0;
var tlen = tarr.length;
for (var ndx = 0; ndx < tlen; ndx++)
{
vtotal += tarr[ndx];
}
var tavg = vtotal / tlen;
return tavg
}
// Callback for data once available
function ParseDataPreAlloc(err,data)
{
var CloseColumn = 3;
var ElapRead = Elapsed();
var DataLines = data.split("\n");
var HeaderLine = DataLines.shift();
var ElapSplit = Elapsed();
var numRows = DataLines.Length;
var dateTime = new Array(numRows);
var day = new Array(numRows);
var open = new Array(numRows);
var close = new Array(numRows);
var high = new Array(numRows);
var low = new Array(numRows);
var volume = new Array(numRows);
var ElapPreAllocArray = Elapsed();
for (var lineNum in DataLines)
{
var aLine = DataLines[lineNum].trim()
if (aLine.length < 3) continue;
var FldArr = aLine.split(",");
dateTime[lineNum] = FldArr[0];
day[lineNum] = FldArr[1];
open[lineNum] = parseFloat(FldArr[2]);
close[lineNum] = parseFloat(FldArr[3]);
high[lineNum] = parseFloat(FldArr[4]);
low[lineNum] = parseFloat(FldArr[5]);
volume[lineNum] = parseInt(FldArr[6]);
}
var ElapBuildOut = Elapsed();
var tAvg = calcAverageX(close);
var ElapCalcAvg = Elapsed();
var ElapTot = ElapRead + ElapSplit + ElapBuildOut; // average explicitly ignored
// Generate Timing
console.log("number of lines=" + DataLines.length);
console.log("Elapsed Read =" + ElapRead);
console.log("Elapsed Split=" + ElapSplit);
console.log("pre-alloc array=" + ElapPreAllocArray);
console.log("BuildParsedArray=" + ElapBuildOut);
console.log("calcAvgClose=" + tAvg);
console.log("total Run= " + ElapTot);
return [dateTime, day, open, close, high, low, volume];
}
// Callback for data once available
function ParseData(err,data)
{
var ElapRead = Elapsed();
var DataLines = data.split("\n");
var HeaderLine = DataLines.shift();
var ElapSplit = Elapsed();
var StockData = [];
for (var lineNum in DataLines)
{
var aLine = DataLines[lineNum].trim()
if (aLine.length > 3)
{
FldArr = aLine.split(",");
tEle = [FldArr[0], FldArr[1], parseFloat(FldArr[2]), parseFloat(FldArr[3]), parseFloat(FldArr[4]), parseFloat(FldArr[5]), parseInt(FldArr[6])];
StockData.push(tEle);
}
}
var ElapBuildOut = Elapsed();
var tAvg = CalcAverageOpen(StockData);
var ElapCalcAvg = Elapsed();
var ElapTot = ElapRead + ElapSplit + ElapBuildOut; // average explicitly ignored
// Generate Timing
console.log("number of lines=" + DataLines.length);
console.log("Elapsed Read =" + ElapRead);
console.log("Elapsed Split=" + ElapSplit);
console.log("BuildParsedArray=" + ElapBuildOut);
console.log("calcAvgOpen=" + ElapCalcAvg);
console.log("total Run= " + ElapTot);
console.log("calculated average open=" + tAvg);
return StockData;
}
start = Elapsed(); // start the clock after all the libraries are loaded and parsed
fs.readFile('2014.M1.csv', "ascii", ParseData);
start = Elapsed(); // start the clock after all the libraries are loaded and parsed
//fs.readFile('c:/JOE/stock/JTDATA/symbols/SPY/2012.M1.csv', "ascii", ParseDataPreAlloc);