cvxportfolio icon indicating copy to clipboard operation
cvxportfolio copied to clipboard

Can not set attribute Error in get_trades when adding terminal_weights constraint in MPO example

Open kch382001 opened this issue 5 years ago • 2 comments

The terminal_weights setup in MultiPeriodOpt example seems to fail when calling run_backtest function. python 3.6 + cvxportfolio 0.05 + cvxpy 1.0.25 + numpy 1.17.1 + pandas 0.22.0.

terminal_weights = pd.Series(index=returns.columns, data=0)
terminal_weights['USDOLLAR'] = 1

policy = \
      cp.MultiPeriodOpt(return_forecast=returns_forecast,
                        costs=[gamma_risk*risk_model, gamma_tcost*simulated_tcost, optimization_hcost],
                        constraints=[cp.LeverageLimit(3)],
                        trading_times=list(returns.index[(returns.index>=start_t)&(returns.index<=end_t)]),
                        lookahead_periods=lookahead_periods,
                        terminal_weights=terminal_weights)
    
...
result = simulator.run_backtest(w_b*1e8, start_time = start_t, end_time=end_t, policy=policy)

It error out in policies.py file:

if self.terminal_weights is not None:
            prob_arr[-1].constraints += [wplus == self.terminal_weights.values]  # <-- Error Out

The error message is:

File "/home/kec/.local/lib/python3.6/site-packages/cvxportfolio/policies.py", line 374, in get_trades
    prob_arr[-1].constraints += [wplus == self.terminal_weights.values]
AttributeError: can't set attribute

anyone knows how to fix this problem?

kch382001 avatar Sep 09 '19 18:09 kch382001

@kch382001 A better title for the issue would be "Can't set attribute error when adding constraint in get_trades"

joseortiz3 avatar Sep 09 '19 23:09 joseortiz3

self.constraints is a property. You can use direct reference self._constraints to resolve the problem.

Sent from my iPhone

On Sep 9, 2019, at 7:38 PM, Jose Ortiz [email protected] wrote:

A better title for the issue would be "Can't set attribute error when adding constraint in get_trades"

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or mute the thread.

kch382001 avatar Sep 10 '19 01:09 kch382001