diff --git a/pypfopt/discrete_allocation.py b/pypfopt/discrete_allocation.py index f4033e0d..3bf1ec1d 100644 --- a/pypfopt/discrete_allocation.py +++ b/pypfopt/discrete_allocation.py @@ -177,18 +177,35 @@ def greedy_portfolio(self, reinvest=False, verbose=False): self.weights.sort(key=lambda x: x[1], reverse=True) # If portfolio contains shorts - if self.weights[-1][1] < 0: - longs = {t: w for t, w in self.weights if w >= 0} + if self.weights and self.weights[-1][1] < 0: + longs = {t: w for t, w in self.weights if w > 0} shorts = {t: -w for t, w in self.weights if w < 0} + short_total_weight = sum(shorts.values()) + shorts = {t: w / short_total_weight for t, w in shorts.items()} + short_val = self.total_portfolio_value * self.short_ratio + + if not longs: + if verbose: + print("\nAllocating short sub-portfolio...") + da2 = DiscreteAllocation( + shorts, + self.latest_prices[shorts.keys()], + total_portfolio_value=short_val, + ) + short_alloc, short_leftover = da2.greedy_portfolio() + self.allocation = {t: -w for t, w in short_alloc.items()} + self.allocation = self._remove_zero_positions(self.allocation) + return ( + self.allocation, + (self.total_portfolio_value - short_val) + short_leftover, + ) + # Make them sum to one long_total_weight = sum(longs.values()) - short_total_weight = sum(shorts.values()) longs = {t: w / long_total_weight for t, w in longs.items()} - shorts = {t: w / short_total_weight for t, w in shorts.items()} # Construct long-only discrete allocations for each - short_val = self.total_portfolio_value * self.short_ratio long_val = self.total_portfolio_value if reinvest: long_val += short_val @@ -318,17 +335,34 @@ def lp_portfolio(self, reinvest=False, verbose=False, solver=None): # end todo if any([w < 0 for _, w in self.weights]): - longs = {t: w for t, w in self.weights if w >= 0} + longs = {t: w for t, w in self.weights if w > 0} shorts = {t: -w for t, w in self.weights if w < 0} + short_total_weight = sum(shorts.values()) + shorts = {t: w / short_total_weight for t, w in shorts.items()} + short_val = self.total_portfolio_value * self.short_ratio + + if not longs: + if verbose: + print("\nAllocating short sub-portfolio:") + da2 = DiscreteAllocation( + shorts, + self.latest_prices[shorts.keys()], + total_portfolio_value=short_val, + ) + short_alloc, short_leftover = da2.lp_portfolio(solver=solver) + self.allocation = {t: -w for t, w in short_alloc.items()} + self.allocation = self._remove_zero_positions(self.allocation) + return ( + self.allocation, + (self.total_portfolio_value - short_val) + short_leftover, + ) + # Make them sum to one long_total_weight = sum(longs.values()) - short_total_weight = sum(shorts.values()) longs = {t: w / long_total_weight for t, w in longs.items()} - shorts = {t: w / short_total_weight for t, w in shorts.items()} # Construct long-only discrete allocations for each - short_val = self.total_portfolio_value * self.short_ratio long_val = self.total_portfolio_value if reinvest: long_val += short_val diff --git a/tests/test_discrete_allocation.py b/tests/test_discrete_allocation.py index f0953186..2a915f97 100644 --- a/tests/test_discrete_allocation.py +++ b/tests/test_discrete_allocation.py @@ -454,3 +454,31 @@ def test_allocation_errors(): latest_prices.iloc[0] = np.nan with pytest.raises(TypeError): DiscreteAllocation(w, latest_prices) + + +def test_short_only_allocation(): + prices = pd.Series({"AAPL": 150.0, "MSFT": 250.0}) + weights = {"AAPL": -0.6, "MSFT": -0.4} + + da = DiscreteAllocation(weights, prices, total_portfolio_value=10000) + alloc_g, leftover_g = da.greedy_portfolio() + assert alloc_g == {"AAPL": -40, "MSFT": -16} + assert leftover_g == 0.0 + + alloc_lp, leftover_lp = da.lp_portfolio() + assert alloc_lp == {"AAPL": -40, "MSFT": -16} + assert leftover_lp == 0.0 + + +def test_zero_long_with_shorts_allocation(): + prices = pd.Series({"AAPL": 150.0, "MSFT": 250.0}) + weights = {"AAPL": 0.0, "MSFT": -1.0} + + da = DiscreteAllocation(weights, prices, total_portfolio_value=10000) + alloc_g, leftover_g = da.greedy_portfolio() + assert alloc_g == {"MSFT": -40} + assert leftover_g == 0.0 + + alloc_lp, leftover_lp = da.lp_portfolio() + assert alloc_lp == {"MSFT": -40} + assert leftover_lp == 0.0