-
Notifications
You must be signed in to change notification settings - Fork 38
/
fixutils.js
139 lines (105 loc) · 3.79 KB
/
fixutils.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
var SOHCHAR = exports.SOHCHAR = String.fromCharCode(1);
exports.getCurrentUTCTimeStamp = function(){ return getUTCTimeStamp(new Date()); }
var getUTCTimeStamp = exports.getUTCTimeStamp = function(datetime){
var timestamp = datetime || new Date();
var year = timestamp.getUTCFullYear();
var month = timestamp.getUTCMonth() +1;
var day = timestamp.getUTCDate();
var hours = timestamp.getUTCHours();
var minutes = timestamp.getUTCMinutes();
var seconds = timestamp.getUTCSeconds();
var millis = timestamp.getUTCMilliseconds();
if (month < 10) { month = '0' + month;}
if (day < 10) { day = '0' + day;}
if (hours < 10) { hours = '0' + hours;}
if (minutes < 10) { minutes = '0' + minutes;}
if (seconds < 10) { seconds = '0' + seconds;}
if (millis < 10) {
millis = '00' + millis;
} else if (millis < 100) {
millis = '0' + millis;
}
var ts = [year, month, day, '-' , hours, ':' , minutes, ':' , seconds, '.' , millis].join('');
return ts;
}
var checksum = exports.checksum = function(str){
var chksm = 0;
for (var i = 0; i < str.length; i++) {
chksm += str.charCodeAt(i);
}
chksm = chksm % 256;
var checksumstr = '';
if (chksm < 10) {
checksumstr = '00' + (chksm + '');
}
else if (chksm >= 10 && chksm < 100) {
checksumstr = '0' + (chksm + '');
}
else {
checksumstr = '' + (chksm + '');
}
return checksumstr;
}
//TODO change name to converMapToFIX
var convertRawToFIX = exports.convertRawToFIX = function(map){
return convertToFIX(map, map[8], map[52], map[49], map[56], map[34]);
}
var convertToFIX = exports.convertToFIX = function(msgraw, fixVersion, timeStamp, senderCompID, targetCompID, outgoingSeqNum){
//sys.log('c2F:'+JSON.stringify(msgraw));
//defensive copy
var msg = {};
for (var tag in msgraw) {
if (msgraw.hasOwnProperty(tag)) msg[tag] = msgraw[tag];
}
delete msg['9']; //bodylength
delete msg['10']; //checksum
//TODO why is there a timestamp when a timeStamp is passed in?
//var timestamp = new Date();
var headermsgarr = [];
var bodymsgarr = [];
var trailermsgarr = [];
//msg['8'] = fixVersion; //fixversion
//msg['52'] = timeStamp; //timestamp
//msg['49'] = senderCompID; //sendercompid
//msg['56'] = targetCompID; //targetcompid
//msg['34'] = outgoingSeqNum; //seqnum
headermsgarr.push('35=' + msg['35'] , SOHCHAR);
headermsgarr.push('52=' + timeStamp , SOHCHAR);
headermsgarr.push('49=' + senderCompID , SOHCHAR);
headermsgarr.push('56=' + targetCompID , SOHCHAR);
headermsgarr.push('34=' + outgoingSeqNum , SOHCHAR);
for (var tag in msg) {
if (msg.hasOwnProperty(tag)
&& tag !== '8'
&& tag !== '9'
&& tag !== '35'
&& tag !== '10'
&& tag !== '52'
&& tag !== '49'
&& tag !== '56'
&& tag !== '34'
&& tag !== ""
) bodymsgarr.push(tag, '=' , msg[tag] , SOHCHAR);
}
var headermsg = headermsgarr.join('');
var trailermsg = trailermsgarr.join('');
var bodymsg = bodymsgarr.join('');
var outmsgarr = [];
outmsgarr.push('8=' , fixVersion , SOHCHAR);
outmsgarr.push('9=' , (headermsg.length + bodymsg.length + trailermsg.length) , SOHCHAR);
outmsgarr.push(headermsg);
outmsgarr.push(bodymsg);
outmsgarr.push(trailermsg);
var outmsg = outmsgarr.join('');
outmsg += '10=' + checksum(outmsg) + SOHCHAR;
return outmsg;
}
var convertToMap = exports.convertToMap = function(msg) {
var fix = {};
var keyvals = msg.split(SOHCHAR);
for (var kv in Object.keys(keyvals)) {
var kvpair = keyvals[kv].split('=');
fix[kvpair[0]] = kvpair[1];
}
return fix;
}