AutoTrader Utilities Module#
from autotrader import utilities
Configuration Utilities#
Get Broker Configuration#
- autotrader.utilities.get_broker_config(broker: str, global_config: dict | None = None, environment: str = 'paper') dict #
Returns a broker configuration dictionary.
- Parameters:
broker (str) – The name(s) of the broker/exchange. Specify multiple exchanges using comma separation.
global_config (dict) – The global configuration dictionary.
environment (str, optional) – The trading evironment (‘demo’ or ‘real’).
Get Data Configuration#
- autotrader.utilities.get_data_config(feed: str, global_config: dict | None = None, **kwargs) dict #
Returns a data configuration dictionary for AutoData. :param feed: The name of the data feed. :type feed: str :param global_config: The global configuration dictionary. :type global_config: dict
YAML Utilities#
Read YAML#
- autotrader.utilities.read_yaml(file_path: str) dict #
Function to read and extract contents from .yaml file.
- Parameters:
file_path (str) – The absolute filepath to the yaml file.
- Returns:
The loaded yaml file in dictionary form.
- Return type:
dict
Write YAML#
- autotrader.utilities.write_yaml(data: dict, filepath: str) None #
Writes a dictionary to a yaml file.
- Parameters:
data (dict) – The dictionary to write to yaml.
filepath (str) – The filepath to save the yaml file.
- Returns:
The data will be written to the filepath provided.
- Return type:
None
Unpickle Broker Instance#
- autotrader.utilities.unpickle_broker(picklefile: str = '.virtual_broker')#
Unpickles a virtual broker instance for post-processing.
TradeAnalysis Class#
- class autotrader.utilities.TradeAnalysis(broker, broker_histories: dict, instrument: str | None = None, price_history: DataFrame | None = None)#
AutoTrader trade analysis class.
- instruments_traded#
The instruments traded during the trading period.
- Type:
list
- account_history#
A timeseries history of the account during the trading period.
- Type:
pd.DataFrame
- holding_history#
A timeseries summary of holdings during the trading period, by portfolio allocation fraction.
- Type:
pd.DataFrame
- order_history#
A timeseries history of orders placed during the trading period.
- Type:
pd.DataFrame
- cancelled_orders#
Orders which were cancelled during the trading period.
- Type:
pd.DataFrame
- trade_history#
A history of all trades (fills) made during the trading period.
- Type:
pd.DataFrame
- analyse_account(broker, broker_histories: dict, instrument: str | None = None) None #
Analyses trade account and creates summary of key details.
- static create_fill_summary(fills: list, broker_name: str | None = None)#
Creates a dataframe of fill history.
- static create_position_history(trade_history: DataFrame, account_history: DataFrame) DataFrame #
Creates a history of positions held, recording number of units held at each timestamp.
- static create_trade_summary(trades: dict | None = None, orders: dict | None = None, instrument: str | None = None, broker_name: str | None = None) DataFrame #
Creates a summary dataframe for trades and orders.
- summary() dict #
Constructs a trading summary for printing.
DataStream Class#
- class autotrader.utilities.DataStream(**kwargs)#
Data stream class.
This class is intended to provide a means of custom data pipelines.
- instrument#
The instrument being traded.
- Type:
str
- feed#
The data feed.
- Type:
str
- data_filepaths#
The filepaths to locally stored data.
- Type:
str|dict
- quote_data_file#
The filepaths to locally stored quote data.
- Type:
str
- auxdata_files#
The auxiliary data files.
- Type:
dict
- strategy_params#
The strategy parameters.
- Type:
dict
- get_data#
The AutoData instance.
- Type:
AutoData
- data_start#
The backtest start date.
- Type:
datetime
- data_end#
The backtest end date.
- Type:
datetime
- portfolio#
The instruments being traded in a portfolio, if any.
- Type:
bool|list
- data_path_mapper#
A callable to map an instrument to an absolute filepath of data for that instrument.
- Type:
callable
Notes
A ‘dynamic’ dataset is one where the specific products being traded change over time. For example, trading contracts on an underlying product. In this case, dynamic_data should be set to True in AutoTrader.add_data method. When True, the datastream will be refreshed each update interval to ensure that data for the relevant contracts are being provided.
When the data is ‘static’, the instrument being traded does not change over time. This is the more common scenario. In this case, the datastream is only refreshed during livetrading, to accomodate for new data coming in. In backtesting however, the entire dataset can be provided after the initial call, as it will not evolve during the backtest. Note that future data will not be provided to the strategy; instead, the data returned from the datastream will be filtered by each AutoTraderBot before being passed to the strategy.
- get_trading_bars(data: DataFrame, quote_bars: bool, timestamp: datetime | None = None, processed_strategy_data: dict | None = None) dict #
Returns a dictionary of the current bars of the products being traded, based on the up-to-date data passed from autobot.
- Parameters:
data (pd.DataFrame) – The strategy base OHLC data.
quote_bars (bool) – Boolean flag to signal that quote data bars are being requested.
processed_strategy_data (dict) – A dictionary containing all of the processed strategy data, allowing flexibility in what bars are returned.
- Returns:
A dictionary of OHLC bars, keyed by the product name.
- Return type:
dict
Notes
The quote data bars dictionary must have the exact same keys as the trading bars dictionary. The quote_bars boolean flag is provided in case a distinction must be made when this method is called.
- match_quote_data(data: DataFrame, quote_data: DataFrame) DataFrame #
Function to match index of trading data and quote data.
- refresh(timestamp: datetime | None = None)#
Returns up-to-date trading data for AutoBot to provide to the strategy.
- Parameters:
timestamp (datetime, optional) – The current timestamp, which can be used to fetch data if need. Note that look-ahead is checked for in AutoTrader.autobot, so the data returned from this method can include all available data. The default is None.
- Returns:
data (pd.DataFrame) – The OHLC price data.
multi_data (dict) – A dictionary of DataFrames.
quote_data (pd.DataFrame) – The quote data.
auxdata (dict) – Strategy auxiliary data.
Monitor#
- class autotrader.utilities.Monitor(config_filepath: str | None = None, config: dict | None = None, *args, **kwargs)#
- get_broker(broker) AbstractBroker #
Returns the broker object.
- run() None #
Runs the monitor indefinitely.
- static start_server(port)#
Starts the http server for Prometheus.