This repository was archived by the owner on May 29, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathorders.js
More file actions
290 lines (258 loc) · 9.29 KB
/
orders.js
File metadata and controls
290 lines (258 loc) · 9.29 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
import endpoints from './api/endpoints';
import Ticker from './core/ticker';
// The maximum number of orders retrieved by the /orders API.
const ORDERS_PER_PAGE = 20;
const isCanadianSecurity = (exchange) => ['TSX', 'TSX-V'].includes(exchange);
/*
* Retrieve filtered orders by ticker and status.
*/
function filteredOrders(list, ticker, status) {
const orderFilter = (order) => {
if (ticker) {
const target = new Ticker({ symbol: order.symbol, id: order.security_id });
// order objects don't include exchanges, so we are unable to make
// a strong comparison without requiring a linear increase of
// endpoint calls (which is not reasonable).
//
// The user should provide the security id for a strong comparison here.
if (!new Ticker(ticker).weakEquals(target)) {
return false;
}
}
return order.status === status;
};
// Apply filter to the result
const filtered = list.orders.filter(orderFilter);
return {
orders: filtered,
total: filtered.length,
};
}
class Orders {
/**
* Createw a new OrderHistory object associated with an Https worker state.
*
* @param {*} httpsWorker
*/
constructor(httpsWorker, data) {
this.worker = httpsWorker;
this.data = data;
}
/**
* Collects orders (filled, pending, cancelled) for the provided page and
* account id.
*
* @param {*} accountId The specific account in the Wealthsimple Trade account
* @param {*} pageNum The orders page index to seek to
*/
async page(accountId, pageNum) {
return this.worker.handleRequest(endpoints.ORDERS_BY_PAGE, {
offset: (pageNum - 1) * ORDERS_PER_PAGE,
accountId,
});
}
/**
* Collects all orders (filled, pending, cancelled) for the specific account.
*
* @param {*} accountId The specific account in the Wealthsimple Trade account
*/
async all(accountId) {
// We start by capturing the first page of orders in order to
// determine the number of pages available
const data = await this.page(accountId, 1);
const pages = Math.ceil(data.total / ORDERS_PER_PAGE);
if (pages > 1) {
const tasks = [];
// Build the task queue that will retrieve the remaining pages of orders
for (let pageNum = 2; pageNum <= pages; pageNum++) {
tasks.push(this.page(accountId, pageNum).then((result) => result.orders));
}
// Out-of-order invocation is desired. What matters is that Promise.all will
// guarantee that the data order is preserved.
const result = await Promise.all(tasks);
result.forEach((list) => data.orders.push(...list));
// Update the total attribute to reflect the new size
data.total = data.orders.length;
}
return data;
}
/**
* Retrieves pending orders for the specified security in the account.
*
* @param {*} accountId The specific account in the Wealthsimple Trade account
* @param {*} ticker (optional) The security symbol
*/
async pending(accountId, ticker) {
const orders = await this.all(accountId);
return filteredOrders(orders, ticker, 'submitted');
}
/**
* Retrieves filled orders for the specified security in the account.
*
* @param {*} accountId The specific account in the Wealthsimple Trade account
* @param {*} ticker (optional) The security symbol
*/
async filled(accountId, ticker) {
const orders = await this.all(accountId);
return filteredOrders(orders, ticker, 'posted');
}
/**
* Retrieves cancelled orders for the specified security in the account.
*
* @param {*} accountId The specific account in the Wealthsimple Trade account
* @param {*} ticker (optional) The security symbol
*/
async cancelled(accountId, ticker) {
const orders = await this.all(accountId);
return filteredOrders(orders, ticker, 'cancelled');
}
/**
* Cancels the pending order specified by the order id.
*
* @param {*} orderId The pending order to cancel
*/
async cancel(orderId) {
return {
order: orderId,
response: await this.worker.handleRequest(endpoints.CANCEL_ORDER, { orderId }),
};
}
/**
* Cancels all pending orders under the Wealthsimple Trade Account.
*
* @param {*} accountId The specific account in the Wealthsimple Trade account
*/
async cancelPending(accountId) {
const pending = await this.pending(accountId);
return Promise.all(pending.orders.map(async (order) => this.cancel(order.order_id)));
}
/**
* Market buy a security through the Wealthsimple Trade application.
*
* @param {*} accountId The account to make the transaction from
* @param {*} ticker The security symbol
* @param {*} quantity The number of securities to purchase
*/
async marketBuy(accountId, ticker, quantity) {
const details = await this.data.getSecurity(ticker, true);
return this.worker.handleRequest(endpoints.PLACE_ORDER, {
security_id: details.id,
limit_price: !(new Ticker(ticker).crypto) ? details.quote.amount : undefined,
quantity,
order_type: 'buy_quantity',
order_sub_type: 'market',
time_in_force: 'day',
account_id: accountId,
});
}
/**
* Limit buy a security through the Wealthsimple Trade application.
*
* @param {*} accountId The account to make the transaction from
* @param {*} ticker The security symbol
* @param {*} limit The maximum price to purchase the security at
* @param {*} quantity The number of securities to purchase
*/
async limitBuy(accountId, ticker, limit, quantity) {
return this.worker.handleRequest(endpoints.PLACE_ORDER, {
security_id: (await this.data.getSecurity(ticker)).id,
limit_price: limit,
quantity,
order_type: 'buy_quantity',
order_sub_type: 'limit',
time_in_force: 'day',
account_id: accountId,
});
}
/**
* Stop limit buy a security through the Wealthsimple Trade application.
*
* @param {*} accountId The account to make the transaction from
* @param {*} ticker The security symbol
* @param {*} stop The price at which the order converts to a limit order
* @param {*} limit The maximum price to purchase the security at
* @param {*} quantity The number of securities to purchase
*/
async stopLimitBuy(accountId, ticker, stop, limit, quantity) {
const security = await this.data.getSecurity(ticker);
// The Wealthsimple Trade backend doesn't check for this, even though the app does..
if (isCanadianSecurity(security.stock.primary_exchange) && stop !== limit) {
throw new Error('TSX/TSX-V securities must have an equivalent stop and limit price.');
}
return this.worker.handleRequest(endpoints.PLACE_ORDER, {
security_id: security.id,
stop_price: stop,
limit_price: limit,
quantity,
order_type: 'buy_quantity',
order_sub_type: 'stop_limit',
time_in_force: 'day',
account_id: accountId,
});
}
/**
* Market sell a security through the Wealthsimple Trade application.
*
* @param {*} accountId The account to make the transaction from
* @param {*} ticker The security symbol
* @param {*} quantity The number of securities to purchase
*/
async marketSell(accountId, ticker, quantity) {
const details = await this.data.getSecurity(ticker, true);
return this.worker.handleRequest(endpoints.PLACE_ORDER, {
security_id: details.id,
market_value: !(new Ticker(ticker).crypto) ? details.quote.amount : undefined,
quantity,
order_type: 'sell_quantity',
order_sub_type: 'market',
time_in_force: 'day',
account_id: accountId,
});
}
/**
* Limit sell a security through the Wealthsimple Trade application.
*
* @param {*} accountId The account to make the transaction from
* @param {*} ticker The security symbol
* @param {*} limit The minimum price to sell the security at
* @param {*} quantity The number of securities to sell
*/
async limitSell(accountId, ticker, limit, quantity) {
return this.worker.handleRequest(endpoints.PLACE_ORDER, {
security_id: (await this.data.getSecurity(ticker)).id,
limit_price: limit,
quantity,
order_type: 'sell_quantity',
order_sub_type: 'limit',
time_in_force: 'day',
account_id: accountId,
});
}
/**
* Stop limit sell a security through the Wealthsimple Trade application.
*
* @param {*} accountId The account to make the transaction from
* @param {*} ticker The security symbol
* @param {*} stop The price at which the order converts to a limit order
* @param {*} limit The minimum price to sell the security at
* @param {*} quantity The number of securities to sell
*/
async stopLimitSell(accountId, ticker, stop, limit, quantity) {
const security = await this.data.getSecurity(ticker);
// The Wealthsimple Trade backend doesn't check for this, even though the app does..
if (isCanadianSecurity(security.stock.primary_exchange) && stop !== limit) {
throw new Error('TSX/TSX-V securities must have an equivalent stop and limit price.');
}
return this.worker.handleRequest(endpoints.PLACE_ORDER, {
security_id: security.id,
stop_price: stop,
limit_price: limit,
quantity,
order_type: 'sell_quantity',
order_sub_type: 'stop_limit',
time_in_force: 'day',
account_id: accountId,
});
}
}
export default Orders;