robertmartin8 / PyPortfolioOpt

Financial portfolio optimisation in python, including classical efficient frontier, Black-Litterman, Hierarchical Risk Parity
https://pyportfolioopt.readthedocs.io/
MIT License
4.43k stars 950 forks source link

Different transaction costs to different assets problem #418

Closed zhangyx0417 closed 2 years ago

zhangyx0417 commented 2 years ago

In a portfolio optimization problem, there are 3 assets in total, and I'd like to assign different transaction cost rates (e.g., 0.01, 0.02, 0.03) to the 3 assets. What should I do?

I've tried ec.add_objective(objective_functions.transaction_cost, w_prev=initial_weights, k=0.1), but I can only pass one transaction cost rate.

robertmartin8 commented 2 years ago

You could create a custom transaction cost objective that supports vector input. This is a quick modification I made by looking at the source code for transaction_cost.

def transaction_cost(w, w_prev, k=0.001):
    return w, k @ (w - w_prev)

Not sure if that'll work, but a simple modification of it probably should!