-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMySqlFix.js
More file actions
384 lines (344 loc) · 13 KB
/
Copy pathMySqlFix.js
File metadata and controls
384 lines (344 loc) · 13 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
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
"use strict";
process.title = 'bbbbb';
var fs = require('fs');
var colors = require('colors');
var FixProtocol = require('./lib/protocol.js'),
tls = require('tls'),
moment = require('moment');
var webSocketsServerPort = 1337;// websocket and http servers
var webSocketServer = require('websocket').server;
var http = require('http');
var server = http.createServer(function(request, response) {
});
server.listen(webSocketsServerPort, function() {
console.log((new Date()) + " Server is listening on port "
+ webSocketsServerPort);
});
var wsServer = new webSocketServer({
// WebSocket server is tied to a HTTP server. WebSocket
// request is just an enhanced HTTP request. For more info
// http://tools.ietf.org/html/rfc6455#page-6
httpServer: server
});
// This callback function is called every time someone
// tries to connect to the WebSocket server
wsServer.on('request', function(request) {
console.log((new Date()) + ' Connection from origin '
+ request.origin + '.'); // accept connection - you should check 'request.origin' to
// make sure that client is connecting from your website
// (http://en.wikipedia.org/wiki/Same_origin_policy)
var connection = request.accept(null, request.origin);
});
// FIX account credentials
var host = 'Yourfix.com';
var port = 443;
var username = 'tudak';
var password = 'sudak';
var TCompID = 'LMXBLM'; //LMXBDM - demo
var protocol = new FixProtocol();
var loginMessage = protocol.encode({
BeginString: 'FIX.4.4',
BodyLength: '%l',
MsgType: 'A',
MsgSeqNum: protocol.seqNum(),
SenderCompID: username,
SendingTime: moment().subtract(0, "hours").format("YYYYMMDD-HH:mm:ss.SSS"),
TargetCompID: TCompID,
Username: username,
Password: password,
EncryptMethod: 0,
HeartBtInt: 30,
ResetSeqNumFlag: 'Y'
}, true);
var EURUSD_Message = protocol.encode({
BeginString: 'FIX.4.4',
BodyLength: '%l',
MsgType: 'V',
MsgSeqNum: protocol.seqNum(),
SenderCompID: username,
SendingTime: moment().subtract(0, "hours").format("YYYYMMDD-HH:mm:ss.SSS"),
TargetCompID: TCompID,
MDReqID: 'EURUSD',
SubscriptionRequestType: 1,
MarketDepth: 1,
MDUpdateType: 0,
NoRelatedSym: 1,
SecurityID: 4001,
SecurityIDSource: 8,
NoMDEntryTypes: 2,
MDEntryType: "0\x01269=1"
}, true);
var GBPUSD_Message = protocol.encode({
BeginString: 'FIX.4.4',
BodyLength: '%l',
MsgType: 'V',
MsgSeqNum: protocol.seqNum(),
SenderCompID: username,
SendingTime: moment().subtract(0, "hours").format("YYYYMMDD-HH:mm:ss.SSS"),
TargetCompID: TCompID,
MDReqID: 'GBPUSD',
SubscriptionRequestType: 1,
MarketDepth: 1,
MDUpdateType: 0,
NoRelatedSym: 1,
SecurityID: 4002,
SecurityIDSource: 8,
NoMDEntryTypes: 2,
MDEntryType: "0\x01269=1"
}, true);
var USDJPY_Message = protocol.encode({
BeginString: 'FIX.4.4',
BodyLength: '%l',
MsgType: 'V',
MsgSeqNum: protocol.seqNum(),
SenderCompID: username,
SendingTime: moment().subtract(0, "hours").format("YYYYMMDD-HH:mm:ss.SSS"),
TargetCompID: TCompID,
MDReqID: 'USDJPY',
SubscriptionRequestType: 1,
MarketDepth: 1,
MDUpdateType: 0,
NoRelatedSym: 1,
SecurityID: 4004,
SecurityIDSource: 8,
NoMDEntryTypes: 2,
MDEntryType: "0\x01269=1"
}, true);
var GBPJPY_Message = protocol.encode({
BeginString: 'FIX.4.4',
BodyLength: '%l',
MsgType: 'V',
MsgSeqNum: protocol.seqNum(),
SenderCompID: username,
SendingTime: moment().subtract(0, "hours").format("YYYYMMDD-HH:mm:ss.SSS"),
TargetCompID: TCompID,
MDReqID: 'GBPJPY',
SubscriptionRequestType: 1,
MarketDepth: 1,
MDUpdateType: 0,
NoRelatedSym: 1,
SecurityID: 4005,
SecurityIDSource: 8,
NoMDEntryTypes: 2,
MDEntryType: "0\x01269=1"
}, true);
var EURJPY_Message = protocol.encode({
BeginString: 'FIX.4.4',
BodyLength: '%l',
MsgType: 'V',
MsgSeqNum: protocol.seqNum(),
SenderCompID: username,
SendingTime: moment().subtract(0, "hours").format("YYYYMMDD-HH:mm:ss.SSS"),
TargetCompID: TCompID,
MDReqID: 'EURJPY',
SubscriptionRequestType: 1,
MarketDepth: 1,
MDUpdateType: 0,
NoRelatedSym: 1,
SecurityID: 4006,
SecurityIDSource: 8,
NoMDEntryTypes: 2,
MDEntryType: "0\x01269=1"
}, true);
var AUDUSD_Message = protocol.encode({
BeginString: 'FIX.4.4',
BodyLength: '%l',
MsgType: 'V',
MsgSeqNum: protocol.seqNum(),
SenderCompID: username,
SendingTime: moment().subtract(0, "hours").format("YYYYMMDD-HH:mm:ss.SSS"),
TargetCompID: TCompID,
MDReqID: 'AUDUSD',
SubscriptionRequestType: 1,
MarketDepth: 1,
MDUpdateType: 0,
NoRelatedSym: 1,
SecurityID: 4007,
SecurityIDSource: 8,
NoMDEntryTypes: 2,
MDEntryType: "0\x01269=1"
}, true);
// var USDCHF_Message = protocol.encode({
// BeginString: 'FIX.4.4',
// BodyLength: '%l',
// MsgType: 'V',
// MsgSeqNum: protocol.seqNum(),
// SenderCompID: username,
// SendingTime: moment().subtract(0, "hours").format("YYYYMMDD-HH:mm:ss.SSS"),
// TargetCompID: TCompID,
// MDReqID: 'USDCHF',
// SubscriptionRequestType: 1,
// MarketDepth: 1,
// MDUpdateType: 0,
// NoRelatedSym: 1,
// SecurityID: 4010,
// SecurityIDSource: 8,
// NoMDEntryTypes: 2,
// MDEntryType: "0\x01269=1"
// }, true);
var USDCAD_Message = protocol.encode({
BeginString: 'FIX.4.4',
BodyLength: '%l',
MsgType: 'V',
MsgSeqNum: protocol.seqNum(),
SenderCompID: username,
SendingTime: moment().subtract(0, "hours").format("YYYYMMDD-HH:mm:ss.SSS"),
TargetCompID: TCompID,
MDReqID: 'USDCAD',
SubscriptionRequestType: 1,
MarketDepth: 1,
MDUpdateType: 0,
NoRelatedSym: 1,
SecurityID: 4013,
SecurityIDSource: 8,
NoMDEntryTypes: 2,
MDEntryType: "0\x01269=1"
}, true);
var NZDUSD_Message = protocol.encode({
BeginString: 'FIX.4.4',
BodyLength: '%l',
MsgType: 'V',
MsgSeqNum: protocol.seqNum(),
SenderCompID: username,
SendingTime: moment().subtract(0, "hours").format("YYYYMMDD-HH:mm:ss.SSS"),
TargetCompID: TCompID,
MDReqID: 'NZDUSD',
SubscriptionRequestType: 1,
MarketDepth: 1,
MDUpdateType: 0,
NoRelatedSym: 1,
SecurityID: 100613,
SecurityIDSource: 8,
NoMDEntryTypes: 2,
MDEntryType: "0\x01269=1"
}, true);
var Gold_Spot_Message = protocol.encode({
BeginString: 'FIX.4.4',
BodyLength: '%l',
MsgType: 'V',
MsgSeqNum: protocol.seqNum(),
SenderCompID: username,
SendingTime: moment().subtract(0, "hours").format("YYYYMMDD-HH:mm:ss.SSS"),
TargetCompID: TCompID,
MDReqID: 'Gold (Spot)',
SubscriptionRequestType: 1,
MarketDepth: 1,
MDUpdateType: 0,
NoRelatedSym: 1,
SecurityID: 100637,
SecurityIDSource: 8,
NoMDEntryTypes: 2,
MDEntryType: "0\x01269=1"
}, true);
var UK_Brent_Spot_Message = protocol.encode({
BeginString: 'FIX.4.4',
BodyLength: '%l',
MsgType: 'V',
MsgSeqNum: protocol.seqNum(),
SenderCompID: username,
SendingTime: moment().subtract(0, "hours").format("YYYYMMDD-HH:mm:ss.SSS"),
TargetCompID: TCompID,
MDReqID: 'UK Brent (Spot)',
SubscriptionRequestType: 1,
MarketDepth: 1,
MDUpdateType: 0,
NoRelatedSym: 1,
SecurityID: 100805,
SecurityIDSource: 8,
NoMDEntryTypes: 2,
MDEntryType: "0\x01269=1"
}, true);
var US_Natural_Gas_Spot_Message = protocol.encode({
BeginString: 'FIX.4.4',
BodyLength: '%l',
MsgType: 'V',
MsgSeqNum: protocol.seqNum(),
SenderCompID: username,
SendingTime: moment().subtract(0, "hours").format("YYYYMMDD-HH:mm:ss.SSS"),
TargetCompID: TCompID,
MDReqID: 'US Natural Gas (Spot)',
SubscriptionRequestType: 1,
MarketDepth: 1,
MDUpdateType: 0,
NoRelatedSym: 1,
SecurityID: 100926,
SecurityIDSource: 8,
NoMDEntryTypes: 2,
MDEntryType: "0\x01269=1"
}, true);
var connectionOptions = {
secureProtocol: 'TLSv1_method'
};
var cleartextStream = tls.connect(port, host,
connectionOptions, function () {
// connected to FIX server
// send a FIX login message
cleartextStream.write(loginMessage);
//console.log(loginMessage.blue);
// send a quote subscription message
cleartextStream.write(EURUSD_Message);
cleartextStream.write(GBPUSD_Message);
cleartextStream.write(USDJPY_Message);
cleartextStream.write(EURJPY_Message);
cleartextStream.write(AUDUSD_Message);
cleartextStream.write(GBPJPY_Message);
cleartextStream.write(USDCAD_Message);
cleartextStream.write(NZDUSD_Message);
cleartextStream.write(Gold_Spot_Message);
cleartextStream.write(UK_Brent_Spot_Message);
cleartextStream.write(US_Natural_Gas_Spot_Message);
});
cleartextStream.setEncoding('utf8');
// parse response from FIX server
cleartextStream.on('data', function (data) {
// parse the FIX message
var data = protocol.decode(data);
switch (data.MsgType) {
case '1':
var beat = protocol.encode({
BeginString: 'FIX.4.4',
BodyLength: '%l',
MsgType: 0,
MsgSeqNum: protocol.seqNum(),
SenderCompID: username,
SendingTime: moment().subtract(0, "hours").format("YYYYMMDD-HH:mm:ss.SSS"),
TargetCompID: TCompID,
TestReqID: data.TestReqID
}, true);
console.log(colors.cyan(data));
cleartextStream.write(beat);
break;
case 'W':
var d = new Date();
var sendTime = d.getFullYear() + "-" + (d.getMonth()+1) + "-" + d.getDate() + " " + d.getHours() + ":" + d.getMinutes()+ ":" + d.getSeconds();
var mDEntryPx = data['MDEntryPx'].toString()
var sql = "UPDATE modx_fix_data SET mDEntryPx_buy = '" + mDEntryPx.split(',')[1] + "', mDEntryPx_sell = '" + mDEntryPx.split(',')[0] + "', sendingTime = '"+ sendTime +"' WHERE securityID = '" + data['SecurityID'] + "'";
//**********************
var mysql = require('mysql');
var pool = mysql.createPool({
connectionLimit : 15,
host : 'localhost',
user : 'admin_rm',
password : 'cwJZ2K0kbR',
database : 'admin_rm'
});
pool.query(sql, function (error, results, fields) {
if (error) throw error;
console.log(colors.yellow('update: mysql'));
});
//if (wsServer.connections.Count > 0) {
wsServer.broadcast(JSON.stringify(data));
//}
break;
default:
//console.log(colors.yellow(data));
break;
}
});
cleartextStream.on('end', function () {
console.log('FIX connection closed'.red);
process.exit(0);
});
cleartextStream.on('error', function (reason) {
console.log('FIX connection error:' + reason);
});