cancel
Showing results for 
Search instead for 
Did you mean: 

How to find the stock_id that raises the BUY signal constantly

hzadonis
New Contributor
Hi, Dear all:
  I want to find out the stock that raises the BUY signal from this Mon to Fri. such as I have the data:

Stockid B/S Date
001 B 2017.02.20
002 B 2017.02.20
003 S 2017.02.21
001 B 2017.02.21
001 B 2017.02.22
003 B 2017.02.22
001 B 2017.02.23
002 S 2017.02.23
001 B 2017.02.24
003 B 2017.02.24
  It seems that Stockid-001 will matches the requirement.
  But how to find out within Q-sql? Many thanks for your tips!

Zheng
6 REPLIES 6

rahul_asati04
Contributor
So you want stock which have Buy row fol all days from monday to friday?
Below query gives that result:

dt:2017.02.20+til 5

select from tbl where BS=`B, ({all dt in x};Date)fby Stockid


Hi, Rahul:
  Thanks for your sample. I tried, it works.
  May I ask what the statement: "all dt in x" meaning?

Zheng

在 2017年2月24日星期五 UTC+8下午7:56:55,RAHUL ASATI写道:
So you want stock which have Buy row fol all days from monday to friday?
Below query gives that result:

dt:2017.02.20+til 5

select from tbl where BS=`B, ({all dt in x};Date)fby Stockid


i'd do it this way

q)t:([]s:1 2 3 1 1 3 1 2 1 3;sig:`$'"BBSBBBBSBB";d:raze 2#'2017.01.31+20+til 5)
q)select from t where ({all x=`B};sig) fby s
OR
q)select from t where 5=({sum x=`B};sig) fby s

sohagan
New Contributor
Below should work for you, and there'll be many more ways too

//dummy data
q)a:([]s:1 2 3 1 1 3 1 2 1 3;t:`$'"BBSBBBBSBB";d:raze 2#'2017.01.31+20+til 5)

q)select distinct s by `week$d from a where s in where 5=count each d group s,(d mod 7) in 2+til 5
d         | s
----------| -
2017.02.20| 1

//join on another week
q)a,:update d+7 from a
q)select where 5=count each group s by `week$d from a where (d mod 7) in 2+til 5,t=`B
d         | s
----------| -
2017.02.20| 1
2017.02.27| 1


//insert some Bs for stock id 2 for days where it had no buy signal in the first week
q)a,:([]s:4#2;t:4#`B;d:2017.02.21+til 4)
q)select where 5=count each group s by `week$d from a where (d mod 7) in 2+til 5,t=`B
d         | s
----------| ---
2017.02.20| 1 2
2017.02.27| ,1

HTH,
Sean

Hi, Sean:
  Thanks for your example. It's easy to understand.
  I tried it, it works.

Thanks very much!
Zheng

在 2017年2月24日星期五 UTC+8下午7:30:42,Sean O'Hagan写道:
Below should work for you, and there'll be many more ways too

//dummy data
q)a:([]s:1 2 3 1 1 3 1 2 1 3;t:`$'"BBSBBBBSBB";d:raze 2#'2017.01.31+20+til 5)

q)select distinct s by `week$d from a where s in where 5=count each d group s,(d mod 7) in 2+til 5
d         | s
----------| -
2017.02.20| 1

//join on another week
q)a,:update d+7 from a
q)select where 5=count each group s by `week$d from a where (d mod 7) in 2+til 5,t=`B
d         | s
----------| -
2017.02.20| 1
2017.02.27| 1


//insert some Bs for stock id 2 for days where it had no buy signal in the first week
q)a,:([]s:4#2;t:4#`B;d:2017.02.21+til 4)
q)select where 5=count each group s by `week$d from a where (d mod 7) in 2+til 5,t=`B
d         | s
----------| ---
2017.02.20| 1 2
2017.02.27| ,1

HTH,
Sean

rahul_asati04
Contributor
Hi Zheng,
'x' will be list of dates for a particular stock id.
fby will send date list for each stock id in function {all dt in x} and this function will then check if all dates(mon - fri) are present in current date list 'x' of stock id  or not.