mobius-trader-api

2.0.2 • Public • Published

MobiusTrader 7 JavaScript Manager API

| English | Русский |

Install

npm install mobius-trader-api --save

or

yarn add mobius-trader-api

Usage

import MobiusTrader from 'mobius-trader-api';

const config = {
  url: 'https://mtrader7api.com/v2', 
  token: '01234567-89ab-cdef-0123-4556789abcde',
};

async function run() {
  const mt7 = await MobiusTrader.getInstance(config);

  const clientId = 123;
  
  try {
    const info = await mt7.call('ClientGet', {
      Id: clientId
    });
    console.log(info);
  } catch (e) {
    console.error(e);
  }
}

run();

Available JSON-RPC API Methods

| English | Русский |

This library is just a wrapper for the methods described in the documentation.

All methods described in the documentation can be called using the call() method:

const client = await mt7.call('ClientGet', {
  'Id': clientId,
});

Search

For search, a universal Search method is implemented, which is very similar to SQL:

const tradingAccounts = await mt7.call('Search',{
    Context: 'TradingAccounts', 
    Select: ['Id'], 
    Where: [
        'ClientId', '=', clientId, 'AND', 
        'CurrencyId', '=', 23
    ], 
    Limit: 10,
    Offset: 0,
    SortBy: 'Id',
    SortDir: 'ASC',
});

Fields:

  • Context - What data needs to be found. Available options: Clients, TradingAccounts, Orders, BinaryOptions
  • Select - List of fields to be returned
  • Where - Search conditions
  • SortBy - Sort field
  • SortDir - Sorting direction. Options available: ASC и DESC
  • GroupBy - Grouping field
  • Limit - The number of rows returned
  • Offset - The number of rows that will be offset

Required fields: Context, Select и Where

####Query Builder This library provides Query Builder, which allows you to more conveniently work with search.

Example:

const response = await mt7.search(
    'Ticket',
    'OpenTime',
    'OpenTime',
    'TradeCmd',
    'Volume',
    'OpenPrice',
    'ClosePrice',
    'SymbolId',
    'Profit',
    'Commission',
    'Swap',
    [mt7.expr('Profit + Commission + Swap'), 'TotalProfit']
  )
    .from(MobiusTrader.SEARCH_CONTEXT.Orders)
    .where('TradingAccountId', '=', 123)
    .andWhere('CloseTime', '>', 0)
    .andWhere('TradeCmd', 'IN', [
      MobiusTrader.TradeCmd.BUY,
      MobiusTrader.TradeCmd.SELL,
    ])
    .limit(10)
    .offset(0)
    .orderBy('Ticket', 'DESC')
    .execute();

// Will return an array of objects with fields listed in select
const orders = response.asArray();

// Array of Tickets
const tickets = response.asArray('Ticket');

// An object where the key is Ticket, and the value is an object with fields.
const mapByTicket = response.asMap('Ticket');

// The object where the key is Ticket and the value is Profit.
const mapProfits = response.asMap('Ticket', 'Profit');

// Returns the first record. Useful if only one entry is selected.
const first = response.first();

Building a query begins with the search() method, which returns an instance of the Search class and makes it possible to assemble subsequent methods for the current object in one chain. As arguments, the method accepts a list of fields that need to be returned (Select in the API).

An array is used to specify the alias for the field:

['FieldName', 'FieldAlias']

If it is necessary to return the aggregated result, then the expr() method is used:

[mt7.expr('Profit + Commission + Swap'), 'TotalProfit']

To specify the context (Context from the API) of the search, the from() method is used. Available options are listed in MobiusTrader.SEARCH_CONTEXT

In order to manipulate query conditions, there are where(), andWhere() and orWhere() methods.

All these methods take three parameters: the name of the field, the operator (equal, greater, less, etc.) and the value to be searched.

Available search operators:

  • >
  • >=
  • <
  • <=
  • !=
  • =
  • IN
  • NOT IN
  • LIKE
  • NOT LIKE
  • REGEXP
  • NOT REGEXP

In case you need to get a multiple condition, special “brackets” are provided whereOpen(), whereClose(), andWhereOpen(), andWhereClose(), orWhereOpen(), orWhereClose().

Suppose you want to select a deal with current 1 and an opening date more than a day ago, or choose a deal with identifier 2 and an opening date less than a day ago.

const response = mt7.search('Ticket', 'CloseTime')
        .from(MobiusTrader.SEARCH_CONTEXT.Orders)
        .whereOpen()
        .where('Ticket', '=', 1)
        .andWhere('CloseTime', '<', Date.now() - 86400000)
        .whereClose()
        .orWhereOpen()
        .where('Ticket', '=', 2)
        .andWhere('CloseTime', '>', Date.now() - 86400000)
        .orWhereClose()
        .execute();

To sort the data, the orderBy() method is used, which takes 2 parameters: the field and the sort direction (SortBy and SortDir in the API)

.orderBy('Ticket', 'DESC')

To limit the number of returned records, the limit() method is used.

If necessary, make a shift (for example, when paginating) offset() is used.

Readme

Keywords

Package Sidebar

Install

npm i mobius-trader-api

Weekly Downloads

17

Version

2.0.2

License

ISC

Unpacked Size

51.5 kB

Total Files

32

Last publish

Collaborators

  • mobius-soft