# MACrossover

MA Crossover triggers either a `MARKET` or a `LIMIT` order when two user-defined moving averages cross. Users can configure either a standard MA or an EMA individually for both long & short signals.

Source:
  - ma_crossover/index.js, line 21

##### Example

    await host.startAO('bfx-ma_crossover', {
      shortType: 'EMA',
      shortEMATF: '1m',
      shortEMAPeriod: '20',
      shortEMAPrice: 'close',
      longType: 'EMA',
      longEMATF: '1m',
      longEMAPeriod: '100',
      longEMAPrice: 'close',
      amount: 1,
      symbol: 'tEOSUSD',
      orderType: 'MARKET',
      action: 'Buy',
      _margin: false,
    })

##### Parameters:

Name | Type | Attributes | Description
--- | --- | --- | ---
`symbol` | string |  | symbol to trade on
`orderType` | string |  | LIMIT or MARKET
`orderPrice` | number |  | price for order if `orderType` is LIMIT
`amount` | number |  | total order amount
`_margin` | boolean |  | if false, order type is prefixed with EXCHANGE
`shortType` | string |  | MA or EMA
`shortEMATF` | string | <optional> | candle time frame for short EMA signal
`shortEMAPeriod` | number | <optional> | cadnel period for short EMA signal
`shortEMAPrice` | string | <optional> | candle price key for short EMA signal
`shortMATF` | string | <optional> | candle time frame for short MA signal
`shortMAPeriod` | number |  | cadnel period for short MA signal
`shortMAPrice` | string | <optional> | candle price key for short MA signal
`longType` | string |  | MA or EMA
`longEMATF` | string | <optional> | candle time frame for long EMA signal
`longEMAPeriod` | number | <optional> | cadnel period for long EMA signal
`longEMAPrice` | string | <optional> | candle price key for long EMA signal
`longMATF` | string | <optional> | candle time frame for long MA signal
`longMAPeriod` | number | <optional> | cadnel period for long MA signal
`longMAPrice` | string | <optional> | candle price key for long MA signal

##### Returns:

def

Type
  object

### Members

#### (static) generateOrder

Source:
  - ma_crossover/util/generate_order.js, line 6

Generates the atomic order as configured in the execution parameters

### Methods

#### (async, static) declareChannels(instance, host)

Source:
  - ma_crossover/meta/declare_channels.js, line 15

Declares necessary candle data channels for updating indicators and eventually triggering atomic order execution.

Part of the `meta` handler section.

##### Parameters:

Name | Type | Description
--- | --- | ---
`instance` | AOInstance | AO instance state
`host` | AOHost | algo host instance for declaring channel requirements

#### (static) declareEvents(instance, host)

Source:
  - ma_crossover/meta/declare_events.js, line 15
See:
  - module:MACrossover.onSelfSubmitOrder

Declares the internal `self:submit_order` handler to the host for event routing.

Part of the `meta` handler section.

##### Parameters:

Name | Type | Description
--- | --- | ---
`instance` | AOInstance | AO instance state
`host` | AOHost | algo host instance for event mapping

#### (static) genPreview(args) → {Array.<object>}

Source:
  - ma_crossover/meta/gen_preview.js, line 12

Stub to conform to the algo order schema.

Part of the `meta` handler section.

##### Parameters:

Name | Type | Description
--- | --- | ---
`args` | object | instance arguments

##### Returns:

preview

Type
  Array.<object>

#### (static) initState(args) → {object}

Source:
  - ma_crossover/meta/init_state.js, line 13

Creates an initial state object for an MACrossover instance to begin executing with.

Part of the `meta` handler section.

##### Parameters:

Name | Type | Description
--- | --- | ---
`args` | object | instance execution parameters

##### Returns:

initialState

Type
  object

#### (async, static) onDataManagedCandles(instance, candles, meta) → {Promise}

Source:
  - ma_crossover/events/data_managed_candles.js, line 22

If the instance has internal indicators, they are either seeded with the initial candle dataset or updated with new candles as they arrive. The candle dataset is saved on the instance state for order generation.

Indicator values are calculated, and if they have crossed the configured atomic order is submitted, and the `'exec:stop`' event is emitted to stop execution and trigger teardown.

##### Parameters:

Name | Type | Description
--- | --- | ---
`instance` | AOInstance | AO instance
`candles` | Array.<object> | incoming candles
`meta` | EventMetaInformation | source channel information

##### Listens to Events:

- AOHost~event:dataManagedCandles

##### Returns:

p - resolves on completion

Type
  Promise

#### (async, static) onLifeStop(instance) → {Promise}

Source:
  - ma_crossover/events/life_stop.js, line 11

Stub to conform to the algo order schema.

##### Parameters:

Name | Type | Description
--- | --- | ---
`instance` | AOInstance | AO instance

##### Listens to Events:

- AOHost~event:lifeStop

##### Returns:

p - resolves on completion

Type
  Promise

#### (async, static) onOrdersOrderCancel(instance, order) → {Promise}

Source:
  - ma_crossover/events/orders_order_cancel.js, line 14

Triggered when an atomic order cancellation is detected, cancels any open orders and emits the `'exec:stop'` event.

##### Parameters:

Name | Type | Description
--- | --- | ---
`instance` | AOInstance | AO instance
`order` | object | order that was cancelled

##### Listens to Events:

- AOHost~event:ordersOrderCancel

##### Returns:

p - resolves on completion

Type
  Promise

#### (async, static) onSelfSubmitOrder(instance) → {Promise}

Source:
  - ma_crossover/events/self_submit_order.js, line 14

Generates and submits the configured order.

##### Parameters:

Name | Type | Description
--- | --- | ---
`instance` | AOInstance | AO instance

##### Listens to Events:

- module:MACrossover~event:selfSubmitOrder

##### Returns:

p - resolves on completion

Type
  Promise

#### (static) processParams(data) → {object}

Source:
  - ma_crossover/meta/process_params.js, line 13

Converts a raw parameters Object received from an UI into a parameters Object which can be used by an MACrossover instance for execution.

Part of the `meta` handler section.

##### Parameters:

Name | Type | Description
--- | --- | ---
`data` | object | raw parameters from an UI

##### Returns:

parameters - ready to be passed to a fresh instance

Type
  object

#### (static) serialize(state) → {object}

Source:
  - ma_crossover/meta/serialize.js, line 14

Creates a POJO from an instance's state which can be stored as JSON in a database, and later loaded with the corresponding `module:MACrossover~unserialize` method.

Part of the `meta` handler section.

##### Parameters:

Name | Type | Description
--- | --- | ---
`state` | object | instance state to be serialized

##### Returns:

pojo - DB-ready plain JS object

Type
  object

#### (static) unserialize(loadedState) → {object}

Source:
  - ma_crossover/meta/unserialize.js, line 12

Converts a loaded POJO into a state object ready for live execution.

Part of the `meta` handler section.

##### Parameters:

Name | Type | Description
--- | --- | ---
`loadedState` | object | data from a DB

##### Returns:

instanceState - ready for execution

Type
  object

#### (static) validateParams(args) → {string}

Source:
  - ma_crossover/meta/validate_params.js, line 37

Verifies that a parameters Object is valid, and all parameters are within the configured boundaries for a valid MACrossover order.

Part of the `meta` handler section.

##### Parameters:

Name | Type | Description
--- | --- | ---

`args`

object

incoming parameters

###### Properties

Name | Type | Attributes | Description
--- | --- | --- | ---
`orderType` | string |  | LIMIT or MARKET
`orderPrice` | number |  | price for order if `orderType` is LIMIT
`amount` | number |  | total order amount
`shortType` | string |  | MA or EMA
`shortEMATF` | string | <optional> | candle time frame for short EMA signal
`shortEMAPeriod` | number | <optional> | cadnel period for short EMA signal
`shortEMAPrice` | string | <optional> | candle price key for short EMA signal
`shortMATF` | string | <optional> | candle time frame for short MA signal
`shortMAPeriod` | number |  | cadnel period for short MA signal
`shortMAPrice` | string | <optional> | candle price key for short MA signal
`longType` | string |  | MA or EMA
`longEMATF` | string | <optional> | candle time frame for long EMA signal
`longEMAPeriod` | number | <optional> | cadnel period for long EMA signal
`longEMAPrice` | string | <optional> | candle price key for long EMA signal
`longMATF` | string | <optional> | candle time frame for long MA signal
`longMAPeriod` | number | <optional> | cadnel period for long MA signal
`longMAPrice` | string | <optional> | candle price key for long MA signal

##### Returns:

error - null if parameters are valid, otherwise a description of which parameter is invalid.

Type
  string

### Events

#### selfSubmitOrder

Source:
  - ma_crossover/meta/declare_events.js, line 19

Triggers atomic order creation and teardown

##### Listeners of This Event:

- module:MACrossover.onSelfSubmitOrder