-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
226 lines (215 loc) · 6.63 KB
/
index.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
var exports = module.exports = {}
var crypto = require('crypto');
var fix = require('fixjs');
var net = require('net');
var tls = require('tls');
var program = require('commander');
var url = require('url');
var uuid = require('uuid');
var Msgs = fix.Msgs;
var Gdax = require('gdax');
var config = require('./config')
const authedClient = new Gdax.AuthenticatedClient(config.apiKey, config.secret, config.passphrase, config.apiURI);
//------------------------------------------------------------------------------
// Use TLS
net = tls;
// Connect the stream!
var stream = net.connect({
host: 'fix.gdax.com',
port: '4198'
}, function() {
console.log('connected to gdax server trough FIX API 4.2');
});
// Log errors
stream.on('error', function(err) {
console.error(err);
});
//----------------------------
var websocket = null;
var orderbook = null;
var client = null
var session = null
var market = {
symbol : 'symbol',
ask : 0,
bid: 0,
spread:0
}
var pendings = []
//-----------------------------
function initClient(symbol) {
account = {
base_currency:symbol.substring(0,3),
quote_currency:symbol.substring(4,7),
baseAvailable:0,
quoteAvailable:0,
total:0
}
market.symbol = symbol
market.base_currency = symbol.substring(0,3)
market.quote_currency = symbol.substring(4,7)
market.BaseAvailable = 0
market.QuoteAvailable = 0
market.TotalAsset = 0
websocket = new Gdax.WebsocketClient(symbol)
orderbook = new Gdax.OrderbookSync(symbol)
client = fix.createClient(stream)
websocket.on('message', function(data) {
if (data.type === 'open') {
if (data.product_id === symbol) {
var askSide = orderbook.book.state().asks;
var bidSide = orderbook.book.state().bids;
if (typeof askSide[0] != 'undefined' && typeof bidSide[0] != 'undefined') {
var lasta = parseFloat(askSide[0].price.toString());
if (market.ask != lasta) {
market.ask = lasta
}
var lastb = parseFloat(bidSide[0].price.toString());
if (market.bid != lastb) {
market.bid = lastb
}
market.spread = parseFloat(askSide[0].price.toString()) - parseFloat(bidSide[0].price.toString())
}
}
}
});
websocket.on('errror', function(data) {
console.log(data);
});
session = client.session(config.apiKey, 'Coinbase');
session.on('logon', function() {
console.log('logged on, '+new Date());
});
// Test request
session.on('TestRequest', function(msg, next) {
console.log('%s', msg);
next();
});
// The rest of these are handlers for various FIX keywords
session.on('ExecutionReport', function(msg, next) {
if (msg.ExecType == 0) {
// console.log(msg);
}else if (msg.ExecType == 1 || msg.ExecType == 2) { // fill
console.log('Order Filled!');
console.log(msg);
} else if (msg.ExecType == 3 || msg.ExecType == 4) { // done/canceled
console.log('Order canceled');
} else if (msg.ExecType == 'D') { // unsolicited reduce
console.log('got unsolicited reduce, new OrderQty:', msg.OrderQty);
} else if (msg.ExecType == 'I') {
if (msg.Symbol == config.symbol) {
var pendi = { side: parseFloat(msg.Side), price : parseFloat(msg.Price) }
pendings.push(pendi)
}
}
next();
});
session.on('OrderCancelReject', function(msg, next) {
console.log('order cancel reject: %s', msg);
next();
});
session.on('Reject', function(msg, next) {
console.log('reject: %s', msg);
next();
});
session.on('send', function(msg) {
//msg.replace(/\s/g,/\r\n/);
// console.log(msg);
// console.log('sending message: %s', msg);
});
session.on('error', function(err) {
console.error(err.stack);
});
session.on('logout', function() {
console.log('logged out');
});
session.on('end', function() {
console.log('session ended');
stream.end();
});
stream.on('end', function() {
console.log('stream ended');
});
//------------------------------------------------------------------------------
// create our own Logon message so we can control the SendingTime and sign it
var logon = new Msgs.Logon();
logon.SendingTime = new Date();
console.log(logon.SendingTime);
logon.HeartBtInt = 30;
logon.EncryptMethod = 0;
logon.set(554, config.passphrase); // FIX 4.4 Password tag
var presign = [
logon.SendingTime,
logon.MsgType,
session.outgoing_seq_num,
session.sender_comp_id,
session.target_comp_id,
config.passphrase
];
var what = presign.join('\x01');
logon.RawData = sign(what, config.secret);
//console.log("logon.RawData: " + logon.RawData);
session.send(logon, true);
function sign(what, secret) {
var key = Buffer(secret, 'base64');
var hmac = crypto.createHmac('sha256', key);
//console.log("presign: " + what);
var signature = hmac.update(what).digest('base64');
return signature;
}
}
//----------------------------
function sendOrder(type, side, price, amount) {
var siz = parseFloat(amount.toFixed(2))
var order = new Msgs.NewOrderSingle();
order.Symbol = config.symbol;
order.ClOrdID = uuid();
order.Side = side;
order.HandlInst = 1;
order.TransactTime = new Date();
order.OrdType = type; // 2=Limit
order.OrderQty =siz;
order.Price = price;
order.TimeInForce = '1'; // 1=GTC
order.set(7928, 'D'); // STP
session.send(order);
}
//----------------------------
function cancelOrder(ClOrdID,OrderID) {
var cancel = new Msgs.OrderCancelRequest();
cancel.Symbol = market.Symbol;
cancel.OrigClOrdID = ClOrdID;
cancel.ClOrdID = 123456;
cancel.OrderID = OrderID;
session.send(cancel);
}
//----------------------------
function updateData(){
pendings = []
var orders = new Msgs.OrderStatusRequest();
orders.OrderID = '*'
session.send(orders)
//--- get account
authedClient.getAccounts(function (err, response, data) {
var t = 0
var ts = 0
for (var i = 0; i < data.length; i++) {
if (data[i].currency === market.base_currency) {
market.BaseAvailable = parseFloat(data[i].balance)
t = parseFloat(data[i].balance*market.bid)
}
if (data[i].currency === market.quote_currency) {
market.QuoteAvailable = parseFloat(data[i].balance)
ts = parseFloat(data[i].balance)
}
}
market.TotalAsset = t + ts
});
}
//-----------------------------
exports.initClient = initClient
exports.updateData = updateData
exports.getData = market
exports.getOrders = pendings
exports.sendOrder = sendOrder
exports.cancelOrder = cancelOrder