- ago
I would like to develop a backtest to do the following.

Use a portfolio of stocks such as S&P 100.
Filter the list of stocks based on a 6 month ROC and select only the top 20%.
Use a short term strategy with weighting to then trade this list of stocks using the Weight factor to select the top 20% of the previously selected stocks ( for the S&P 100, this would give me 4 stocks to buy).
I do not want to combine the long term ROC and short term strategy into a single weighting factor.
Is it possible to do this?
0
225
Solved
11 Replies

Reply

Bookmark

Sort
- ago
#1
QUOTE:
Filter the list of stocks based on a 6 month ROC and select only the top 20%.
Use a short term strategy with weighting to then trade this list of stocks using the Weight factor to select the top 20% of the previously selected stocks (for the S&P 100, this would give me 4 stocks to buy).

You need to do this in the PreExecute{block}. Check out the PreExecute example at https://www.wealth-lab.com/Support/ApiReference/UserStrategyBase#PreExecute

That example employs RSI rather than ROC, but the approach is the same. You compute the ROC for all the stocks in the dataset, then rank their ROC values on a bar-by-bar bases using a sort, and add the top candidates into a List<BarHistory> buys list that you trade from in the Execute{block}.

So you want to add a weight factor as well? Well, you could use a Dictionary<weight,BarHistory> buys list instead, and assign the weight relative to their ranking order or ROC. ("weight" should really be "int" or "double" in this declaration.) You know, I wouldn't get this too complicated on the first day. Get it running first, then add complexity.

I would search the forum for PreExecute to get other examples of how users solved things.
2
- ago
#2
Do not reinvent the wheel.
(It is pretty complex to code this up yourself)

There is the PRPF (Portfoilo Percentile Rank) indicator, available with the latest build of the finantic.Indicators extension:


It does exactly what you are looking for. Just set the "Indicator" parameter of the PFPR indicator to your long term ROC and add a filter condition like PFPR > 80.

This will leave the top 20% of current symbols when ranked by ROC().

0
Best Answer
- ago
#3
And of course, for the weight you can do the same:

Use another PFPR indictor, this time probably with a short term ROC and filter the top 5%: PFPR > 95.

Voilà.
0
- ago
#4
1
Cone8
 ( 6.64% )
- ago
#5
Nice!
However, this won't work for dynamic DataSets. It needs a Value method so that it can be recalculated on the participants' BarHistories for each bar (with a cache so that it is recalculated only when the participants have changed from the previous date/bar).
0
- ago
#6
QUOTE:
this won't work for dynamic DataSets.


Of course it works, see the DataSet in the Picture above: It is a dynmic DataSet.

QUOTE:
it can be recalculated on the participants' BarHistories for each bar

That is exactly how it works

QUOTE:
with a cache

There is a cache. All calculations are done just once (as long as the parameters are not changed, of course)

Summary: This works exactly like the IndexLab indicators but without a file on disk. Instead the cache is hold in memory.
0
Cone8
 ( 6.64% )
- ago
#7
Great. I see it now. Here's an example. EXC was removed from the S&P 100 on 3/18/2024, so it's not part of the calculation after that.

0
- ago
#8
Superticker, DrKoch and Cone, thank you for the assistance. I will try with a building block first and then if that doesnt work i will try with code.
0
- ago
#9
QUOTE:
Summary: This works exactly like the IndexLab indicators but without a file on disk. Instead the cache is held in memory.

Interesting. So if I need an IndexLab function but don't want to cache indicator results on disk, I can use the PFPR indicator instead. That's a neat trick.
0
- ago
#10
QUOTE:
So if I need an IndexLab function but don't want to cache indicator results on disk, I can use the PFPR indicator instead


Sorry, but no.

The PFPR indicator uses that (new) technology but it ill always calculate (only) the percentile rank.

It would be possible to refactor the existing IndexLab indicators to use the same technique, but this is outside the scope of finantic...
1
Cone8
 ( 6.64% )
- ago
#11
IndexLab indicators already implement CalculateCompositeValue(), which calculates based on a dynamic DataSet's participants for each date/bar.
0

Reply

Bookmark

Sort