myfxbook-api-client
Version:
Myfxbook API client for Node.js
207 lines (206 loc) • 7.35 kB
JavaScript
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const node_fetch_1 = __importDefault(require("node-fetch"));
const querystring_1 = __importDefault(require("querystring"));
const API_ROOT_URL = 'https://www.myfxbook.com/api';
class MyfxbookApi {
constructor({ email, password }) {
this.email = email;
this.password = password;
}
/** Get session id (cached value is returned upon subsequent or parallel requests)*/
getSessionId() {
return __awaiter(this, void 0, void 0, function* () {
if (!this.session) {
this.getLoginDataPromise = this.getLoginDataPromise || this.login();
const loginData = yield this.getLoginDataPromise;
this.session = this.session || loginData.session;
}
return this.session;
});
}
makeApiCall(endpoint, params) {
return __awaiter(this, void 0, void 0, function* () {
const url = `${API_ROOT_URL}/${endpoint}.json?${querystring_1.default.stringify(params)}`;
const rawResponse = yield node_fetch_1.default(url, { method: 'post' });
const textResponse = yield rawResponse.text();
let isError = false;
let errorMessage = '';
let parsedData;
try {
parsedData = JSON.parse(textResponse);
if (parsedData.error) {
isError = true;
errorMessage = parsedData.message;
}
}
catch (error) {
const errText = `${endpoint} error: ${JSON.stringify(error)}`;
const originalResponse = `Original response: ${JSON.stringify(textResponse)}`;
isError = true;
errorMessage = `${errText}. ${originalResponse}`;
}
if (isError) {
throw new Error(errorMessage);
}
return parsedData;
});
}
/**
* Fetches login data object
*/
login() {
return __awaiter(this, void 0, void 0, function* () {
return this.makeApiCall('login', {
email: this.email,
password: this.password
});
});
}
/**
* Logs out from current session
*/
logout() {
return __awaiter(this, void 0, void 0, function* () {
return this.makeApiCall('logout', {
session: yield this.getSessionId()
});
});
}
/**
* Get list of all trading accounts
*/
getMyAccounts() {
return __awaiter(this, void 0, void 0, function* () {
return this.makeApiCall('get-my-accounts', {
session: yield this.getSessionId()
});
});
}
/**
* Get list of all watched accounts
*/
getWatchedAccounts() {
return __awaiter(this, void 0, void 0, function* () {
return this.makeApiCall('get-watched-accounts', {
session: yield this.getSessionId()
});
});
}
/**
* Get all open orders for a given account
* @param id id of a trading account
*/
getOpenOrders(id) {
return __awaiter(this, void 0, void 0, function* () {
return this.makeApiCall('get-open-orders', {
session: yield this.getSessionId(),
id: String(id)
});
});
}
/**
* Get all open trades for a given account
* @param id id of a trading account
*/
getOpenTrades(id) {
return __awaiter(this, void 0, void 0, function* () {
return this.makeApiCall('get-open-trades', {
session: yield this.getSessionId(),
id: String(id)
});
});
}
/**
* Get history of all trades for a given account
* @param id id of a trading account
*/
getHistory(id) {
return __awaiter(this, void 0, void 0, function* () {
return this.makeApiCall('get-history', {
session: yield this.getSessionId(),
id: String(id)
});
});
}
/**
* Get daily breakdown of all gains for a given account within time range
* @param id id of a trading account
* @param start start date, format : yyyy-MM-dd
* @param end end date, format : yyyy-MM-dd
*/
getDailyGain(id, start, end) {
return __awaiter(this, void 0, void 0, function* () {
return this.makeApiCall('get-daily-gain', {
session: yield this.getSessionId(),
id: String(id),
start,
end
});
});
}
/**
* Get total gain for a given account within time range
* @param id id of a trading account
* @param start start date, format : yyyy-MM-dd
* @param end end date, format : yyyy-MM-dd
*/
getGain(id, start, end) {
return __awaiter(this, void 0, void 0, function* () {
return this.makeApiCall('get-gain', {
session: yield this.getSessionId(),
id: String(id),
start,
end
});
});
}
/** Get Myfxbook Community Outlook data (https://www.myfxbook.com/community/outlook) */
getCommunityOutlook() {
return __awaiter(this, void 0, void 0, function* () {
return this.makeApiCall('get-community-outlook', {
session: yield this.getSessionId()
});
});
}
/**
* Get community outlook data broken down by a country for provided symbol
* @param symbol a trading instrument (currency pair)
*/
getCommunityOutlookByCountry(symbol) {
return __awaiter(this, void 0, void 0, function* () {
return this.makeApiCall('get-community-outlook-by-country', {
session: yield this.getSessionId(),
symbol
});
});
}
/**
* Get daily breakdown of all account data within time range
* @param id id of a trading account
* @param start start date, format : yyyy-MM-dd
* @param end end date, format : yyyy-MM-dd
*/
getDailyData(id, start, end) {
return __awaiter(this, void 0, void 0, function* () {
return this.makeApiCall('get-data-daily', {
session: yield this.getSessionId(),
id: String(id),
start,
end
});
});
}
}
exports.default = MyfxbookApi;