Adding buy / sell rules to Rotation Strategy
Author: wmrgrove
Creation Date: 10/14/2012 12:23 PM
profile picture

wmrgrove

#1
I've searched all the posts on this topic and learned a lot from the posts, but still having trouble inserting new/additional buy and sell rules in the Rotation Stratiges. Would someone please help me with the following?

If you take the basic WL posted strategy; RSI ROTATION, I would like to add the following "example" rules


ADD BUY RULES : can this be s qualifier for the sort list? (my preference), or just a buy rule after the sort?
CODE:
Please log in to see this code.

ADD SELL RULES (to sell before, or after the set "number of days held", in rotation )
CODE:
Please log in to see this code.


ALSO ADD an ATR stop and PROFIT stop
CODE:
Please log in to see this code.

I have tried lots of ways to do this and got the added buy rule after sort to work, and a stop loss percent to work.
These rotational stratigies are a lot harder for us but key to what I want to do!

Thanks for any help! Bill

profile picture

Eugene

#2
QUOTE:
ADD BUY RULES : can this be s qualifier for the sort list? (my preference), or just a buy rule after the sort?

The buy condition should be added after sorting, that's right.
QUOTE:
ADD SELL RULES (to sell before, or after the set "number of days held", in rotation )

CODE:
Please log in to see this code.

According to the WealthScript Programming Guide (Peeking > Order of Trading Signals), here's how the exit block should look like:
CODE:
Please log in to see this code.

P.S. Don't forget to start the trading loop at 14 * 3 (or with GetTradingLoopStartBar(14 * 3) ) due to the use of a 14-period ATR (the same WealthScript Programming Guide, Indicators > Stability of Indicators).
profile picture

wmrgrove

#3
Eugene, thanks as always for the quick response. I will try these asap! Your comment about "Stability of Indicators", yes I try to watch that closely.

I've noticed that for long period indicators like 200pdSMA or 200pdROC the backtest performance report does not take into account the delay? For example a 3 year backtest using 200pd indicators showing 20% Annual Gain is really understating the result due to the strategies reduced active time versus buy and hold? The exposure % number is also understated I believe? Are the Wealth Lab results in the performance tab also impacted?? Is there any way to remove this effect? Of course the longer the backtest period the less the impact.

Thanks again, Bill
profile picture

Cone

#4
QUOTE:
...showing 20% Annual Gain is really understating the result due to the strategies reduced active time versus buy and hold?
That's correct.

QUOTE:
The exposure % number is also understated I believe?
All portfolio level metrics are understated in this case. See User Guide: Strategy Window > Backtesting Strategies > Inside a Portfolio Simulation > Note on Lead Bars.

QUOTE:
Is there any way to remove this effect?
We've proposed (after a user proposed it) adding an option to calculate portfolio metrics using the period starting from the first trade. Conversely in some cases this may overstate the results. The devil is in the details, and this request hasn't made it high in the priorities yet. Anyway, showing more pessimistic results for backtests isn't necessarily a bad thing, but I'd certainly prefer to err on the side of more accurate results.
profile picture

wmrgrove

#5
Cone, Thanks.
For what it worth I vote for a future enhancement that provides the option to select either, from the Earliest Backtest Date, or TradingLoop Start Bar Date, but not First Trade Date.
profile picture

wmrgrove

#6
Eugene, looks like I need some more help here, sorry!

I tried the rules you gave me above and the strategy sells immediately. My first attemt to post a strategy is below.
This is a "cleaned up version" with only one sort rule. It compiles fine but things are not correct due to my coding limitations. Thanks, Bill

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

Eugene

#7
QUOTE:
For what it worth I vote for a future enhancement that provides the option to select either, from the Earliest Backtest Date, or TradingLoop Start Bar Date, but not First Trade Date.

There has been numerous discussions on the forums. More insight available by searching for Lead Bars or going striaght to the FAQ: FAQ. You can also call your Fidelity rep and tell them you need this as an option on the built-in Performance tab, as we won't be able to consider a feature request modifying the Wealth-Lab client.

It's a considerable effort, and is also not planned for our MS123 Visualizers. However, starting from its upcoming release, all equity/drawdown charts in this library will show the equity/drawdown curves without the "flat line" i.e. from first trade rather than starting date (useful when actual trading date is much later e.g. 200 day SMA or 52 week MA). But to stress what you can find on the forums, modifying the Performance/Performance+ report is not an option.
QUOTE:
I tried the rules you gave me above and the strategy sells immediately.

On a closer look, I came with this version. Try it out:
CODE:
Please log in to see this code.
profile picture

wmrgrove

#8
Eugene, Thanks so much!

This is outstanding! It gives me a lot to work with and learn from!

I have read the recommended posts and will call Fidelity today about the "LEAD BARS" issue.

Thanks again, Bill
profile picture

wmrgrove

#9
Eugene, I've checked a lot of trades and everything seams to work well on the Strategy shown above!

One thing is that a good trending stock will sell after maybe 20 to 40 periods and then buy back as early as the next few days. This is a result of the ranking dropping below top X# then coming right back into top X#. Would it be possible to modify the above script to have two varibles, one for buying in the top X# (say top 5 rank) but only selling if it falls below the top Y# (say top 10 rank)?
I thought I saw an existing Strategy that did this a while ago but can't find it.

Thanks again, Bill
profile picture

Eugene

#10
It might be possible. Search the forums, I believe that exactly this question might have been raised in the past.
profile picture

wmrgrove

#11
Eugene, I found a post where you have modified the rotation Strategy to buy top 1, sell if not top 5. It seams to work but there are gaps in the backtest were it buys none. (try on any list) Before I try to cut and paste this concept into my strategy would you please check it?

Post was Wealth-Lab .NET » Trading Strategies » ROC Symbol Rotation - Modification of the Strategy
dated 10/21/2011 8:46 AM


Thanks for any help! Bill
profile picture

Eugene

#12
Sorry Bill, constant modifications of code are not covered by our support policy. In the end, it removes the great fun of debugging yourself. ;)
profile picture

Eugene

#13
Question moved to the appropriate thread:

ROC Symbol Rotation - Modification of the Strategy
profile picture

Eugene

#14
wmrgrove asked:

Working on a Rotational Strategy I have benn adding multible exits including "external symbol timing". Everything seams to be working fine but I have a few questions on the correct / valid coding process.

I have "cut and pasted" sell rule ideas and want to make sure what I have now is correct? These Rotational Trading Strategies make it hard to see if every thing is working as you want.

On a "Timing Exit" SellAtMarket, I have three rules. From other examples here I've learned how to combine 2 "AND" rules.
At the end of the combined rule and before a 3rd rule, placing the ; character appears to make the 3rd rule an "OR" rule as was desired? If I remove the ; after the combined rule the 3rd rule becomes "AND". Is this a correct code method or should the 3rd "OR" rule have its own SellAt Market rule.

see "cut-out" code here
CODE:
Please log in to see this code.


2nd question, if I want to sell all positions when the "timing Sell Rule" is true, do I just insert SellAtMarket(bar + 1, Position.AllPositions); for this Rotational Strategy??? Is that correct?

Last question / varification, my sma rule above, just before the Profit Limit Sell Rule, is a condition that must be met to execute a 20% or greater profit sell? Is that correct?

Thanks, Bill
profile picture

Eugene

#15
Programming language is not art. Copying and pasting without some knowledge of the basics and understanding will not work.

CODE:
Please log in to see this code.

No, it is absolutely incorrect.
QUOTE:
At the end of the combined rule and before a 3rd rule, placing the ; character appears to make the 3rd rule an "OR" rule as was desired?

I will deliberately not make it easy for you by answering. Instead, I will point you at the FAQ, hoping that you'll learn the basics of language syntax before trying making changes at random:

How do I start with C# ? in the Wealth-Lab Wiki

QUOTE:
2nd question, if I want to sell all positions when the "timing Sell Rule" is true, do I just insert SellAtMarket(bar + 1, Position.AllPositions); for this Rotational Strategy??? Is that correct?

On the surface it may work, but note that the placement should be correct also.

QUOTE:
Last question / varification, my sma rule above, just before the Profit Limit Sell Rule, is a condition that must be met to execute a 20% or greater profit sell? Is that correct?

Same as question #1. Looks like a wild mix of an ATR profit target and Percent profit target. The resulting construct doesn't make any sense and will not work. Instead, I'd suggest you to add a profit-taking rule to a Rule-based Strategy and click "Open in new code window" to have a code that works.
profile picture

wmrgrove

#16
Eugene, I will take the intent of your reply positively. You are correct that the more complex things I want to do require more coding knowledge. I have downloaded the suggest material from the link and will learn more. It is easy to get excited about the capabilities of WL and want to create strategy.

After swollowing my pride I must say that a profit target that combines a minimum percent base and an ATRP adaptive element is not as you stated, "a wild mix of an ATR profit target and Percent profit target. The resulting construct doesn't make any sense and will not work"!! If it dosen't work it is because of syntax, not the concept. I was not asking an opinion on my idea but rather to re-phrase

Last question / varification, my sma rule above, just before the Profit Limit Sell Rule, is a condition that must be met to execute my modified profit target level or greater profit level sell rule? Is that correct?"

Any comments from Forum appreciated, Bill
profile picture

Eugene

#17
Bill,
QUOTE:
After swollowing my pride I must say that a profit target that combines a minimum percent base and an ATRP adaptive element

My apologies, but I never intended to criticize your exit rule concept! I have misread your post saying "Profit Limit Sell Rule, ...a 20% or greater profit sell", mistakenly thinking that what you're trying to incorporate is a simple Profit Target %, coded in a manner that simply didn't align with my definition of that exit. Whereas it was an adaptive exit, and your question actually referred to the "Close below SMA" condition. Sorry for that.

So to answer your last question: yes, it's correct.
profile picture

Eugene

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

Here's to cut the learning curve for you:

QUOTE:
// If I place the ; character at the end of the line, is the next line an “ OR’ function

No. This creates a "detached" boolean condition, unused by the code below it.
QUOTE:
// If I remove the ; character all 3 rules are an “AND” function? Correct?

Well, yes, the two if's are combined.

To create an OR, use || or | (where applicable; for more see any C# 101 book):
CODE:
Please log in to see this code.
profile picture

wmrgrove

#19
Eugene; thanks that helps a lot! My wording could have been clearer.

I know you are trying to help us, " learn how to fish, not just give us fish" !!!

My adaptive profit exit seams to work pretty well with the mix of, fixed percent base, and ATRP adaptation to volatility.

Have a great weekend, Bill