-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSSE_Stocks_traded_Byday_analysis.py
350 lines (225 loc) · 11 KB
/
SSE_Stocks_traded_Byday_analysis.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
344
345
346
347
348
# -*- coding:utf-8 -*-
import datetime
from datetime import date
from chinese_calendar import is_workday,is_holiday
import akshare as ak
import pandas as pd
import numpy as np
import pymysql
from sqlalchemy import create_engine
#以下代码实现个股每日分笔成交数据初步分析,通过更改company code即可实现不同个股切换;
# Query data (only change code and database name is stock_deal_detail_daily)
company_code = 'sz000651'
# date(By day after stock Market Closed )
# use akshare module
### DDD = Deal Detail Daily abbreviation
### 待补充case函数反馈抓取异常;
DDD = ak.stock_zh_a_tick_tx_js(code=company_code)
# query data is all string of the DataFrame formate
# pre handle to numeric and datatime and set datetimeindex
rowdata = DDD.apply(pd.to_numeric,errors= 'ignore')
rowdata['成交时间']=pd.to_datetime(rowdata['成交时间'])
rowdata.set_index('成交时间',inplace=True)
#print(rowdata)
#rowdata 初步处理,生成初步指标数据:
# 开盘价,收盘价 当日幅动等,详见附件思维导入或解析说明
# 1.open price
open_price = rowdata.iat[0,0]
# 2.close price
closed_price = rowdata.iat[len(rowdata.index)-1,0]
# 3.today_change_precent
today_change_precent = (closed_price-open_price)/open_price
# 4.day range
today_high = rowdata['成交价'].max()
today_low = rowdata['成交价'].min()
day_range = (today_high-today_low)/today_low
#print(today_change_precent,day_range)
# 5.turnover
today_turnover = rowdata['成交额'].sum()
# 6.Volume
today_volume = rowdata['成交量'].sum()
# 7.turn over > 500K ration
turnover_500K = rowdata.loc[rowdata['成交额']>500000]
turnover_500K_sum =turnover_500K['成交额'].sum()
#print(today_turnover,turnover_500K_sum)
#print(type(today_turnover),type(turnover_500K_sum))
turnover_500K_ration = turnover_500K_sum/today_turnover
#print(turnover_500K_ration)
# divide B/S
buyorder = rowdata.loc[(rowdata['性质']=='B')]
sellorder= rowdata.loc[(rowdata['性质']=='S')]
# 8.turn over active buy
buyorder_turnover = buyorder['成交额'].sum()
# 9.turn over postibve sell
sellorder_sellorder = sellorder['成交额'].sum()
# 10.net_inflows
net_inflows = buyorder_turnover-sellorder_sellorder
#print(net_inflows)
# By 成交金额By成交金额(区分B/S)
# >1000
# >500
# >200
# >100
# >50
#10. >10Million
buyorder_turnover_10M = buyorder.loc[buyorder['成交额']>10000000]
buyorder_turnover_10M_sum = buyorder_turnover_10M['成交额'].sum()
#print(buyorder_turnover_10M_sum)
sellorder_turnover_10M = sellorder.loc[sellorder['成交额']>10000000]
sellorder_turnover_10M_sum = sellorder_turnover_10M['成交额'].sum()
#print(sellorder_turnover_10M_sum)
net_inflows_10Million = buyorder_turnover_10M_sum-sellorder_turnover_10M_sum
# 10Millin<>5Million
buyorder_turnover_5M = buyorder.loc[(buyorder['成交额']>5000000)&(buyorder['成交额']<10000000)]
buyorder_turnover_5M_sum = buyorder_turnover_5M['成交额'].sum()
#print(buyorder_turnover_5M_sum)
sellorder_turnover_5M = sellorder.loc[(sellorder['成交额']>5000000)&(sellorder['成交额']<10000000)]
sellorder_turnover_5M_sum = sellorder_turnover_5M['成交额'].sum()
#print(sellorder_turnover_5M_sum)
net_inflows_5Million = buyorder_turnover_5M_sum-sellorder_turnover_5M_sum
# 5Millin<>2Million
buyorder_turnover_2M = buyorder.loc[(buyorder['成交额']>2000000)&(buyorder['成交额']<5000000)]
buyorder_turnover_2M_sum = buyorder_turnover_2M['成交额'].sum()
#print(buyorder_turnover_2M_sum)
sellorder_turnover_2M = sellorder.loc[(sellorder['成交额']>2000000)&(sellorder['成交额']<5000000)]
sellorder_turnover_2M_sum = sellorder_turnover_2M['成交额'].sum()
#print(sellorder_turnover_2M_sum)
net_inflows_2Million = buyorder_turnover_2M_sum-sellorder_turnover_2M_sum
# 2Millin<>1Million
buyorder_turnover_1M = buyorder.loc[(buyorder['成交额']>1000000)&(buyorder['成交额']<2000000)]
buyorder_turnover_1M_sum = buyorder_turnover_1M['成交额'].sum()
#print(buyorder_turnover_1M_sum)
sellorder_turnover_1M = sellorder.loc[(sellorder['成交额']>1000000)&(sellorder['成交额']<2000000)]
sellorder_turnover_1M_sum = sellorder_turnover_1M['成交额'].sum()
#print(sellorder_turnover_1M_sum)
net_inflows_1Million = buyorder_turnover_1M_sum-sellorder_turnover_1M_sum
# 1Millin<>500K
buyorder_turnover_500K = buyorder.loc[(buyorder['成交额']>500000)&(buyorder['成交额']<1000000)]
buyorder_turnover_500K_sum = buyorder_turnover_500K['成交额'].sum()
#print(buyorder_turnover_500K_sum)
sellorder_turnover_500K = sellorder.loc[(sellorder['成交额']>500000)&(sellorder['成交额']<1000000)]
sellorder_turnover_500K_sum = sellorder_turnover_500K['成交额'].sum()
#print(sellorder_turnover_500K_sum)
net_inflows_500K = buyorder_turnover_500K_sum-sellorder_turnover_500K_sum
# <500K
buyorder_turnover_500000 = buyorder.loc[buyorder['成交额']<500000]
buyorder_turnover_500000_sum = buyorder_turnover_500000['成交额'].sum()
#print(buyorder_turnover_500000_sum)
sellorder_turnover_500000 = sellorder.loc[sellorder['成交额']<500000]
sellorder_turnover_500000_sum = sellorder_turnover_500000['成交额'].sum()
#print(sellorder_turnover_500000_sum)
net_inflows_1Million = buyorder_turnover_500000_sum-sellorder_turnover_500000_sum
# By成交时间段(区分B/S)
# 9:25~9:45
# 9:45~10:00
# 10:00~10:30
# 10:30~11:30
# 13:00~14:30
# 14:30~15:00
# 9:25~9:45
buyorder_time_index_0945 = buyorder.index.indexer_between_time('09:25:00','9:44:59')
#print(data_num_time)
#print(data_num.iloc[data_num_time])
buyorder_time_0945 = buyorder.iloc[buyorder_time_index_0945]['成交额'].sum()
#print(buyorder_time_0945)
sellorder_time_index_0945 = sellorder.index.indexer_between_time('09:25:00','9:44:59')
sellorder_time_0945 = sellorder.iloc[sellorder_time_index_0945]['成交额'].sum()
#print(sellorder_time_0945)
# 9:46~10:00
buyorder_time_index_1000 = buyorder.index.indexer_between_time('09:45:00','09:59:59')
buyorder_time_1000 = buyorder.iloc[buyorder_time_index_1000]['成交额'].sum()
#print(buyorder_time_1000)
sellorder_time_index_1000 = sellorder.index.indexer_between_time('09:45:00','09:59:59')
sellorder_time_1000 = sellorder.iloc[sellorder_time_index_1000]['成交额'].sum()
#print(sellorder_time_1000)
# 10:00~10:30
buyorder_time_index_1030 = buyorder.index.indexer_between_time('10:00:00','10:29:59')
buyorder_time_1030 = buyorder.iloc[buyorder_time_index_1030]['成交额'].sum()
#print(buyorder_time_1030)
sellorder_time_index_1030 = sellorder.index.indexer_between_time('10:00:00','10:29:59')
sellorder_time_1030= sellorder.iloc[sellorder_time_index_1030]['成交额'].sum()
#print(sellorder_time_1030)
# 10:30~11:30
buyorder_time_index_1130 = buyorder.index.indexer_between_time('10:30:00','11:30:59')
buyorder_time_1130 = buyorder.iloc[buyorder_time_index_1130]['成交额'].sum()
#print(buyorder_time_1130)
sellorder_time_index_1130 = sellorder.index.indexer_between_time('10:30:00','11:30:59')
sellorder_time_1130 = sellorder.iloc[sellorder_time_index_1130]['成交额'].sum()
#print(sellorder_time_1130)
# 13:00~14:30
buyorder_time_index_1430 = buyorder.index.indexer_between_time('13:00:00','14:29:59')
buyorder_time_1430 = buyorder.iloc[buyorder_time_index_1430]['成交额'].sum()
#print(buyorder_time_1430)
sellorder_time_index_1430 = sellorder.index.indexer_between_time('13:00:00','14:29:59')
sellorder_time_1430 = sellorder.iloc[sellorder_time_index_1430]['成交额'].sum()
#print(sellorder_time_1430)
# 14:30~15:00
buyorder_time_index_1500 = buyorder.index.indexer_between_time('14:30:00','15:00:59')
buyorder_time_1500 = buyorder.iloc[buyorder_time_index_1500]['成交额'].sum()
#print(buyorder_time_1500)
sellorder_time_index_1500 = sellorder.index.indexer_between_time('14:30:00','15:00:59')
sellorder_time_1500 = sellorder.iloc[sellorder_time_index_1500]['成交额'].sum()
#print(sellorder_time_1500)
datatime=datetime.date.today()
#td=datetime.date.today()+datetime.timedelta(days=-3)
#trade_date_str =trade_date.strftime("%Y%m%d")
reviewdata_list=[datatime,open_price,closed_price,today_change_precent,today_turnover,today_volume,turnover_500K_ration,buyorder_turnover,sellorder_sellorder,net_inflows,
buyorder_turnover_10M_sum,sellorder_turnover_10M_sum,buyorder_turnover_5M_sum,sellorder_turnover_5M_sum,buyorder_turnover_2M_sum,sellorder_turnover_2M_sum,buyorder_turnover_1M_sum,sellorder_turnover_1M_sum,buyorder_turnover_500K_sum,sellorder_turnover_500K_sum,
buyorder_time_0945,sellorder_time_0945,buyorder_time_1000,sellorder_time_1000,buyorder_time_1030,sellorder_time_1030,buyorder_time_1000,sellorder_time_1000,buyorder_time_1130,sellorder_time_1130,buyorder_time_1430,sellorder_time_1430,buyorder_time_1500,sellorder_time_1500,]
#print(reviewdata_list)
reviewdata_dic={
'datatime':datatime,
'open_price':open_price,
'closed_price':closed_price,
'today_change_precent':today_change_precent,
'today_volume':today_volume,
'turnover_500K_ration':turnover_500K_ration,
'buyorder_turnover':buyorder_turnover,
'sellorder_sellorder':sellorder_sellorder,
'net_inflows':net_inflows,
'buyorder_turnover_10M_sum':buyorder_turnover_10M_sum,
'sellorder_turnover_10M_sum':sellorder_turnover_10M_sum,
'buyorder_turnover_5M_sum':buyorder_turnover_5M_sum,
'sellorder_turnover_5M_sum':sellorder_turnover_5M_sum,
'buyorder_turnover_2M_sum':buyorder_turnover_2M_sum,
'sellorder_turnover_2M_sum':sellorder_turnover_2M_sum,
'sellorder_turnover_1M_sum':sellorder_turnover_1M_sum,
'buyorder_turnover_500K_sum':buyorder_turnover_500K_sum,
'sellorder_turnover_500K_sum':sellorder_turnover_500K_sum,
'buyorder_time_0945':buyorder_time_0945,
'sellorder_time_0945':sellorder_time_0945,
'buyorder_time_1000':buyorder_time_1000,
'sellorder_time_1000':sellorder_time_1000,
'buyorder_time_1030':buyorder_time_1030,
'sellorder_time_1030':sellorder_time_1030,
'buyorder_time_1000':buyorder_time_1000,
'sellorder_time_1000':sellorder_time_1000,
'buyorder_time_1130':buyorder_time_1130,
'sellorder_time_1130':sellorder_time_1130,
'buyorder_time_1430':buyorder_time_1430,
'sellorder_time_1430':sellorder_time_1430,
'buyorder_time_1500':buyorder_time_1500,
'sellorder_time_1500':sellorder_time_1500,
}
#print(reviewdata_dic)
reviewdata=pd.DataFrame(reviewdata_dic,index=[0])
#reviewdata.set_index('datatime',inplace=True)
#print(reviewdata)
rowdata_datetime = DDD.apply(pd.to_numeric,errors= 'ignore')
rowdata_datetime['成交时间']=pd.to_datetime(rowdata_datetime['成交时间'])
#print(rowdata_datetime)
'''
# 创建数据库
conn = pymysql.connect(host='localhost',user='root',passwd='cucumber',charset='utf8')
cursor = conn.cursor()
cursor.execute("CREATE DATABASE IF NOT EXISTS stock_deal_detail_daily")
conn.close()
'''
# store ddd data to sql
from sqlalchemy import create_engine
tablename_ddd_rowdata = company_code+'_ddd_rowdata'
tablename_ddd_reviewdata = company_code+'_ddd_reviewdata'
#print(tablename_ddd_rowdata,tablename_ddd_reviewdata)
conn = create_engine('mysql+pymysql://root:cucumber@localhost/stock_deal_detail_daily',encoding='utf-8')
rowdata_datetime.to_sql(name=tablename_ddd_rowdata, con=conn, if_exists='append', index=False)
reviewdata.to_sql(name=tablename_ddd_reviewdata, con=conn, if_exists='append', index=False)