-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.js
156 lines (135 loc) · 4.03 KB
/
utils.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
const defaultOrderLogger = (data, done) => done()
let currentOrderLogger = defaultOrderLogger
const invokeOrderLogger = data => (
new Promise((resolve, reject) => {
currentOrderLogger(data, () => resolve(data))
})
)
const setOrderLogger = orderLogger => {
currentOrderLogger = orderLogger
}
const getFirstActiveAccount = (api) => (
new Promise((resolve, reject) => {
Promise.resolve()
.then(() => api.getAccounts())
.then(response => {
const account = response.results.filter(result => !result.deactivated)[0] || null
if (account) {
resolve(account)
} else {
reject({error: "Account not found."})
}
})
.catch(reject)
})
)
const getInstrument = (api, symbol) => (
new Promise((resolve, reject) => {
Promise.resolve()
.then(() => api.searchInstruments(symbol))
.then(response => {
const instrument = response.results.filter(result => result.symbol === symbol)[0] || null
if (instrument) {
resolve(instrument)
} else {
reject({error: `Symbol '${symbol}' not found.`})
}
})
.catch(reject)
})
)
const getMarketOrderConfig = (api, symbol, quantity) => (
new Promise((resolve, reject) => {
let account, instrument
Promise.resolve()
.then(() => getFirstActiveAccount(api))
.then(foundAccount => {
account = foundAccount
return getInstrument(api, symbol)
})
.then(foundInstrument => {
instrument = foundInstrument
return api.getQuote(symbol)
})
.then(quote => {
const ask = parseFloat(quote.ask_price), bid = parseFloat(quote.bid_price), last = parseFloat(quote.last_trade_price)
resolve({
account: account.url,
instrument: instrument.url,
symbol,
type: "market",
time_in_force: "gfd",
trigger: "immediate",
price: Math.round((quantity > 0 ? Math.max(ask, last) : Math.min(bid, last)) * 100) / 100,
quantity: Math.abs(quantity),
side: quantity > 0 ? "buy" : "sell"
})
})
.catch(reject)
})
)
const getPositionForSymbol = (api, symbol) => (
new Promise((resolve, reject) => {
let account, instrument
Promise.resolve()
.then(() => getFirstActiveAccount(api))
.then(foundAccount => {
account = foundAccount
return getInstrument(api, symbol)
})
.then(foundInstrument => {
instrument = foundInstrument
return account.$positions()
})
.then(response => {
resolve(response.results.filter(result => result.instrument === instrument.url)[0] || null)
})
.catch(reject)
})
)
const placeOrderAtMarket = (api, symbol, quantity) => (
new Promise((resolve, reject) => {
Promise.resolve()
.then(() => getMarketOrderConfig(api, symbol, quantity))
.then(config => invokeOrderLogger(config))
.then(config => api.placeOrder(config))
.then(orderResponse => invokeOrderLogger(orderResponse))
.then(orderResponse => resolve(orderResponse))
.catch(error => invokeOrderLogger(error).then(error => reject(error)))
})
)
const sellAllAtMarket = (api, symbol) => (
new Promise((resolve, reject) => {
Promise.resolve()
.then(() => getPositionForSymbol(api, symbol))
.then(position => placeOrderAtMarket(api, symbol, position ? parseFloat(position.quantity) * -1 : 0))
.then(status => {
resolve(status)
})
.catch(reject)
})
)
const buyAtMarketByTarget = (api, symbol, purchaseTarget) => (
new Promise((resolve, reject) => {
Promise.resolve()
.then(() => api.getQuote(symbol))
.then(quote => {
const buyPrice = Math.max(parseFloat(quote.ask_price), parseFloat(quote.last_trade_price))
return placeOrderAtMarket(api, symbol, buyPrice === 0 ? 0 : (Math.round(purchaseTarget / buyPrice) || 1))
})
.then(status => {
resolve(status)
})
.catch(reject)
})
)
module.exports = {
setOrderLogger,
getFirstActiveAccount,
getInstrument,
getMarketOrderConfig,
getPositionForSymbol,
placeOrderAtMarket,
sellAllAtMarket,
buyAtMarketByTarget
}