portfolio
Classes
Portfolio class for managing trading positions and cash flow. |
Module Contents
- class portfolio.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)
- Parameters:
symbols (list[str])
position_manager (trading.src.portfolio.position.PositionManager | None)
time_step (tuple[alpaca.data.timeframe.TimeFrameUnit, int])
- cfg
- vbt_pf: vectorbt.Portfolio | None = None
- position_manager = None
- time_step
- persistent_df: pandas.DataFrame | None = None
- as_vbt_pf(df=None)
Update the portfolio with new data.
- Parameters:
df (pandas.DataFrame | None)
- Return type:
vectorbt.Portfolio
- state()
Get the current state of the portfolio. :returns: [internal_cash, positions]. :rtype: pd.Series
- Return type:
numpy.ndarray
- 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
- Parameters:
df (pandas.DataFrame)
prices (numpy.ndarray)
trade_mode (trading.cli.alg.config.TradeMode)
- Return type:
numpy.ndarray
- 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
- Parameters:
df (pandas.DataFrame)
prices (numpy.ndarray)
trade_mode (trading.cli.alg.config.TradeMode)
- Return type:
numpy.ndarray
- update_position_batch(df)
Update positions for a batch of tickers at a given timestamp. @TODO clean up and make more efficient
- Parameters:
df (pandas.DataFrame)
- Return type:
tuple[float, list[alpaca.trading.requests.MarketOrderRequest] | None]
- 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
- Parameters:
df (pandas.DataFrame)
normalized_actions (bool)
- Return type:
dict
- classmethod load(cfg, file_path)
Load backtesting results from a JSON file.
- Parameters:
file_path (pathlib.Path)
- Return type:
- save(file_path, df=None)
- Parameters:
file_path (str)
df (pandas.DataFrame | None)
- save_plots(backtest_dir, tickers=None)
- Parameters:
backtest_dir (pathlib.Path)
tickers (list[str] | None)
- reset()
Reset the portfolio to an empty state.
- set_vbt(pf)
Set the portfolio object with backtest results.
- Parameters:
pf (vectorbt.Portfolio)
- _get_plots()
Get the plots for the portfolio.
- Return type:
dict[str, vectorbt._typing.BaseFigure]
- plot()
Plot the results of the backtest.
- stats()
Return the statistics of the backtest.
- orders()
Return the orders of the backtest.
- trades()
Return the trades of the backtest.
- __repr__()
String representation of the Portfolio.
- Return type:
str
- get_positions()
Return the current positions in the portfolio.
- Return type:
trading.src.portfolio.position.PositionManager
- analysis(analysis_config, df=None)
- Parameters:
df (pandas.DataFrame | None)