Portfolio strategies
-
Hello!
I am considering to give backtrader a try. However, before diving deep into the code, I've got a general question regarding portfolio strategies.
Let's assume I have a pandas dataframe in wide format, i.e. columns representing individual assets and rows representing returns at a given time.
I am looking for setting up a relative momentum strategy. Therefore, for every row, I need to compute a ranking. The topN
assets will then be bought while the worstN
assets will be sold short.At the end of the day, I am interested in the overall portfolio performance. Is a "portfolio strategy" like this possible to implement using backtrader or is backtrader more suited to analyze individual asset strategies?
-
@andi Yes you can definitely do this in Backtrader. Based upon what you are saying, there are two way I could recommend.
- You could preprocess for each stock their momentum in Pandas, and then calculate a ranking. You could then import this into Backtrader and use the ranking line.
- Or you could just import the OHLCV into Backtrader, and then use the built in momentum indicator to calculate the momentum for each stock at each bar. Then you could right a script to rank the results.
Once ranked using either one or two, you sort your list and go long the best N assets and short the poorest N assets.
Handling this type of backtest I would label as moderately difficult and may take you a bit of time to get up the backtrader learning curve.
We're here to help as you go through it.
Good luck.
-
@run-out Sounds good to me. However, just to clearify a point with respect to "portfolio evaluation": I definitely need performance reports/plots on an aggregated portfolio level. Is this possible or is the performance report only available for individual securities?
Also, just out of curiosity, I already have a pandas dataframe with trading signals as per close in the form of +1 / -1 for long and short signals. Would it be possible to use this dataframe?
-