# AOHost

The AOHost class provides a wrapper around the algo order system, and manages lifetime events/order execution. Internally it hosts a Manager instance from bfx-api-node-core for communication with the Bitfinex API, and listens for websocket stream events in order to update order state/trigger algo order events.

Execution is handled by an event system, with events being triggered by Bitfinex API websocket stream payloads, and the algo orders themselves.

To start/stop algo orders, `gid = startAO(id, args)` and `stopAO(gid)` methods are provided, with the generated group ID (`gid`) being the same as that used for all atomic orders created by the individual algo orders.

## Constructor

#### new AOHost(argsopt)

Source:
  - ao_host.js, line 68

##### Parameters:

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

`args`

object

<optional>

arguments

###### Properties

Name | Type | Attributes | Description
--- | --- | --- | ---
`db` | object | <optional> | optional
`wsURL` | string | <optional> | wss://api.bitfinex.com/ws/2
`restURL` | string | <optional> | https://api.bitfinex.com
`agent` | object | <optional> | optional proxy agent
`aos` | Array.<object> | <optional> | algo orders to manage
`dms` | number | <optional> | dead man switch, active 4 (default)

### Members

#### (static) TEARDOWN_GRACE_PERIOD_MS :number

Source:
  - ao_host.js, line 760
Default Value:
  - 10000

How long orders are allowed to settle for before teardown in ms.

##### Type:

- number

### Methods

#### aosRunning() → {boolean}

Source:
  - ao_host.js, line 520

##### Returns:

aosRunning - true if any algo order is currently running

Type
  boolean

#### close() → {Promise}

Source:
  - ao_host.js, line 140

Close the exchange adapter connection.

##### Returns:

p - resolves on connection close

Type
  Promise

#### connect()

Source:
  - ao_host.js, line 182

Opens a new socket connection on the internal adapter

#### getAdapter() → {object}

Source:
  - ao_host.js, line 124

Get configured exchange adapter

##### Returns:

adapter

Type
  object

#### getAO(id) → {object}

Source:
  - ao_host.js, line 201

Returns the algo order definition identified by the provided ID

##### Parameters:

Name | Type | Description
--- | --- | ---
`id` | string | i.e. bfx-iceberg

##### Returns:

aoDef

Type
  object

#### getAOInstance(gid) → {object}

Source:
  - ao_host.js, line 211

Returns the active AO instance state identified by the provided GID

##### Parameters:

Name | Type | Description
--- | --- | ---
`gid` | string | algo order group ID

##### Returns:

state - algo order state

Type
  object

#### getAOInstances() → {Array.<object>}

Source:
  - ao_host.js, line 220

Returns an array of all running algo order instances

##### Returns:

aoInstances

Type
  Array.<object>

#### getAOs() → {Array}

Source:
  - ao_host.js, line 191

Fetch configured algorithmic orders

##### Returns:

aos

Type
  Array

#### reconnect()

Source:
  - ao_host.js, line 131

Disconnect & reconnect the exchange adapter

#### (async) startAO(id, args, gidCBopt) → {string}

Source:
  - ao_host.js, line 419

Creates and starts a new algo order instance, based on the AO def identified by the supplied ID

##### Parameters:

Name | Type | Attributes | Description
--- | --- | --- | ---
`id` | string |  | algo order definition ID, i.e. bfx-iceberg
`args` | object |  | algo order arguments/parameters
`gidCB` | function | <optional> | callback to acquire GID prior to ao:start

##### Returns:

gid - instance GID

Type
  string

#### (async) stopAO(gid)

Source:
  - ao_host.js, line 493

Stops an algo order instance by GID

##### Parameters:

Name | Type | Description
--- | --- | ---
`gid` | string | algo order instance GID

### Events

#### dataBook

Source:
  - ao_host.js, line 305

Triggered when an order book update is received.

##### Parameters:

Name | Type | Description
--- | --- | ---
`update` | Array.<Array> | incoming snapshot or price level
`meta` | EventMetaInformation | source channel information

#### dataCandles

Source:
  - ao_host.js, line 287

Triggered when a candle snapshot or individual candle is received.

##### Parameters:

Name | Type | Description
--- | --- | ---
`update` | Array.<Array> | incoming snapshot or single candle
`meta` | EventMetaInformation | source channel information

#### dataManagedBook

Source:
  - ao_host.js, line 324

Triggered when an order book update is received, and an internally managed order book instance is updated. The entire order book is passed to the event listeners.

##### Parameters:

Name | Type | Description
--- | --- | ---
`book` | object | full order boook
`meta` | EventMetaInformation | source channel information

##### Listeners of This Event:

- module:AccumulateDistribute.onDataManagedBook
- module:TWAP.onDataManagedBook

#### dataManagedCandles

Source:
  - ao_host.js, line 344

Triggered when a candle update is received, and an internally managed candle dataset is updated. The entire dataset is passed to the event listeners.

##### Parameters:

Name | Type | Description
--- | --- | ---
`candles` | Array.<object> | full dataset
`meta` | EventMetaInformation | source channel information

##### Listeners of This Event:

- module:AccumulateDistribute.onDataManagedCandles
- module:MACrossover.onDataManagedCandles

#### dataNotification

Source:
  - ao_host.js, line 232

Triggered when a notification is received.

##### Parameters:

Name | Type | Description
--- | --- | ---
`notification` | Array.<Array> | incoming notification data
`meta` | EventMetaInformation | source channel information

#### dataTicker

Source:
  - ao_host.js, line 251

Triggered when a ticker is received.

##### Parameters:

Name | Type | Description
--- | --- | ---
`ticker` | Array.<Array> | incoming ticker data
`meta` | EventMetaInformation | source channel information

#### dataTrades

Source:
  - ao_host.js, line 269

Triggered when a trade snapshot or single trade is received

##### Parameters:

Name | Type | Description
--- | --- | ---
`update` | Array.<Array> | incoming snapshot or single trade
`meta` | EventMetaInformation | source channel information

##### Listeners of This Event:

- module:AccumulateDistribute.onDataTrades
- module:TWAP.onDataTrades

#### errorsInsufficientBalance

Source:
  - host/events/insufficient_balance.js, line 18

Triggered when an order fails due to have insufficient balance

##### Parameters:

Name | Type | Description
--- | --- | ---
`order` | object | the order that failed
`notification` | object | the incoming notification

##### Listeners of This Event:

- module:DefaultErrorHandlers.onErrorInsufficientBalance

#### errorsMinimumSize

Source:
  - host/events/minimum_size_error.js, line 18

Triggered when an order fails due to being below the minimum size for that market.

##### Parameters:

Name | Type | Description
--- | --- | ---
`order` | object | the order that failed
`notification` | object | the incoming notification

##### Listeners of This Event:

- module:DefaultErrorHandlers.onErrorMinimumSize

#### lifeStart

Source:
  - ao_host.js, line 555

Triggered when an algorithmic order begins execution.

##### Listeners of This Event:

- module:AccumulateDistribute.onLifeStart
- module:Iceberg.onLifeStart
- module:MACrossver.onLifeStart
- module:OCOCO.onLifeStart
- module:PingPong.onLifeStart
- module:TWAP.onLifeStart

#### lifeStop

Source:
  - ao_host.js, line 593

Triggered when an algorithmic order ends execution.

##### Listeners of This Event:

- module:AccumulateDistribute.onLifeStop
- module:Iceberg.onLifeStop
- module:MACrossover.onLifeStop
- module:OCOCO.onLifeStop
- module:PingPong.onLifeStop
- module:TWAP.onLifeStop

#### ordersOrderCancel

Source:
  - host/ws2/process_message.js, line 97

Triggered on atomic order cancellation

##### Parameters:

Name | Type | Description
--- | --- | ---
`order` | object | the order that was cancelled

##### Listeners of This Event:

- module:AccumulateDistribute.onOrdersOrderCancel
- module:Iceberg.onOrdersOrderCancel
- module:MACrossover.onOrdersOrderCancel
- module:OCOCO.onOrdersOrderCancel
- module:OCOCO.onOrdersOrderFill
- module:PingPong.onOrdersOrderCancel
- module:TWAP.onOrdersOrderCancel

#### ordersOrderError

Source:
  - host/ws2/process_message.js, line 120

Triggered on a generic order error

##### Parameters:

Name | Type | Description
--- | --- | ---
`order` | object | the order that caused the error

##### Listeners of This Event:

- module:DefaultErrorHandlers.onOrdersOrderError

#### ordersOrderFill

Source:
  - host/ws2/process_message.js, line 105

Triggered on atomic order fill

##### Parameters:

Name | Type | Description
--- | --- | ---
`order` | object | the order that was filled

##### Listeners of This Event:

- module:AccumulateDistribute.onOrdersOrderFill
- module:Iceberg.onOrdersOrderFill
- module:PingPong.onOrdersOrderFill
- module:TWAP.onOrdersOrderFill