cvxgrp / cvxportfolio

Portfolio optimization and back-testing.
https://www.cvxportfolio.com
GNU General Public License v3.0
981 stars 255 forks source link

Single-step use question #121

Open cadedoescode opened 10 months ago

cadedoescode commented 10 months ago

Is there any information on how this package could be used to simply run a single optimizer step and return the weights (no backtest, save compute time, etc.)

Is this supported, or is there any recommendation?

I suppose simply calling backtest() with start_date equal to the last date could work. However, the last bar has nans for w_plus, which makes sense. Maybe this could be workarounded by just adding another dummy row or something but I'm just curious if theres been any thought around this.

enzbus commented 10 months ago

You're looking for the execute method of a policy object, I think. Let me know if it works for you, you need to instantiate a market data object for it, in a similar way of a market simulator.

You're right on the way the back-test result is formatted. In the future it will be possible to continue a back-test from the end, at the last timestamp you have the weights but not w_plus.

enzbus commented 9 months ago

PS I added a new option to cvx.DownloadedMarketData and cvx.UserProvidedMarketData, do_asset_selection, which is better set to False for online usage. This is only in the repository version at this point, and I'm testing it with examples/strategies/dow30_daily.py, running it every day at market open New York time. It will take some days to make it to a pip release.

pcgm-team commented 9 months ago

Another question about execute and online usage (and looking at your example dow30_daily).

cvx.UserProvidedMarketData (or in your case cvx.DownloadedMarketData) tends to be using open prices. But if you are online isn't the most recent price going to be close? Because you don't have the bar full bar yet to use open. do the 'price' df have one extra row than the volume and returns dfs passed into cvx.UserProvidedMarketData?

enzbus commented 9 months ago

You only need past returns (open prices are used by execute to give number of shares to trade, optionally). Past returns are passed down to all elements of a policy (covariance estimators, ...) to make their forecasts. If you don't know the most recent past return it's not necessarily a big deal. For online execution, though, you should know the most recent one to get the dollar holdings you have at time t. As you correctly say, Yahoo Finance doesn't provide accurate open prices a few minutes after market open. If I find another free source to use for that I'd switch, but for now I'm using the Yahoo Finance ones and then, next day, recompute the holdings for the past day. You can see it done in the commits, some names get a little adjustment. This is not part of the main library, it's logic specific for the examples strategies that are run online. I'll maybe work on making it smarter, but if you have paid data sources that give you more accurate prices you should probably use them. The example strategies do the best I can with free data.

pcgm-team commented 9 months ago

That clarifies, thanks.