Stocks and Commodities: Jun 2007 - Rectangles

Interested in sharing a snippet of code? A nice chart? Want to show off your great multi-system, or get feedback on one you're considering? Here you can share your creations with others.

Stocks and Commodities: Jun 2007 - Rectangles

Postby Overload » Wed May 16, 2007 9:12 am

This post is for those interested in exploring "How Effective Are Rectangles?", an article by Markos Katsanos in the June 2007 issue of Stocks and Commodities magazine.

You can import the necessary Setups by right-clicking on the following file and selecting "Save Target As...":

http://www.stratasearch.com/files/tasc_jun2007.sse

After the above file has been saved on your computer, use the File > Import Database menu to locate and import it. You will then have access to these items:

A Strategy named:
Rectangle Breakout Strategy

4 Custom Formulas named:
IsRectangle
RectangleBreakout
RectangleHigh
RectangleLow

A Trading Rule named:
Rectangle Breakout Long (Primary)

And a new Chart named:
Rectangle Formation

There's an old Steve Martin routine where he says "YOU... can make a million dollars and NEVER pay taxes." He then quietly mumbles "first, get a million dollars" before moving on to the part about never paying taxes. This month's article on Rectangles is somewhat similar by claiming that you can improve your exits from rectangles; but first, he quietly says, find a rectangle formation. And then he moves on to discuss how one can best exit from that rectangle formation entry. (Side note: I e-mailed the author requesting the details of his Rectangle Formation formula, but he did not respond).

To implement this as a full-blown strategy that can be back-tested, we first needed to have a Custom Formula that would identify rectangle formations to begin with. Mr. Katsanos did at least provide the following criteria: "a sideways price action bounded by two horizontal or nearly horizontal support & resistance lines." Based on this description, I was able to set up a parameter-based Custom Formula that identifies such support & resistance lines, while still requiring a range of up and down movement in the middle as well as sideways action during that entire period.

The article primarily focuses on identifying a price target once a rectangle breakout has occurred. But price targets can also be somewhat vague in their implementation as a full trading system. What do you do if a price target isn't reached? Do you still exit at the price target even if the other indicators show there is still plenty of upward potential? These questions also were not discussed.

To implement this as a Strategy, I set it up to enter a long position when the price broke above the top line of the rectangle. The position would then exit based on the author's calculated price target. In case the price target was not reached, I also placed a stop at the bottom line of the rectangle.

It's first important to note that rectangle formations are relatively rare. As the author said, he found only 100 in the entire 2004-2005 period. Because of this, and because I wanted to test a range of parameter sets, I ran my analysis on 5 years of data for All Securities. What surprised me was just how well this indicator performed on its own. Nearly all parameter sets tested proved to be profitable at the portfolio-sizes of 5 and 10. As well, nearly all parameter sets had a percentage profitable better than 50%, with many being 75% or higher. I did not examine standard deviations or other performance values, and the holding periods were rather long (100 to 200 days). But this brief analysis at least showed that the rectangle breakout indicator has some potential, particularly if it is used alongside other indicators.

Users that wish to investigate this indicator further should prepare themselves for a slow analysis, since it will likely need to be run against "All Securities" to obtain a worthwhile number of trades. For this reason, the Primary Trading Rule with this plug-in is not turned on for processing by default. If you wish to include this indicator in your automated searches, enter the Setups > AutoSearch Setups and update the setup of your choice (Generic AutoSearch for most OneClick Searches). On the Primary tab, locate "Rectangle Breakout Long" and double-click the associated Random box. Or, if you wish your automated search to use this indicator exclusively as the starting point, double-click the associated Always box.

I don't think it was the author's intention that we'd be focusing on the entry side of the rectangle breakout rather than the predicted exit that he discussed, but it's the entry that seems to be the real benefit for system traders.

Rectangle Formation Chart:
Image
Overload
 
Posts: 2248
Joined: Wed Nov 30, 2005 12:14 pm

Re: Stocks and Commodities: Jun 2007 - Rectangles

Postby Jaded » Sun Apr 22, 2012 3:04 am

I finally pinpointed (many of) my memory allocation error (malloc) issues down to the Rectangle Breakout Long trading rule. I was running against a 3 year period of a sector consisting of only 5 ETF's.. This trading rule would spike memory usage almost immediately. Could you look into what was causing this massive spike and if it can be fixed, or if the custom formula's themselves just use a lot of RAM.

Hope the info helps others who have the trading rule imported/enabled and are running into random malloc issues.
Jaded
 
Posts: 151
Joined: Mon May 14, 2007 6:49 pm

Re: Stocks and Commodities: Jun 2007 - Rectangles

Postby Overload » Sun Apr 22, 2012 12:22 pm

Thanks for the tip. I will look into it.

Pete
Overload
 
Posts: 2248
Joined: Wed Nov 30, 2005 12:14 pm

Re: Stocks and Commodities: Jun 2007 - Rectangles

Postby Overload » Wed Apr 25, 2012 2:33 pm

I did some tests on this formula and wasn't able to find any memory leaks. However, you are correct that it does require a large amount of RAM due to the large size of the script itself, and that could be problematic if you're pushing the RAM limits already with a large sector and large evaluation period.

I'll put this on the list to see if the RAM usage can be minimized for lengthy scripts like this one. But in the meantime, if you're having problems with memory faults in StrataSearch, removing these rectangle formulas from your AutoSearch Setups might prove helpful.

Pete
Overload
 
Posts: 2248
Joined: Wed Nov 30, 2005 12:14 pm

Re: Stocks and Commodities: Jun 2007 - Rectangles

Postby marco » Sat May 14, 2016 6:07 pm

Pete,

Looking for a way to code horizontal support and resistance lines I came to this thread. I'm hopeful that the "IsRectangle" custom formula can be modified to achieve this but I'm afraid this goes beyond my coding capabilities. Any help would be appreciated.
Last edited by marco on Sat May 14, 2016 8:19 pm, edited 2 times in total.
marco
 
Posts: 52
Joined: Mon May 27, 2013 6:15 pm

Re: Stocks and Commodities: Jun 2007 - Rectangles

Postby Overload » Sat May 14, 2016 7:55 pm

Rectangles actually identify a trading range, and then use the upper and lower bounds of that trading range as the support and resistance. But that's more than you need here, and probably more than you want since this particular formula produces pretty few trades based on the trading range requirements.

As a suggestion, you might try using just the upper and lower bounds. For example, in the IsRectangle formula, use only low1, low2 and low3. If the close drops below low1, it has broken the small support level. If it drops below low1 and low2, it has broken the medium support level. And if it drops below low1, low2 and low3, it has broken below the large support level.

I haven't coded this and have no idea how it would work, but it seems like a reasonable approach. You can play around with different "days" values to see what works best. And you could also use high1, high2 and high3 similarly for resistance.

Pete
Overload
 
Posts: 2248
Joined: Wed Nov 30, 2005 12:14 pm

Re: Stocks and Commodities: Jun 2007 - Rectangles

Postby marco » Sat May 14, 2016 8:28 pm

Can you walk me through this? Should I delete the lower part in the custom formula where the highs are coded? And what do I do next if I want to create a strategy where I enter when price is just above horizontal support?

Or maybe I could use the "GetTrendlineUp" custom indicator; would GetTrendlineUp(0.01) result in identifying an almost horizontal trendline? And is it there a way to require a minimum length of the trendline?
marco
 
Posts: 52
Joined: Mon May 27, 2013 6:15 pm

Re: Stocks and Commodities: Jun 2007 - Rectangles

Postby Overload » Mon May 16, 2016 10:12 am

I was thinking something like this:

Code: Select all
// Custom Formula Name: SupportLevel
days = parameter("Days");

periodlen = days / 3;

// get the low from 3 different points
low1 = low(L, periodlen-1);
low2 = ref(low(L, periodlen-1), -periodlen);
low3 = ref(low(L, periodlen-1), -(periodlen*2));

count = 0;
count = if(close <= low1, count + 1, count);
count = if(close <= low1 and close <= low2, count + 1, count);
count = if(close <= low1 and close <= low2 and close <= low3, count + 1, count);

SupportLevel = count;


Again, I haven't tested this, but this is the type of approach I was thinking. I have no idea how it will actually work.

As for trendlines, sorry but the length of the trendline is dependent on the percentage movements, and can't be limited by length.

Pete
Overload
 
Posts: 2248
Joined: Wed Nov 30, 2005 12:14 pm


Return to Shared Area

cron