MultiAssets resulting in broker.getposition() == "nan"
-
I'm trying to backtest with a large set of assets, which include new securities that go public and start trading after the start date of the simulation (and for which there are prices prior to the IPO date). Conversely, I also have shares that go private (or M&A) and cease to exist before the simulation ends.
I was leaving the absent prices as "nan", but I realize that the broker cannot propertly calculate the position. So I decided to substitute the "nan"s for zeros, but then I got a division by zero error.
(1) Is there any workaround to fix this situation?
(2) On a separate note, how does backtrader treat positions in securities that cease to exist?I could not find any related posts, so please apologize me if this issue has already been brought up.
PS: Eliminating the assets will not work, otherwise I would incur in selection bias. That is, I need to consider allocation within the entire pool of assets that existed at a certain point in time.
-
@randrade said in MultiAssets resulting in broker.getposition() == "nan":
(and for which there are prices prior to the IPO date).
I meant to say: for which there are NO prices prior to the IPO date.
-
ok, for item (1), apparently I just need to exclude empty data points in the CSVs I am importing. In other words, the first data point will be the first day of trading (or the previous day if I use the IPO price in it).
Now I just need to figure out how to treat assets that cease to exist (e.g. delisted). In particular, how backtrader treats assets that cease to exist and happen to be allocated to the portfolio.
-
Actually, I think deleting empty data points messes with the logic that determines the first day of the simulation. So disregard my previous comment.
I am almost convinced at this point that there is no fix to the issues I mentioned.
-
You need to remove data with no prices and use both
next
andprenext
to code trade logic. IIRC you need to add longest data feed first,bit will get used as baseline data axis.Thus was discussed several times in the forum.
-
I found 2 or 3 similar discussions, but the answer to them either :
- pointed to a page that discusses handling of data with different frequencies (e.g. daily and weekly"), which is not exactly the issue at hand -- btw, for some reason frequency seems to be addressed as timeframe in the docs; or
- were simply "read the documentation"
I confess I was hoping to find easier instructions on how to procede, particularly since this is a fairly common setup for doing backtests with shares (i.e. some companies IPO during the simulation), and the first page of the documentation advertises its ease of use.
I will continue doing some tests now that you mentioned that the order that you load the data matters, and will update if I achieve any progress.