Counting half cycles

Hello, I have a table with historical financial data. One of the columns is 50 moving average (sma50) of the closeMid column, another column is a cyclic function (KStoch5), 

I added a column to mark the candles where KStoch5 passes from <50 to >50 or viceversa (halfcyclechange), one column with the difference between the adjacent sma50 values and another column marking when sma50 slope changes from negative to positive or viceversa (sma50slpchng). 

q)CandlesList:update sma50dif:sma50-prev sma50 by symbol from CandlesList;
q)CandlesList:update sma50slpchng:1 by symbol from CandlesList where prev sma50dif>0,sma50dif<0
q)CandlesList:update sma50slpchng:1 by symbol from CandlesList where prev sma50dif<0,sma50dif>0
q)CandlesList:update halfcyclechange:1 by symbol from CandlesList where prev KStoch5>50 , KStoch5<50
q)CandlesList:update halfcyclechange:1 by symbol from CandlesList where prev KStoch5<50 , KStoch5>50

I want to count the half cycles of KStoch5, resetting the count when sma50 slope changes from negative to positive or viceversa like in this chart (not corresponding with the data in the table).

q)CandlesList:update sma50dif:sma50-prev sma50 by symbol from CandlesList;
q)CandlesList:update sma50slpchng:1 by symbol from CandlesList where prev sma50dif>0,sma50dif<0
q)CandlesList:update sma50slpchng:1 by symbol from CandlesList where prev sma50dif<0,sma50dif>0
q)CandlesList:update halfcyclechange:1 by symbol from CandlesList where prev KStoch5>50 , KStoch5<50
q)CandlesList:update halfcyclechange:1 by symbol from CandlesList where prev KStoch5<50 , KStoch5>50

But I can’t find the way to mark the half cycles in this way… (Column HalfCycle added manually):

symbol time closeMid sma50 KStoch5 sma50dif sma50slpchng halfcyclechange HalfCycle

----------------------------------------------------------------------------------------------------------------------------------------------------------------

AUD_JPY 2014.01.05D05:00:00.000000000 93.397 93.14448 59.46023 0.01017 3

AUD_JPY 2014.01.06D05:00:00.000000000 93.2005 93.14934 33.97708 0.00486 1 4

AUD_JPY 2014.01.07D05:00:00.000000000 93.713 93.16612 47.38964 0.01678 4

AUD_JPY 2014.01.08D05:00:00.000000000 93.1395 93.17041 41.35175 0.00429 4

AUD_JPY 2014.01.09D05:00:00.000000000 93.432 93.18752 28.47566 0.01711 4

AUD_JPY 2014.01.10D05:00:00.000000000 93.703 93.19868 64.55236 0.01116 1 5

AUD_JPY 2014.01.12D05:00:00.000000000 93.4345 93.19459 70.67227 -0.00409 1 1

AUD_JPY 2014.01.13D05:00:00.000000000 93.3045 93.18332 47.75528 -0.01127 1 2

AUD_JPY 2014.01.14D05:00:00.000000000 93.0705 93.16351 27.14908 -0.01981 2

AUD_JPY 2014.01.15D05:00:00.000000000 92.3115 93.1368 11.37559 -0.02671 2

AUD_JPY 2014.01.16D05:00:00.000000000 92.0795 93.09643 14.29453 -0.04037 2

AUD_JPY 2014.01.17D05:00:00.000000000 91.6095 93.06154 14.23241 -0.03489 2

AUD_JPY 2014.01.19D05:00:00.000000000 91.356 93.03164 10.23116 -0.0299 2

AUD_JPY 2014.01.20D05:00:00.000000000 92.281 93.01935 33.00667 -0.01229 2

AUD_JPY 2014.01.21D05:00:00.000000000 92.3955 93.00462 73.07235 -0.01473 1 3

AUD_JPY 2014.01.22D05:00:00.000000000 91.766 92.97471 67.86911 -0.02991 3

AUD_JPY 2014.01.23D05:00:00.000000000 89.9865 92.92108 23.88258 -0.05363 1 4

AUD_JPY 2014.01.24D05:00:00.000000000 88.912 92.83573 6.676808 -0.08535 4

AUD_JPY 2014.01.26D05:00:00.000000000 89.355 92.76403 15.13066 -0.0717 4

AUD_JPY 2014.01.27D05:00:00.000000000 90.2225 92.70282 31.5371 -0.06121 4

AUD_JPY 2014.01.28D05:00:00.000000000 90.924 92.64743 55.7373 -0.05539 1 5

AUD_JPY 2014.01.29D05:00:00.000000000 89.4155 92.56205 52.95727 -0.08538 5

AUD_JPY 2014.01.30D05:00:00.000000000 89.7545 92.49787 42.5492 -0.06418 1 6

AUD_JPY 2014.01.31D05:00:00.000000000 89.2785 92.43552 33.73887 -0.06235 6

AUD_JPY 2014.02.02D05:00:00.000000000 89.602 92.38095 25.73611 -0.05457 6

AUD_JPY 2014.02.03D05:00:00.000000000 89.928 92.30635 47.11115 -0.0746 6

AUD_JPY 2014.02.04D05:00:00.000000000 90.1925 92.23452 67.30254 -0.07183 1 7

AUD_JPY 2014.02.05D05:00:00.000000000 91.0625 92.17591 83.27506 -0.05861 7

AUD_JPY 2014.02.06D05:00:00.000000000 91.214 92.12337 90.23065 -0.05254 7

AUD_JPY 2014.02.07D05:00:00.000000000 91.6865 92.10604 91.0994 -0.01733 7

AUD_JPY 2014.02.09D05:00:00.000000000 91.4965 92.08104 89.95854 -0.025 7

AUD_JPY 2014.02.10D05:00:00.000000000 92.056 92.07147 92.07563 -0.00957 7

AUD_JPY 2014.02.11D05:00:00.000000000 92.808 92.086 95.98224 0.01453 1 1

AUD_JPY 2014.02.12D05:00:00.000000000 91.257 92.06967 54.7474 -0.01633 1 1


Any help will be appreciated.


Cheers

update HalfCycle:sums halfcyclechange by sums sma50slpchng,symbol from CandlesList

Thanks a lot!

Hi Francisco

I think you can probably simplify your code a bit

// sample table

q)t:(symbol:`AUDUSD; sma50:93 + .14493 .14934 .166 .170 .187 .198 .195 .183 .163 .137 .096 .062; KStoch5:59.46 33.977 47.389 41.35 28.48 64.55 70.67 47.75 27.14 11.37 14.29 14.23)              

// Create a function as this seems to be a pattern

// this will work out whether the sign of a sequence changes

q)f:{differ[first x;x:signum x]}                                                                                                                                                                   

// add in the two temporary columns

q)update h:f[KStoch5 - 50], s:f[deltas sma50] by symbol from t                                                                                                                                    

symbol sma50    KStoch5 h s


AUDUSD 93.14493 59.46   0 0

AUDUSD 93.14934 33.977  1 0

AUDUSD 93.166   47.389  0 0

AUDUSD 93.17    41.35   0 0

AUDUSD 93.187   28.48   0 0

AUDUSD 93.198   64.55   1 0

AUDUSD 93.195   70.67   0 1

AUDUSD 93.183   47.75   1 0

AUDUSD 93.163   27.14   0 0

AUDUSD 93.137   11.37   0 0

AUDUSD 93.096   14.29   0 0

AUDUSD 93.062   14.23   0 0

// or just calculate the half cycles in a single step

q)update halfcycle:sums f[KStoch5 - 50] by symbol,({sums f[deltas x]};sma50) fby symbol from t                                                                                                     

symbol sma50    KStoch5 halfcycle


AUDUSD 93.14493 59.46   0        

AUDUSD 93.14934 33.977  1        

AUDUSD 93.166   47.389  1        

AUDUSD 93.17    41.35   1        

AUDUSD 93.187   28.48   1        

AUDUSD 93.198   64.55   2        

AUDUSD 93.195   70.67   0        

AUDUSD 93.183   47.75   1        

AUDUSD 93.163   27.14   1        

AUDUSD 93.137   11.37   1        

AUDUSD 93.096   14.29   1        

AUDUSD 93.062   14.23   1        

Thanks

Jonny 

AquaQ Analytics

Thanks…