Portfolio

class ConvexTrader.Portfolio(gamma=0.5)

Bases: object

execute_trade(trade: Trade)
Executes a trade (buy or sell) by updating holdings and adjusting portfolio weights accordingly.

trade: A Trade object containing trade details (symbol, quantity, trade_type).

Args:

trade: A Trade object containing trade details (symbol, quantity, trade_type)

Raises:

ValidationError: If there are insufficient shares for a sell trade

get_weights() Dict[str, float]

Returns a dictionary of stock symbols and their corresponding weights in the portfolio.

multi_period_optimize(H: int, r_t: ndarray, gamma_t: ndarray, psi_t: ndarray, phi_trade: List[ndarray], phi_hold: List[ndarray]) ndarray

Solve the multi-period optimization problem using the multi_period_optimization function.

Args:

H: Number of future periods to optimize. r_t: Matrix of expected returns, where each row corresponds to a future period. gamma_t: Vector of risk-aversion parameters for each period. psi_t: Vector of risk factors for each period. phi_trade: List of transaction cost vectors for each period. phi_hold: List of holding cost vectors for each period.

Returns:

Numpy array: Optimal trade vectors over all periods (z matrix).

Raises:

ValidationError: If input parameters are invalid OptimizationError: If optimization fails

single_period_optimize(expected_returns: ndarray, gamma: float) ndarray

Solve the single-period optimization problem using the provided single_period_optimization function.

Args:

expected_returns: Numpy array of expected returns for each stock in the portfolio. gamma: Risk-aversion parameter.

Returns:

Numpy array: Optimal trade vector (z).

Raises:

ValidationError: If input parameters are invalid OptimizationError: If optimization fails

total_value(current_prices: Dict[str, float]) float

Calculates the total market value of the portfolio based on current stock prices.

update_weights()

Updates the portfolio weights based on the current holdings.