-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstudy.wj.txt
111 lines (86 loc) · 3.93 KB
/
study.wj.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
Goal: + Study functional design of wj, which is a compact k code for joins measured by time intervals.
+ From its k code extract a series of subexpressions and variables.
+ Finally call all subexpressions and verify its result equals calling function wj.
Here is original code from q.k for functions ww, wj, wj1
/wj[-1000 2000+\:trade`time;`sym`time;trade;(quote;(max;`ask);(min;`bid))] (given `sym`time xasc quote)
ww:{[a;w;f;y;z]f,:();e:+1_z;z:*z;y,'+f!+{[e;d;a;b]e@'d@\:a+!b-a}[*e;z f:*|e]/'+$[#g;(g#z)?g#y;0]|/:a+$[#g:-1_f;(f#z)bin@[f#y;*|f;:;]@;z[*f]bin]'w}
wj:{[w;f;y;z]ww[0 1;w;f;y;z]};
wj1:{[w;f;y;z]ww[1;w-1 0;f;y;z]}
Note: wj calls ww
wj has a parameter w which takes measurable time intervals. wj[(time1;time2);....
wj1 on the other hand takes specific time events. wj1[(time1;time1);...
===================================================================
Study: Breakdown wj's ww into subexpressions, for study
This study requires input into a q console.
===================================================================
/ For this study, renamed ww as wwX and changed parameter names from ww:{[a;w;f;y;z]...} to wwX:{[a;w;c;t;z]...}
/ Renaming eased tracking parameters through its function body.
/ Below is wwX with carriage returns to identify function variables and subexpressions.
/ We call subexpressions in this sequence: t1,t2,t3,t4,t5
wwX:{[a;w;c;t;z]
c,:();
e:+1_z;
z:*z;
t,'+c!+{[e;d;a;b]e@'d@\:a+!b-a}[*e;z c:*|e]/' / subexpression t3,t4,t5
+$[#g;(g#z)?g#t;0]|/: / subexpression t2
a+$[#g:-1_c;(c#z)bin@[c#t;*|c;:;]@;z[*c]bin]'w / subexpression t1
}
Tables used for study: (copy and paste into q console)
q)show t:([]sym:4#`aapl;time:09:30:01 09:30:04 09:30:05 09:30:08;price:100 103 101 99);
q)show q:([] sym:8#`aapl;time:09:30:01+(til 5),7 8 9;ask:101 103 103 104 105 103 102 100;bid:98 99 102 103 103 100 100 99);
/ Note that in this study, many variables and subexpressions are in 'q)k)' space due to their syntax
/ input into q console, these initialized variables normally passed to wj.
q)w:-2 1+\:t `time;
q)c:`sym`time;
/ note t and q are tables previously defined, above
q)z:(q;(::;`ask);(::;`bid));
/ input into q console, a default parameter of ww function.
q)a:0 1;
/ input into q console, variables from wwX's function body.
q)c,:();
q)k)e:+1_z;
q)k)z:*z;
// input these wwX function body subexpressions as steps into q console
q)k)t1:a+$[#g:-1_c;(c#z)bin@[c#t;*|c;:;]@;z[*c]bin]'w
// sub sub expressions of t1 for study.
// play with only after proving subexpressions t1,t2,t3,t4,t5
q)k)t1a:((c#z)bin@[c#t;*|c;:;]@)
q)k)t1b:t1a'w
q)k)t1c:a+t1b
q)k)t2:+$[#g;(g#z)?g#t;0]|/:t1
// variable 'c' has two different value sets: `sym`time and `ask`bid.
// Renamed c as cX for `ask`bid...
// variable 'a' in t3 is not a in t1.
// in t3, variables 'a' and 'b' take values from t2, see t4.
// So renamed variables 'a' and 'b' to 'aa' and 'bb' in subexpression t3
q)k)cX:*|e
q)k)t3:{[e;z]{[e;d;aa;bb]e@'d@\:aa+!bb-aa}[*e;z cX]}
q)k)t4:t3[e;z]/'t2
q)k)t5:t,'+cX!+t4
q)t5
sym time price ask bid
--------------------------------------------------
aapl 09:30:01 100 101 103 98 99
aapl 09:30:04 103 103 103 104 105 99 102 103 103
aapl 09:30:05 101 103 104 105 102 103 103
aapl 09:30:08 99 105 103 102 103 100 100
// For verification.....
q)w:-2 1+\:t `time;
q)c:`sym`time;
q)z:(q;(::;`ask);(::;`bid));
q)wj[w;c;t;z]
sym time price ask bid
--------------------------------------------------
aapl 09:30:01 100 101 103 98 99
aapl 09:30:04 103 103 103 104 105 99 102 103 103
aapl 09:30:05 101 103 104 105 102 103 103
aapl 09:30:08 99 105 103 102 103 100 100
// For a quick look at wj1
q)z:(q;(::;`ask);(::;`bid));
q)wj1[(t.time;t.time);c;t;z]
sym time price ask bid
---------------------------
aapl 09:30:01 100 101 98
aapl 09:30:04 103 104 103
aapl 09:30:05 101 105 103
aapl 09:30:08 99 103 100