Using "Price >= Highest Price in Look Back period"
Author: sdbens20
Creation Date: 12/8/2012 9:19 AM
profile picture

sdbens20

#1
I'm trying to write a rules-based strategy that buys a stock after the 30-day %K stochastic has crossed above 50 but not until the day during the five days following the crossing that the opening price is higher than the high for the day of the crossing. I'm using the following set of rules:

Buy at Market
Stochastic %K crosses above over bought level (StockK Period = 30, Overbought Level = 50)
Price >= Highest Price in look back period (Price1 = Open, Price2 = High, Lookback Period = 5)
Sell at Market
Stochastic %K is oversold (StockK = 30, Oversold Level = 50)

No signal are generated on one year's SPY daily data (Yahoo's adjusted). BUYS should occur on 4/27/12, 6/19/12, 6/27/12, 7/27/12, 10/4/12, and 11/29/12.

Would you please tell me why this set of rules is producing no signals?

Thank you,

Sherm
profile picture

Eugene

#2
Sherm,
QUOTE:
but not until the day during the five days following the crossing that the opening price is higher than the high for the day of the crossing.

By default, conditions you add to an entry or exit are ANDed. Getting "but not until the day..." to work is a bit tricky in the Editor. You could try using Multi-Condition Groups there in the following order to achieve what you want.

Note the order of rules to understand the logic:

1.1. Open > Highest High(5) is processed on the current bar
2.1. A Multi-Condition Group found...
2.2. ...Strategy tries to find at least 1 condition(s) in the past i.e. within the specified lookback period (up to 6 bars back). The (only) condition here is the StochK crossover.

Hope that helps.
profile picture

sdbens20

#3
I'll give your suggestion a try.

Thank you.
profile picture

Eugene

#4
For more reference on MC groups see the User Guide > Strategy Window > Strategy Builder > MC, Multi-Condition Group.

EDIT 06/24/2020: Also check out our tutorial video on Youtube:

Wealth-Lab 101: Strategy Builder Multi-Condition Groups
profile picture

sdbens20

#5
It seems that "Price >= Highest(or Lowest) Price in look back period" does not create signals if Opens are not compared to previous Opens, Closes to Closes, etc. For example, comparing an Open to a previous High will not generate a signal. Is this finding an error in the functioning of this condition or is the condition functioning as designed?
profile picture

Eugene

#6
No, it does not seem so to me. Here's the rule converted to code and it does generate signals:

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

sdbens20

#7
Did you try running the condition in the Rules Editor?
profile picture

Eugene

#8
It does not matter where it's run.
profile picture

sdbens20

#9
In the Rules Editor, on one year's SPY daily data:

Buy at Market
Price >= Highest Price in look back period (Price1 = Open, Price2 = Open, Lookback Period = 5)
Sell at Market
Stochastic %K is oversold (StockK = 10, Oversold Level = 50)

produces 16 BUYS.

Buy at Market
Price >= Highest Price in look back period (Price1 = Open, Price2 = High, Lookback Period = 5)
Sell at Market
Stochastic %K is oversold (StockK = 10, Oversold Level = 50)

produces no signals.
profile picture

Eugene

#10
And what should it prove?

"Price2 = Open, Low and Close" all produce signals. "Price2 = High", naturally, triggers much rarely. For instance, only 19 times on the Dow 30 stocks on one year's daily data. 63% Dow stocks have hit, and 37% didn't.

If this particular code misses any signal it should take (not this one from your yesterday's thread), let me know. Otherwise I see no problem here.

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

sdbens20

#11
Eugene,

Since I don't know how to code strategies in WLP, I rely on using the Rules Editor. Please answer my question based upon investigating it from looking at the rules involved, not upon code.

Did you get the same results on SPY that I reported in my 10:04 post? Please try the rules-based strategy I submitted and let me know if your results are different. Maybe your "And what should it prove?" question will be answered.

Thank you.

Sherm
profile picture

Eugene

#12
Sherm,

Of course I looked at it from the both sides, and went farther by estimating the percentage of occurences of this condition. I believe that I provided a sufficient hint that, unfortunately, seems to have been overlooked. I suggest you open a chart and notice how frequently does the open price gaps above the highest high of 5 days.

Just give it a thought, and you'll quickly see what was wrong in your presumption that it's something related to how Wealth-Lab works. Gaps of that order do not happen every day, as opposed to the open price exceeding (say) a highest low. There's absolutely nothing wrong in that it didn't happen in SPY this year.
profile picture

sdbens20

#13
Shouldn't the rules-based strategy sent you at 10:04 this morning produce a BUY on SPY daily data on 1/4/12? My chart doesn't show any trades for the entire year.

profile picture

Eugene

#14
That's a good case.

No, the rule-based strategy shouldn't produce a buy on Jan 4 because that rule, converted to code by Rule Wizard itself as
CODE:
Please log in to see this code.

has a chance to trigger only if the open price is equal to the high price of that bar i.e. if price had a full gap up and then went only south. However, the open on Jan 3 was 125.87 and the high was 126.49, not satisfying the criteria.

For example, "Price1 = High" and "Price2 = High" has no problem meeting the requirement as high of a bar can always be equal to itself.

The open price of that bar is within the lookback period. Should the rule had been written as follows,
CODE:
Please log in to see this code.

it would have triggered a buy at market on Jan 4.

Anticipating your reasonable question how to catch that setup using rules, I'd suggest you take the "Price makes a new high within period" condition instead. If exit rules provide (e.g. StochK threshold was set to 80 rather than 50), that would make it trigger on Jan 4.
profile picture

sdbens20

#15
Your suggestion won't trigger a BUY according to the criteria I described in my original post on 12/8.

Thanks anyhow.
profile picture

Eugene

#16
QUOTE:
Your suggestion won't trigger a BUY according to the criteria I described in my original post on 12/8.

Let's go through some of the dates from your original post:
QUOTE:
BUYS should occur on 4/27/12, 6/19/12, 6/27/12, 7/27/12, 10/4/12, and 11/29/12.

6/27/12: should not buy. the open price of 6/26/12 was not nearly close to exceeding the 5-day highest high.
7/27/12: should not buy. Same as above: 4-day highest high at best.
11/29/12: should not buy: the open price of 11/28 was the 4-day's lowest. Buy should happen on 11/30.

You might have some success with this trick, based on including Shifted Moving Average (from Community Indicators) in a rule condition, additive to open price making a new 5-day high.

The idea is to delay the high. Although it's not ideal, that's the closest I could devise using rules.

Of course, the best option is to start learning how to code: How do I start with C# ?, exploring the many code examples out there. "Open code in new Strategy window" makes it particularly easy to make a slight modification to previously a rule-based strategy - like the one suggested on 12/10/2012 2:04 AM.
profile picture

Supersol

#17
Hi Eugene,

I have a question on the code you are discussing in this thread with reference to Post #14:
Where is the difference between using either
a1) ROC.GetSeriesValue(bar,Open) or
a2) Open[bar]

and where are the differences in the results when using:
b1) Highest.Value(bar-1,High,5)
b2) Highest.Series(High,5)[bar-1]

I haven´t found any documentation about GetSeriesValue so far.

Thanks,
M
profile picture

Eugene

#18
QUOTE:
I haven´t found any documentation about GetSeriesValue so far.

Disregard it. It's not a convention, just a ROC something.

QUOTE:
and where are the differences in the results when using:
b1) Highest.Value(bar-1,High,5)
b2) Highest.Series(High,5)[bar-1]

The key difference is that you generally would want to call .Series because that speeds up the code by referring to a cached copy of the DataSeries. For example, when plotting it, checking it on every bar inside the main loop, or when building a DataSeries on top of Highest.

On the other hand, if you only need that value sporadically, you might want to use .Value (where available -- you will not find it among Community Indicators) and save a little RAM. For example, if you were exiting a short position with a specific entry signal and wanted to calculate a dynamic value, like the highest Close since trade inception, then .Value is your choice:

CODE:
Please log in to see this code.


To sum things up:

1. The static Series method is typically used to create instances of a DataSeries in trading systems and cache them in RAM, saving from needless re-calculation of the indicator over and over and avoiding multiple copies.

2. The static Value method allows to return an indicator's value "on the fly" for a particular bar number, w/o having to create and keep the indicator's instance at the price of speed.
profile picture

Supersol

#19
Understood, thanks Eugene!
profile picture

cicerotullius

#20
May I add here a question:

pls. Eugene, the Rules Wizzard is creating a certain highest High Formula, as you posted yourself like this:

if (ROC.GetSeriesValue(bar,Close)>=Highest.Value(bar,Close,2))

or

if (ROC.GetSeriesValue(bar,Open)>=Highest.Value(bar,Open,5))

What is the ROC doing inside of highest high?

What not simply such formula:

if (High(bar)>= Highest.Value(bar,High,2))

I really dont understand what ROC is doing inside of a HH formula.

thx
profile picture

Eugene

#21
The irony is that ROC has nothing to do with this, Highest in particular. Its GetSeriesValue method is nothing but wrapper for the Close (Open etc) for the current bar. Here it's equivalent to Bars.Close[bar] (Bars.Open[bar] etc). It looks like some leftover or internal wrapper. Not elegant for sure but does nothing wrong. No other indicator seems to have a forgotten leftover like this.

That's why I said: disregard it.
profile picture

cicerotullius

#22
ok thx, so my formular:

if (High(bar)>= Highest.Value(bar,High,2))

works equally?
profile picture

Eugene

#23
Equal to what? There are 3 formulas. Based on what I said above, and after correcting your syntax:
CODE:
Please log in to see this code.
profile picture

streak

#24
Then why not
CODE:
Please log in to see this code.
?

ie generally wouldn't one step back from the 'current' bar when comparing to some historic series like
CODE:
Please log in to see this code.
?
profile picture

cicerotullius

#25
Thx a lot. Yes eugene you are right, this is code that I wanted, actually with the -1 that streak mentioned.

as for streak: if( High[bar] >= High[bar-1] ) this I dont want to use, but actually the one with the series because its a "highest high", meaning if high price goes above ALL the highs from the last period, where period will be defined (is it two like here or 20 is up to optimizer)

thx agian
profile picture

Eugene

#26
streak's right, a 1-bar offset i.e. [bar-1] is typically used here for comparison purposes.
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).