This class is the base class for all bot states. It contains the most basic functionality that all states need.

Hierarchy

  • default

Constructors

  • Initialize the bot state. This is the base class for all bot states, and run on every tick, whenever a State is created.

    Parameters

    • trade: any

      Trade information represented as a JSON object

    • systemConfig: any

      System configuration

    • database: default

      Database object

    • order: Order

      Order object

    Returns default

Properties

config: any

Same as "System configuration (systemConfig)"

database: default

Database object. Used to store and retrieve trade information.

notifications: default

Notifications object. Used to send notifications to Telegram, Slack, Discord, etc.

order: Order

Order object. Used to place and cancel orders.

systemConfig: any

Contains the concatinated configuration from the config.yml and strategy.yml files

taapi: default

Taapi object. Used to retrieve indicators and other information from the Taapi API.

trade: any

Trade information represented as a JSON object

utilities: default

Utilities object. Used to perform various utility functions.

Methods

  • Adds a calculation to the bulk construct. As standard, you may add 20 calculations to a construct, but if your plan supports multiple constructs, you can fetch data accross multiple assets pairs / timeframes in one request.

    See

    Example

    this.addCalculation("rsi", "BTC/USDT", "1h", "rsi_1h"); // Construct 1
    this.addCalculation("ema", "BTC/USDT", "1h", "ema_1h", { period: 20 }); // Construct 1
    this.addCalculation("macd", "BTC/USDT", "4h", "macd_4h"); // Construct 2
    this.addCalculation("ema", "ETH/USDT", "1d", "ema_1d", { period: 200 }, "bybit"); // Construct 3 + Override default exchange

    Parameters

    • indicator: string

      Name of the indicator to calculate. See https://taapi.io/indicators/ for a list of indicators.

    • interval: string

      Timeframe to calculate the indicator on. A list of available timeframes can be found at https://taapi.io/exchanges/ for your exchange.

    • id: string

      Your own unique ID for this calculation. This is used to retrieve the result later.

    • params: object

      Parameters to pass to the indicator. Please see the above link for the indicator you're using to see what parameters are available.

    • symbol: string = ...

      Defaults to the symbol of the current trade, but can be overridden, say if you're trading an altcoin but want to compare it to BTC.

    Returns void

  • Adds a trade log of type TAAPI_EXECUTE_BULK to the trade.

    Parameters

    • ta: {}

      The TAAPI.IO response to store

      Returns Promise<void>

    • Adds a log entry to the trade.

      Parameters

      • title: string

        Title of the log entry

      • type: TradeLogType

        Type of the log entry

      • data: any = {}

        Data to store with the log entry, defaults to an empty object

      Returns Promise<void>

    • Helper function which first records the trade to the historical trades collection, then deletes it from the active trades collection.

      If live trading is enabled, this function will check if in a position.

      Todo

      Finish up this function with live trading enabled.

      Returns Promise<void>

    • Calculates the profit of a trade in percent.

      Parameters

      • direction: string = "long"

        'long' or 'short', defaults to 'long'

      • trade: any = null

        The trade object to calculate the profit for, defaults to the current trade

      Returns number

    • Helper function for utilities.calculateStoplossPercent()

      Parameters

      • stoplossPrice: number

        The stoploss price

      • currentPrice: number

        The current price

      Returns number

    • Helper function for utilities.calculateTargetPercent()

      Parameters

      • targetPrice: number

        The target price

      • currentPrice: number

        The current price

      Returns number

    • Archives the trade, then creates a new trade with the same exchangeId, symbol and interval.

      The start state can be overridden by passing a state name to the startState parameter.

      Parameters

      • startState: string = "start"

      Returns Promise<any>

    • Changes the state of the trade.

      Parameters

      • newState: string

        The new state to change to. Must be a valid state matching the filename of a state class, ie. State_state-name.js

      • addLog: boolean = true

        Whether or not to add a log entry for this state change

      • message: string = ""

        Optional message to include in the log entry

      Returns Promise<void>

    • Helper function for the database.deleteTrade() function

      Returns Promise<void>

    • Enters a position on the exchange. The order type, slippage, leverage and time in force are all configurable in the config file.

      If live trading is disabled, a trade simulation will be performed instead, simply assuming the order was filled at the current price, and all other orders placed succecssfully.

      Parameters

      • side: string

        'LONG', 'SHORT', 'BUY' or 'SELL' in any case

      • currentPrice: number

        The current price of the asset

      • exitPrice: number = 0

        The price to exit the position at

      • stopPrice: number = 0

        The stoploss price

      Returns Promise<any>

    • Fetches the TAAPI.IO data for the calculations added to the bulk construct.

      Parameters

      • storeResults: boolean = false

        Whether or not to store the results in the database. Good for debugging, but can result in a lot of data being stored.

      Returns Promise<any>

    • Exits the position on the exchange.

      If live trading is disabled, a trade simulation will be performed instead, simply assuming the order was filled at the current price, and all other orders placed succecssfully.

      Returns Promise<any>

    • Fetches the current allocation stats from the database.

      Returns Promise<any>

    • Checks if the entry price has been hit.

      If live trading is enabled, it will check if the ENTRY order has been FILLED. Otherwise, it will check if the entry price has been hit using the provided candles.

      Parameters

      • candles: Candle[]

        Array of candles in descending order (aka newest first)

      Returns boolean

    • Checks if the stoploss price has been hit.

      If live trading is enabled, it will check if the STOPLOSS order has been FILLED. Otherwise, it will check if the stoploss price has been hit using the provided candles.

      Parameters

      • candles: Candle[]

        Array of candles in descending order (aka newest first)

      • ignoreCandlesCount: number = 0

        Number of candles to ignore from the beginning of the candles array. Sometimes useful with HFT simulations.

      Returns boolean

    • Checks if the target price has been hit.

      If live trading is enabled, it will check if the TAKE_PROFIT order has been FILLED. Otherwise, it will check if the target price has been hit using the provided candles.

      Parameters

      • candles: Candle[]

        Array of candles in descending order (aka newest first)

      Returns boolean

    • Persists the trade data to the database.

      Returns Promise<void>

    • Helper function for the database.recordHistoricalTrade() function

      Returns Promise<void>

    • Helper function for the taapi.resetBulkConstructs() function.

      Returns void

    • Sets the entry details for the trade.

      Deprecated

      • Replaced by enterPosition() and exitPosition()

      Parameters

      • price: number

        Entry price

      • targetPrice: number

        Target price

      • stoplossPrice: number

        Stoploss price

      • direction: string = "auto"

        Direction of the trade (long or short, default: auto)

      Returns Promise<void>

    • Sets the exit details for the trade.

      Deprecated

      • Replaced by enterPosition() and exitPosition()

      Parameters

      • price: number

        Exit price

      • reason: string

        Exit reason

      Returns Promise<void>

    • Sets the stoploss price for the trade.

      Parameters

      • price: number
      • persist: boolean = true

      Returns Promise<void>

    • Updates the trade with the provided data.

      Parameters

      • data: {}

        Data to update the trade with

        Returns Promise<void>