|
| 1 | +import { axios } from "@pipedream/platform"; |
| 2 | + |
1 | 3 | export default { |
2 | 4 | type: "app", |
3 | 5 | app: "oanda", |
4 | | - propDefinitions: {}, |
| 6 | + propDefinitions: { |
| 7 | + baseCurrency: { |
| 8 | + type: "string", |
| 9 | + label: "Base Currency", |
| 10 | + description: "The base currency of the currency pair.", |
| 11 | + }, |
| 12 | + quoteCurrency: { |
| 13 | + type: "string", |
| 14 | + label: "Quote Currency", |
| 15 | + description: "The quote currency of the currency pair.", |
| 16 | + }, |
| 17 | + changeThreshold: { |
| 18 | + type: "number", |
| 19 | + label: "Change Threshold", |
| 20 | + description: "The threshold for significant change in forex rate to trigger the event.", |
| 21 | + }, |
| 22 | + instrumentFilter: { |
| 23 | + type: "string", |
| 24 | + label: "Instrument Filter", |
| 25 | + description: "Filter by instrument.", |
| 26 | + optional: true, |
| 27 | + }, |
| 28 | + tradeTypeFilter: { |
| 29 | + type: "string", |
| 30 | + label: "Trade Type Filter", |
| 31 | + description: "Filter by trade type.", |
| 32 | + optional: true, |
| 33 | + options: [ |
| 34 | + { |
| 35 | + label: "Buy", |
| 36 | + value: "BUY", |
| 37 | + }, |
| 38 | + { |
| 39 | + label: "Sell", |
| 40 | + value: "SELL", |
| 41 | + }, |
| 42 | + ], |
| 43 | + }, |
| 44 | + orderTypeFilter: { |
| 45 | + type: "string", |
| 46 | + label: "Order Type Filter", |
| 47 | + description: "Filter by order type.", |
| 48 | + optional: true, |
| 49 | + options: [ |
| 50 | + { |
| 51 | + label: "Market", |
| 52 | + value: "MARKET", |
| 53 | + }, |
| 54 | + { |
| 55 | + label: "Limit", |
| 56 | + value: "LIMIT", |
| 57 | + }, |
| 58 | + { |
| 59 | + label: "Stop", |
| 60 | + value: "STOP", |
| 61 | + }, |
| 62 | + ], |
| 63 | + }, |
| 64 | + instrument: { |
| 65 | + type: "string", |
| 66 | + label: "Instrument", |
| 67 | + description: "The financial instrument to be used.", |
| 68 | + }, |
| 69 | + orderType: { |
| 70 | + type: "string", |
| 71 | + label: "Order Type", |
| 72 | + description: "Type of the order.", |
| 73 | + options: [ |
| 74 | + { |
| 75 | + label: "Market", |
| 76 | + value: "MARKET", |
| 77 | + }, |
| 78 | + { |
| 79 | + label: "Limit", |
| 80 | + value: "LIMIT", |
| 81 | + }, |
| 82 | + { |
| 83 | + label: "Stop", |
| 84 | + value: "STOP", |
| 85 | + }, |
| 86 | + ], |
| 87 | + }, |
| 88 | + units: { |
| 89 | + type: "integer", |
| 90 | + label: "Units", |
| 91 | + description: "Number of units to trade.", |
| 92 | + }, |
| 93 | + priceThreshold: { |
| 94 | + type: "number", |
| 95 | + label: "Price Threshold", |
| 96 | + description: "Optional price threshold for the order.", |
| 97 | + optional: true, |
| 98 | + }, |
| 99 | + timeInForce: { |
| 100 | + type: "string", |
| 101 | + label: "Time In Force", |
| 102 | + description: "Optional time in force for the order.", |
| 103 | + optional: true, |
| 104 | + options: [ |
| 105 | + { |
| 106 | + label: "Good Till Cancelled (GTC)", |
| 107 | + value: "GTC", |
| 108 | + }, |
| 109 | + { |
| 110 | + label: "Immediate Or Cancel (IOC)", |
| 111 | + value: "IOC", |
| 112 | + }, |
| 113 | + { |
| 114 | + label: "Fill Or Kill (FOK)", |
| 115 | + value: "FOK", |
| 116 | + }, |
| 117 | + ], |
| 118 | + }, |
| 119 | + granularity: { |
| 120 | + type: "string", |
| 121 | + label: "Granularity", |
| 122 | + description: "The granularity of the historical price data.", |
| 123 | + options: [ |
| 124 | + { |
| 125 | + label: "Minute", |
| 126 | + value: "M1", |
| 127 | + }, |
| 128 | + { |
| 129 | + label: "Hour", |
| 130 | + value: "H1", |
| 131 | + }, |
| 132 | + { |
| 133 | + label: "Daily", |
| 134 | + value: "D", |
| 135 | + }, |
| 136 | + ], |
| 137 | + }, |
| 138 | + startTime: { |
| 139 | + type: "string", |
| 140 | + label: "Start Time", |
| 141 | + description: "The start time for historical price data (ISO 8601 format).", |
| 142 | + }, |
| 143 | + endTime: { |
| 144 | + type: "string", |
| 145 | + label: "End Time", |
| 146 | + description: "The end time for historical price data (ISO 8601 format).", |
| 147 | + }, |
| 148 | + }, |
5 | 149 | methods: { |
6 | | - // this.$auth contains connected account data |
7 | | - authKeys() { |
8 | | - console.log(Object.keys(this.$auth)); |
| 150 | + _baseUrl(isDemo = false) { |
| 151 | + return isDemo |
| 152 | + ? "https://api-fxpractice.oanda.com/v3" |
| 153 | + : "https://api-fxtrade.oanda.com/v3"; |
| 154 | + }, |
| 155 | + _makeRequest({ |
| 156 | + $ = this, |
| 157 | + path, |
| 158 | + isDemo, |
| 159 | + ...otherOpts |
| 160 | + }) { |
| 161 | + return axios($, { |
| 162 | + ...otherOpts, |
| 163 | + url: `${this._baseUrl(isDemo)}${path}`, |
| 164 | + headers: { |
| 165 | + Authorization: `Bearer ${this.$auth.personal_token}`, |
| 166 | + }, |
| 167 | + }); |
| 168 | + }, |
| 169 | + async listTrades(opts = {}) { |
| 170 | + const { |
| 171 | + instrumentFilter, tradeTypeFilter, ...otherOpts |
| 172 | + } = opts; |
| 173 | + const params = {}; |
| 174 | + if (tradeTypeFilter) { |
| 175 | + params.type = tradeTypeFilter; |
| 176 | + } |
| 177 | + if (instrumentFilter) { |
| 178 | + params.instrument = instrumentFilter; |
| 179 | + } |
| 180 | + return this._makeRequest({ |
| 181 | + path: `/accounts/${this.$auth.account_id}/trades`, |
| 182 | + params, |
| 183 | + ...otherOpts, |
| 184 | + }); |
| 185 | + }, |
| 186 | + async createOrder(opts = {}) { |
| 187 | + const { |
| 188 | + instrument, orderType, units, priceThreshold, timeInForce, ...otherOpts |
| 189 | + } = opts; |
| 190 | + const data = { |
| 191 | + order: { |
| 192 | + type: orderType, |
| 193 | + instrument: instrument, |
| 194 | + units: units, |
| 195 | + timeInForce: timeInForce || "FOK", |
| 196 | + }, |
| 197 | + }; |
| 198 | + if (priceThreshold) { |
| 199 | + data.order.price = priceThreshold; |
| 200 | + } |
| 201 | + return this._makeRequest({ |
| 202 | + method: "POST", |
| 203 | + path: `/accounts/${this.$auth.account_id}/orders`, |
| 204 | + data, |
| 205 | + ...otherOpts, |
| 206 | + }); |
| 207 | + }, |
| 208 | + async getHistoricalPrices(opts = {}) { |
| 209 | + const { |
| 210 | + instrument, granularity, startTime, endTime, ...otherOpts |
| 211 | + } = opts; |
| 212 | + const params = { |
| 213 | + granularity: granularity, |
| 214 | + from: startTime, |
| 215 | + to: endTime, |
| 216 | + }; |
| 217 | + return this._makeRequest({ |
| 218 | + path: `/instruments/${instrument}/candles`, |
| 219 | + params, |
| 220 | + ...otherOpts, |
| 221 | + }); |
| 222 | + }, |
| 223 | + async getOrderStatus(opts = {}) { |
| 224 | + const { |
| 225 | + orderTypeFilter, ...otherOpts |
| 226 | + } = opts; |
| 227 | + const params = {}; |
| 228 | + if (orderTypeFilter) { |
| 229 | + params.type = orderTypeFilter; |
| 230 | + } |
| 231 | + return this._makeRequest({ |
| 232 | + path: `/accounts/${this.$auth.account_id}/orders`, |
| 233 | + params, |
| 234 | + ...otherOpts, |
| 235 | + }); |
| 236 | + }, |
| 237 | + async getPricingData(opts = {}) { |
| 238 | + const { |
| 239 | + baseCurrency, quoteCurrency, ...otherOpts |
| 240 | + } = opts; |
| 241 | + const instrument = `${baseCurrency}_${quoteCurrency}`; |
| 242 | + const params = { |
| 243 | + instruments: instrument, |
| 244 | + }; |
| 245 | + return this._makeRequest({ |
| 246 | + path: "/pricing", |
| 247 | + params, |
| 248 | + ...otherOpts, |
| 249 | + }); |
| 250 | + }, |
| 251 | + async paginate(fn, ...opts) { |
| 252 | + let results = []; |
| 253 | + let response = await fn(...opts); |
| 254 | + results = results.concat(response.trades || response.orders || []); |
| 255 | + while (response.nextPage) { |
| 256 | + response = await fn({ |
| 257 | + page: response.nextPage, |
| 258 | + ...opts, |
| 259 | + }); |
| 260 | + results = results.concat(response.trades || response.orders || []); |
| 261 | + } |
| 262 | + return results; |
9 | 263 | }, |
10 | 264 | }, |
11 | 265 | }; |
0 commit comments