-
Notifications
You must be signed in to change notification settings - Fork 0
/
old_evExtraction.py
343 lines (327 loc) · 9.59 KB
/
old_evExtraction.py
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
class evExtraction(object):
"""description of class"""
import sys
sys.path.append('C:/Users/steve/Documents/Visual Studio 2015/Projects/PythonStats/x64/Debug/')
sys.path.append('../PythonUtility/')
import DataTypeUtility as dt_util
import DbUtility as db_util
def tsql_bevt_ptr1(_db_name , _schema, _table):
sqlstr = """
-- pattern.1
-- BP↑[--this duration--]TP↓
SELECT
*
FROM
(
SELECT
*,
DATEDIFF(s, PreviousDatetime, Datetime) AS Duration
FROM
(
SELECT
*
FROM
(
SELECT
*,
LAG(ActType,1,NULL) OVER(ORDER BY AutoId) AS PreviousAct,
LAG(Datetime,1,NULL) OVER(ORDER BY AutoId) AS PreviousDatetime,
LAG(Chg,1,NULL) OVER(ORDER BY AutoId) AS PreviousChg
FROM
(
SELECT
[Chg]
,[ActType]
,[Value]
,[DateTime]
,[AutoId]
FROM [{0}].[{1}].[{2}]
WHERE (ActType = 'TQ.↓' ) OR ([ActType] = 'BP' and Chg > 0)
) AS NEXT1
) AS NEST2
) AS NEST3
) AS NEST4
WHERE ActType <> PreviousAct and ActType = 'TQ.↓' and Duration > 1
""".format(_db_name , _schema, _table)
return sqlstr
def tsql_bevt_ptr2(_db_name , _schema, _table):
sqlstr = """
-- pattern.2
-- TP↓BP↓[--this duration--]BP↑
SELECT
*
FROM
(
SELECT
*,
DATEDIFF(s, PreviousDatetime, Datetime) AS Duration
FROM
(
SELECT
*
FROM
(
SELECT
*,
LAG(ActType,1,NULL) OVER(ORDER BY AutoId) AS PreviousAct,
LAG(Datetime,1,NULL) OVER(ORDER BY AutoId) AS PreviousDatetime,
LAG(Chg,1,NULL) OVER(ORDER BY AutoId) AS PreviousChg,
LAG(ActType,2,NULL) OVER(ORDER BY AutoId) AS PPAct,
LAG(Datetime,2,NULL) OVER(ORDER BY AutoId) AS PPDatetime,
LAG(Chg,2,NULL) OVER(ORDER BY AutoId) AS PPChg
FROM
(
SELECT
[Chg]
,[ActType]
,[DateTime]
,[AutoId]
FROM [{0}].[{1}].[{2}]
WHERE (ActType = 'TP.↓' ) OR ([ActType] = 'BP')
) AS NEXT1
) AS NEST2
) AS NEST3
) AS NEST4
WHERE PPAct = 'TP.↓' AND (PreviousAct = 'BP' and PreviousChg < 0) AND (ActType = 'BP' and Chg > 0) AND Duration > 1
""".format(_db_name , _schema, _table)
return sqlstr
def tsql_bevt_ptr3(_db_name , _schema, _table):
sqlstr = """
"""
return sqlstr
def tsql_bevt_ptr4(_db_name , _schema, _table):
sqlstr = """
-- pattern.4
-- TP↓BP↓[--this duration,PriceChg--]TP↑
SELECT
*
FROM
(
SELECT
*,
DATEDIFF(s, PreviousDatetime, Datetime) AS Duration,
Value - PreviousValue AS ValChg
FROM
(
SELECT
*
FROM
(
SELECT
*,
LAG(ActType,1,NULL) OVER(ORDER BY AutoId) AS PreviousAct,
LAG(Datetime,1,NULL) OVER(ORDER BY AutoId) AS PreviousDatetime,
LAG(Chg,1,NULL) OVER(ORDER BY AutoId) AS PreviousChg,
LAG(Value,1,NULL) OVER(ORDER BY AutoId) AS PreviousValue,
LAG(ActType,2,NULL) OVER(ORDER BY AutoId) AS PPAct,
LAG(Datetime,2,NULL) OVER(ORDER BY AutoId) AS PPDatetime,
LAG(Chg,2,NULL) OVER(ORDER BY AutoId) AS PPChg
FROM
(
SELECT
[Chg]
,[ActType]
,[DateTime]
,[Value]
,[AutoId]
FROM [{0}].[{1}].[{2}]
WHERE (ActType LIKE 'TP.%' ) OR ([ActType] = 'BP')
) AS NEXT1
) AS NEST2
) AS NEST3
) AS NEST4
WHERE PPAct = 'TP.↓' AND (PreviousAct = 'BP' and PreviousChg < 0) AND ActType = 'TP.↑' AND Duration > 1
""".format(_db_name , _schema, _table)
return sqlstr
def tsql_bevt_ptr5(_db_name , _schema, _table):
sqlstr = """
-- pattern.5
-- TP↓TP↓BP↓[--this duration--]AP↓
SELECT
*
FROM
(
SELECT
*,
DATEDIFF(s, PreviousDatetime, Datetime) AS Duration
FROM
(
SELECT
*
FROM
(
SELECT
*,
LAG(ActType,1,NULL) OVER(ORDER BY AutoId) AS PreviousAct,
LAG(Datetime,1,NULL) OVER(ORDER BY AutoId) AS PreviousDatetime,
LAG(Chg,1,NULL) OVER(ORDER BY AutoId) AS PreviousChg
FROM
(
SELECT TOP 1000
[Chg]
,[ActType]
,[DateTime]
,[AutoId]
FROM [{0}].[{1}].[{2}]
WHERE (ActType = 'AP' and Chg < 0 ) OR ([ActType] = 'BP' and Chg < 0)
) AS NEXT1
) AS NEST2
) AS NEST3
) AS NEST4
WHERE ActType <> PreviousAct and ActType = 'AP' and Duration > 1
""".format(_db_name , _schema, _table)
return sqlstr
def tsql_bevt_ptr6(_db_name , _schema, _table):
sqlstr = """
-- pattern.6
-- TP↑TP↑<-TotalQty,
SELECT
*
FROM
(
SELECT
*,
DATEDIFF(s, PreviousDatetime, Datetime) AS Duration
FROM
(
SELECT
*
FROM
(
SELECT
*,
LAG(ActType,1,NULL) OVER(ORDER BY AutoId) AS PreviousAct,
LAG(Datetime,1,NULL) OVER(ORDER BY AutoId) AS PreviousDatetime,
LAG(Chg,1,NULL) OVER(ORDER BY AutoId) AS PreviousChg,
LAG(Value,1,NULL) OVER(ORDER BY AutoId) AS PreviousValue
FROM
(
SELECT
[Chg]
,[ActType]
,[Value]
,[DateTime]
,[AutoId]
FROM [{0}].[{1}].[{2}]
WHERE (ActType = 'TQ.↑' )
) AS NEXT1
) AS NEST2
) AS NEST3
) AS NEST4
WHERE Duration < 10 and (Value + PreviousValue > 200)
""".format(_db_name , _schema, _table)
return sqlstr
def tsql_bevt_ptr7(_db_name , _schema, _table):
sqlstr = """
-- pattern.7
-- BP↓AP↓[--this duration,PriceChg--]AP↑
SELECT
*
FROM
(
SELECT
*,
DATEDIFF(s, PreviousDatetime, Datetime) AS Duration,
Value - PreviousValue AS ValChg
FROM
(
SELECT
*
FROM
(
SELECT
*,
LAG(ActType,1,NULL) OVER(ORDER BY AutoId) AS PreviousAct,
LAG(Datetime,1,NULL) OVER(ORDER BY AutoId) AS PreviousDatetime,
LAG(Chg,1,NULL) OVER(ORDER BY AutoId) AS PreviousChg,
LAG(Value,1,NULL) OVER(ORDER BY AutoId) AS PreviousValue,
LAG(ActType,2,NULL) OVER(ORDER BY AutoId) AS PPAct,
LAG(Datetime,2,NULL) OVER(ORDER BY AutoId) AS PPDatetime,
LAG(Chg,2,NULL) OVER(ORDER BY AutoId) AS PPChg
FROM
(
SELECT
[Chg]
,[ActType]
,[DateTime]
,[Value]
,[AutoId]
FROM [{0}].[{1}].[{2}]
WHERE (ActType = 'AP') OR ([ActType] = 'BP' and Chg < 0)
) AS NEXT1
) AS NEST2
) AS NEST3
) AS NEST4
WHERE PPAct = 'BP' AND (PreviousAct = 'AP' and PreviousChg < 0) AND (ActType = 'AP' and Chg > 0) AND Duration > 1
""".format(_db_name , _schema, _table)
return sqlstr
def tsql_bevt_ptr8(_db_name , _schema, _table):
sqlstr = """
-- pattern.8
-- TP↓[--this duration--]AP↑
SELECT
*
FROM
(
SELECT
*,
DATEDIFF(s, PreviousDatetime, Datetime) AS Duration
FROM
(
SELECT
*
FROM
(
SELECT
*,
LAG(ActType,1,NULL) OVER(ORDER BY AutoId) AS PreviousAct,
LAG(Value,1,NULL) OVER(ORDER BY AutoId) AS PreviousValue,
LAG(Datetime,1,NULL) OVER(ORDER BY AutoId) AS PreviousDatetime,
LAG(Chg,1,NULL) OVER(ORDER BY AutoId) AS PreviousChg
FROM
(
SELECT
[Chg]
,[ActType]
,[Value]
,[DateTime]
,[AutoId]
FROM [{0}].[{1}].[{2}]
WHERE (ActType = 'TQ.↓' ) OR ([ActType] = 'AP' and Chg > 0)
) AS NEXT1
) AS NEST2
) AS NEST3
) AS NEST4
WHERE PreviousAct = 'TQ.↓' and ActType = 'AP' and Duration > 1
""".format(_db_name , _schema, _table)
return sqlstr
def tsql_leadtime(_db_name, _schema, _table, _product, _evt_time, _num_of_obs=100):
sqlstr = """
SELECT TOP 1
LEAD(DateTime,100-1,NULL) OVER(ORDER BY DateTime) AS Next100TradeTimestamp
FROM [DWConfiguration].[dbo].[configuration_node]
WHERE ActType LIKE 'TP%' and Symbol = '1321-TSE'
""".format(_num_of_obs, _db_name, _schema, _table, _product, _evt_time)
return sqlstr
# SELECT TOP 1
#LEAD(DateTime,{0}-1,NULL) OVER(ORDER BY DateTime) AS Next100TradeTimestamp
#FROM [{1}].[{2}].[{3}]
#WHERE ActType LIKE 'TP%' and Symbol = {4} and DateTime >= CONVERT(DATETIME,{5})
def tsql_post_evt_stat(_db_name, _schema, _table, _product, _benchprice, _evt_time, _lead_time, _num_of_obs):
sqlstr = """
SELECT
COUNT(CASE WHEN Value > {4} THEN 1 END) / ({7} * 1.0) AS UpProb,
COUNT(CASE WHEN Value < {4} THEN 1 END) / ({7} * 1.0) AS DnProb
FROM [{0}].[{1}].[{2}]
WHERE Symbol = {3} and DateTime Between CONVERT(DATETIME,{5}) and {6} and ActType LIKE 'TP%'
""".format(_db_name, _schema, _table, _product, _benchrice, _evt_time, _lead_time, _num_of_obs)
return sqlstr
# do some test
#selected_cols = ["Date", "Time", "ActType"]
#sqlstr = tsql_bevt_ptr1("DWConfiguration","dbo","configuration_node")
#db_cursor = db_util.mssql_exec("P11013", "sa", "Bigdata01", "DWConfiguration", sqlstr)
#db_util.show_rows(db_cursor)
# post stat
sqlstr = tsql_leadtime("DWConfiguration", "dbo", "configuration_node", "1321-TSE", "2015-11-27 12:50:22")
db_cursor = db_util.mssql_exec("P11013", "sa", "Bigdata01", "DWConfiguration", sqlstr)
db_util.show_rows(db_cursor)