-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.js
184 lines (155 loc) · 4.78 KB
/
client.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
var crypto = require('crypto');
var Fix = require('./fix.js');
var moment = require('moment');
var Config = require('./config.js');
var LogWriter = require('./logging.js');
var Order = require('./order.js');
var OrderStatus = require('./orderstatus.js');
var Cancel = require('./cancel.js');
var Sequence = require('./sequence.js');
const apiKey = "add API key here";
const privateKey = "add API secret key here";
const host = 'fix.btcmarkets.net';
var config = new Config
config.setParams(host, 443, apiKey, privateKey, true);
var logger = new LogWriter()
var sendingTime = moment().utc()
var sendingTimeForMsg = sendingTime.format("YYYYMMDD-HH:mm:ss.SSS")
var beginSeq = Fix.seqNum()
let connection;
let order;
let orderStatus;
let cancel;
let sequence;
init();
function runTest() {
order.sendLimitOrderWithClOrdId("1","BTC-AUD", "BUY", "GTC", 0.1,0.3);
//order.sendMarketOrderWithClOrdId("test-id-102", "BTC-AUD", "SELL", "IOC", 100);
//cancel.sendCancelWithOrderId("1");
//orderStatus.sendOrderStatusWithOrderId("test-id-1011", "10211814");
//orderStatus.sendOrderStatusWithOrderId("10219320");
}
function init() {
var signature = signMessage(beginSeq,config.getApiKey(), sendingTime, config.getPrivateKey())
var loginMessage = {
BeginString: 'FIX.4.4',
BodyLength: '%l',
MsgType: 'A',
MsgSeqNum: beginSeq,
SenderCompID: config.getApiKey(),
SendingTime: sendingTimeForMsg,
TargetCompID: 'BTCM',
EncryptMethod: 0,
HeartBtInt: 30,
ResetSeqNumFlag: 'Y',
RawDataLength: signature.length,
RawData: signature,
CancelOnDisconnect: 2
};
var logoutMessage = {
BeginString: 'FIX.4.4',
BodyLength: '%l',
MsgType: '5',
MsgSeqNum: beginSeq,
SenderCompID: config.getApiKey(),
TargetCompID: 'BTCM',
SendingTime: sendingTimeForMsg
};
var heartbeatMsg = {
BeginString: 'FIX.4.4',
BodyLength: '%l',
MsgType: 0,
MsgSeqNum: 0,
SenderCompID: config.getApiKey(),
SendingTime: moment().utc().format("YYYYMMDD-HH:mm:ss.SSS"),
TargetCompID: 'BTCM',
};
var testResponseMsg = {
BeginString: 'FIX.4.4',
BodyLength: '%l',
MsgType: 0,
MsgSeqNum: 0,
SenderCompID: config.getApiKey(),
SendingTime: moment().utc().format("YYYYMMDD-HH:mm:ss.SSS"),
TargetCompID: 'BTCM',
TestReqID: ''
};
var resendRequestMsg = {
BeginString: 'FIX.4.4',
BodyLength: '%l',
MsgType: 2,
MsgSeqNum: 0,
SenderCompID: config.getApiKey(),
SendingTime: moment().utc().format("YYYYMMDD-HH:mm:ss.SSS"),
TargetCompID: 'BTCM',
};
connection = config.connect(Fix.message(loginMessage,true), logger);
connection.setEncoding('utf8');
order = new Order(config.getApiKey(), connection, logger)
orderStatus = new OrderStatus(config.getApiKey(), connection, logger)
cancel = new Cancel(config.getApiKey(), connection, logger)
sequence = new Sequence(config.getApiKey(), connection, logger)
connection.on('data', function(data) {
var msgs = Fix.internalsplit(data)
msgs.forEach(function(value) {
var msg = Fix.read(value);
logger.write('Inbound',msg)
if (msg.MsgType == 'A') {
runTest();
}
if (msg.MsgType === '0') {
handleHeartbeat();
}
if (msg.MsgType == '1') {
handleTestRequest(data);
}
});
});
connection.on('end', function() {
console.log('FIX connection closed');
process.exit(0);
});
connection.on('error', function(reason) {
console.log('FIX connection error: ' + reason);
});
function doSequenceReset(){
sequence.sendSequenceReset(Fix.lastSeqNum()*2 + 1);
}
function doLogOut(){
logoutMessage.SendingTime=moment().utc().format("YYYYMMDD-HH:mm:ss.SSS")
logout = Fix.message(logoutMessage, true);
connection.write(logout);
logger.write("Outbound",Fix.read(logout))
}
}
function signMessage(seqNum, apiKey, timestamp, secret) {
var msg = seqNum+'A'+apiKey+timestamp
var key = Buffer.from(secret, 'base64');
var hmac = crypto.createHmac('SHA512', key);
var signature = hmac.update(msg).digest('base64');
return signature;
}
function handleTestRequest(data){
testResponseMsg.TestReqID=data.TestReqID
testResponseMsg.MsgSeqNum=Fix.seqNum()
testResponseMsg.SendingTime=moment().utc().format("YYYYMMDD-HH:mm:ss.SSS")
response = Fix.message(testResponseMsg, true);
connection.write(response);
logger.write('Outbound',Fix.read(response));
}
function handleHeartbeat(){
heartbeatMsg.MsgSeqNum=Fix.seqNum()
heartbeatMsg.SendingTime=moment().utc().format("YYYYMMDD-HH:mm:ss.SSS")
response = Fix.message(heartbeatMsg, true);
connection.write(response);
logger.write('Outbound',Fix.read(response));
}
function handleResendRequest(){
resendRequestMsg.MsgSeqNum=Fix.seqNum()
resendRequestMsg.SendingTime=moment().utc().format("YYYYMMDD-HH:mm:ss.SSS")
resendRequestMsg.BeginSeqNo=4
resendRequestMsg.EndSeqNo=Fix.lastSeqNum()-1
response = Fix.message(resendRequestMsg, true);
connection.write(response);
logger.write('Outbound',Fix.read(response));
}