-
Notifications
You must be signed in to change notification settings - Fork 0
/
ex-read-and-split.js
76 lines (64 loc) · 1.92 KB
/
ex-read-and-split.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
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
var dataOut = [];
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])];
dataOut.push(tEle);
}
}
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);
}
// Callback for data once available
function ParseData(err,data)
{
var DataLines = data.split("\n");
var HeaderLine = DataLines.shift();
var ElapReadAndSplit = Elapsed();
var StockData = ParseStockCSV(DataLines);
var ElapBuildOut = Elapsed();
var tAvg = CalcAverageOpen(StockData);
var ElapCalcAvg = Elapsed();
var ElapTot = ElapReadAndSplit + ElapBuildOut + ElapCalcAvg;
// Generate Timing
console.log("number of lines=" + DataLines.length);
console.log("Elapsed Read and Split=" + ElapReadAndSplit);
console.log("BuildParsedArray=" + ElapBuildOut);
console.log("calcAvgOpen=" + ElapCalcAvg);
console.log("total Run= " + ElapTot);
console.log("calculated average open=" + tAvg);
return StockData;
}
fs.readFile('2014.M1.csv', "ascii", ParseData);