-
Notifications
You must be signed in to change notification settings - Fork 10
/
Orders.js
267 lines (207 loc) · 5.97 KB
/
Orders.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
const basePath = '/orders';
// These can be useful when constructing certain payloads.
// The user can simply access these values from OrdersService.
// const { orderTypes } = FTXUS.Orders;
const orderTypes = {
STOP: 'stop',
TRAILING_STOP: 'trailing_stop',
TAKE_PROFIT: 'take_profit'
}
class OrdersService {
constructor(client) {
this._client = client;
this._orderType = orderTypes;
}
get orderTypes() {
return this._orderType;
}
async getOpenOrders(market) {
const c = c.newBaseURL();
u.pathname = basePath;
if (market) {
u.searchParams.append('market', market);
}
const r = await c.get(endpoint);
return r.data.result;
}
async getHistory(market=null, opts={ startTime: null, endTime: null, limit: 100 }) {
const { startTime, endTime, limit } = opts;
const c = this._client;
const u = c.newBaseURL();
u.pathname = basePath + '/history';
u.searchParams.append('limit', limit);
if (market) {
u.searchParams.append('market', market);
}
if (startTime) {
u.searchParams.append('start_time', startTime);
}
if (endTime) {
u.searchParams.append('end_time', endtime);
}
const r = await c.get(u);
return r.data.result;
}
async getOpenTriggerOrders(opts={ market: null, type: null }) {
const c = this._client;
const u = c.newBaseURL();
u.pathname = '/conditional_orders';
if (market) {
u.searchParams.append('market', market);
}
if (type) {
u.searchParams.append('type', type);
}
const r = await c.get(u);
return r.data.result;
}
async getTriggerOrderTriggers(orderId) {
const c = this._client;
const u = c.newBaseURL();
u.pathname = `/conditional_orders/${orderId}/triggers`;
const r = await c.get(u);
return r.data.result;
}
async getTriggerOrderHistory(opts={
market: null,
startTime: null,
endTime: null,
side: null,
type: null,
orderType: null,
limit: null
}) {
const c = this._client;
const u = c.newBaseURL();
u.pathname = '/conditional_orders/history';
// Loop through the provided optional parameters
// and add the key/value pairs to the URL as search params
Object.keys(opts).forEach(key => {
if (opts[key]) {
u.searchParams.append(key, opts[key]);
}
});
const r = await c.get(u);
return r.data.result;
}
async placeOrder(market, side, price, type, size, opts= {
reduceOnly: false,
ioc: false,
postOnly: false,
clientId: null
}) {
const c = this._client;
const u = c.newBaseURL();
u.pathname = '/orders';
const payload = { market, side, price, type, size, ...opts };
const r = await c.post(u, payload);
return r.data.result;
}
async placeTriggerOrder(market, side, size, type, opts={
reduceOnly: false,
retryUntilFilled: false,
stopLoss: {
triggerPrice: 0,
orderPrice: 0
},
trailingStop: {
trailValue: 0
},
takeProfit: {
triggerPrice: 0,
orderPrice: 0
}
}) {
const c = this._client;
const u = c.newBaseURL();
u.pathname = '/conditional_orders';
const { reduceOnly, retryUntilFilled, stopLoss, trailingStop, takeProfit } = opts;
const payload = { market, side, size, type, reduceOnly, retryUntilFilled };
if (stopLoss.triggerPrice !== 0) {
payload['stopLoss'] = stopLoss;
}
if (trailingStop.trailValue !== 0) {
payload['trailingStop'] = trailingStop;
}
if (takeProfit.triggerPrice !== 0) {
payload['takeProfit'] = takeProfit;
}
const r = await c.post(u, payload);
return r.data.result;
}
async modifyOrder(opts={ orderId: null, price: null, size: null, clientId: null }) {
// Ensure that the minimal required parameters are provided to the method
if (opts.orderId && opts.clientId) {
return new Error(`Both an Order ID and Client ID was passed. Provide only one of he two parameters depending on how you wish to modify your order.`);
}
if (!opts.orderId && !opts.clientId) {
return new Error(`You must provide either an Order ID or Client ID to modify your order.`);
}
if (!opts.price && !opts.size) {
return new Error(`Tried to modify Order ${opts.orderId} without providing a price or size`);
}
const c = this._client;
const u = c.newBaseURL();
if (opts.orderId) {
u.pathname = `/orders/${opts.orderId}/modify`;
}
if (opts.clientId) {
u.pathname = `/orders/by_client_id/${opts.clientId}/modify`;
}
const payload = {};
if (opts.price) {
payload.price = opts.price;
}
if (opts.size) {
payload.size = opts.size;
}
if (opts.cliendId) {
payload.clientId = opts.clientId;
}
const r = c.post(u, payload);
return r.data.result;
}
async getOrderStatus(id) {
const c = this._client;
const u = c.newBaseURL();
u.pathname = `/orders/${id}`;
const r = await c.get(u);
return r.data.result;
}
async getOrderStatusByClientId(id) {
const c = this._client;
const u = c.newBaseURL();
u.pathname = `/orders/by_client_id/${client_order_id}`;
const r = await c.get(u);
return r.data.result;
}
async cancelById(id) {
const c = this._client;
const u = c.newBaseURL();
u.pathname = `/orders/${id}`;
const r = await c.delete(u);
return r.data.result;
}
async cancelByClientId(id) {
const c = this._client;
const u = c.newBaseURL();
u.pathname = `/orders/by_client_id/${id}`;
const r = await c.delete(u);
return r.data.result;
}
async cancelOpenTriggerOrders(id) {
const c = this._client;
const u = c.newBaseURL();
u.pathname = `/conditional_orders/${id}`;
const r = await c.delete(u);
return r.data.result;
}
async cancelAllOrders() {
const c = this._client;
const u = c.newBaseURL();
u.pathname = `/orders}`;
const r = await c.delete(u);
return r.data.result;
}
}
module.exports = OrdersService;