Simple FxTrade
A node js wrapper for the Oanda Rest v20 api to make things really simple.
Overview
This package is a wrapper around the Oanda Rest-v20 api. All request parameters, payloads or response structures are documented there so you won't find that information here.
The purpose of this package is to simplify the url constructions and expose the endpoints in a nice way for any Nodejs based projects. The package should also work as described in browsers via fx
global or by require
/ import
.
Here is an example:
const fx = ; // Just an example, you need an async function to use async / awaitconst run = async { // Configure api key, you can set env: OANDA_API_KEY instead and remove this call fx; // Get accounts for authorized user (using OANDA_API_KEY env var) const accounts: id = await fx // Set the id context for all future api calls fx; // Get the instruments for the account const instruments = await fx; console; // [...] logs many instruments} // Call the above function. Included to reduce any possible confusion;
Install
Install via npm with the following
npm i --save simple-fxtrade
Usage
Configuration
To change certain aspects of the api wrapper, you can use the configure
function. For example you may want to set the apiKey
.
// Use configure to change the default values which are shown belowfx; // You needn't set all values, a more realistic example may be likefx;
-
apiKey
- Default: env variableOANDA_API_KEY
. Sets theAuthorization
header key. Best practice would be to use some config or env management package to set this and never pass the value. But you can if you want. -
live
- Default:false
. This affects the endpoint url. If set to true the host isfxtrade.oanda.com
, when false itsfxpractice.oanda.com
-
accountId
- Default:not set
. If you know the accountId you could set it here. Most endpoints need the accountId set -
fullResponse
- Default:false
. When this not set to true, you get a short form response for all requests like the following:{status: 200,headers: {}, // Some headerscandles: [] // The relevant payload that you would expect}if the
fullResponse
is set to true then you get a fullaxios
response.
If you are messing around on a demo account, with the OANDA_API_KEY
set, then you may never need to use configure
. Instead you can just use setAccount
, in fact for most of the endpoints you must set the account before hand. Heres what it might look like if you maybe know the id.
// Usually get the accounts and the id, but then set it// All defaults are already configured to workfx;
Api Endpoints
The following api endpoints are implemented. In most cases the request parameters are the same and response is a direct pass through. Each heading matches with the official documentation headings.
Note: ALL api examples assume that you have setAccount(id)
as there are only 3 or so routes that dont need it set
Account
Only the accounts()
function can be used without setting the accountId
// GET /accountsconst accounts = await fx; // GET /accounts/:idconst account = await fx; // PATCH /accounts/:id/configurationawait ; // GET /accounts/:accountId/summary - Notice the accountId is used from the configconst account = await fxsummary; // GET /accounts/:accountId/instrumentsconst instruments = await fx; // GET /accounts/:id/changesconst changes = await fx;
Instrument
The only relevant function here is candles()
it doesn't require the accountId
.
// GET /instrument/:id/candlesconst candles instrument = await fx;
Order
All of the functions for orders require the accountId
to be set.
// GET /accounts/:accountId/ordersconst orders = await fx; // GET /accounts/:accountId/orders/:idconst orders = await fx; // POST /accounts/:accountId/ordersawait fxorders; // PUT /accounts/:accountId/orders/:idawait fxorders; // See oanda docs for payload examples // PUT /accounts/:accountId/orders/:id/cancelawait fxorders; // PUT /accounts/:accountId/orders/:id/clientExtensionsawait fxorders;
Trade
All of the functions require the accountId
to be set. See oanda docs for payload examples. They are just provided as an object parameter.
// GET /accounts/:accountId/tradesconst trades = await fx; // GET /accounts/:accountId/trades/:idconst trade = await fx; // PUT /accounts/:accountId/trades/:id/closeawait fxtrades // PUT /accounts/:accountId/trades/:id/clientExtensionsawait fxtrades; // PUT /accounts/:accountId/trades/:id/ordersawait fxtrades; // etc...
Position
Again all functions need the accountId
to have been set before hand.
// GET /accounts/:accountId/positionsconst positions = await fx; // GET /accounts/:accountId/positions/:id <- instrument idconst position = await fx; // PUT /accounts/:accountId/positions/:id/closeawait fxpositions;
Transaction
Set the accountId
first
// GET /accounts/:accountId/transactionsconst transactions = await fx // GET /accounts/:accountId/transactions/:idconst transactions = await fx
Pricing
// GET /accounts/:accountId/pricing?instruments=AUD_USDconst prices = await fx
Stream Endpoints
Good news, both the pricing
and the transactions
streams are implemented. Here is an example of using the pricing stream.
// GET /accounts/:accountId/pricing/stream?instruments=AUD_USDconst stream = await fxpricing; // Handle some datastream; // Sometime later when done with the streamstream;