ROC Symbol Rotation - Modification of the Strategy
Author: bostel
Creation Date: 10/20/2011 5:33 AM
profile picture

bostel

#1
The symbol rotation strategy works great. I would like to make a minor change to the strategy. By construction the original strategy is always holding 5 stocks/funds with the highest ROC.

I would like to change the buy rule and let the exit rule as it ist. Instead of buying the 5 symbols with the highest ROC i would just like to buy the symbol with the highest ROC. Maybe i have to create a second list but i have no idea how and i have not found any ideas in the forum how to do it.

Any ideas? Many thanks in advance
Jan
CODE:
Please log in to see this code.
profile picture

Eugene

#2
To hold just one strongest stock, either manually enter "1" in the parameter slider dialog or modify the parameter:
CODE:
Please log in to see this code.
profile picture

bostel

#3
Eugene, thanks for the quick reply. I know that i can change the number of symbols in the parameter slider dialog. However, when I enter 1 as you suggested the stock will be sold if the ROC is not the highest in the list anymore. I want to hold the stock unless the ROC of the stock dropped and the stock is number six in the list.

1 Buy signal: Buy highest ROC (1 symbol)
2 Sell signal: Sell if the stock is not in the list with the 5 highest ROC anymore

Example with 3 symbols: Buy highest symbol; sell if the symbol drops to the last place, i.e. lowest ROC

symbol x ROC 10
symbol y ROC 8
symbol z ROC 5

Buy symbol x

symbol x ROC 8
symbol y ROC 10
symbol z ROC 6

hold symbol x
buy symbol Y

symbol x ROC 5
symbol y ROC 9
symbol z ROC 6

sell symbol x
hold symbol Y

I hope my example is understandable.

profile picture

Eugene

#4
Well, then it sounds like something that we have coded before and that your guess re: second list was right:

"How would I then modify the code to continue to buy the top 3, but only sell when it fell out of the top 10?"

Rotation Enhancement: buy Top 3, sell when falls out of Top 10

Will the code below do the job (untested)?
CODE:
Please log in to see this code.
profile picture

bostel

#5
Many thanks Eugene.

I used your code but unfortunately the code is not working as expected. As i am not a programmer i don't understand your code completely. The main change to the old code are those lines

CODE:
Please log in to see this code.


As guessed you created a second list. I don't understand how to change the number of stocks in both lists. I just want to buy the highest ROC. The initial default is three and i have no clue how i can change that.

I want to learn programming any recommendations how to start.

Jan
profile picture

Eugene

#6
OK here's a rewrite. Is this closer?
CODE:
Please log in to see this code.
profile picture

bostel

#7
Yes, the code works. Many thanks for your help
profile picture

Eugene

#8
You're welcome
profile picture

bostel

#9
Hi there,

i want to take the survivorship bias into account and create a watchlist with all members when they entered or exited the index. Searching through wealth-lab I found the wiki help regarding the survivorship issue and this thread:

http://www.wealth-lab.com/Forum/Posts/WatchLists-Evolving-Watchlist-30340

The wiki code works great with a simple cross over entry and exit. However, I still don't know how to incorporate the code in the strategy .Eugene, any ideas?

Many thanks
Jan
profile picture

Eugene

#10
What's the problem?
profile picture

bostel

#11
Hi Eugene,

i want to take into account the survivorship bias issue in your code from 10/21/2011 8:46. As I said i was able to build - more or less copy and paste - an easy code (see below). However I have no idea how to incorporate those lines in your code.

Many thanks
Jan

CODE:
Please log in to see this code.
profile picture

Eugene

#12
Jan,

You're moving in the right direction. Although I haven't experimented with coupling these creatures together, I think you need to perform the onWatch check in the if(buyPosition) block. Consequently, all related code (i.e. check for firstDateAvailable, symbolList etc.) have to be inserted there. Of course, the IndexHelper and firstDateAvailable are to be defined before the main loop like you did in your code.

p.s. Just in case, the online help: Backtesting with Dynamic (Evolving) WatchLists
profile picture

bostel

#13
Hi Eugene,

I have defined the Indexhelper and firstDateAvailable before the mainloop, as suggested. However i have no idea how to combine the buy instruction. I would be very grateful if you could give me another hint how to solve the problem.

Many thanks
Jan







profile picture

Eugene

#14
Jan,

Here you have the block of code that checks whether is the instrument available for trading on current date:
CODE:
Please log in to see this code.

If the onWatch variable is true, it was; so we can go on with trading the symbol.

So I'm really at a loss what's the problem you're having with inserting that self-contained code block right after SetContext() and before the entry. After completing this step just prefix the BuyAtMarket entry with an onWatch check:
CODE:
Please log in to see this code.

At first glance, looks like a copy/paste job to me. However, I only created the supporting classes; neither I'm an active user nor have any historical index composition lists to troubleshoot/verify the resulting code.
profile picture

bostel

#15
As always thanks for the fast reply Eugene. I truly appreciate your help. I had a mistake in the original code. However, now the code ist working as desired.
profile picture

bostel

#16
Sorry, I thought that the code is working properly but unfortunately there is still a problem. I have the original list with every symbol (all symbols including survivorship bias) which are sorted by their ROCs and then check if the symbol is in my txt file. However, if the TOP ROC symbol is not in the txt. file no trade is executed. Consequently, I have to sort all symbols in my txt file not in the original watchlist and then buy the TOP ROC of the txt file.

Any idea how to fix this Eugene?

Many thanks
Jan
CODE:
Please log in to see this code.
profile picture

bostel

#17
I have not found a solution yet.

Instead of sorting the ROCs of the whole DataSet i have to sort the ROCs of the symbols listed in the corresponding txt file. I know that wealth lab is no programming/coding guide. However, do you have any idea how to solve my problem.

Thanks for your support
Jan
profile picture

Eugene

#18
Sorry Jan, I have no idea.
profile picture

gbullr

#19
Stuck on Logic and Other.

Want to buy only if dolvol > 5000000 and the symbol is in the sorted list.

So sort DataSet on Jan 1
Order for top quartile cround / 4 where cround = list.count / 4
and buy symbols only from the sorted list.

CODE:
Please log in to see this code.



profile picture

Nadja

#20
Dear all
I was able to code an interesting strong stock rotation strategy for the Nasdaq 100 Index, now it's the idea of eliminating the survivorship bias to further validate it. The document "Backtesting with Dynamic WatchLists" and the discussion in this thread have helped me a lot, and I have created all the necessary data files. The concept is to update the list at the end of each year and then trade on the basis of this list the next year, and so on.

My problem is, that I don't know where I can get the data for the stocks that are delisted, and this is about one third of the set for the Yahoo static provider.

Thank you to everybody in advance who indicates me a good source to buy or get these end of day data, I would like to go back to 1997 approximately.

Kind regards

Andreas



profile picture

Eugene

#21
Hi Andreas,

We recommend PremiumData by Norgate:

Is there data that maintains older issues for delisted, bankrupt companies?
S&P 500 Index - Added/Deleted Components

Additionally, there's CSI Data whose price tag is not nearly as optimal as Norgate's is.
profile picture

Eugene

#22
wmrgrove wrote:
QUOTE:
I did see the comment "Yes, the code works. Many thanks for your help" and was excited about integrating the code.

As I looked at the backtest more closely it seams to be doing something strange? So, I ran many rotational strategies on many different lists and as you reduce the "number of symbols" picked in the buy you start to see gaps. The one above gets a lot more gaps with the 2 different rankings ( list and listsell ).

So first a general question, if you run any of these strategies (Published rotational) with
1. one trade per symbol 100% equity and 1.1 margin and the only buy/sell rule is, hold x number of days
2. using a simple , say RSI or ROC for ranking

Should there be any 100% cash periods scattered in say a 4 year backtest, after the int lead bars?????

Or more specificly if you (Eugene) take the code you wrote above and run it using one symbol only, 100% equity and 1.1 margin, on the sp100 list for 4 years, you will get a bunch of 100% cash periods in the equity curve.

I'm just trying to learn if these cash gaps/periods are caused by something I don't understand, or it is a coding issue in the software.
A final note, rotation strategies like RSI Rotation that do not have the "Wait X periods before selling" do not appear to have this issue. Thanks, Bill
profile picture

Eugene

#23
Bill, what you described is not a use case, and it contradicts the purpose of building a rotational strategy. The idea of rotation implies having several stocks to rotate to and from. I never considered this scenario. Good luck.
profile picture

wmrgrove

#24
Eugene, I only used the select one symbol as an example that really shows up the "issue". It occurs with more than one position selected.

I know I have asked a lot of questions as a new user but at least I am taking the time to put them here on the forum so other new users can benefit (hopefully)! I'm sure you are also very busy with many projects. But, I have a real question here and would like some help!

If you take your script above ( 10/21/2011 8:46 AM ) and select 2 positions for "List" and 2 positions, for "Listsell" with 1.1 margin and one position / 50% equity, 4 years on the sp100, the equity curve shows 98% invested! Now change only the "Listsell number of positions while looking at the equity chart. As you increase the number of "Listsell" the equity chart show less then 98% invested and sometimes no buys at all for 6-15 days or more. Why do these gaps occur and the strategy becomes less than 98% invested when you only change the "listsell" quantity??????

In my backtest of above "Exposure" goes from 98% (due to int bars) at listsell= 2, to 88% "Exposure" at listsell = 3 !

All I'm asking is if something is occuring that is correct that I don't understand, or there is a conflict in the code.

Maybe someone else on the forum can help?
Thans, Bill
profile picture

Eugene

#25
With 50% per position and 1.1 margin, List/Listsell = 2/2, there still can be skipped trades. This explains the exposure.

Bill, probably I didn't express myself well. It's been a year so I vaguely recall the script. Neither I have the desire to go through it again, nor the time for its debugging - which ultimately is user's task. The team's been busy with immediate activities and the upcoming launch of our new service. With all respect I leave debugging of this script up to you. If you aren't satisfied with the way it works, you are welcome to code an alternative and share it with the community. Best of luck.
profile picture

wmrgrove

#26
Eugene , Thank you again for all the help. I just did not see why it was skipping trades. I appreciate all the help you have given me!

THESE ROTATIONAL STRATEGIES are very very interesting. Buying the top 5 for example and not allowing rotation till the top 5 drop to the top 10 or 15 really reduces the "churn" and combined with some simple timing and other buy qualifiers makes for an interesting longer term (weeks / months) strategy.

Thanks again, Bill
profile picture

ashridharani

#27
Hi,

I need help with a modification for the ROC Rotation strategy. I wanted to add a Volume filter. I wanted the Rotation Strategy to only buy stocks, that for example had an average daily volume for 30 days greater than 100K.

I tried adding that condition to the "Buy" and while the strategy compiles, it does not work as intended based on my test run.

Another way, which may be a better approach, would be to create a symbol list from existing data set that meets the Volume criteria and then apply to that list the ROC rule. But having read the above links and other postings, its clear as mud to me, in very early stage of C# coding as to how to interject a second list. I would appreciated any help that anyone can provide.

Following is the code and where I am at with it.

Thanks.

CODE:
Please log in to see this code.
profile picture

Eugene

#28
Follow my reply #2 in either Tactical Asset Allocation or in Rotation Strategy / Context question.

You'll need to expand the ROCHolder type to hold the Volume as well:

CODE:
Please log in to see this code.


Then store the average volume:

CODE:
Please log in to see this code.


Finally, use it:

CODE:
Please log in to see this code.
profile picture

ashridharani

#29
Thanks Eugene for the links and quick response. It helped in clarifying few things.

Regards,
Anish
profile picture

ashridharani

#30
Hi Eugene,

I wanted to PrintDebug the symbols and their corresponding "holder.roc" value which forms the basis for their ranking. My goal is to see the list every night when I run the strategy and get a feel for which symbols (in the given dataset) are moving up or down. I added the PrintDebug code (below), but when I run it, I get the symbols listed multiple times with different values next to them. And, please pardon my ignorance here, but I'm assuming that the different values are probably the individual values of the different components within the holder.roc formula. I would appreciate if you could help me modify the printdebug code line to get the results that I was attempting to get.

Thanks.

CODE:
Please log in to see this code.
profile picture

Eugene

#31
Adding one line should do it:

CODE:
Please log in to see this code.
profile picture

ashridharani

#32
Thanks Eugene, that did it. The only issue still there is it sorts in alphabetical order (based on symbol) in the Printdebug window. I wanted the Printdebug report to sort in a descending order using "holder.roc" as a primary sort. Can it be done?

Thanks.

Anish
profile picture

Eugene

#33
You're printing before the list gets sorted by by ROC. Paste the snippet after sorting and reversing the list:

CODE:
Please log in to see this code.

profile picture

ashridharani

#34
Thanks Eugene.

I'm posting the snippet below again with a small correction and a minor formatting modification for other users. Because we changed the location of the code, the "symbol" was not defined and I corrected for it.

CODE:
Please log in to see this code.
profile picture

ashridharani

#35
Hi,

I wanted to modify the exit strategy to do the following in the rotational strategy for any given active position.

a) If the position has been held for at least ten days (bars) and

b) The value or the score of the ranking formula is less than minus 6 (for example) then the position should exit next day.

As long as the above conditions are false then the active position will be continued to be held as per the existing days to hold rule.

I made the changes that I thought would do that but it is not working as I thought. In my back testing the strategy is currently holding a position that has ranking score of minus 12 and has been less than the minus 6 for more than three bars, but did not trigger a sell. I would appreciate if you could look at it and let me know what I did wrong.

I've attached the a snippet of the code with my modified sell rule below . Thanks.


CODE:
Please log in to see this code.

profile picture

Eugene

#36
CODE:
Please log in to see this code.

This does not make sense: ALL these operations you're making refer to the charted symbol. You certainly do not want that as the results are in la la land. On how to do it properly, review my post #28 above along with linked topics. It just takes to access the holder.roc property.

profile picture

ashridharani

#37
Thanks Eugene. I'll take look at that post again.
profile picture

ashridharani

#38
Hi,

Using the generic Rotation code, I modified it to create the following code. In my modified strategy I wanted to use the absolute value of the Volatility Series. In my code (please see "holder.roc" line) I used Math.Abs to convert the value of Volatility Series into an absolute vaule. I ran compile and got no error message.

I decided to plot the "holder.roc" formula on a chart and I got error messages. Please see "PlotSeries paneROC3 code line. The error messages are as follows:
1) The best overload method match for 'System Math.Abs(sbyte) has some invalid arguments.
2) Argument 1: cannot convert from 'WealthLab Indicators.Volatility' to 'sbyte'.

I would appreciate if you could please help me fix the error so I can plot it in the chart.

Thanks.

CODE:
Please log in to see this code.


profile picture

Eugene

#39
Hi Anish,

1. Your code peeks into the future because selling at open of the bar preceeds accessing the same bar's close price:
CODE:
Please log in to see this code.

The correct fragment would be:
CODE:
Please log in to see this code.

A rule of thumb is that the specified bar should not exceed bar+1.

2. As to your PlotSeries needs, it takes a simple replacement of Math.Abs with DataSeries.Abs:
CODE:
Please log in to see this code.
profile picture

ashridharani

#40
Hi Eugene,

Thanks for the correct absolute function for the plot series.

I will also fix the other error with respect to the BuyAtLimit code. Much appreciate it.

Regards,
Anish
profile picture

Carova

#41
I want to find the "Goldilock's" area for buying and selling. I modified the strategy
CODE:
Please log in to see this code.


Did I do it correctly? Obviously not! All help appreciated. Thanks!

Vince
profile picture

Eugene

#42
Vince,

Obvious to whom? There's a dozen or two of published rotation strategy twists. Are you really using the strategy modification as per "How would I then modify the code to continue to buy the top 3, but only sell when it fell out of the top 10?"
CODE:
Please log in to see this code.


If not, this alone should be enough to add to the basic rotation strategy template:
CODE:
Please log in to see this code.

This website uses cookies to improve your experience. We'll assume you're ok with that, but you can opt-out if you wish (Read more).