Back to Community
Help With Optimize TargetWeights

I am really confused with how optimize works and am having trouble comprehending what is going on with this algo. My pipeline returns 10 stocks and with order_target_percent I can get 5% allocations in each stock but when I try and use the new optimize target weights for .05 the algo seems to purchase each stock, sell most minutes later and hold only a couple overnight with the wrong weights. Any help or insight would be appreciated.

Clone Algorithm
5
Loading...
Backtest from to with initial capital
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
Returns 1 Month 3 Month 6 Month 12 Month
Alpha 1 Month 3 Month 6 Month 12 Month
Beta 1 Month 3 Month 6 Month 12 Month
Sharpe 1 Month 3 Month 6 Month 12 Month
Sortino 1 Month 3 Month 6 Month 12 Month
Volatility 1 Month 3 Month 6 Month 12 Month
Max Drawdown 1 Month 3 Month 6 Month 12 Month
# Backtest ID: 5a1903b5dfe39843c1d06315
There was a runtime error.
4 responses

Jay has order_optimal_portfolio() within a loop above. Is there a way to order individual stocks selectively with Optimize? Below is more like I'm currently using in case it might help get you rolling.

Clone Algorithm
3
Loading...
Backtest from to with initial capital
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
Returns 1 Month 3 Month 6 Month 12 Month
Alpha 1 Month 3 Month 6 Month 12 Month
Beta 1 Month 3 Month 6 Month 12 Month
Sharpe 1 Month 3 Month 6 Month 12 Month
Sortino 1 Month 3 Month 6 Month 12 Month
Volatility 1 Month 3 Month 6 Month 12 Month
Max Drawdown 1 Month 3 Month 6 Month 12 Month
# Backtest ID: 5a192e8f6365a2445e5bdceb
There was a runtime error.

One confusion with the 'order_optimal_portfolio' may be that it places the orders all at once (unlike the 'order_target' methods). Therefore, there's no need to loop through all the stocks one wants to order. Simply set up a pandas series, or python dict, associating each security with it's desired weight. Use that series to create a 'TargetWeights' objective, then use that objective when executing the 'order_optimal_portfolio' method.

Any securities not in 'TargetWeights' will be assumed to have a target of 0 percent. Therefore, 'order_optimal_portfolio' will try to close any open positions without a weight. This is the cause of the behavior "the algo seems to purchase each stock, sell most minutes later and hold only a couple overnight with the wrong weights". Looping through 'order_optimal_portfolio' with a single weight in each iteration will try and close positions from previous iterations (because it assumes a zero weight).

The 'order_optimal_portfolio' method also checks for 'can_trade'. It's not required to explicitly do this. It won't cause an error if a security can't trade (though will log a warning). However, there could be reasons to check if a security can trade and one can still do it anyway. One may always want to hold a fixed number of securities so verifying they all can trade may be important.

@Blue Seahawk uses the 'positionConcentration.with_equal_bounds constraint (not objective) to get equal weighting. If using other constraints this is a good way to go. Other constraints may want to tweek the desired weights so it's good to give those weights a range. However, for plain down and dirty order a fixed weight of each security then using the 'TargetWeights' objective is very straightforward.

See attached algorithm. Take a look at the logs for the trades which were placed. Good luck.

Clone Algorithm
6
Loading...
Backtest from to with initial capital
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
Returns 1 Month 3 Month 6 Month 12 Month
Alpha 1 Month 3 Month 6 Month 12 Month
Beta 1 Month 3 Month 6 Month 12 Month
Sharpe 1 Month 3 Month 6 Month 12 Month
Sortino 1 Month 3 Month 6 Month 12 Month
Volatility 1 Month 3 Month 6 Month 12 Month
Max Drawdown 1 Month 3 Month 6 Month 12 Month
# Backtest ID: 5a199b3491c5ef401da02339
There was a runtime error.

This was very helpful, thank you

What is the best way to ensure a minimum amounts of positions using Optimize, but sized based on conviction? E.g. 500 positions in the portfolio with sizing for stock x ranked #1 being higher than stock y ranked #400. I'm having difficulty arriving at an in between of MaximizeAlpha and TargetWeight, the former resulting in variable position sizing but uncertain # of positions and the latter resulting in fixed sizing and # of positions. I want variable sizing but fixed # of positions.