[![npm version](https://badgen.now.sh/npm/v/jsmex-trader-sdk)](https://www.npmjs.com/package/jsmex-trader-sdk)
[![npm downloads](https://badgen.now.sh/npm/dm/jsmex-trader-sdk)](https://www.npmjs.com/package/jsmex-trader-sdk)

jsmex-trader-sdk -- JSMEX Trader SDK
====================================================

![ALT JSMEX LOGO](https://static.jsmex.io/slice.svg)
[![NPM](https://nodei.co/npm/jsmex-trader-sdk.png)](https://www.npmjs.com/package/jsmex-trader-sdk)

`jsmex-trader-sdk` is a library for nodejs which gives you access to jsmex's API interface. This
allows you to create JavaScript strategies which can be deployed in jsmex server and executed in JavaScript environments completely *isolated* from each other.

* [JSMEX](#jsmex)
* [Requirements](#requirements)
* [API Documentation](#api-documentation)
	* [Trader](#class-trader-core)
* [Examples](#examples)
* [Jsmex Strategy Screenshot](#screenshots)
* [Pipeline](#pipeline)
* [Thanks](#thanks)
* [Contact us](#contact-us)
* [News](#news)
* [Chat](#chat)


JSMEX
-----
[JSMEX](https://jsmex.io) is the first programmable cryptocurrency exchange! [Sign Up](https://my.jsmex.io/app/auth/signup)
Every minute all the uploaded strategies are executed in parallel *isolated environments* on our servers.

REQUIREMENTS
------------

This project requires nodejs LTS version 10.4.0 (or later).

To install this module run `npm install jsmex-trader-sdk`.

API DOCUMENTATION
-----------------

 `jsmex-trader-sdk` is a CCXT extension library is used to build strategies for `jsmex runtime executor`. It provides quick access to market data for storage, analysis, visualization, indicator development, algorithmic trading, bot programming, and related software engineering.

### Class: `Trader` *[core]*
This class provide the core interface to ccxt API.
Trader instances will inherit all the wrapped ccxt methods and will execute in sequence onInit, onTick and onExit.

##### `Trader.ccxtExchanges.jsmex.ccxtInstance`
This is the ccxt instance of the jsmex exchange, if you are not interested in using the wrapper you can directly refer to this reference.

##### `Trader.fetchDepth(options)`
* `options` *[object]*
	* `symbol` *[string]* - Trading symbol
* return a *[Promise<DepthDTO>]*

##### `Trader.fetchOHLCV(options)`
* `options` *[object]*
	* `symbol` *[string]* - Trading symbol
	* `resolution` *[string]* - Kline resolution
	* `since` *[number]* - Time in milliseconds
	* `limit` *[number]* - Number of klines
* return a *[Promise<KlineDTOArray>]*

##### `Trader.cancelOrder(options)`
* `options` *[object]*
	* `symbol` *[string]* - Trading symbol
	* `id` *[number]* - id
* return a *[Promise<OrderDTO>]*

##### `Trader.cancelAllOrders(options)`
* return a *[Promise<OrderDTOArray>]*

##### `Trader.fetchOrders(options)`
* `options` *[object]*
	* `symbol` *[string]* - Trading symbol
	* `offset` *[number]* - Offset
	* `limit` *[number]* - Limit records 
* return a *[Promise<OrderDTOArray>]*

##### `Trader.fetchTransactions(options)`
* `options` *[object]*
	* `symbol` *[string]* - Trading symbol
	* `offset` *[number]* - Offset
	* `limit` *[number]* - Limit records 
* return a *[Promise<TransactionDTOArray>]*

##### `Trader.putOrder(options)`
* `options` *[object]*
	* `symbol` *[string]* - Trading symbol
	* `price` *[string]* - Price
	* `amount` *[string]* - Amount
	* `side` *[string]* - Side
	* `type` *[string]* - Type
* return a *[Promise<OrderDTO>]*

##### `Trader.pipeline(options)`
This is restricted API, it allows bulk operations
* `options` *[object]*
	* `symbol` *[string]* - Trading symbol
	* `pipeline` *[array]* - Pipeline array
* return a *[Promise<Array>]*

##### `Trader.balance(options)`
* return a *[Promise<object>]*

##### `Trader.fetchKeyValueStorage(options)`
List key-value storage
* return an *[object]*

##### `Trader.setKeyValueStorage(options)`
Set an element in key-value storage
* `options` *[object]*
	* `key` *[string]* - Key
	* `value` *[string]* - Value
* return an *[object]*

##### `Trader.setBulkKeyValueStorage(options)`
Bulk set in key-value storage
* `options` *[object]*
	* `bulk` *[array]* - array of key-value object as described in setKeyValueStorage
* return an *[object]*

##### `Trader.getKeyValueStorage(options)`
Get an element in key-value storage
* `options` *[object]*
	* `key` *[string]* - Key
* return an *[object]*

##### `Trader.deleteKeyValueStorage(options)`
Delete an element in key-value storage
* `options` *[object]*
	* `key` *[string]* - Key
* return an *[object]*

##### `Trader.open(index)`
* `index` *[number]* - Kline index
* return a *[BigNumber]*

##### `Trader.high(index)`
* `index` *[number]* - Kline index
* return a *[BigNumber]*

##### `Trader.low(index)`
* `index` *[number]* - Kline index
* return a *[BigNumber]*

##### `Trader.close(index)`
* `index` *[number]* - Kline index
* return a *[BigNumber]*

##### `Trader.volume(index)`
* `index` *[number]* - Kline index
* return a *[BigNumber]*

##### `Trader.balance(index)`
* return a *[object]*

##### `Trader.transactions(index)`
* return a *[TransactionDTO]*

##### `Trader.orders(index)`
* return a *[OrderDTO]*

##### `Trader.getPublicData(fetchDepthParams, fetchOHLCVParams)`
Automatically called in `Trader._onInit`, results are parsed in `Trader.update` 
* `fetchDepthParams` *[object]* - ccxt interface params
* `fetchOHLCVParams` *[object]* - ccxt interface params
* return a *[Promise]*

##### `Trader.getPrivateData(fetchOrdersParams, fetchTransactionsParams)`
Automatically called in `Trader._onInit`, results are parsed in `Trader.update` 
* `fetchOrdersParams` *[object]* - ccxt interface params
* `fetchTransactionsParams` *[object]* - ccxt interface params
* return a *[Promise]*


EXAMPLES
--------
Below is a sample program which shows basic usage of the library.

```js
// file index.js
(async function () {
  try {
    const PanicCloseStrategy = require('./strategy.js');
    await PanicCloseStrategy.start()
  } catch (err) {
    console.error('err', err)
  }
})()

```

```js
// file strategy.js
global.log = global.log || console.log 
global.error = global.error || console.error

// never use console in your program, it doesn't exists in the isolated environment, use global.log in order to receive logs in your account dashboard
module.exports = class extends (global.Trader || require('jsmex-trader-sdk')) {
  constructor (options) {
    super(options)
    this.defaultSymbol = this.options.defaultSymbol
  }

  /**
     * SamplePanicCloseStrategy on init method
     *
     * @author jsb4ch <jsb4ch@jsmex.io>
     * @since 1.1.0
    */
  onInit () {

  }

  /**
     * SamplePanicCloseStrategy on tick method
     *
     * @author jsb4ch <jsb4ch@jsmex.io>
     * @since 1.1.0
    */
  async onTick () {
    try {
      let close = this.close()
      let yesterdayOpen = this.open(24)
      let rate = (
        close.minus(yesterdayOpen)
      ).div(yesterdayOpen)
      if (rate.lte(-0.2)) {
        return this.cancelAllOrders()
      } else {
        global.log(rate.toNumber())
      }
    } catch (err) {
      global.error(err)
    }
  }

  /**
     * SamplePanicCloseStrategy on tick method
     *
     * @author jsb4ch <jsb4ch@jsmex.io>
     * @since 1.1.0
    */
  onExit () {

  }

  /**
     * SamplePanicCloseStrategy static options
     *
     * @author jsb4ch <jsb4ch@jsmex.io>
     * @since 1.1.0
    */
  static get options () {
    return {
      ...this.defaultOptions,
      defaultSymbol: 'BTC/USDT',
      exchanges: {
        jsmex: {
          wwwDomain: 'jsmex.io',
          apiDomain: 'api.jsmex.io'
        }
      },
      ccxt: {
        fetchOHLCV: {
          resolution: '10m',
          limit: 500
        }
      }
    }
  }
}
```

For othe examples, like how to use key-value storage, check out `examples` directory.

SCREENSHOTS
-----------
Strategy Editor
![ALT JSMEX SCREENSHOT](https://static.jsmex.io/screenshot.png)

Tradingview (test environment)
![ALT JSMEX SCREENSHOT](https://static.jsmex.io/tradingview.png)

PIPELINE
--------
In our pipeline for 2020 there are really interesting features:
* Q1 Strategy performance ranking
* Q2 Strategy Copy Trading

THANKS
------
Special thanks to:
* CCXT Team
* Hapi for Joi
* MikeMcl for bignumber.js

CONTACT US
----------
jsb4ch@jsmex.io

NEWS
----
https://t.me/jsmexio

TWITTER
-------
https://twitter.com/jsmexchange