2

In the field of stock market technical analysis there is the concept of rectangular price congestion levels, that is: the price goes up and down essentially never breaking the previous high and low price levels for some time, forming the figure of a rectangule. E.g.: http://cf.ydcdn.net/1.0.0.25/images/invest/congestion%20area.jpg.

Edit: to me clearer: the stock as well as the forex market is made by sets of movements called "impulse" and "correction", the first one being in the direction of the current stock's trendand the other in the opposite. When the stock is moving in the direction of the trend, the impulse movement is always bigger than the following correction, but sometimes what happens is a that the correction end-up being at the same size of the impulse. So for example, in a stock with a positive trend, the impulse movement moved from price $10,00 to $15,00, and than a correction appeared dropping the price to $12,00. When the new impulse appeared, thought, instead of passing the previous high value ($15,00), it stooped exactly on it, being followed by a new correction that dropped the price exactly to the previous low price ($12,00). So now we may draw two paralel horizontal lines in the stock's graph: one in the $15,00 price and other in the $12,00, forming a channel where the price is "congestioned" inside. And if we draw two vertical bars in the extreme sides, we have a rectangle: one that has its top bar in the high level and other in the low one.

I'm trying to create an algorithm in C++/Qt capable of detecting such patterns with candlestick data inside a list container (using Qt -> QList), but currently I'm doing research to see if anybody knows about someone who already did such code so I save lots of efforts and time in developing such algorithm.

So my first question will be: does anybody knows and open-source code that can detect such figure? - Obviously doesn't have to be exactly in this conditions, but if there is a code that do a similar taks, only needing for me to do the adjustments, that would be fine.

In the other hand, how could I create such algorithm anyway? It's clear the the high spot is to detect the high and low levels and than just control when those levels are 'broken' to detect the end of the figure, but how could I do that in an efficient way? Today the best thing I'm able to do is to detect high-and-low levels using time as parameter (e.g. "the highest price in four candles", and this using a very expensive code.

Momergil
  • 437
  • 1
  • 5
  • 20
  • I'm not sure if your problem is well defined. Surely, starting at time T = 0, and running through to arbitrary point in time T = n, you'll always be able to draw a rectangle around that region? What are the criteria for the rectangle to be considered a positive result? Does it need to move up and down a certain number of times? Is there a minimum time duration? Does the difference between min and max candle points have to be of a minimum value? – JBentley Dec 05 '12 at 03:26
  • Finding the maximum and minimum in a certain range is not difficult, and I doubt you'd have to do it very often. Is efficiency really a problem? – Beta Dec 05 '12 at 03:26
  • Beta, right now efficiency is not a problem, but one day it will be :) Imagin a unique software veryfing minute by minute such kind of chart patterns (and another dozen ones) in six different time frames in something like one thousand stock charts. In that case, efficiency is really important given the fact the software's user is supposed to be able to continuely use his PC while my software runs in background! (not mentioning other functionalities...) – Momergil Dec 05 '12 at 12:39
  • @Beta, and also I'm not quite certain my method actually is good enough. Of course I did it very basically - it's not supposed to be on its finals yet - but still would be interesting if I found someone that knows how to do better. – Momergil Dec 05 '12 at 12:40
  • @JonBentley, it's not that simple. It's not a metter of simply drawing rectangules, but drawing them in specific places given that places's "design". Watch carefully the image I linked in my question. There is no criteria for positive results; right now I'm only trying to identify the pattern, not deal with it. No to all other questions. Read the question carefully: I need to identify when such rectangular formations happen, that's it, no specific configuration. – Momergil Dec 05 '12 at 12:43
  • 1
    @Momergil Ok, so you're only interested in identifying rectangular formations, not in assigning values of worth to them. In that case, I stand by what I said - the problem is not well defined. You've said "drawing them in specific places given that place's "design"". What do you mean by that? What is it about a place's design that makes it specifically worth drawing a rectangle around? – JBentley Dec 05 '12 at 17:01
  • 1
    @Momergil E.g. you've referred to a diagram, but it's not obvious why the rectangle should be drawn where it is. As user605547 said in his answer, it is ambiguous and subjective where you choose to "see" the rectangle. For example, I've modified the image and drawn several other rectangles which, in the absence of any criteria, are all valid, as they all enclose a region where the price doesn't breach a high or low for some arbitrary length of time: [click here](http://tinypic.com/r/bgzr45/6). If you simply want an algorithm to identify ALL possible such rectangles then I can post an answer. – JBentley Dec 05 '12 at 17:16
  • @JonBentley, OK, I think I picked up your point - though I still think that the image I gave was somehow intuitive specially when I mentioned about calculating high and low price levels. Anyway, I'll edit my question. And besides, looking carefully I think I understand now why you missed my point: is because the figure I posted actually don't show a rectangle, but a channel ^^ Sorry for missing that, but in technical analysis we take them as being equal, so I didn't noticed ^^ – Momergil Dec 06 '12 at 00:12

2 Answers2

2

Technical analysis is very vague and subjective, hard to code in a program when everyone sees different things in the same chart. A good start would be to use some cost function such as choosing levels that minimizing the sum of squared distances, which penalizes large deviations more than smaller ones.

user805547
  • 1,245
  • 1
  • 15
  • 23
1

You should use the idea of 'hysteresis' thresholding; you create a 4-level state machine for how the price breaks the low (L) or high (H) levels. (first time reaches new low level) L->L, (return to low level) H->L,(new high level) H->H, and then (return to high level) L->H.

Arcturus
  • 550
  • 2
  • 6
  • could you please explain betther your idea? Concepts such as 'hysteresis' thresholding and 4-level state machine are not familiar to me, and so I couldn't understand your answer. – Momergil Dec 06 '12 at 20:15
  • Fair enough; check the 'Schmidt Trigger' section of this wiki article, http://en.wikipedia.org/wiki/Hysteresis#Electronic_circuits – Arcturus Dec 07 '12 at 01:22