portfolio ========= .. py:module:: portfolio Classes ------- .. autoapisummary:: portfolio.Portfolio Module Contents --------------- .. py:class:: Portfolio(cfg, symbols, position_manager = None, time_step = (TimeFrameUnit.Day, 1)) Portfolio class for managing trading positions and cash flow. Very much Stateful TODO there is not a clean seperation between portfolio logic and the underlying broker api and state. It is good to have the portfolio state mirrored internally via the df, but self.cash, self.initial_cash, etc... are not reflections of the underlying broker api, (ALPACA, LOCAL, REMOTE). ? position manager acts as the broker api here, interacting with the positions, trades, and orders, should we offload many of the portfolio attributes to the position manager? (and rename it) .. py:attribute:: cfg .. py:attribute:: vbt_pf :type: vectorbt.Portfolio | None :value: None .. py:attribute:: position_manager :value: None .. py:attribute:: time_step .. py:attribute:: persistent_df :type: pandas.DataFrame | None :value: None .. py:method:: as_vbt_pf(df = None) Update the portfolio with new data. .. py:method:: state() Get the current state of the portfolio. :returns: [internal_cash, positions]. :rtype: pd.Series .. py:method:: enforce_trade_rules(df, prices, trade_mode = TradeMode.CONTINUOUS) Enforce trade rules on the actions. From trade sizes to actual trade sizes based on configuration and current portfolio state. @TODO clean up and make more efficient .. py:method:: scale_actions(df, prices, trade_mode = TradeMode.CONTINUOUS) Scale the actions to the maximum position size. From signal strength to a desired trade size @TODO clean up and make more efficient .. py:method:: update_position_batch(df) Update positions for a batch of tickers at a given timestamp. @TODO clean up and make more efficient .. py:method:: step(df, normalized_actions = False) Scale the actions and take a step in the portfolio environment. 1. scale actions to trade size 2. enforce trade limits 3. update positions @TODO clean up and make more efficient .. py:method:: load(cfg, file_path) :classmethod: Load backtesting results from a JSON file. .. py:method:: save(file_path, df = None) .. py:method:: save_plots(backtest_dir, tickers = None) .. py:method:: reset() Reset the portfolio to an empty state. .. py:method:: set_vbt(pf) Set the portfolio object with backtest results. .. py:method:: _get_plots() Get the plots for the portfolio. .. py:method:: plot() Plot the results of the backtest. .. py:method:: stats() Return the statistics of the backtest. .. py:method:: orders() Return the orders of the backtest. .. py:method:: trades() Return the trades of the backtest. .. py:method:: __repr__() String representation of the Portfolio. .. py:method:: get_positions() Return the current positions in the portfolio. .. py:method:: analysis(analysis_config, df = None)