Skip to content

Commit

Permalink
Working sp utility model.
Browse files Browse the repository at this point in the history
  • Loading branch information
Sinbad-The-Sailor committed Feb 11, 2024
1 parent 90839a3 commit 4e18a23
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 8 deletions.
11 changes: 7 additions & 4 deletions run.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from src.abacus.utils.portfolio import Portfolio
from src.abacus.simulator.simulator import Simulator
from src.abacus.assessor.risk_assessor import RiskAssessor
from src.abacus.optimizer.optimizer import SPMaximumUtility
from src.abacus.optimizer.optimizer import SPMaximumUtility, SPMaximumGain



Expand Down Expand Up @@ -44,13 +44,16 @@


# Mock prices...
# price_tensor = torch.tensor([ [[1000]], [[0]], [[0]], [[0]]])
# inital_prices = torch.tensor([ [[10]], [[10]], [[10]], [[10]]])
price_tensor = torch.tensor([ [[1000]], [[0]], [[0]], [[0]]])
inital_prices = torch.tensor([ [[10]], [[10]], [[10]], [[10]]])

# Create optimizer with different optimization models...
optimizer = SPMaximumUtility(portfolio, simulator._price_tensor, simulator._inital_prices)
optimizer = SPMaximumGain(portfolio, price_tensor, inital_prices)
optimizer.solve()

print()
optimizer = SPMaximumUtility(portfolio, price_tensor, inital_prices)
optimizer.solve()


print("OK!")
35 changes: 35 additions & 0 deletions src/abacus/optimizer/optimization_models/sp_maximize_gain.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
problem spMaximizeGain;

set assets;

param dt > 0;
param risk_free_rate;
param inital_cash > 0;
param number_of_scenarios > 0 integer;
param number_of_assets > 0 integer;
param inital_holdings {assets};
param inital_prices {assets};
param prices {1..number_of_scenarios, assets};

var x_buy {assets};
var x_sell {assets};

maximize Objective:
sum{i in 1..number_of_scenarios} 1/number_of_scenarios * ((inital_cash + sum{j in assets} (inital_prices[j] * (x_sell[j] - x_buy[j]))) * exp(risk_free_rate * dt) + sum{j in assets} (prices[i, j] * (inital_holdings[j] + x_buy[j] - x_sell[j])));


subject to SHORTING_CONSTRAINT {j in assets}:
inital_holdings[j] + x_buy[j] - x_sell[j] >= 0
;

subject to LEVERAGE_CONSTRAINT:
inital_cash + sum{j in assets} ( (x_sell[j] - x_buy[j]) * inital_prices[j]) >= 0
;

subject to FEASIBLE_CONSTRAINT_BUY {j in assets}:
x_buy[j] >= 0
;

subject to FEASIBLE_CONSTRAINT_SELL {j in assets}:
x_sell[j] >= 0
;
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ set assets;

param dt > 0;
param risk_free_rate;
param gamma;
param inital_cash > 0;
param number_of_scenarios > 0 integer;
param number_of_assets > 0 integer;
Expand All @@ -15,7 +16,7 @@ var x_buy {assets};
var x_sell {assets};

maximize Objective:
sum{i in 1..number_of_scenarios} 1/number_of_scenarios * ((inital_cash + sum{j in assets} (inital_prices[j] * (x_sell[j] - x_buy[j]))) * exp(risk_free_rate * dt) + sum{j in assets} (prices[i, j] * (inital_holdings[j] + x_buy[j] - x_sell[j])));
sum{i in 1..number_of_scenarios} 1/number_of_scenarios * ((inital_cash + sum{j in assets} (inital_prices[j] * (x_sell[j] - x_buy[j]))) * exp(risk_free_rate * dt) + sum{j in assets} (prices[i, j] * (inital_holdings[j] + x_buy[j] - x_sell[j]))) ** gamma / gamma;


subject to SHORTING_CONSTRAINT {j in assets}:
Expand Down
41 changes: 38 additions & 3 deletions src/abacus/optimizer/optimizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,42 @@ def _check_initialization(self):
if not self._ampl:
raise ValueError("AMPL has not been initalized.")

def _check_valid_model_specification(self):
# TODO: Implement when approriate.
...


class SPMaximumGain(OptimizationModel):

_model_specification = OptimizationSpecifications.SP_MAXIMIZE_GAIN

def __init__(self, portfolio: Portfolio, price_tensor: torch.Tensor, inital_prices: torch.Tensor):
super().__init__(portfolio, price_tensor)
self._inital_prices = inital_prices

def solve(self):
super().solve()
print(self._ampl.get_variable("x_buy").get_values())
print(self._ampl.get_variable("x_sell").get_values())
print(self._ampl.eval("display Objective;"))

def _set_ampl_data(self):
assets = self._portfolio.instruments
asset_identifiers = [instrument.identifier for instrument in assets]
instrument_holdings = np.array(list(self._portfolio.holdings.values()))
price_tensor = np.array(self._simulation_tensor[:,-1,:])
inital_prices = dict(zip(asset_identifiers, np.array(self._inital_prices.reshape(4))))
tensor_size = price_tensor.shape
number_of_assets = tensor_size[0]
number_of_scenarios = tensor_size[1]
price_dict = {(j+1, asset.identifier): price_tensor[asset.id][j] for asset in assets for j in range(number_of_scenarios)}

self._ampl.get_set("assets").set_values(asset_identifiers)
self._ampl.param["risk_free_rate"] = 0.05
self._ampl.param["dt"] = 10/365
self._ampl.param["number_of_assets"] = number_of_assets
self._ampl.param["number_of_scenarios"] = number_of_scenarios
self._ampl.param["inital_cash"] = self._portfolio._cash
self._ampl.param["inital_holdings"] = instrument_holdings
self._ampl.param["inital_prices"] = inital_prices
self._ampl.param["prices"] = price_dict



Expand Down Expand Up @@ -86,6 +119,7 @@ def _set_ampl_data(self):
self._ampl.get_set("assets").set_values(asset_identifiers)
self._ampl.param["risk_free_rate"] = 0.05
self._ampl.param["dt"] = 10/365
self._ampl.param["gamma"] = -2
self._ampl.param["number_of_assets"] = number_of_assets
self._ampl.param["number_of_scenarios"] = number_of_scenarios
self._ampl.param["inital_cash"] = self._portfolio._cash
Expand All @@ -94,6 +128,7 @@ def _set_ampl_data(self):
self._ampl.param["prices"] = price_dict



class MPCMaximumUtility(OptimizationModel):

_model_specification = OptimizationSpecifications.MPC_MAXIMIZE_UTILITY
Expand Down
1 change: 1 addition & 0 deletions src/abacus/utils/enumerations.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@

class OptimizationSpecifications(Enum):
SP_MAXIMIZE_UTILITY = "sp_maximize_utility.mod"
SP_MAXIMIZE_GAIN = "sp_maximize_gain.mod"
MPC_MAXIMIZE_UTILITY = "mpc_maximize_utility.mod"

0 comments on commit 4e18a23

Please sign in to comment.