9

This question and related answers will be for educational or learning purpose only.

This question is much different from my other post and is not duplicate. Since it was creating confusion and as suggested by @MT0, I am posting this as a new question here.

I have below table, where I upload stock data on daily basis.

/* CREATE TABLE */
CREATE TABLE RAW_SOURCE(
  Stock  VARCHAR(100),
  Close_Date DATE,
  Open   NUMBER,
  High   NUMBER,
  Low    NUMBER,
  Close  NUMBER,
  Volume NUMBER
);

/* INSERT QUERY NO: 1 */
INSERT INTO RAW_SOURCE(Stock, Close_Date, Open, High, Low, Close, Volume)
VALUES
(
'XYZ', '01/01/2021', 40, 40.5, 38.5, 38.8, 83057
);

/* INSERT QUERY NO: 2 */
INSERT INTO RAW_SOURCE(Stock, Close_Date, Open, High, Low, Close, Volume)
VALUES
(
'XYZ', '02/01/2021', 39.2, 39.2, 37.2, 37.8, 181814
);

/* INSERT QUERY NO: 3 */
INSERT INTO RAW_SOURCE(Stock, Close_Date, Open, High, Low, Close, Volume)
VALUES
(
'XYZ', '03/01/2021', 38, 38.5, 36.5, 37, 117378
);

/* INSERT QUERY NO: 4 */
INSERT INTO RAW_SOURCE(Stock, Close_Date, Open, High, Low, Close, Volume)
VALUES
(
'XYZ', '04/01/2021', 36.5, 36.6, 35.6, 35.7, 93737
);

/* INSERT QUERY NO: 5 */
INSERT INTO RAW_SOURCE(Stock, Close_Date, Open, High, Low, Close, Volume)
VALUES
(
'XYZ', '05/01/2021', 35.35, 36.8, 35.1, 36.7, 169106
);

/* INSERT QUERY NO: 6 */
INSERT INTO RAW_SOURCE(Stock, Close_Date, Open, High, Low, Close, Volume)
VALUES
(
'XYZ', '06/01/2021', 36.5, 38.5, 36.5, 38, 123179
);

/* INSERT QUERY NO: 7 */
INSERT INTO RAW_SOURCE(Stock, Close_Date, Open, High, Low, Close, Volume)
VALUES
(
'XYZ', '07/01/2021', 37.5, 39.5, 37.3, 39.4, 282986
);

/* INSERT QUERY NO: 8 */
INSERT INTO RAW_SOURCE(Stock, Close_Date, Open, High, Low, Close, Volume)
VALUES
(
'XYZ', '08/01/2021', 39, 40.5, 38.5, 40, 117437
);

/* INSERT QUERY NO: 9 */
INSERT INTO RAW_SOURCE(Stock, Close_Date, Open, High, Low, Close, Volume)
VALUES
(
'XYZ', '09/01/2021', 39.7, 39.8, 39.3, 39.4, 873009
);

/* INSERT QUERY NO: 10 */
INSERT INTO RAW_SOURCE(Stock, Close_Date, Open, High, Low, Close, Volume)
VALUES
(
'XYZ', '10/01/2021', 39.2, 39.2, 37.2, 37.8, 62522
);

/* INSERT QUERY NO: 11 */
INSERT INTO RAW_SOURCE(Stock, Close_Date, Open, High, Low, Close, Volume)
VALUES
(
'XYZ', '11/01/2021', 38, 38.5, 36.5, 37, 114826
);

/* INSERT QUERY NO: 12 */
INSERT INTO RAW_SOURCE(Stock, Close_Date, Open, High, Low, Close, Volume)
VALUES
(
'XYZ', '12/01/2021', 36.5, 37.9, 36.3, 37.8, 281461
);

/* INSERT QUERY NO: 13 */
INSERT INTO RAW_SOURCE(Stock, Close_Date, Open, High, Low, Close, Volume)
VALUES
(
'XYZ', '13/01/2021', 37.5, 39.5, 37.3, 39.4, 77334
);

/* INSERT QUERY NO: 14 */
INSERT INTO RAW_SOURCE(Stock, Close_Date, Open, High, Low, Close, Volume)
VALUES
(
'XYZ', '14/01/2021', 39, 40.5, 38.5, 40, 321684
);

Below is the sample data for one stock "XYZ":

+-------+------------+-------+------+------+-------+--------+
| Stock | Close Date | Open  | High | Low  | Close | Volume |
+-------+------------+-------+------+------+-------+--------+
| XYZ   | 01-01-2021 |    40 | 40.5 | 38.5 |  38.8 |  83057 |
| XYZ   | 02-01-2021 |  39.2 | 39.2 | 37.2 |  37.8 | 181814 |
| XYZ   | 03-01-2021 |    38 | 38.5 | 36.5 |    37 | 117378 |
| XYZ   | 04-01-2021 |  36.5 | 36.6 | 35.6 |  35.7 |  93737 |
| XYZ   | 05-01-2021 | 35.35 | 36.8 | 35.1 |  36.7 | 169106 |
| XYZ   | 06-01-2021 |  36.5 | 38.5 | 36.5 |    38 | 123179 |
| XYZ   | 07-01-2021 |  37.5 | 39.5 | 37.3 |  39.4 | 282986 |
| XYZ   | 08-01-2021 |    39 | 40.5 | 38.5 |    40 | 117437 |
| XYZ   | 09-01-2021 |  39.7 | 39.8 | 39.3 |  39.4 | 873009 |
| XYZ   | 10-01-2021 |  39.2 | 39.2 | 37.2 |  37.8 |  62522 |
| XYZ   | 11-01-2021 |    38 | 38.5 | 36.5 |    37 | 114826 |
| XYZ   | 12-01-2021 |  36.5 | 37.9 | 36.3 |  37.8 | 281461 |
| XYZ   | 13-01-2021 |  37.5 | 39.5 | 37.3 |  39.4 |  77334 |
| XYZ   | 14-01-2021 |    39 | 40.5 | 38.5 |    40 | 321684 |
+-------+------------+-------+------+------+-------+--------+

Over the period of time, there will be more than thousands of records for each stock symbol and I would like to identify candlestick pattern only at the top of upmove/uptrend or at the bottom of downmove/downtrend but NOT in sideways (Since this will be false positive). Below is the sample screeshot:

enter image description here

Assuming today is 12th Jan 2021, below is the expected output:

+-------+-------------------+------------+------------+--------------+--------+---------------+
| Stock | Consecutive Count | Start Date |  End Date  | Latest Close | Volume |    Pattern    |
+-------+-------------------+------------+------------+--------------+--------+---------------+
| XYZ   |                 3 | 09-01-2021 | 12-01-2021 |         37.8 | 281461 | Piercing Line |
+-------+-------------------+------------+------------+--------------+--------+---------------+

Since the source table will have many other stocks, would like to show results on 12th Jan 2021 for other stocks as well if there is any pattern identified. I feel this is quite challenging and complex logic. Hence seeking help here. Thanks in advance.

Update: Thank you @JustinCave
Here's the formula for calculation: For Bullish Engulfing:

O1 > C1 and C > O and C > H1 and O < L1
where,

O1 = Previous day Open price  
C1 = Previous day Close price  
C  = Today's Close price  
O  = Today's Open price  
H1 = Previous day High price  
L1 = Previous day Low price 

For Bearish Harami:

(O1 < C1) and (O > C) and (O < C1) and (C > O1) and (H < H1) and (L > L1)
where,
    O1 = Previous day Open price  
    C1 = Previous day Close price  
    C  = Today's Close price  
    O  = Today's Open price  
    H1 = Previous day High price  
    L1 = Previous day Low price 
    H  = Today's High price  
    L  = Today's Low price 

For Piercing Line:

(O < C) and (O1 > C1) and (C > (C1 + O1)/2) and (O < C1) and (C < O1)
    where,
        O1 = Previous day Open price  
        C1 = Previous day Close price  
        C  = Today's Close price  
        O  = Today's Open price 
4
  • 2
    This seems like a problem which can be solved via MATCH_RECOGNIZE. Take a look at this Oracle Deep Dive explaining how to use it (The example they use is actually stock patterns). (Link)
    – Del
    Commented Oct 4, 2021 at 19:55
  • If you don't want to adapt the match_recognize logic that @MT0 provided you in your other question, you'd at least need to specify the exact logic you want to use. What constitutes a "Piercing Line" for example rather than a "Bullish Engulfing"? Commented Oct 4, 2021 at 20:05
  • Thank you @Del. I am going thru the link.
    – Richa
    Commented Oct 4, 2021 at 20:27
  • Thank you @JustinCave. I have updated the formula in OP.
    – Richa
    Commented Oct 4, 2021 at 20:28

2 Answers 2

9

Patterns in MATCH_RECOGNIZE work in a similar fashion to regular expressions; you want something like:

(Note: your PIERCING_LINE formula does not give the expected output so I have assumed you want C > (C1 + O1)/2 rather than C > C1 + (O1/2).)

SELECT *
FROM   raw_source
MATCH_RECOGNIZE (
  PARTITION BY stock
  ORDER BY Close_Date
  MEASURES
    CLASSIFIER() AS pttrn
  ALL ROWS PER MATCH
  PATTERN (
    ^initial_value
    |
    down+ (bullish_engulfing | piercing_line | $)
    |
    up+ (bearish_harami | $)
    |
    other
  )
  DEFINE
    down AS
          PREV(open) > open
      AND PREV(close) > close
      AND PREV(open) > PREV(close)
      AND open > close,
    up AS
          PREV(open) < open
      AND PREV(close) < close
      AND PREV(open) < PREV(close)
      AND open < close,
    bullish_engulfing AS
      -- O1 > C1 and C > O and C > H1 and O < L1
          PREV(open) > PREV(close)
      AND close > open
      AND close > PREV(high)
      AND open  < PREV(low),
    bearish_harami AS
      -- O1 < C1 and O > C and O < C1 and C > O1 and H < H1 and L > L1
          PREV(open) < PREV(close)
      AND open > close
      AND open < PREV(close)
      AND close > PREV(open)
      AND high < PREV(high)
      AND low > PREV(low),
    piercing_line AS
      -- O < C and O1 > C1 and C > (C1 + O1)/2 and O < C1 and C < O1
          open < close
      AND PREV(open) > PREV(close)
      AND close > (PREV(close) + PREV(open))/2
      AND open < PREV(close)
      AND close < PREV(open)
)

Which outputs:

STOCK CLOSE_DATE PTTRN OPEN HIGH LOW CLOSE VOLUME
XYZ 01/01/2021 INITIAL_VALUE 40 40.5 38.5 38.8 83057
XYZ 02/01/2021 DOWN 39.2 39.2 37.2 37.8 181814
XYZ 03/01/2021 DOWN 38 38.5 36.5 37 117378
XYZ 04/01/2021 DOWN 36.5 36.6 35.6 35.7 93737
XYZ 05/01/2021 BULLISH_ENGULFING 35.35 36.8 35.1 36.7 169106
XYZ 06/01/2021 UP 36.5 38.5 36.5 38 123179
XYZ 07/01/2021 UP 37.5 39.5 37.3 39.4 282986
XYZ 08/01/2021 UP 39 40.5 38.5 40 117437
XYZ 09/01/2021 BEARISH_HARAMI 39.7 39.8 39.3 39.4 873009
XYZ 10/01/2021 DOWN 39.2 39.2 37.2 37.8 62522
XYZ 11/01/2021 DOWN 38 38.5 36.5 37 114826
XYZ 12/01/2021 PIERCING_LINE 36.5 37.9 36.3 37.8 281461
XYZ 13/01/2021 UP 37.5 39.5 37.3 39.4 77334
XYZ 14/01/2021 UP 39 40.5 38.5 40 321684

db<>fiddle here

11
  • Thank you so much @MT0. It is working fine if I run this query in SQL commands but getting issue in front end of Oracle Apex. Give error "ORA-00911: invalid character". I think it is because of carat and dollar symbol. May I know if there's a fix for this? Thanks in advance.
    – Richa
    Commented Oct 5, 2021 at 7:08
  • 1
    Hi @MT0, now I know how to add latest day close and previous day close. May I know how to add below formula: 4 days ago CLOSE less than 4 days ago OPEN and 3 days ago CLOSE less than 3 days ago OPEN and 2 days ago CLOSE less than 2 days ago OPEN
    – Richa
    Commented Oct 5, 2021 at 10:35
  • 1
    close < LAG(open, 4) OVER (PARTITION BY stock ORDER BY close_date) for the first one and I'm sure you can adapt it to the second and third ... you can probably do it with MATCH_RECOGNIZE if you have a pattern that is 4-days wide but there is not enough context on how you would match the intervening days to be able to give that answer.
    – MT0
    Commented Oct 5, 2021 at 10:41
  • 1
    @Richa Your code works db<>fiddle.
    – MT0
    Commented Oct 11, 2021 at 7:34
  • 1
    Wow you are genius @MT0. You saved my day. Thank you again for quick help. I highly appreciate. You make it sound so simple. I was placing case statements inside define block and pattern. My bad. Thank you so much.
    – Richa
    Commented Oct 11, 2021 at 7:51
6

I've upvoted @MT0's answer and I would use match_recognize for this sort of thing myself since this is squarely the sort of problem it is designed to deal with. However, match_recognize is a pretty sophisticated construct and the patterns you're looking for are pretty simple. So as expressed, you could solve your problem with a simpler query that just uses a few lag analytic functions. As the patterns you're looking for get more sophisticated, you'll find that it'll be easier to express them using match_recognize and harder to handle them just with lag but the current problem is relatively easy to express this way.

Note that I'm making the same change to the "Piercing Line" formula that @MT0 did

with data as (
select src.stock,
       src.close_date,
       src.open o,
       src.close c,
       src.high h,
       src.low l,
       lag(src.open) over (partition by src.stock order by src.close_date) o1,
       lag(src.close) over (partition by src.stock order by src.close_date) c1,
       lag(src.high) over (partition by src.stock order by src.close_date) h1,
       lag(src.low) over (partition by src.stock order by src.close_date) l1
  from raw_source src
)
select d.*,
       case when o1 > c1 and c > o and c > h1 and o < l1
            then 'Bullish Engulfing'
            when (O1 < C1) and (O > C) and (O < C1) and (C > O1) and (H < H1) and (L > L1)
            then 'Bearish Harami'
            when (O < C) and (O1 > C1) and (C > (C1 + O1)/2) and (O < C1) and (C < O1)
            then 'Piercing Line'
         end pattern
  from data d
     

which produces the same results in the pattern column in this dbfiddle. Since we can use the same syntax you're using to express the formulas, though, it may be easier to follow the logic in this query than to understand the match_recognize syntax.

5
  • Thank you so much @JustinCave. I highly appreciate your time and help. Also thank you for the correction in OP. May I know if there's a way to figure out how many days did the stock was in downmove or upmove. For example as per the screenshot in OP, if today's date is 05/01/2021 (DD/MM/YYYY), is it possible to visualize or get the output saying trend was down for past 4 days?
    – Richa
    Commented Oct 5, 2021 at 7:14
  • @Richa - How do you define "down" or "up"? Current close lower than prior day close? Or something else? Commented Oct 5, 2021 at 7:18
  • Hi @JustinCave. That's very good question: By using below query I get "Consecutive Days Count" which shows if the trend is in downmove based on the close price. WITH StockRow AS (SELECT stock, close, close_date, ROW_NUMBER() OVER(PARTITION BY stock ORDER BY close_date) rn FROM raw_source), RunGroup AS (SELECT Base.stock, Base.close_date, MAX(Restart.rn) OVER(PARTITION BY Base.stock ORDER BY Base.close_date) groupingId FROM StockRow Base LEFT JOIN StockRow Restart ON Restart.stock = Base.stock AND Restart.rn = Base.rn - 1 AND Restart.close > Base.close)..
    – Richa
    Commented Oct 5, 2021 at 9:07
  • ..SELECT stock, COUNT() AS consecutiveCount, MIN(close_date) AS startDate, MAX(close_date) AS endDate FROM RunGroup GROUP BY stock, groupingId HAVING COUNT() >= 3 ORDER BY stock, startDate
    – Richa
    Commented Oct 5, 2021 at 9:07
  • Since there was character limit. Posted the query in two different comments. This is what I am trying to achieve i.e., first, to identify trend. Second, to identify candlestick pattern in combination with trend. As per the screenshot in OP
    – Richa
    Commented Oct 5, 2021 at 9:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.