-
Notifications
You must be signed in to change notification settings - Fork 0
/
fix.js
300 lines (268 loc) · 6.5 KB
/
fix.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
/**
* NodeFIX
* FIX protocol message builder and reader
*
* Version: 1.0
*
* Copyright (c) 2014 Ahmad Said
* NodeFIX is currently available for use in all personal or commercial projects
* under both the MIT (http://www.opensource.org/licenses/mit-license.php) and GPL
* version 2.0 (http://www.gnu.org/licenses/gpl-2.0.html) licenses. This means that you can
* choose the license that best suits your project and use it accordingly.
*
* Although not required, the author would appreciate an email letting him
* know of any substantial use of NodeFIX. You can reach the author at:
* mody at amaroks dot com
*
* */
const moment = require('moment');
module.exports = (function() {
function Fix() {
// incomplete FIX dict
this.DICT = {
BeginString: 8,
BodyLength: 9,
MsgType: 35,
SenderCompID: 49,
TargetCompID: 56,
MsgSeqNum: 34,
PossDupFlag: 43,
PossResend: 97,
SendingTime: 52,
OrigSendingTime: 122,
TestReqID: 112,
BeginSeqNo: 7,
EndSeqNo: 16,
RefSeqNum: 45,
RefTagID: 371,
RefMsgType: 372,
SessionRejectReason: 373,
Text: 58,
GapFillFlag: 123,
NewSeqNo: 36,
OrderID: 37,
SecondaryExecID: 527,
ClOrdID: 11,
OrigClOrdID: 41,
OrdStatusReqID: 790,
Symbol: 55,
ExecID: 17,
ExecType: 150,
OrdStatus: 39,
OrdRejReason: 103,
Account: 1,
Side: 54,
OrdType: 40,
Price: 44,
StopPx: 99,
TimeInForce: 59,
LastQty: 32,
LastPx: 31,
LeavesQty: 151,
CumQty: 14,
AvgPx: 6,
TransactTime: 60,
SettlDate: 64,
CxlRejResponseTo: 434,
CxlRejReason: 102,
EncryptMethod: 98,
HeartBtInt: 108,
ResetSeqNumFlag: 141,
MaxMessageSize: 383,
Username: 553,
Password: 554,
ExecInst: 18,
TradeRequestID: 568,
TradeRequestType: 569,
SubscriptionRequestType: 263,
LastRptRequested: 912,
TradeDate: 75,
TotNumTradeReports: 748,
TradeRequestResult: 749,
TradeRequestStatus: 750,
CheckSum: 10,
SecurityID: 48,
SecurityIDSource: 22,
OrderQty: 38,
NoDates: 580,
NoSides: 552,
MDEntryType: 269,
MDReqID: 262,
MarketDepth: 264,
MDUpdateType: 265,
NoRelatedSym: 146,
NoMDEntryTypes: 267,
NoMDEntries: 268,
MDEntryPx: 270,
MDEntrySize: 271,
MDEntryDate: 272,
MDEntryTime: 273,
RawDataLength: 95,
RawData: 96,
CancelOnDisconnect: 35002,
SelfTradePrevention: 7928
};
// SOH delimiter
this.SOH = "\x01";
// empty string
this.E_STR = "";
// trimmed message
this.TRIMED = this.E_STR;
// SeqNum generator
this.SEQNUM = 0;
// ClOrdID for orders
this.CLORDID_PREFIX = "CLORD";
}
/**
* FIX message constuctor
*
* @param {object} obj
* @param {boolean} T_SOH
* @returns {String}
*/
Fix.prototype.message = function(obj, add_tsoh) {
var message = this.E_STR, i = 0;
// clear any previous values
this.TRIMED = this.E_STR;
for (var key in obj) {
message += this.DICT[key] + "=" + obj[key];
i++;
if (add_tsoh) {
message += this.SOH;
}
var not = [this.DICT.BeginString, this.DICT.BodyLength, this.DICT.CheckSum];
if (not.indexOf(this.DICT[key]) === -1) {
this.TRIMED += this.DICT[key] + "=" + obj[key] + this.SOH;
}
}
message = message.replace('%l', this._bodyLength());
message += this.DICT.CheckSum + '=' + this._checkSum(message, message.length) + this.SOH;
return message;
};
Fix.prototype._bodyLength = function() {
return this.TRIMED.length;
};
/**
* FIX message checksum calculator
*
* @param {String} buff
* @param {Number} buffLength
* @returns {Number}
*/
Fix.prototype._checkSum = function(buff, buffLength) {
for (var idx = 0, cks = 0; idx < buffLength; cks += this._ord(buff[idx++])) {
}
var sum = cks % 256;
switch (sum.toString().length) {
case 1:
sum = '00' + sum;
break;
case 2:
sum = '0' + sum;
break;
}
return sum;
};
Fix.prototype._ord = function(str) {
var code = str.charCodeAt(0);
// High surrogate (could change last hex to 0xDB7F to treat high private surrogates as single characters)
if (0xD800 <= code && code <= 0xDBFF) {
var hi = code;
if (str.length === 1) {
return code;
// we could also throw an error as it is not a complete character, but someone may want to know
}
var low = str.charCodeAt(1);
return ((hi - 0xD800) * 0x400) + (low - 0xDC00) + 0x10000;
}
// Low surrogate
if (0xDC00 <= code && code <= 0xDFFF) {
return code;
// we could also throw an error as it is not a complete character, but someone may want to know
}
return code;
};
/**
* FIX message parser
*
* @param {string} fixString
* @returns {Array}
*/
Fix.prototype.read = function(fixString) {
if (!fixString) {
throw new Error("Please provide FIX message");
}
var itemsArray = fixString.split(/\x01/);
var flippedDic = this._flipp(this.DICT);
var newObject = {};
itemsArray.forEach(function(value) {
var item = value.split("=");
if (typeof flippedDic[item[0]] !== 'undefined') {
if (typeof newObject[flippedDic[item[0]]] !== 'undefined') {
if (typeof newObject[flippedDic[item[0]]] === 'object') {
newObject[flippedDic[item[0]]].push(item[1]);
} else {
newObject[flippedDic[item[0]]] = [
newObject[flippedDic[item[0]]],
item[1]
];
}
} else {
newObject[flippedDic[item[0]]] = item[1];
}
} else {
if (item[0] !== "") {
newObject[item[0]] = item[1];
}
}
});
return newObject;
};
/**
* resets sequence number
* @returns {undefined}
*/
Fix.prototype.resetSeq = function(newSeqNum) {
this.SEQNUM = newSeqNum;
};
Fix.prototype._flipp = function(trans) {
var key, tmp_ar = {};
// Duck-type check for our own array()-created PHPJS_Array
if (trans && typeof trans === 'object' && trans.change_key_case) {
return trans.flip();
}
for (key in trans) {
if (!trans.hasOwnProperty(key)) {
continue;
}
tmp_ar[trans[key]] = key;
}
return tmp_ar;
};
/**
* generates a sequence number
* @returns {Number}
*/
Fix.prototype.seqNum = function() {
this.SEQNUM++;
return this.SEQNUM;
};
Fix.prototype.lastSeqNum = function() {
return this.SEQNUM;
};
Fix.prototype.clOrdId = function() {
return this.CLORDID_PREFIX+moment().utc().format("YYYYMMDD-HHmmssSSS");
};
Fix.prototype.internalsplit = function(msgs) {
var output = []
var separateMsgs = msgs.split("8=FIX.4.4");
separateMsgs.forEach(function(value) {
if (value.length > 0){
value= "8=FIX.4.4" + value
output.push(value)
}
});
return output;
};
return new Fix();
})();