Retracement from High with SMA Above/Below for X days
Author: ahz882002
Creation Date: 12/27/2016 2:58 PM

#### ahz882002

#1
Is there a strategy already exist for the following combinations:
1. Entry at market when Fast Moving average (Ex 50 days) cross slow moving average (say 200 day) only if previous high to low has >=20%.
2. Exit when price moves below slow moving average (say 50DMA) only when the price has stayed below 50DMA for x numbers of trades (ex 10).

#### Eugene

#2
1. What is "previous high to low..."? Please clarify.
2. How does your system exit from those first 10 trades?

#### ahz882002

#3
Let me explain:
1. High/Low with the attached chart as an example.
2. The system exist the trade also explain in the attached charts.

Your help is greatly appreciated. Let me know if the attached chart help explain the idea clearly.

#### Eugene

#4
Ouch, not a PDF again! :) There is no need to use PDF for posting screenshots and/or error messages. If it's a modal Windows error dialog, you could strike Ctrl-C to copy the underlying error message and Ctrl-V to paste it when replying (a standard Windows shortcut). Please only attach text as text (TXT) and images as images (PNG preferred, MS Paint which is built in any Windows will do it). Thank you for your understanding.

1a. When defining the "downtrend cycle", any 20% trough should be okay?
1b. How long to wait for a crossover after the "downtrend cycle"? In other words, when do you stop waiting and invalidate the setup if a crossover fails to materialize after N (bars i.e. days, weeks, years)?

2. Just to confirm. The "number of trades" in your initial post was an error and actually should be read as "number of trading sessions", right?

#### Cone

#5
1. It looks to me like you:
(a) Find the Highest point prior to the most recent CrossUnder.
(b) Keep track of the Lowest point since (a)
(c) When price has retraced 20% of (a)-(b) && the fast sma CrossOver slow sma, BuyAtMarket.

The only question about (c) is if the 20% rule has to be true on the precise day of the crossover? In other words, do you still buy if the 20% retracement happens several bars after the crossover?

#### Cone

#6
Here's a shot at it. You must have Community.Indicators and Community.Components installed from Extensions.

CODE:

#### Eugene

#7
Nice code Robert. Here's my take at it (w/o using CrossUnderBar from C.Indicators):

CODE:

#### ahz882002

#8
Robert and Eugene,

Thanks a lot for the quick turn on the code. I will give this a try. Sorry for the PDF files.

Sorry for the delay in answering your questions: Below are my answers to your questions:

From Eugene:
QUOTE:
1a. When defining the "downtrend cycle", any 20% trough should be okay?

Answer: Yes any trough of >=20%is OK.
QUOTE:
1b. How long to wait for a crossover after the "downtrend cycle"? In other words, when do you stop waiting and invalidate the setup if a crossover fails to materialize after N (bars i.e. days, weeks, years)?

Answer: Good question.. But it could be indefinite. If need to define, maybe 5 years

QUOTE:
2. Just to confirm. The "number of trades" in your initial post was an error and actually should be read as "number of trading sessions", right?

Answers: You're correct that it shoull be # of trading sessions", not # of trades.

From Robert:

QUOTE:
The only question about (c) is if the 20% rule has to be true on the precise day of the crossover? In other words, do you still buy if the 20% retracement happens several bars after the crossover?

Answer: Yes. As a matter of fact, the most likely scenario is that the cross over happens after the >20% retracement.

#### Eugene

#9
QUOTE:
Answer: Good question.. But it could be indefinite. If need to define, maybe 5 years

There's a "Timeout Entry" parameter (set at 20 bars by default) which you can adjust to your taste. Any acceptable value could do - even go beyond the upper limit of 200. Click the blue label and type in your own value if you need 5 years (~1260 bars on average).

#### ahz882002

#10
Hello,

I did some testing on the two sets of code.
Eugene's code seems to have triggers the entry the way I was envisioning. However my exist strategy seems to be too sensitive.

Could we change the exit strategy to be: Exit with % trialing stop to be 2X of the past 50 day standard division?

Thanks.

#### Eugene

#11
QUOTE:
Could we change the exit strategy to be: Exit with % trialing stop to be 2X of the past 50 day standard division?

Hmm, what is "division"? If you mean standard deviation then give this a go:

CODE:

#### ahz882002

#12
Thanks, Eugene.

A few questions to help me fine turn...
1. I assume the current code watches a trough for 20 trading session before establishing the uptrend by <<paramTimeout = CreateParameter("Timeout entry", 20, 10, 200, 10); >> If I were to change the time out to be 1000 trading sessions, do I then change the above to be:
paramTimeout = CreateParameter("Timeout entry", 1000, 10, 200, 10);

If not, please suggest how I should change the code.

2. If I were to change Peak to trough decline from >=20% to >=30%, do I change this statement to be:
paramDecline = CreateParameter("Decline %", 30, 5, 50, 5);

Alan

#### Eugene

#13
Alan, changing the parameters is meant to be interactive for the user. Check out the Wealth-Lab User Guide > Reference > Data Panel > Strategy Parameters.

#### ahz882002

#14
Eugene,

Thanks a lot. This really helped.

While running two different versions of your code above (the earlier one is with stay-out period as exit strategy, while the latter one is based on standard deviation). I found that the standard deviation parameter is not available on the data panel for optimization purpose.

Can you please make changes to replace the exist strategy to be the following and with proper parameters for fine tune optimization?

1. Instead of using standard deviation, please calculate the volatility using average # of days (such as 50 days) of (High of day - Low of day))
2. Calculate stop price: Program two multipliers (m1, m2) with a duration of d1 (# of sessions) for 1st volatility (m1*average difference), after which the 2nd volatility (multiplier m2 * 50day average difference)..
3. Also define an absolute stop price of X% below the entry price.
4. Exit at the stoplimit price at one of the 3 stop prices above

Really appreciate your help.

#### Eugene

#15
Hi Alan,

Although I provided you with a framework to start your own strategy programming, our support doesn't cover custom code and/or its constant modifications.

On how to use Strategy parameters please refer to downloadable strategies, our the Wiki, the WealthScript Programming Guide (for example, Programming Trading Strategies > Strategy Parameters), QuickRef, forum discussions etc. Given the abundance of examples, learning how to expose the standard deviation parameter should not take more than two minutes of your time.

#### ahz882002

#16
Eugene,

Thanks for your help so far. I understand your position. I will look into more on programming and do some changes to the code. I may come back with more questions while I explore more. Once again, Thanks.