Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • #16
    I apologize that my explanation was not clear enough. The idea behind this data is that I have obtained lots of buy signals using a certain formula to buy a certain stock. However, I want a delay in when the actual (3rd column) buy signal is generated; this needs to occur when there are 3 consecutive buy signals. The generated new BUY signal is the starting points of the SELL signal. I want to buy the stock at time T and sell it after a fixed time of 4 periods. So I buy the stock and 4 periods later I sell it.

    Basically;
    1) Three consecutive 'buy' signals (1, 2, 3) (2nd column) produce a 'BUY' signal (3) (3rd column).
    2) If a 'BUY' signal is produced in the 3rd column, a SELL signal is generated after 4 periods.
    3) All signals between BUY and SELL in the 3rd column are ignored.
    4) If it happens that a BUY and SELL signal in the 3rd column are generated in the same place, the SELL signals 'wins' (since I want to buy the stock, hold it, and sell it 4 periods later).
    5) There can never be two BUY signals or two SELL signals after each other in the third column. After a BUY 4 periods later is a SELL. But after a SELL there can directly be a BUY, or after 1 period, or 2 periods, or more etc. , that depends on whether there are 3 consecutive buy signals in the 2nd column.

    1. BUY
    2. BUY
    3. BUY BUY
    4.
    5.
    6.
    7. SELL
    8.
    9.
    With point (3) above I mean the following. In the example below no BUY signal is generated at, for example at period 5, because although there are indeed three consecutive buy signals (3, 4, 5), there was already a buy signal triggered earlier and a sell signal 4 period later, and hence all signals in between are ignored. The same applies to why a buy signal in the third column at 6 does not work; there are indeed three consecutive buy signals in column 2 (4, 5, 6), but there was a buy signal earlier and I haven't closed that trade yet (I hold it and sell it after 4 periods). The second BUY signal (8) is correct and should be generated because there were 3 consecutive buy signals in the 2nd column (marked in red), and there is no issue with conflicting signals since the last signal was a SELL and because after a stock is bought and sold, the next trade can take place. So, after each BUY and SELL in the 3rd column, it starts again with the question whether there are 3 consecutive buy signals.

    1. BUY
    2. BUY
    3. BUY BUY
    4. BUY
    5. BUY
    6. BUY
    7. BUY SELL
    8. BUY BUY
    9.
    10.
    11.
    12. SELL
    This follows the same reasoning as the previous example.

    1. BUY
    2. BUY
    3. BUY BUY
    4. BUY
    5.
    6. BUY
    7. BUY SELL
    8. BUY BUY
    9.
    10.
    11.
    12. SELL
    In the example below, you would also expect a BUY signal at (7). However, the SELL signal has priority since the stock needs to be sold first (I only allow 1 trade performed at the same time, no multiple ongoing/open positions).

    1. BUY
    2. BUY
    3. BUY BUY
    4.
    5. BUY
    6. BUY
    7. BUY SELL
    8.
    I hope that the rules are clear! You're helping me a lot with this, I really appreciate your help

    Comment


    • #17
      Thanks for the further detail. See if this does it.

      Code:
      * Example generated by -dataex-. For more info, type help dataex
      clear
      input str3 buy_signal
      ""  
      ""  
      ""  
      ""  
      ""  
      ""  
      ""  
      "BUY"  
      "BUY"  
      ""  
      ""  
      ""  
      "BUY"
      "BUY"
      "BUY"
      "BUY"
      "BUY"
      "BUY"
      "BUY"
      "BUY"
      "BUY"
      "BUY"
      "BUY"  
      "BUY"  
      "BUY"  
      ""  
      "BUY"  
      "BUY"  
      ""  
      "BUY"  
      "BUY"  
      "BUY"  
      ""  
      ""  
      ""  
      ""  
      "BUY"  
      "BUY"  
      "BUY"  
      ""  
      "BUY"  
      "BUY"
      "BUY"
      "BUY"
      ""  
      "BUY"  
      "BUY"  
      "BUY"  
      ""  
      "BUY"  
      ""  
      "BUY"  
      "BUY"  
      ""  
      ""  
      ""  
      "BUY"  
      ""  
      ""  
      ""  
      "BUY"
      "BUY"
      "BUY"
      ""  
      ""  
      ""  
      ""  
      "BUY"  
      "BUY"  
      end
      
      gen order=_n
      preserve
      tempfile wanted
      gen tag=buy_signal=="BUY" & buy_signal[_n-1]==""
      replace tag= tag[_n-1]+1 if !tag & buy_signal=="BUY"
      gen group= sum(tag==1) if tag
      bys group: gen length=_N-3 if !missing(group)
      gen new_signal= buy_signal
      sort order
      replace new_signal="TEMP" if missing(new_signal) & !mod(length[_n-1], 5) & length>0 & buy_signal[_n+1]=="BUY" & buy_signal[_n+2]=="BUY" & buy_signal[_n+3]=="BUY"
      replace new_signal="" if new_signal[_n-1]=="TEMP"
      replace new_signal="BUY" if new_signal=="TEMP"
      replace tag=new_signal=="BUY" & new_signal[_n-1]==""
      replace tag= tag[_n-1]+1 if !tag & buy_signal=="BUY"
      replace group= sum(tag==1) if tag
      bys group: drop if inlist(tag, 1, 2)
      bys group (order): replace tag=_n-1
      gen wanted="BUY" if (!tag|!mod(tag, 5)) &!missing(group)
      save `wanted'
      restore
      merge 1:1 * using `wanted', nogen
      sort order
      replace wanted= "SELL" if wanted[_n-4]=="BUY"
      drop length new_signal
      Res.:

      Code:
      . l, sep(0)
      
           +-----------------------------------------+
           | buy_si~l   order   tag   group   wanted |
           |-----------------------------------------|
        1. |                1     0       .          |
        2. |                2     1       .          |
        3. |                3     2       .          |
        4. |                4     3       .          |
        5. |                5     4       .          |
        6. |                6     5       .          |
        7. |                7     6       .          |
        8. |      BUY       8     .       .          |
        9. |      BUY       9     .       .          |
       10. |               10     7       .          |
       11. |               11     8       .          |
       12. |               12     9       .          |
       13. |      BUY      13     .       .          |
       14. |      BUY      14     .       .          |
       15. |      BUY      15     0       2      BUY |
       16. |      BUY      16     1       2          |
       17. |      BUY      17     2       2          |
       18. |      BUY      18     3       2          |
       19. |      BUY      19     4       2     SELL |
       20. |      BUY      20     5       2      BUY |
       21. |      BUY      21     6       2          |
       22. |      BUY      22     7       2          |
       23. |      BUY      23     8       2          |
       24. |      BUY      24     9       2     SELL |
       25. |      BUY      25    10       2      BUY |
       26. |               26    10       .          |
       27. |      BUY      27     .       .          |
       28. |      BUY      28     .       .          |
       29. |               29    11       .     SELL |
       30. |      BUY      30     .       .          |
       31. |      BUY      31     .       .          |
       32. |      BUY      32     0       4      BUY |
       33. |               33    12       .          |
       34. |               34    13       .          |
       35. |               35    14       .          |
       36. |               36    15       .     SELL |
       37. |      BUY      37     .       .          |
       38. |      BUY      38     .       .          |
       39. |      BUY      39     0       5      BUY |
       40. |               40    16       .          |
       41. |      BUY      41     .       .          |
       42. |      BUY      42     .       .          |
       43. |      BUY      43     .       .     SELL |
       44. |      BUY      44     0       7      BUY |
       45. |               45    17       .          |
       46. |      BUY      46     .       .          |
       47. |      BUY      47     .       .          |
       48. |      BUY      48     0       8     SELL |
       49. |               49    18       .          |
       50. |      BUY      50     .       .          |
       51. |               51    19       .          |
       52. |      BUY      52     .       .          |
       53. |      BUY      53     .       .          |
       54. |               54    20       .          |
       55. |               55    21       .          |
       56. |               56    22       .          |
       57. |      BUY      57     .       .          |
       58. |               58    23       .          |
       59. |               59    24       .          |
       60. |               60    25       .          |
       61. |      BUY      61     .       .          |
       62. |      BUY      62     .       .          |
       63. |      BUY      63     0      12      BUY |
       64. |               64    26       .          |
       65. |               65    27       .          |
       66. |               66    28       .          |
       67. |               67    29       .     SELL |
       68. |      BUY      68     .       .          |
       69. |      BUY      69     .       .          |
           +-----------------------------------------+

      Comment


      • #18
        Yes, it works! Awesome. Thank you!

        Comment


        • #19
          Originally posted by Andrew Musau View Post
          Thanks for the further detail. See if this does it.

          Code:
          * Example generated by -dataex-. For more info, type help dataex
          clear
          input str3 buy_signal
          ""
          ""
          ""
          ""
          ""
          ""
          ""
          "BUY"
          "BUY"
          ""
          ""
          ""
          "BUY"
          "BUY"
          "BUY"
          "BUY"
          "BUY"
          "BUY"
          "BUY"
          "BUY"
          "BUY"
          "BUY"
          "BUY"
          "BUY"
          "BUY"
          ""
          "BUY"
          "BUY"
          ""
          "BUY"
          "BUY"
          "BUY"
          ""
          ""
          ""
          ""
          "BUY"
          "BUY"
          "BUY"
          ""
          "BUY"
          "BUY"
          "BUY"
          "BUY"
          ""
          "BUY"
          "BUY"
          "BUY"
          ""
          "BUY"
          ""
          "BUY"
          "BUY"
          ""
          ""
          ""
          "BUY"
          ""
          ""
          ""
          "BUY"
          "BUY"
          "BUY"
          ""
          ""
          ""
          ""
          "BUY"
          "BUY"
          end
          
          gen order=_n
          preserve
          tempfile wanted
          gen tag=buy_signal=="BUY" & buy_signal[_n-1]==""
          replace tag= tag[_n-1]+1 if !tag & buy_signal=="BUY"
          gen group= sum(tag==1) if tag
          bys group: gen length=_N-3 if !missing(group)
          gen new_signal= buy_signal
          sort order
          replace new_signal="TEMP" if missing(new_signal) & !mod(length[_n-1], 5) & length>0 & buy_signal[_n+1]=="BUY" & buy_signal[_n+2]=="BUY" & buy_signal[_n+3]=="BUY"
          replace new_signal="" if new_signal[_n-1]=="TEMP"
          replace new_signal="BUY" if new_signal=="TEMP"
          replace tag=new_signal=="BUY" & new_signal[_n-1]==""
          replace tag= tag[_n-1]+1 if !tag & buy_signal=="BUY"
          replace group= sum(tag==1) if tag
          bys group: drop if inlist(tag, 1, 2)
          bys group (order): replace tag=_n-1
          gen wanted="BUY" if (!tag|!mod(tag, 5)) &!missing(group)
          save `wanted'
          restore
          merge 1:1 * using `wanted', nogen
          sort order
          replace wanted= "SELL" if wanted[_n-4]=="BUY"
          drop length new_signal
          Res.:

          Code:
          . l, sep(0)
          
          +-----------------------------------------+
          | buy_si~l order tag group wanted |
          |-----------------------------------------|
          1. | 1 0 . |
          2. | 2 1 . |
          3. | 3 2 . |
          4. | 4 3 . |
          5. | 5 4 . |
          6. | 6 5 . |
          7. | 7 6 . |
          8. | BUY 8 . . |
          9. | BUY 9 . . |
          10. | 10 7 . |
          11. | 11 8 . |
          12. | 12 9 . |
          13. | BUY 13 . . |
          14. | BUY 14 . . |
          15. | BUY 15 0 2 BUY |
          16. | BUY 16 1 2 |
          17. | BUY 17 2 2 |
          18. | BUY 18 3 2 |
          19. | BUY 19 4 2 SELL |
          20. | BUY 20 5 2 BUY |
          21. | BUY 21 6 2 |
          22. | BUY 22 7 2 |
          23. | BUY 23 8 2 |
          24. | BUY 24 9 2 SELL |
          25. | BUY 25 10 2 BUY |
          26. | 26 10 . |
          27. | BUY 27 . . |
          28. | BUY 28 . . |
          29. | 29 11 . SELL |
          30. | BUY 30 . . |
          31. | BUY 31 . . |
          32. | BUY 32 0 4 BUY |
          33. | 33 12 . |
          34. | 34 13 . |
          35. | 35 14 . |
          36. | 36 15 . SELL |
          37. | BUY 37 . . |
          38. | BUY 38 . . |
          39. | BUY 39 0 5 BUY |
          40. | 40 16 . |
          41. | BUY 41 . . |
          42. | BUY 42 . . |
          43. | BUY 43 . . SELL |
          44. | BUY 44 0 7 BUY |
          45. | 45 17 . |
          46. | BUY 46 . . |
          47. | BUY 47 . . |
          48. | BUY 48 0 8 SELL |
          49. | 49 18 . |
          50. | BUY 50 . . |
          51. | 51 19 . |
          52. | BUY 52 . . |
          53. | BUY 53 . . |
          54. | 54 20 . |
          55. | 55 21 . |
          56. | 56 22 . |
          57. | BUY 57 . . |
          58. | 58 23 . |
          59. | 59 24 . |
          60. | 60 25 . |
          61. | BUY 61 . . |
          62. | BUY 62 . . |
          63. | BUY 63 0 12 BUY |
          64. | 64 26 . |
          65. | 65 27 . |
          66. | 66 28 . |
          67. | 67 29 . SELL |
          68. | BUY 68 . . |
          69. | BUY 69 . . |
          +-----------------------------------------+
          Hello. Instead of buying the stock and selling it 4 periods later I also want to generate the buy and sell signals if the period is nog 4 but 2, 6, and 8 periods. So create a variable where I buy the stock and sell it 2 periods later. Create another variable with where the sell signal is 4 periods later, another variable where the sell signal is 8 period later. I tried to change the code of 4 periods later (as you wrote above) to 6 periods later to the code below, but it doesn't work for all the signals. How should I change the code you sent above so that not after 4 periods a sell signal is created, but after 2, 6, 8 (new separate variables)?

          Code:
          gen order=_n
          preserve
          tempfile wanted
          gen tag=buy_signal=="BUY" & buy_signal[_n-1]==""
          replace tag= tag[_n-1]+1 if !tag & buy_signal=="BUY"
          gen group= sum(tag==1) if tag
          bys group: gen length=_N-3 if !missing(group)
          gen new_signal = buy_signal
          sort order
          replace new_signal="TEMP" if missing(new_signal) & !mod(length[_n-1], 5) & length>0 & buy_signal[_n+1]=="BUY" & buy_signal[_n+2]=="BUY" & buy_signal[_n+3]=="BUY"
          replace new_signal="" if new_signal[_n-1]=="TEMP"
          replace new_signal="BUY" if new_signal=="TEMP"
          replace tag=new_signal=="BUY" & new_signal[_n-1]==""
          replace tag= tag[_n-1]+1 if !tag & buy_signal=="BUY"
          replace group= sum(tag==1) if tag
          bys group: drop if inlist(tag, 1, 2)
          bys group (order): replace tag=_n-1
          gen wanted="BUY" if (!tag|!mod(tag, 5)) &!missing(group)
          save `wanted'
          restore
          merge 1:1 * using `wanted', nogen
          sort order
          replace wanted= "SELL" if wanted[_n-6]=="BUY"
          drop length new_signal
          [/QUOTE]














          Comment


          • #20
            Look at the code and replace the numbers in the highlighted lines.

            gen order=_n
            preserve
            tempfile wanted
            gen tag=buy_signal=="BUY" & buy_signal[_n-1]==""
            replace tag= tag[_n-1]+1 if !tag & buy_signal=="BUY"
            gen group= sum(tag==1) if tag
            bys group: gen length=_N-3 if !missing(group)
            gen new_signal= buy_signal
            sort order
            replace new_signal="TEMP" if missing(new_signal) & !mod(length[_n-1], 5) & length>0 & buy_signal[_n+1]=="BUY" & buy_signal[_n+2]=="BUY" & buy_signal[_n+3]=="BUY"
            replace new_signal="" if new_signal[_n-1]=="TEMP"
            replace new_signal="BUY" if new_signal=="TEMP"
            replace tag=new_signal=="BUY" & new_signal[_n-1]==""
            replace tag= tag[_n-1]+1 if !tag & buy_signal=="BUY"
            replace group= sum(tag==1) if tag
            bys group: drop if inlist(tag, 1, 2)
            bys group (order): replace tag=_n-1
            gen wanted="BUY" if (!tag|!mod(tag, 5)) &!missing(group)
            save `wanted'
            restore
            merge 1:1 * using `wanted', nogen
            sort order
            replace wanted= "SELL" if wanted[_n-4]=="BUY"
            drop length new_signal
            Post back with a data example if you fail.

            Comment


            • #21
              Originally posted by Andrew Musau View Post
              Look at the code and replace the numbers in the highlighted lines.



              Post back with a data example if you fail.
              Thank you very very much for your response. For the case where I want to sell the stock 2 periods later instead of 4, I changed the red lines to the following:



              gen order=_n
              preserve
              tempfile wanted
              gen tag=buy_signal=="BUY" & buy_signal[_n-1]==""
              replace tag= tag[_n-1]+1 if !tag & buy_signal=="BUY"
              gen group= sum(tag==1) if tag
              bys group: gen length=_N-2 if !missing(group)
              gen new_signal = buy_signal
              sort order
              replace new_signal="TEMP" if missing(new_signal) & !mod(length[_n-1], 3) & length>0 & buy_signal[_n+1]=="BUY" & buy_signal[_n+2]=="BUY" & buy_signal[_n+3]=="BUY"
              replace new_signal="" if new_signal[_n-1]=="TEMP"
              replace new_signal="BUY" if new_signal=="TEMP"
              replace tag=new_signal=="BUY" & new_signal[_n-1]==""
              replace tag= tag[_n-1]+1 if !tag & buy_signal=="BUY"
              replace group= sum(tag==1) if tag
              bys group: drop if inlist(tag, 1, 2)
              bys group (order): replace tag=_n-1
              gen wanted="BUY" if (!tag|!mod(tag, 3)) &!missing(group)
              save `wanted'
              restore
              merge 1:1 * using `wanted', nogen
              sort order
              replace wanted= "SELL" if wanted[_n-2]=="BUY"
              drop length new_signal
              And I tried this as well:

              gen order=_n
              preserve
              tempfile wanted
              gen tag=buy_signal=="BUY" & buy_signal[_n-1]==""
              replace tag= tag[_n-1]+1 if !tag & buy_signal=="BUY"
              gen group= sum(tag==1) if tag
              bys group: gen length=_N-2 if !missing(group)
              gen new_signal = buy_signal
              sort order
              replace new_signal="TEMP" if missing(new_signal) & !mod(length[_n-1], 3) & length>0 & buy_signal[_n+1]=="BUY"
              replace new_signal="" if new_signal[_n-1]=="TEMP"
              replace new_signal="BUY" if new_signal=="TEMP"
              replace tag=new_signal=="BUY" & new_signal[_n-1]==""
              replace tag= tag[_n-1]+1 if !tag & buy_signal=="BUY"
              replace group= sum(tag==1) if tag
              bys group: drop if inlist(tag, 1, 2)
              bys group (order): replace tag=_n-1
              gen wanted="BUY" if (!tag|!mod(tag, 3)) &!missing(group)
              save `wanted'
              restore
              merge 1:1 * using `wanted', nogen
              sort order
              replace wanted= "SELL" if wanted[_n-2]=="BUY"
              drop length new_signal
              Code:
              * Example generated by -dataex-. To install: ssc install dataex
              clear
              input str3 buy_signal float(order tag group) str4 wanted
              ""    100 48  . ""    
              "BUY" 101  .  . ""    
              ""    102 49  . ""    
              "BUY" 103  .  . ""    
              "BUY" 104  .  . ""    
              ""    105 50  . ""    
              ""    106 51  . ""    
              ""    107 52  . ""    
              "BUY" 108  .  . ""    
              ""    109 53  . ""    
              "BUY" 110  .  . ""    
              ""    111 54  . ""    
              ""    112 55  . ""    
              "BUY" 113  .  . ""    
              ""    114 56  . ""    
              "BUY" 115  .  . ""    
              "BUY" 116  .  . ""    
              "BUY" 117  0 25 "BUY"
              "BUY" 118  1 25 ""    
              ""    119 57  . "SELL"
              "BUY" 120  .  . ""    
              "BUY" 121  .  . ""    
              "BUY" 122  0 26 "BUY"
              "BUY" 123  1 26 ""    
              "BUY" 124  2 26 "SELL"
              "BUY" 125  3 26 "BUY"
              "BUY" 126  4 26 ""    
              "BUY" 127  5 26 "SELL"
              "BUY" 128  6 26 "BUY"
              "BUY" 129  7 26 ""    
              "BUY" 130  8 26 "SELL"
              "BUY" 131  9 26 "BUY"
              "BUY" 132 10 26 ""    
              "BUY" 133 11 26 "SELL"
              ""    134 58  . ""    
              "BUY" 135  .  . ""    
              "BUY" 136  .  . ""    
              "BUY" 137  .  . ""    
              ""    138 59  . ""    
              ""    139 60  . ""    
              ""    140 61  . ""    
              "BUY" 141  .  . ""    
              "BUY" 142  .  . ""    
              "BUY" 143  0 29 "BUY"
              "BUY" 144  1 29 ""    
              ""    145 62  . "SELL"
              ""    146 63  . ""    
              ""    147 64  . ""    
              ""    148 65  . ""    
              ""    149 66  . ""    
              ""    150 67  . ""    
              ""    151 68  . ""    
              ""    152 69  . ""    
              "BUY" 153  .  . ""    
              ""    154 70  . ""    
              "BUY" 155  .  . ""    
              ""    156 71  . ""    
              "BUY" 157  .  . ""    
              ""    158 72  . ""    
              "BUY" 159  .  . ""    
              ""    160 73  . ""    
              ""    161 74  . ""    
              ""    162 75  . ""    
              ""    163 76  . ""    
              "BUY" 164  .  . ""    
              "BUY" 165  .  . ""    
              "BUY" 166  0 34 "BUY"
              "BUY" 167  1 34 ""    
              "BUY" 168  2 34 "SELL"
              "BUY" 169  3 34 "BUY"
              "BUY" 170  4 34 ""    
              "BUY" 171  5 34 "SELL"
              "BUY" 172  6 34 "BUY"
              "BUY" 173  7 34 ""    
              "BUY" 174  8 34 "SELL"
              "BUY" 175  9 34 "BUY"
              "BUY" 176 10 34 ""    
              "BUY" 177 11 34 "SELL"
              ""    178 77  . ""    
              ""    179 78  . ""    
              ""    180 79  . ""    
              ""    181 80  . ""    
              ""    182 81  . ""    
              "BUY" 183  .  . ""    
              "BUY" 184  .  . ""    
              "BUY" 185  0 35 "BUY"
              "BUY" 186  1 35 ""    
              ""    187 82  . "SELL"
              "BUY" 188  .  . ""    
              "BUY" 189  .  . ""    
              "BUY" 190  0 36 "BUY"
              "BUY" 191  1 36 ""    
              "BUY" 192  2 36 "SELL"
              "BUY" 193  3 36 "BUY"
              "BUY" 194  4 36 ""    
              ""    195 83  . "SELL"
              "BUY" 196  .  . ""    
              "BUY" 197  .  . ""    
              "BUY" 198  0 37 "BUY"
              "BUY" 199  1 37 ""    
              end
              All good, but at str3 number 137 it doesn't generate a BUY + SELL signal. However, sometimes it does when there are 3 consecutive buy signals. Also, sometimes the buy signal is generated at the 4th buy signal instead of the 3rd (there should 3 consecutive buy signals and then at the 3rd is the buy signal). So indeed the sell signal is generated after 2 periods instead of 4 but the buy signal is wrong sometimes. Did I change the red lines to the wrong numbers?
              Last edited by Sophie Wesselink; 09 Nov 2021, 04:09.

              Comment


              • #22
                Create another variable with where the sell signal is 4 periods later, another variable where the sell signal is 8 period later. I tried to change the code of 4 periods later (as you wrote above) to 6 periods later to the code below, but it doesn't work for all the signals. How should I change the code you sent above so that not after 4 periods a sell signal is created, but after 2, 6, 8 (new separate variables)?
                I think I misunderstood your request from yesterday. If you only want to modify the sell signal and not the buy signal, then just work from the initial code. Here is an example for 4, 2, and 6 periods (wanted, wanted2 and wanted3, respectively). Note that these arbitrary lengths can coincide with existing buy signals in the original variable. Added code is highlighted.

                Code:
                * Example generated by -dataex-. For more info, type help dataex
                clear
                input str3 buy_signal
                ""  
                ""  
                ""  
                ""  
                ""  
                ""  
                ""  
                "BUY"  
                "BUY"  
                ""  
                ""  
                ""  
                "BUY"
                "BUY"
                "BUY"
                "BUY"
                "BUY"
                "BUY"
                "BUY"
                "BUY"
                "BUY"
                "BUY"
                "BUY"  
                "BUY"  
                "BUY"  
                ""  
                "BUY"  
                "BUY"  
                ""  
                "BUY"  
                "BUY"  
                "BUY"  
                ""  
                ""  
                ""  
                ""  
                "BUY"  
                "BUY"  
                "BUY"  
                ""  
                "BUY"  
                "BUY"
                "BUY"
                "BUY"
                ""  
                "BUY"  
                "BUY"  
                "BUY"  
                ""  
                "BUY"  
                ""  
                "BUY"  
                "BUY"  
                ""  
                ""  
                ""  
                "BUY"  
                ""  
                ""  
                ""  
                "BUY"
                "BUY"
                "BUY"
                ""  
                ""  
                ""  
                ""  
                "BUY"  
                "BUY"  
                end
                
                gen order=_n
                preserve
                tempfile wanted
                gen tag=buy_signal=="BUY" & buy_signal[_n-1]==""
                replace tag= tag[_n-1]+1 if !tag & buy_signal=="BUY"
                gen group= sum(tag==1) if tag
                bys group: gen length=_N-3 if !missing(group)
                gen new_signal= buy_signal
                sort order
                replace new_signal="TEMP" if missing(new_signal) & !mod(length[_n-1], 5) & length>0 & buy_signal[_n+1]=="BUY" & buy_signal[_n+2]=="BUY" & buy_signal[_n+3]=="BUY"
                replace new_signal="" if new_signal[_n-1]=="TEMP"
                replace new_signal="BUY" if new_signal=="TEMP"
                replace tag=new_signal=="BUY" & new_signal[_n-1]==""
                replace tag= tag[_n-1]+1 if !tag & buy_signal=="BUY"
                replace group= sum(tag==1) if tag
                bys group: drop if inlist(tag, 1, 2)
                bys group (order): replace tag=_n-1
                gen wanted="BUY" if (!tag|!mod(tag, 5)) &!missing(group)
                save `wanted'
                restore
                merge 1:1 * using `wanted', nogen
                sort order
                replace wanted= "SELL" if wanted[_n-4]=="BUY"
                drop length new_signal
                
                gen wanted2= cond(wanted=="BUY", "BUY", cond(wanted[_n+2]=="SELL", "SELL",  ""))
                gen wanted3= cond(wanted=="BUY", "BUY", cond(wanted[_n-2]=="SELL", "SELL",  ""))

                Res.:

                Code:
                . l, sep(0)
                
                     +-------------------------------------------------------------+
                     | buy_si~l   order   tag   group   wanted   wanted2   wanted3 |
                     |-------------------------------------------------------------|
                  1. |                1     0       .                              |
                  2. |                2     1       .                              |
                  3. |                3     2       .                              |
                  4. |                4     3       .                              |
                  5. |                5     4       .                              |
                  6. |                6     5       .                              |
                  7. |                7     6       .                              |
                  8. |      BUY       8     .       .                              |
                  9. |      BUY       9     .       .                              |
                 10. |               10     7       .                              |
                 11. |               11     8       .                              |
                 12. |               12     9       .                              |
                 13. |      BUY      13     .       .                              |
                 14. |      BUY      14     .       .                              |
                 15. |      BUY      15     0       2      BUY       BUY       BUY |
                 16. |      BUY      16     1       2                              |
                 17. |      BUY      17     2       2               SELL           |
                 18. |      BUY      18     3       2                              |
                 19. |      BUY      19     4       2     SELL                     |
                 20. |      BUY      20     5       2      BUY       BUY       BUY |
                 21. |      BUY      21     6       2                         SELL |
                 22. |      BUY      22     7       2               SELL           |
                 23. |      BUY      23     8       2                              |
                 24. |      BUY      24     9       2     SELL                     |
                 25. |      BUY      25    10       2      BUY       BUY       BUY |
                 26. |               26    10       .                         SELL |
                 27. |      BUY      27     .       .               SELL           |
                 28. |      BUY      28     .       .                              |
                 29. |               29    11       .     SELL                     |
                 30. |      BUY      30     .       .                              |
                 31. |      BUY      31     .       .                         SELL |
                 32. |      BUY      32     0       4      BUY       BUY       BUY |
                 33. |               33    12       .                              |
                 34. |               34    13       .               SELL           |
                 35. |               35    14       .                              |
                 36. |               36    15       .     SELL                     |
                 37. |      BUY      37     .       .                              |
                 38. |      BUY      38     .       .                         SELL |
                 39. |      BUY      39     0       5      BUY       BUY       BUY |
                 40. |               40    16       .                              |
                 41. |      BUY      41     .       .               SELL           |
                 42. |      BUY      42     .       .                              |
                 43. |      BUY      43     .       .     SELL                     |
                 44. |      BUY      44     0       7      BUY       BUY       BUY |
                 45. |               45    17       .                         SELL |
                 46. |      BUY      46     .       .               SELL           |
                 47. |      BUY      47     .       .                              |
                 48. |      BUY      48     0       8     SELL                     |
                 49. |               49    18       .                              |
                 50. |      BUY      50     .       .                         SELL |
                 51. |               51    19       .                              |
                 52. |      BUY      52     .       .                              |
                 53. |      BUY      53     .       .                              |
                 54. |               54    20       .                              |
                 55. |               55    21       .                              |
                 56. |               56    22       .                              |
                 57. |      BUY      57     .       .                              |
                 58. |               58    23       .                              |
                 59. |               59    24       .                              |
                 60. |               60    25       .                              |
                 61. |      BUY      61     .       .                              |
                 62. |      BUY      62     .       .                              |
                 63. |      BUY      63     0      12      BUY       BUY       BUY |
                 64. |               64    26       .                              |
                 65. |               65    27       .               SELL           |
                 66. |               66    28       .                              |
                 67. |               67    29       .     SELL                     |
                 68. |      BUY      68     .       .                              |
                 69. |      BUY      69     .       .                         SELL |
                     +-------------------------------------------------------------+
                
                .

                Comment


                • #23
                  Thank you for helping me! Unfortunately, the code does not seem to work for all signals. For example, at obs 18 of wanted2 there should be a buy signal because there were 3 consecutive buy signals. Also, the buy and sell signals from wanted3 are incorrect because there should not be any signals in between a buy and 6 periods later a sell (all signals in between are ignored). I think the code for 4 periods should not be the starting point for the code for 2 and 6 maybe? Because then the buy signals from the code for 4 are still there while 6 is a longer period, so the buy signal in between based on the code from 4 (if that's the case) should be ignored.

                  Comment


                  • #24
                    It is a while since I wrote the 4 period code and it appears that your description focuses on the sell signal. I cannot remember the rules for the buy signal and would not want to guess in terms of 2 periods, 6 periods, 8, etc. So rather than going round in circles, manually create the variables using the dataex example below, post and we can take it from there.

                    Code:
                    * Example generated by -dataex-. For more info, type help dataex
                    clear
                    input str3 buy_signal
                    ""   
                    ""   
                    ""   
                    ""   
                    ""   
                    ""   
                    ""   
                    "BUY"
                    "BUY"
                    ""   
                    ""   
                    ""   
                    "BUY"
                    "BUY"
                    "BUY"
                    "BUY"
                    "BUY"
                    "BUY"
                    "BUY"
                    "BUY"
                    "BUY"
                    "BUY"
                    "BUY"
                    "BUY"
                    "BUY"
                    ""   
                    "BUY"
                    "BUY"
                    ""   
                    "BUY"
                    "BUY"
                    "BUY"
                    ""   
                    ""   
                    ""   
                    ""   
                    "BUY"
                    "BUY"
                    "BUY"
                    ""   
                    "BUY"
                    "BUY"
                    "BUY"
                    "BUY"
                    ""   
                    "BUY"
                    "BUY"
                    "BUY"
                    ""   
                    "BUY"
                    ""   
                    "BUY"
                    "BUY"
                    ""   
                    ""   
                    ""   
                    "BUY"
                    ""   
                    ""   
                    ""   
                    "BUY"
                    "BUY"
                    "BUY"
                    ""   
                    ""   
                    ""   
                    ""   
                    "BUY"
                    "BUY"
                    end

                    Comment


                    • #25

                      Code:
                      * Example generated by -dataex-. For more info, type help dataex clear input str3 buy_signal wanted2 wanted4 wanted6
                      "" "" "" ""
                      "" "" "" ""
                      "" "" "" ""
                      "" "" "" ""
                      "" "" "" ""
                      "" "" "" ""
                      "" "" "" ""  
                      "BUY" "" "" ""
                      "BUY" "" "" ""
                      "" "" "" ""  
                      "" "" "" ""
                      "" "" "" ""
                      "BUY" "" "" ""
                      "BUY" "" "" ""
                      "BUY" "BUY" "BUY" "BUY"
                      "BUY" "" "" ""
                      "BUY" "SELL" "" ""
                      "BUY" "BUY" "" ""
                      "BUY" "" "SELL" ""
                      "BUY" "SELL" "BUY" ""
                      "BUY" "BUY" "" "SELL"
                      "BUY" "" "" "BUY"
                      "BUY" "SELL" "" ""
                      "BUY" "BUY" "SELL" ""
                      "BUY" "" "BUY" ""
                      "" "SELL" "" ""
                      "BUY" "" "" ""
                      "BUY" "" "" "SELL"
                      "" "" "SELL" ""
                      "BUY" "" "" ""
                      "BUY" "" "" ""
                      "BUY" "BUY" "BUY" "BUY"
                      "" "" "" ""  
                      "" "SELL" "" ""
                      "" "" "" ""
                      "" "" "SELL" ""
                      "BUY" "" "" ""
                      "BUY" "" "" "SELL"
                      "BUY" "BUY" "BUY" "BUY"
                      "" "" "" ""
                      "BUY" "SELL" "" ""
                      "BUY" "" "" ""  
                      "BUY" "BUY" "SELL" ""
                      "BUY" "" "BUY" ""
                      "" "SELL" "" "SELL"
                      "BUY" "" "" ""
                      "BUY" "" "" ""
                      "BUY" "BUY" "SELL" "BUY"
                      "" "" "" ""
                      "BUY" "SELL" "" ""
                      "" "" "" ""  
                      "BUY" "" "" ""
                      "BUY" "" "" ""
                      "" "" "" "SELL"
                      "" "" "" ""  
                      "" "" "" "" 
                      "BUY" "" "" ""
                      "" "" "" ""
                      "" "" "" ""
                      "" "" "" ""
                      "BUY" "" "" ""
                      "BUY" "" "" ""
                      "BUY" "BUY" "BUY" "BUY"
                      "" "" "" ""
                      "" "SELL" "" ""
                      "" "" "" ""  
                      "" "" "SELL ""
                      "BUY" "" "" ""
                      "BUY" "" "" "SELL" 
                          end
                      Last edited by Sophie Wesselink; 09 Nov 2021, 16:38.

                      Comment


                      • #26
                        Here is code for 2 periods alongside that of 4 periods. The complication arises once your defined window exceeds the 3 consecutive periods as that is your rule for when a buy signal is triggered. Here, you have to account for overlaps. I will come up with something general for T>3 periods. The lines in blue account for the overlap issue, those in red are common across windows.

                        Code:
                        * Example generated by -dataex-. For more info, type help dataex 
                        clear 
                        input str3(buy_signal wanted2 wanted4 wanted6)
                        "" "" "" ""
                        "" "" "" ""
                        "" "" "" ""
                        "" "" "" ""
                        "" "" "" ""
                        "" "" "" ""
                        "" "" "" ""  
                        "BUY" "" "" ""
                        "BUY" "" "" ""
                        "" "" "" ""  
                        "" "" "" ""
                        "" "" "" ""
                        "BUY" "" "" ""
                        "BUY" "" "" ""
                        "BUY" "BUY" "BUY" "BUY"
                        "BUY" "" "" ""
                        "BUY" "SELL" "" ""
                        "BUY" "BUY" "" ""
                        "BUY" "" "SELL" ""
                        "BUY" "SELL" "BUY" ""
                        "BUY" "BUY" "" "SELL"
                        "BUY" "" "" "BUY"
                        "BUY" "SELL" "" ""
                        "BUY" "BUY" "SELL" ""
                        "BUY" "" "BUY" ""
                        "" "SELL" "" ""
                        "BUY" "" "" ""
                        "BUY" "" "" "SELL"
                        "" "" "SELL" ""
                        "BUY" "" "" ""
                        "BUY" "" "" ""
                        "BUY" "BUY" "BUY" "BUY"
                        "" "" "" ""  
                        "" "SELL" "" ""
                        "" "" "" ""
                        "" "" "SELL" ""
                        "BUY" "" "" ""
                        "BUY" "" "" "SELL"
                        "BUY" "BUY" "BUY" "BUY"
                        "" "" "" ""
                        "BUY" "SELL" "" ""
                        "BUY" "" "" ""  
                        "BUY" "BUY" "SELL" ""
                        "BUY" "" "BUY" ""
                        "" "SELL" "" "SELL"
                        "BUY" "" "" ""
                        "BUY" "" "" ""
                        "BUY" "BUY" "SELL" "BUY"
                        "" "" "" ""
                        "BUY" "SELL" "" ""
                        "" "" "" ""  
                        "BUY" "" "" ""
                        "BUY" "" "" ""
                        "" "" "" "SELL"
                        "" "" "" ""  
                        "" "" "" "" 
                        "BUY" "" "" ""
                        "" "" "" ""
                        "" "" "" ""
                        "" "" "" ""
                        "BUY" "" "" ""
                        "BUY" "" "" ""
                        "BUY" "BUY" "BUY" "BUY"
                        "" "" "" ""
                        "" "SELL" "" ""
                        "" "" "" ""  
                        "" "" "SELL ""
                        "BUY" "" "" ""
                        "BUY" "" "" "SELL" 
                        end
                        
                        
                        *4 PERIODS
                        gen order=_n
                        preserve
                        tempfile wanted
                        gen tag=buy_signal=="BUY" & buy_signal[_n-1]==""
                        replace tag= tag[_n-1]+1 if !tag & buy_signal=="BUY"
                        gen group= sum(tag==1) if tag
                        bys group: gen length=_N-3 if !missing(group)
                        gen new_signal= buy_signal
                        sort order
                        replace new_signal="TEMP" if missing(new_signal) & !mod(length[_n-1], 5) & length>0 & buy_signal[_n+1]=="BUY" & buy_signal[_n+2]=="BUY" & buy_signal[_n+3]=="BUY"
                        replace new_signal="" if new_signal[_n-1]=="TEMP"
                        replace new_signal="BUY" if new_signal=="TEMP"
                        replace tag=new_signal=="BUY" & new_signal[_n-1]==""
                        replace tag= tag[_n-1]+1 if !tag & buy_signal=="BUY"
                        replace group= sum(tag==1) if tag
                        bys group: drop if inlist(tag, 1, 2)
                        bys group (order): replace tag=_n-1
                        gen wanted44="BUY" if (!tag|!mod(tag, 5)) &!missing(group)
                        save `wanted'
                        restore
                        merge 1:1 * using `wanted', nogen
                        sort order
                        replace wanted44= "SELL" if wanted44[_n-4]=="BUY"
                        drop length new_signal
                        keep buy wanted*
                        
                        *2 PERIODS
                        gen order=_n
                        preserve
                        tempfile wanted
                        gen tag=buy_signal=="BUY" & buy_signal[_n-1]==""
                        replace tag= tag[_n-1]+1 if !tag & buy_signal=="BUY"
                        gen group= sum(tag==1) if tag
                        bys group: gen length=_N-3 if !missing(group)
                        gen new_signal= buy_signal
                        sort order
                        replace new_signal="BUY" if missing(new_signal) & !mod(length[_n-1], 3) & length>0 & buy_signal[_n+1]=="BUY" 
                        replace tag= tag[_n-1]+1 if !tag & buy_signal=="BUY"
                        replace group= sum(tag==1) if tag
                        bys group: drop if inlist(tag, 1, 2)
                        bys group (order): replace tag=_n-1
                        gen wanted22="BUY" if (!tag|!mod(tag, 3)) &!missing(group)
                        save `wanted'
                        restore
                        merge 1:1 * using `wanted', nogen
                        sort order
                        replace wanted22= "SELL" if wanted22[_n-2]=="BUY"
                        drop length new_signal
                        keep buy wanted*
                        assert !missing(wanted2)==!missing(wanted22)
                        assert !missing(wanted4)==!missing(wanted44)

                        Comment

                        Working...
                        X