## Functions
defineStrategy(args) ⇒ Object

Returns an initial strategy state object for the provided arguments. The state object can then be used with the various HF strategy methods.

getCandle(state, n, symbol, tf) ⇒ Object

Reverses candle sort, n = 0 for most recent candle

getCandles(state, forSymbol, forTF) ⇒ Array.<Object>
getDefaultCandles(state) ⇒ Array.<Object>

Returns the candle dataset for the default symbol/tf pair

getDefaultSymbol(strategyState) ⇒ string

Returns the default symbol for the strategy; a strategy can have either a set of multiple symbols, a single default 'symbol' defined, or both.

getDefaultTF(strategyState) ⇒ string

Returns the default time frame for the strategy; a strategy can have either a set of multiple time frames, a single default time frame, or both.

getDefaultTrades(state) ⇒ Array.<Object>

Returns the trade dataset for the default symbol

getLastPrice(state, symbol, tf) ⇒ Object

Get the last price & timestamp received for the specified symbol & tf.

getNumCandles(state, symbol, tf) ⇒ number
getNumTrades(state, symbol) ⇒ number
getPosition(state, forSymbol) ⇒ Object

Returns the position for the specified symbol

getTrade(state, n, symbol, tf) ⇒ Object

Reverses trade sort, n = 0 for most recent trade

getTrades(state, symbol) ⇒ Array.<Object>

Returns all known trades for the specified symbol

isLong(state, symbol) ⇒ boolean

Queries if a long position is open for the specified symbol

isShort(state, symbol) ⇒ boolean

Queries if a short position is open for the specified symbol

addIndicatorData(state, type, update, f)

Adds the provided data update packet to all strategy indicators matching the update type.

findIndicator(state, f) ⇒ Indicator

Returns the first indicator in the strategy for which the provided function evaluates to true.

forEachIndicator(state, f)

Calls the provided function with each indicator on the strategy.

indicatorsExec(strategyState, fn) ⇒ Object

Executes the specified function in all strategy indicators, and returns the results in a map matching the indicator map structure.

indicators(strategyState) ⇒ Object

Strategy indicators selector

indicatorsReady(state) ⇒ boolean

Checks if all indicators have been marked as ready (seed period fulfilled)

indicatorValues(strategyState) ⇒ Object

Returns a map of all indicator values for the provided strategy

resetIndicators(state)

Resets all of the strategy's indicators

seedPeriodForIndicators(state) ⇒ number

Returns the minimum seed period required for the strategy

updateIndicatorData(state, type, update, f)
closeOpenPositions(state) ⇒ Promise

Closes all open positions with market orders

closePosition(state, orderParams) ⇒ Promise

Closes an open position with an order. Throws an error if no position is open for the order's symbol.

closePositionLimit(state, orderParams) ⇒ Promise

Closes a position with a limit order

closePositionMarket(state, orderParams) ⇒ Promise

Closes a position with a market order

closePositionWithOrder(state, orderParams) ⇒ Promise

Closes an open position with an order. Throws an error if no position is open for the order's symbol.

openLongPosition(state, orderParams) ⇒ Promise

Alias for openPositon

openLongPositionLimit(state, orderParams) ⇒ Promise

Opens a new long position with a limit order

openLongPositionMarket(state, orderParams) ⇒ Promise

Opens a new long position with a market order

openPosition(state, orderParams) ⇒ Promise

Opens a position with a new order; resolves to an error if a position is already open.

openPositionLimit(state, orderParams) ⇒ Promise

Opens a new position with a limit order

openPositionMarket(state, orderParams) ⇒ Promise

Opens a new position with a market order

openPositionWithOrder(state, orderParams) ⇒ Promise

Submits a new order via ws2 with the supplied parameters, creates a new strategy trade and creates a position.

If no ws client is available, no data is saved & no order is dispatched

openShortPosition(state, orderParams) ⇒ Promise

Opens a short position (negates passed amount)

openShortPositionLimit(state, orderParams) ⇒ Promise

Opens a short position (negates passed amount)

openShortPositionMarket(state, orderParams) ⇒ Promise

Opens a short position (negates passed amount)

positionPL(state, symbol, closePrice) ⇒ number

Returns the P/L figure for the specified position, taking into account all trades & a close with the provided price (optional)

updateLongPosition(state, orderParams) ⇒ Promise
updateLongPositionLimit(state, orderParams) ⇒ Promise

Updates a long position with a limit order

updateLongPositionMarket(state, orderParams) ⇒ Promise

Updates a long position with a market order

updatePosition(state, orderParams) ⇒ Promise

Alias for updatePositionWithOrder

updatePositionLimit(state, orderParams) ⇒ Promise

Updates a position with a limit order

updatePositionMarket(state, orderParams) ⇒ Promise

Updates a new position with a market order

updatePositionWithOrder(state, orderParams) ⇒ Promise

Submits a new order via ws2 with the supplied parameters, creates a new strategy trade and updates the current position.

If no ws client is available, no data is saved & no order is dispatched

updateShortPosition(state, orderParams) ⇒ Promise

Updates a short position (negates passed amount)

updateShortPositionLimit(state, orderParams) ⇒ Promise

Updates a short position (negates passed amount)

updateShortPositionMarket(state, orderParams) ⇒ Promise

Updates a short position (negates passed amount)

onCandle(state, candle) ⇒ Promise

Called for an incoming (new) candle. Propagates the candle via onPriceUpdate

onCandleUpdate(state, candle) ⇒ Promise

Called for an incoming candle update, for a candle previously passed to onCandle() (same candle, new prices). Propagates the candle via onPriceUpdate

onPriceUpdate(state, update) ⇒ Promise

Passes the incoming price update to the relevant strategy lifecycle methods:

onPriceUpdate - always called onEnter - called if no position is open onUpdateShort - called if a short position is open onUpdateLong - called if a long position is open onUpdateClosing - called if a position is open but currently closing onUpdate - called if a position is open

Returns the resulting strategy state

onSeedCandle(state, candle) ⇒ Promise

Called for an incoming (new) seed-period candle

onSeedCandleUpdate(state, candle) ⇒ Promise

Called for an incoming seed-period candle update, for a candle previously passed to onSeedCandle() (same candle, new prices)

onSeedTrade(state, trade) ⇒ Promise

Called for incoming seed-period trades.

onTrade(state, candle) ⇒ Promise

Called for an incoming (new) trade. Propagates the trade via onPriceUpdate

## defineStrategy(args) ⇒ Object Returns an initial strategy state object for the provided arguments. The state object can then be used with the various HF strategy methods. **Kind**: global function **Returns**: Object - state - initial strategy state | Param | Type | Description | | --- | --- | --- | | args | Object | | | args.id | string | strategy ID | | args.name | string | strategy name (human readable) | | args.margin | boolean | if true, trades on margin, otherwise exchange | | args.symbol | string | default symbol for data/trades | | args.tf | string | default candle time frame | | args.candlePrice | string | key to access on candle for price (defaults to close) | | args.indicators | Object | managed indicators map | | args.onPriceUpdate | Method | | | args.onEnter | Method | | | args.onUpdate | Method | | | args.onUpdateLong | Method | | | args.onUpdateShort | Method | | | args.onUpdateClosing | Method | | | args.onPositionOpen | Method | | | args.onPositionUpdate | Method | | | args.onPositionClose | Method | | | args.onStart | Method | | | args.onStop | Method | | ## getCandle(state, n, symbol, tf) ⇒ Object Reverses candle sort, n = 0 for most recent candle **Kind**: global function **Returns**: Object - candle | Param | Type | Default | Description | | --- | --- | --- | --- | | state | Object | | | | n | number | 0 | index | | symbol | string | | | | tf | string | | | ## getCandles(state, forSymbol, forTF) ⇒ Array.<Object> **Kind**: global function **Returns**: Array.<Object> - candles - sorted oldest first | Param | Type | Description | | --- | --- | --- | | state | Object | | | forSymbol | string | defaults to strategy symbol | | forTF | string | defaults to strategy tf | ## getDefaultCandles(state) ⇒ Array.<Object> Returns the candle dataset for the default symbol/tf pair **Kind**: global function **Returns**: Array.<Object> - candles | Param | Type | | --- | --- | | state | Object | ## getDefaultSymbol(strategyState) ⇒ string Returns the default symbol for the strategy; a strategy can have either a set of multiple symbols, a single default 'symbol' defined, or both. **Kind**: global function **Returns**: string - defaultSymbol | Param | Type | | --- | --- | | strategyState | Object | ## getDefaultTF(strategyState) ⇒ string Returns the default time frame for the strategy; a strategy can have either a set of multiple time frames, a single default time frame, or both. **Kind**: global function **Returns**: string - defaultTF | Param | Type | | --- | --- | | strategyState | Object | ## getDefaultTrades(state) ⇒ Array.<Object> Returns the trade dataset for the default symbol **Kind**: global function **Returns**: Array.<Object> - trades | Param | Type | | --- | --- | | state | Object | ## getLastPrice(state, symbol, tf) ⇒ Object Get the last price & timestamp received for the specified symbol & tf. **Kind**: global function **Returns**: Object - pp - price point, { mts, price } (null if unavailable) | Param | Type | Description | | --- | --- | --- | | state | Object | | | symbol | string | optional, defaults to strategy symbol | | tf | string | optional, defaults to strategy tf | ## getNumCandles(state, symbol, tf) ⇒ number **Kind**: global function **Returns**: number - numCandles | Param | Type | Description | | --- | --- | --- | | state | Object | | | symbol | string | defaults to strategy symbol | | tf | string | defaults to strategy time frame | ## getNumTrades(state, symbol) ⇒ number **Kind**: global function **Returns**: number - numTrades | Param | Type | Description | | --- | --- | --- | | state | Object | | | symbol | string | defaults to strategy symbol | ## getPosition(state, forSymbol) ⇒ Object Returns the position for the specified symbol **Kind**: global function **Returns**: Object - position - may be null | Param | Type | Description | | --- | --- | --- | | state | Object | | | forSymbol | string | optional, defaults to strategy symbol | ## getTrade(state, n, symbol, tf) ⇒ Object Reverses trade sort, n = 0 for most recent trade **Kind**: global function **Returns**: Object - trade | Param | Type | Default | Description | | --- | --- | --- | --- | | state | Object | | | | n | number | 0 | index | | symbol | string | | | | tf | string | | | ## getTrades(state, symbol) ⇒ Array.<Object> Returns all known trades for the specified symbol **Kind**: global function **Returns**: Array.<Object> - trades - sorted oldest first | Param | Type | Default | Description | | --- | --- | --- | --- | | state | Object | | | | symbol | string | null | defaults to strategy symbol | ## isLong(state, symbol) ⇒ boolean Queries if a long position is open for the specified symbol **Kind**: global function **Returns**: boolean - isLong | Param | Type | Description | | --- | --- | --- | | state | Object | | | symbol | string | optional, defaults to strategy symbol | ## isShort(state, symbol) ⇒ boolean Queries if a short position is open for the specified symbol **Kind**: global function **Returns**: boolean - isShort | Param | Type | Description | | --- | --- | --- | | state | Object | | | symbol | string | optional, defaults to strategy symbol | ## addIndicatorData(state, type, update, f) Adds the provided data update packet to all strategy indicators matching the update type. **Kind**: global function | Param | Type | | --- | --- | | state | Object | | type | string | | update | Object | | f | function | ## findIndicator(state, f) ⇒ Indicator Returns the first indicator in the strategy for which the provided function evaluates to true. **Kind**: global function **Returns**: Indicator - i - null if not found | Param | Type | Description | | --- | --- | --- | | state | Object | strategy state | | f | function | called with (indicator, id) | ## forEachIndicator(state, f) Calls the provided function with each indicator on the strategy. **Kind**: global function | Param | Type | | --- | --- | | state | Object | | f | function | ## indicatorsExec(strategyState, fn) ⇒ Object Executes the specified function in all strategy indicators, and returns the results in a map matching the indicator map structure. **Kind**: global function **Returns**: Object - results | Param | Type | Description | | --- | --- | --- | | strategyState | Object | | | fn | string | function name | ## indicators(strategyState) ⇒ Object Strategy indicators selector **Kind**: global function **Returns**: Object - indicators | Param | Type | | --- | --- | | strategyState | Object | ## indicatorsReady(state) ⇒ boolean Checks if all indicators have been marked as ready (seed period fulfilled) **Kind**: global function **Returns**: boolean - ready - true if all indicators are ready | Param | Type | | --- | --- | | state | Object | ## indicatorValues(strategyState) ⇒ Object Returns a map of all indicator values for the provided strategy **Kind**: global function **Returns**: Object - iv | Param | Type | | --- | --- | | strategyState | Object | ## resetIndicators(state) Resets all of the strategy's indicators **Kind**: global function | Param | Type | Description | | --- | --- | --- | | state | Object | strategy state | ## seedPeriodForIndicators(state) ⇒ number Returns the minimum seed period required for the strategy **Kind**: global function **Returns**: number - seedPeriod | Param | Type | | --- | --- | | state | Object | ## updateIndicatorData(state, type, update, f) **Kind**: global function | Param | Type | | --- | --- | | state | Object | | type | string | | update | Object \| number | | f | function | ## closeOpenPositions(state) ⇒ Promise Closes all open positions with market orders **Kind**: global function **Returns**: Promise - p - resolves to nextState | Param | Type | | --- | --- | | state | Object | ## closePosition(state, orderParams) ⇒ Promise Closes an open position with an order. Throws an error if no position is open for the order's symbol. **Kind**: global function **Returns**: Promise - p - resolves to nextState | Param | Type | | --- | --- | | state | Object | | orderParams | Object | ## closePositionLimit(state, orderParams) ⇒ Promise Closes a position with a limit order **Kind**: global function **Returns**: Promise - p - resolves to nextState | Param | Type | | --- | --- | | state | Object | | orderParams | Object | ## closePositionMarket(state, orderParams) ⇒ Promise Closes a position with a market order **Kind**: global function **Returns**: Promise - p - resolves to nextState | Param | Type | | --- | --- | | state | Object | | orderParams | Object | ## closePositionWithOrder(state, orderParams) ⇒ Promise Closes an open position with an order. Throws an error if no position is open for the order's symbol. **Kind**: global function **Returns**: Promise - p - resolves to nextState | Param | Type | | --- | --- | | state | Object | | orderParams | Object | ## openLongPosition(state, orderParams) ⇒ Promise Alias for openPositon **Kind**: global function **Returns**: Promise - p - resolves to nextState | Param | Type | | --- | --- | | state | Object | | orderParams | Object | ## openLongPositionLimit(state, orderParams) ⇒ Promise Opens a new long position with a limit order **Kind**: global function **Returns**: Promise - p - resolves to nextState | Param | Type | | --- | --- | | state | Object | | orderParams | Object | ## openLongPositionMarket(state, orderParams) ⇒ Promise Opens a new long position with a market order **Kind**: global function **Returns**: Promise - p - resolves to nextState | Param | Type | | --- | --- | | state | Object | | orderParams | Object | ## openPosition(state, orderParams) ⇒ Promise Opens a position with a new order; resolves to an error if a position is already open. **Kind**: global function **Returns**: Promise - p - resolves to nextState | Param | Type | Description | | --- | --- | --- | | state | Object | | | orderParams | Object | passed directly to order constructor | ## openPositionLimit(state, orderParams) ⇒ Promise Opens a new position with a limit order **Kind**: global function **Returns**: Promise - p - resolves to nextState | Param | Type | | --- | --- | | state | Object | | orderParams | Object | ## openPositionMarket(state, orderParams) ⇒ Promise Opens a new position with a market order **Kind**: global function **Returns**: Promise - p - resolves to nextState | Param | Type | | --- | --- | | state | Object | | orderParams | Object | ## openPositionWithOrder(state, orderParams) ⇒ Promise Submits a new order via ws2 with the supplied parameters, creates a new strategy trade and creates a position. If no ws client is available, no data is saved & no order is dispatched **Kind**: global function **Returns**: Promise - p - resolves to nextState | Param | Type | | --- | --- | | state | Object | | orderParams | Object | | orderParams.symbol | string | | orderParams.type | string | | orderParams.amount | number | ## openShortPosition(state, orderParams) ⇒ Promise Opens a short position (negates passed amount) **Kind**: global function **Returns**: Promise - p - resolves to nextState | Param | Type | Description | | --- | --- | --- | | state | Object | | | orderParams | Object | | | orderParams.amount | number | required | ## openShortPositionLimit(state, orderParams) ⇒ Promise Opens a short position (negates passed amount) **Kind**: global function **Returns**: Promise - p - resolves to nextState | Param | Type | Description | | --- | --- | --- | | state | Object | | | orderParams | Object | | | orderParams.amount | number | required | ## openShortPositionMarket(state, orderParams) ⇒ Promise Opens a short position (negates passed amount) **Kind**: global function **Returns**: Promise - p - resolves to nextState | Param | Type | Description | | --- | --- | --- | | state | Object | | | orderParams | Object | | | orderParams.amount | number | required | ## positionPL(state, symbol, closePrice) ⇒ number Returns the P/L figure for the specified position, taking into account all trades & a close with the provided price (optional) **Kind**: global function **Returns**: number - pl | Param | Type | Description | | --- | --- | --- | | state | Object | | | symbol | string | defaults to default strat symbol | | closePrice | number | optional, used if position not already closed | ## updateLongPosition(state, orderParams) ⇒ Promise **Kind**: global function **Returns**: Promise - p - resolves to nextState | Param | Type | | --- | --- | | state | Object | | orderParams | Object | ## updateLongPositionLimit(state, orderParams) ⇒ Promise Updates a long position with a limit order **Kind**: global function **Returns**: Promise - p - resolves to nextState | Param | Type | | --- | --- | | state | Object | | orderParams | Object | ## updateLongPositionMarket(state, orderParams) ⇒ Promise Updates a long position with a market order **Kind**: global function **Returns**: Promise - p - resolves to nextState | Param | Type | | --- | --- | | state | Object | | orderParams | Object | ## updatePosition(state, orderParams) ⇒ Promise Alias for updatePositionWithOrder **Kind**: global function **Returns**: Promise - p - resolves to nextState | Param | Type | Description | | --- | --- | --- | | state | Object | | | orderParams | Object | passed directly to order constructor | ## updatePositionLimit(state, orderParams) ⇒ Promise Updates a position with a limit order **Kind**: global function **Returns**: Promise - p - resolves to nextState | Param | Type | | --- | --- | | state | Object | | orderParams | Object | ## updatePositionMarket(state, orderParams) ⇒ Promise Updates a new position with a market order **Kind**: global function **Returns**: Promise - p - resolves to nextState | Param | Type | | --- | --- | | state | Object | | orderParams | Object | ## updatePositionWithOrder(state, orderParams) ⇒ Promise Submits a new order via ws2 with the supplied parameters, creates a new strategy trade and updates the current position. If no ws client is available, no data is saved & no order is dispatched **Kind**: global function **Returns**: Promise - p - resolves to nextState | Param | Type | | --- | --- | | state | Object | | orderParams | Object | | orderParams.symbol | string | | orderParams.type | string | | orderParams.amount | number | ## updateShortPosition(state, orderParams) ⇒ Promise Updates a short position (negates passed amount) **Kind**: global function **Returns**: Promise - p - resolves to nextState | Param | Type | Description | | --- | --- | --- | | state | Object | | | orderParams | Object | | | orderParams.amount | number | required | ## updateShortPositionLimit(state, orderParams) ⇒ Promise Updates a short position (negates passed amount) **Kind**: global function **Returns**: Promise - p - resolves to nextState | Param | Type | Description | | --- | --- | --- | | state | Object | | | orderParams | Object | | | orderParams.amount | number | required | ## updateShortPositionMarket(state, orderParams) ⇒ Promise Updates a short position (negates passed amount) **Kind**: global function **Returns**: Promise - p - resolves to nextState | Param | Type | Description | | --- | --- | --- | | state | Object | | | orderParams | Object | | | orderParams.amount | number | required | ## onCandle(state, candle) ⇒ Promise Called for an incoming (new) candle. Propagates the candle via onPriceUpdate **Kind**: global function **Returns**: Promise - p - resolves to nextState | Param | Type | | --- | --- | | state | Object | | candle | Candle | ## onCandleUpdate(state, candle) ⇒ Promise Called for an incoming candle update, for a candle previously passed to onCandle() (same candle, new prices). Propagates the candle via onPriceUpdate **Kind**: global function **Returns**: Promise - p - resolves to nextState | Param | Type | | --- | --- | | state | Object | | candle | Candle | ## onPriceUpdate(state, update) ⇒ Promise Passes the incoming price update to the relevant strategy lifecycle methods: onPriceUpdate - always called onEnter - called if no position is open onUpdateShort - called if a short position is open onUpdateLong - called if a long position is open onUpdateClosing - called if a position is open but currently closing onUpdate - called if a position is open Returns the resulting strategy state **Kind**: global function **Returns**: Promise - p - resolves to nextState | Param | Type | Description | | --- | --- | --- | | state | Object | | | update | Object | incoming price update | ## onSeedCandle(state, candle) ⇒ Promise Called for an incoming (new) seed-period candle **Kind**: global function **Returns**: Promise - p - resolves to nextState | Param | Type | | --- | --- | | state | Object | | candle | Candle | ## onSeedCandleUpdate(state, candle) ⇒ Promise Called for an incoming seed-period candle update, for a candle previously passed to onSeedCandle() (same candle, new prices) **Kind**: global function **Returns**: Promise - p - resolves to nextState | Param | Type | | --- | --- | | state | Object | | candle | Candle | ## onSeedTrade(state, trade) ⇒ Promise Called for incoming seed-period trades. **Kind**: global function **Returns**: Promise - p - resolves to nextState | Param | Type | | --- | --- | | state | Object | | trade | Trade | ## onTrade(state, candle) ⇒ Promise Called for an incoming (new) trade. Propagates the trade via onPriceUpdate **Kind**: global function **Returns**: Promise - p - resolves to nextState | Param | Type | | --- | --- | | state | Object | | candle | Candle |