I am looking to enter a long trade if one of the next three bars crosses above the high of the signal bar or a short if one of the next three bars crosses below the signal bar. I know the code for the next bar, but can't figure out one of the next three bars. Below is a simple MA crossover strategy that I was trying to apply that condition to.
CODE:
Please log in to see this code.
Size:
Color:
Well, you can code it brute force by ORing the conditions together:
CODE:
Please log in to see this code.
That's going to be the fastest way to do it in C# because the double OR construct will stop testing as soon as a condition is found true. For example, if condition1 is false and condition2 is true, C# will take the branch without ever testing condition3 saving run-time time.
But I gather you're looking for a more compact way to do this with a WL library call, and the WL Community.Components library has
four functions which can help. Check out
Series Is Above http://www2.wealth-lab.com/WL5Wiki/SeriesIsAbove.ashx. Again, there are four functions all starting with "Series Is" at
http://www2.wealth-lab.com/WL5Wiki/AllPages.aspx?Cat=Community%20IndicatorsBe sure to download and install the Community.Components library first if you haven't already done so using the WL Extension Manager.
Also, remember you're testing the "last three bars" in the
past, not the "next three bars" in the future. WL simulations are based only on past bar behavior.
Size:
Color:
The way you should handle this kind of
"if one of the next X bars triggers..." is through this useful design pattern:
WealthScript Techniques | Setups, Triggers, Delays, and TimeoutsHaving applied this pattern you should get something like this:
CODE:
Please log in to see this code.
I'm just not clear to me what
bar did you really mean for the stop order: the low of the setup bar (can be any of the three) or the low of the penultimate bar.
Size:
Color:
Thanks to both Superticker and Eugene for your assistance. Eugene, the stop bar would be the low of the bar where the fast MA crosses the slow MA. The goal is to try to avoid the whipsaws where the fast MA crosses down for only 1 bar and then reverses up. The reason for looking at the next 3 bars following a cross is that often the breakdown bar (the bar where the cross occurs) is a wide range bar, which is often followed by a narrow range bar or two that do not exceed the low of the breakdown bar. The way I wrote it, I was missing a lot of good trades that initiated on the 2nd or 3rd bar after the MA cross. The goal is to employ a hedging strategy with minimal whipsaws, yet catches the majority of the pullbacks that last a week or longer.
Size:
Color:
QUOTE:
Eugene, the stop bar would be the low of the bar where the fast MA crosses the slow MA.
Then the ShortAtStop line should get the job done:
CODE:
Please log in to see this code.
Size:
Color:
Yep, that looks like it did it. Thanks again Eugene.
Size:
Color:
You're welcome.
Size:
Color: