AutoBot#

Every time you run a strategy in AutoTrader, a trading bot (from the class AutoTraderBot), will be deployed for each instrument in your strategy’s watchlist. Each trading bot is therefore responsible for trading a single instrument using the rules of a single strategy, until it is terminated. All methods of the AutoTraderBot class are private, as it is unlikely you will ever need to call them as a user. However, you may want to use the bot instance for other things, such as backtest plotting. For this purpose, you can use the get_bots_deployed method of AutoTrader.

class autotrader.autobot.AutoTraderBot(instrument: str, strategy_dict: dict, broker, deploy_dt: datetime, data_dict: dict, quote_data_path: str, auxdata: dict, autotrader_instance)#

AutoTrader Trading Bot.

instrument#

The trading instrument assigned to the bot.

Type:

str

data#

The OHLC price data used by the bot.

Type:

pd.DataFrame

quote_data#

The OHLC quote data used by the bot.

Type:

pd.DataFrame

MTF_data#

The multiple timeframe data used by the bot.

Type:

dict

backtest_results#

A class containing results from the bot in backtest. This is available only after a backtest.

Type:

TradeAnalysis

_check_auxdata(auxdata: dict, timestamp: datetime, indexing: str = 'open', tail_bars: int | None = None, check_for_future_data: bool = True) dict#

Function to check the strategy auxiliary data.

Parameters:
  • auxdata (dict) – The strategy’s auxiliary data.

  • timestamp (datetime) – The current timestamp.

  • indexing (str, optional) – How the OHLC data has been indexed (either by bar ‘open’ time, or bar ‘close’ time). The default is ‘open’.

  • tail_bars (int, optional) – If provided, the data will be truncated to provide the number of bars specified. The default is None.

  • check_for_future_data (bool, optional) – A flag to check for future entries in the data. The default is True.

Returns:

The checked auxiliary data.

Return type:

dict

_check_data(timestamp: datetime, indexing: str = 'open') dict#

Function to return trading data based on the current timestamp. If dynamc_data updates are required (eg. when livetrading), the datastream will be refreshed each update to retrieve new data. The data will then be checked to ensure that there is no future data included.

Parameters:
  • timestamp (datetime) – DESCRIPTION.

  • indexing (str, optional) – DESCRIPTION. The default is ‘open’.

Returns:

  • strat_data (dict) – The checked strategy data.

  • current_bars (dict(pd.core.series.Series)) – The current bars for each product.

  • quote_bars (dict(pd.core.series.Series)) – The current quote data bars for each product.

  • sufficient_data (bool) – Boolean flag whether sufficient data is available.

_check_last_bar(current_bars: dict) bool#

Checks for new data to prevent duplicate signals.

static _check_ohlc_data(ohlc_data: DataFrame, timestamp: datetime, indexing: str = 'open', tail_bars: int | None = None, check_for_future_data: bool = True) DataFrame#

Checks the index of inputted data to ensure it contains no future data.

Parameters:
  • ohlc_data (pd.DataFrame) – DESCRIPTION.

  • timestamp (datetime) – The current timestamp.

  • indexing (str, optional) – How the OHLC data has been indexed (either by bar ‘open’ time, or bar ‘close’ time). The default is ‘open’.

  • tail_bars (int, optional) – If provided, the data will be truncated to provide the number of bars specified. The default is None.

  • check_for_future_data (bool, optional) – A flag to check for future entries in the data. The default is True.

Raises:

Exception – When an unrecognised data indexing type is specified.

Returns:

past_data – The checked data.

Return type:

pd.DataFrame

_check_orders(orders) list#

Checks that orders returned from strategy are in the correct format.

Return type:

List of Orders

Notes

An order must have (at the very least) an order type specified. Usually, the direction will also be required, except in the case of close order types. If an order with no order type is provided, it will be ignored.

_check_strategy_for_plot_data(use_strat_plot_data: bool = False)#

Checks the bot’s strategy to see if it has the plot_data attribute.

Returns:

plot_data – The data to plot.

Return type:

pd.DataFrame

Notes

This method is a placeholder for a future feature, allowing customisation of what is plotted by setting plot_data and plot_type attributes from within a strategy.

_create_trade_results(broker_histories: dict) dict#

Constructs bot-specific trade summary for post-processing.

_get_iteration_range() int#

Checks mode of operation and returns data iteration range. For backtesting, the entire dataset is iterated over. For livetrading, only the latest candle is used. ONLY USED IN BACKTESTING NOW.

_qualify_orders(orders: list, current_bars: dict, quote_bars: dict) None#

Passes price data to order to populate missing fields.

_refresh_data(timestamp: datetime | None = None, **kwargs)#

Refreshes the active Bot’s data attributes for trading.

When backtesting without dynamic data updates, the data attributes of the bot will be constant. When using dynamic data, or when livetrading in continuous mode, the data attributes will change as time passes, reflecting more up-to-date data. This method refreshes the data attributes for a given timestamp by calling the datastream object.

Parameters:
  • timestamp (datetime, optional) – The current timestamp. If None, datetime.now() will be called. The default is None.

  • **kwargs (dict) – Any other named arguments.

Raises:

Exception – When there is an error retrieving the data.

Returns:

The up-to-date data will be assigned to the Bot instance.

Return type:

None

_replace_data(data: DataFrame) None#

Function to replace the data assigned locally and to the strategy. Called when there is a mismatch in data lengths during multi-instrument backtests in periodic update mode.

static _submit_order(broker, order, *args, **kwargs)#

The default order execution method.

_update(i: int | None = None, timestamp: datetime | None = None) None#

Update strategy with the latest data and generate a trade signal.

Parameters:
  • i (int, optional) – The indexing parameter used when running in periodic update mode. The default is None.

  • timestamp (datetime, optional) – The timestamp parameter used when running in continuous update mode. The default is None.

Returns:

Trade signals generated will be submitted to the broker.

Return type:

None

_update_virtual_broker(current_bars: dict) None#

Updates virtual broker with latest price data.