-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGdaxExchange.js
226 lines (202 loc) · 7.19 KB
/
GdaxExchange.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
/* eslint-disable no-unused-vars */
/* eslint-disable class-methods-use-this */
/* eslint-disable no-underscore-dangle */
import { AuthenticatedClient, OrderbookSync } from 'gdax';
import Exchange from '../../core/Exchange';
import { APIType, APIMode, ConnectionStatus, TradingMode } from '../../core/ExchangeConfig';
import { logTrace, logError, logDebug, logInfo, logVerbose } from '../../core/logger';
import { OrderType } from '../../core/StrategyConfig';
import { truncate } from '../../helpers/numbers';
class GdaxExchange extends Exchange {
constructor(options = {}) {
super(options);
this.apiType = APIType.WEB_SOCKET;
if (this.apiMode === APIMode.PRODUCTION) {
this.apiUri = 'https://api.pro.coinbase.com';
this.wsUri = 'wss://ws-feed.pro.coinbase.com';
} else {
this.apiUri = 'https://api-public.sandbox.pro.coinbase.com';
this.wsUri = 'wss://ws-feed-public.sandbox.pro.coinbase.com';
}
}
async _buy(params = {}) {
try {
let result;
if (this.tradingMode === TradingMode.LIVE) {
const orderParams = this._parseOrderParams(params);
result = await this.authClient.buy(orderParams);
logDebug('GDAX API - buy response:', result);
// Update balances after buy order.
// TODO: Balance update should happen when orders are matched, not placed.
this._updateAccountBalances();
} else {
// TODO: Handle PAPER and SIMULATION scenarios.
result = Promise.resolve(true);
}
// TODO: Implement logic when placing buy orders (Add to list of open orders?).
// TODO: Better return for promises.
return result;
} catch (error) {
logError('', error);
throw error;
}
}
_cancelAllOrders() {
return Promise.resolve(false);
}
_cancelOrder(params = {}) {
return Promise.resolve(false);
}
async _connect() {
this.authClient = new AuthenticatedClient(this.auth.key, this.auth.secret, this.auth.passphrase, this.apiUri);
await this._loadOrderBook();
await this._updateAccountBalances();
}
_listenOrderBookMessages() {
if (!this._orderBook) {
return;
}
this._orderBook.on('open', (data) => {
this.connectionStatus = ConnectionStatus.CONNECTED;
const self = this;
setInterval(() => self._updateAccountBalances(), self.balanceUpdateInterval);
logTrace('Order book message "open":', data);
});
this._orderBook.on('sync', (data) => {
logTrace('Order book message "sync":', data);
});
this._orderBook.on('synced', (data) => {
logTrace('Order book message "synced":', data);
});
this._orderBook.on('message', (data) => {
// this._stats = {
// updated: new Date(),
// last_msg: data,
// };
switch (data.type) {
case 'match':
this._last_price = data.price;
this.strategies.updateMarketData({
instrumentId: data.product_id,
eventType: data.type,
size: data.size,
price: data.price,
side: data.side,
});
logTrace('Order book message "message":', data);
logDebug(`Match:\t${data.side}\t${data.size}\t${data.price}\t${new Date(data.time).toLocaleTimeString()}.`);
break;
default:
logTrace(data.type);
break;
}
});
this._orderBook.on('error', (error) => {
logError('OrderBook Error:', error);
});
}
async _loadOrderBook() {
if (!this._orderBook) {
try {
logTrace(`Loading order book. API URI: ${this.apiUri}. WS URI: ${this.wsUri}`);
this._orderBook = new OrderbookSync(this.instruments, this.apiUri, this.wsUri, this.auth);
this._listenOrderBookMessages();
} catch (error) {
logError('Error loading orderbook.', error);
throw error;
}
}
}
/**
* @description Map default Order param structure to GDAX's order params payload.
* @param {Dictionary} params Order params.
*/
_parseOrderParams(params) {
const orderParams = {
type: params.orderType === OrderType.MARKET ? 'market' : 'limit',
side: params.side,
product_id: params.instrumentId,
};
// TODO: get decimal precision from product config.
// Defaults to size; then, checks for funds.
if (params.size && params.size > 0) {
orderParams.size = truncate(params.size, 8).toString();
} else if (params.funds && params.funds > 0) {
orderParams.funds = truncate(params.funds, 2).toString();
}
if (params.orderType !== OrderType.MARKET && params.price && params.price > 0) {
orderParams.price = truncate(params.price, 2).toString();
}
logDebug('Order params parsed:', orderParams);
return orderParams;
}
// TODO: Properly create Promise.
async _sell(params) {
try {
let result;
if (this.tradingMode === TradingMode.LIVE) {
const orderParams = this._parseOrderParams(params);
result = await this.authClient.sell(orderParams);
logInfo(`Sell order placed on ${this.name}. API response:`, result);
// Update balances after sell order.
// TODO: Balance update should happen when orders are matched, not placed.
this._updateAccountBalances();
} else {
// TODO: Handle PAPER and SIMULATION scenarios.
result = Promise.resolve(true);
}
// TODO: Implement logic when placing sell orders (Add to list of open orders?).
// TODO: Better return for promises.
return result;
} catch (error) {
logError('', error);
throw error;
}
}
_sellAccountPositions(params) {
Promise.resolve(false);
}
_sellProductPositions(params = {}) {
Promise.resolve(false);
}
_sendOrder(params = {}) {
Promise.resolve(false);
}
async _updateAccountBalances(params = {}) {
function padStart(value = '', pad = 30) {
return value.padStart(pad, ' ');
}
try {
const balances = {};
const accounts = await this.authClient.getAccounts();
logTrace('GDAX API - getAccounts response:', accounts);
logVerbose(`Account balances on ${this.name}:`);
logVerbose(`${padStart('Currency', 10)}${padStart('Balance')}${padStart('Hold')}${padStart('Available')}`);
Object.values(accounts)
.filter((item) => item.balance > 0)
.forEach((item) => {
balances[item.currency] = item;
logVerbose(
`${padStart(item.currency, 10)}${padStart(item.balance)}${padStart(item.hold)}${padStart(item.available)}`
);
});
this._balances = balances;
} catch (error) {
logError(`Error updating account balance on ${this.name}. Params: ${JSON.stringify(params)}`, error);
throw error;
}
}
async _updateProductBalance(params = {}) {
try {
const balance = await this.authClient.getAccount(params.id);
logTrace('GDAX API - getAccount response:', balance);
this._balances[balance.currency] = balance;
logDebug(`Product "${balance.currency}" balance on ${this.name}:`, balance);
Promise.resolve(true);
} catch (error) {
logError(`Error updating product balance on ${this.name}. Params: ${JSON.stringify(params)}`, error);
throw error;
}
}
}
export default GdaxExchange;