-
Notifications
You must be signed in to change notification settings - Fork 8
/
Oanda.js
53 lines (51 loc) · 1.77 KB
/
Oanda.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
const qs = require('querystring')
const Oanda = {
// Initalizes the API-WRAPPER Object with config inputs
init: function(config) {
const Accounts = require('./lib/endpoints/accounts')
const Instruments = require('./lib/endpoints/instruments')
const Positions = require('./lib/endpoints/positions')
const Pricing = require('./lib/endpoints/pricing')
const Orders = require('./lib/endpoints/orders')
const Transactions = require('./lib/endpoints/transactions')
const Trades = require('./lib/endpoints/trades')
Object.assign(this, Accounts, Instruments, Positions, Transactions, Trades, Orders, Pricing)
this.baseURL = (function() {
if (config.env === 'fxPractice') {
return 'https://api-fxpractice.oanda.com/v3/'
} else {
return 'https://api-fxtrade.oanda.com/v3/'
}
})()
this.accountURL = this.baseURL + 'accounts/' + config.accountID
this.accountID = config.accountID
this.header = {
headers: {
'Authorization': config.auth,
'Accept-Datetime-Format': config.dateFormat
}
}
},
formatQuery: function(query) {
if (Object.values(this.header.headers).includes('RFC3339')) {
if (query.time) query.time = query.time.toJSON()
if (query.from) query.from = query.from.toJSON()
if (query.to) query.to = query.to.toJSON()
}
for (let prop in query) {
if (!!Array.isArray(query[prop])) query[prop] = query[prop].join()
}
return '?' + qs.stringify(query)
},
formatTime: function(time) {
if (time !== undefined) {
if (Object.values(this.header.headers).includes('RFC3339')) {
return '?time=' + time.toJSON().replace(/[:]/g, "%3A")
} else {
return '?time=' + time
}
}
return ''
}
}
module.exports = Oanda