forked from cjh4312/stock-quotes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
threadRealTime.py
229 lines (212 loc) · 12.1 KB
/
threadRealTime.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
# This Python file uses the following encoding: utf-8
# if __name__ == "__main__":
# pass
from PySide6.QtCore import Signal,QThread
import globalVariable
import pandas as pd
import requests
import os
class RealTimeThread(QThread):
# 通过类成员对象定义信号对象
_signal = Signal()
def __init__(self,parent):
super(RealTimeThread, self).__init__()
self.parent=parent
def __del__(self):
self.wait()
# def get_time_share_tick_data(self):
# code=self.parent.stock_code
# self.isIndex=False
# if code[0]=='3' or code[0]=='0' or code[0]=='8' or code[0]=='4':
# num=0
# if code[0:3]=='399':
# self.isIndex=True
# else:
# num=1
# if code[0]=='s' and code[2]=='.' and code[3].isdigit():
# self.pre_stock_code=code
# code=code[3:9]
# self.isIndex=True
# self.parent.tableView.get_time_share_tick(num,code)
def get_time_share_tick_data(self):
code=self.parent.stock_code
self.data=pd.DataFrame()
self.isIndex=False
if code[0:4]=='100.' or code[0:4]=='103.' or code[0:4]=='104.' or not code[0:2].isdigit():
self.parent.time_share_data=self.data
self._signal.emit()
return
if globalVariable.marketNum==1 or globalVariable.marketNum==3:
if code[0:4]=='105.' or code[0:4]=='106.' or code[0:4]=='107.':
return
if code[0]=='3' or code[0]=='0' or code[0]=='8' or code[0]=='4':
num=0
if code[0:3]=='399':
self.isIndex=True
else:
num=1
if code[0]=='s' and code[2]=='.' and code[3].isdigit():
self.pre_stock_code=code
code=code[3:9]
self.isIndex=True
(self.parent.pre_close,self.data)=self.parent.worldIndex.get_time_share_tick(num,code)
elif globalVariable.marketNum==2 or globalVariable.marketNum==4:
if code[0:4]=='105.' or code[0:4]=='106.' or code[0:4]=='107.':
(self.parent.pre_close,self.data)=self.parent.worldIndex.get_us_time_share_tick(code)
def deal_with_time_share_tick_html(self):
self.parent.time_share_data = ''
l = len(self.data)
for i in range(0,l):
ok=self.data[i].split(",", 5)
if ok[4]=='2':
flag='<span style="color:red">B</span>'
elif ok[4]=='1':
flag='<span style="color:green">S</span>'
else:
flag=' '
a=f'<div>{ok[0]} '
if float(ok[1])>self.parent.pre_close:
b=f'<a style="color:red">{ok[1]}</a> '
else:
b = f'<a style="color:green">{ok[1]}</a> '
if not self.isIndex:
if i!=l-1:
self.parent.time_share_data=f"{self.parent.time_share_data}{a}{b}{ok[2]}{flag} {ok[3]}\n"
else:
self.parent.time_share_data = f"{self.parent.time_share_data}{a}{b}{ok[2]}{flag} {ok[3]}"
if self.parent.now_close<self.parent.pre_close:
self.parent.baseInformation.base_info['self.now_price_data'].setPalette(globalVariable.pegreen)
self.parent.baseInformation.base_info['self.pctChg_data'].setPalette(globalVariable.pegreen)
else:
self.parent.baseInformation.base_info['self.now_price_data'].setPalette(globalVariable.pered)
self.parent.baseInformation.base_info['self.pctChg_data'].setPalette(globalVariable.pered)
self.parent.baseInformation.base_info['self.now_price_data'].setText(str(self.parent.now_close))
self.parent.baseInformation.base_info['self.pctChg_data'].setText(f"{round(((self.parent.now_close-self.parent.pre_close)*100/self.parent.pre_close),2)}%")
def deal_with_time_share_tick_data(self):
self.parent.time_share_data=''
j=0
l=len(self.data)
self.parent.price=[0]*l
self.parent.price_len=[0]*l
self.parent.flag_direction=[0]*l
self.parent.flag_pos=[0]*(l+1)
self.parent.line_len=[0]*(l+1)
if not globalVariable.isZhMarketDay():
self.parent.text_count=l
elif l<14:
self.parent.text_count=l
else:
self.parent.text_count=14
for i in range(l-self.parent.text_count,l):
ok=self.data[i].split(",", 5)
if ok[4]=='2':
flag='B'
elif ok[4]=='1':
flag='S'
else:
flag=' '
a=len(ok[2])
b=len(ok[3])
if not self.isIndex:
if i!=l-1:
self.parent.time_share_data=f"{self.parent.time_share_data}{'{0:<12}'.format(ok[0])}{ok[1]} {format(ok[2], '>15')[a:15]}{flag} {format(ok[3], '>8')[b:8]}\n"
self.parent.price[j]=float(ok[1])
self.parent.price_len[j]=len(ok[1])
self.parent.flag_direction[j]=int(ok[4])
j=j+1
else:
self.parent.time_share_data=f"{self.parent.time_share_data}{'{0:<12}'.format(ok[0])}{ok[1]} {format(ok[2], '>15')[a:15]}{flag} {format(ok[3], '>8')[b:8]}"
self.parent.price[j]=float(ok[1])
self.parent.price_len[j]=len(ok[1])
self.parent.flag_direction[j]=int(ok[4])
self.parent.now_close=float(ok[1])
if self.parent.now_close<self.parent.pre_close:
self.parent.baseInformation.base_info['self.now_price_data'].setPalette(globalVariable.pegreen)
self.parent.baseInformation.base_info['self.pctChg_data'].setPalette(globalVariable.pegreen)
else:
self.parent.baseInformation.base_info['self.now_price_data'].setPalette(globalVariable.pered)
self.parent.baseInformation.base_info['self.pctChg_data'].setPalette(globalVariable.pered)
self.parent.baseInformation.base_info['self.now_price_data'].setText(str(self.parent.now_close))
self.parent.baseInformation.base_info['self.pctChg_data'].setText(f"{round(((self.parent.now_close-self.parent.pre_close)*100/self.parent.pre_close),2)}%")
else:
if i!=len(self.data)-1:
self.parent.time_share_data=f"{self.parent.time_share_data}{'{0:<12}'.format(ok[0])+ok[1]} {format(ok[2], '>18')[a:18]}{flag}\n"
self.parent.price[j]=float(ok[1])
self.parent.price_len[j]=len(ok[1])
self.parent.flag_direction[j]=int(ok[4])
j=j+1
else:
self.parent.time_share_data=f"{self.parent.time_share_data}{'{0:<12}'.format(ok[0])+ok[1]} {format(ok[2], '>18')[a:18]}{flag}"
self.parent.price[j]=float(ok[1])
self.parent.price_len[j]=len(ok[1])
self.parent.flag_direction[j]=int(ok[4])
self.parent.now_close=float(ok[1])
j=0
#print(self.parent.time_share_data)
for i in range(len(self.parent.time_share_data)):
if self.parent.time_share_data[i]=='S' or self.parent.time_share_data[i]=='B':
self.parent.flag_pos[j+1]=i+1
if self.parent.time_share_data[i]=='\n':
self.parent.line_len[j+1]=i+1
j=j+1
#self.parent.text_count=j+1
def flash_buy_sell_and_capital(self):
headers={'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36 Edg/105.0.1343.27'}
code=self.parent.stock_code
if globalVariable.marketNum==1 or globalVariable.marketNum==3:
if code[0:1]=='s' or code[0:3]=='399' or code[0:2]=='BK' or code[0:2]=='10' or not code[0:2].isdigit():
return
num=0
if code[0] == '6':
num=1
url='http://25.push2.eastmoney.com/api/qt/stock/get?ut=fa5fd1943c7b386f172d6893dbfba10b&fltt=2&invt=2&volt=2&fields=f116,f84,f85,f162,f31,f32,f33,f34,f35,f36,f37,f38,f39,f40,f20,f19,f18,f17,f16,f15,f14,f13,f12,f11,f531&secid={}.{}&_=1666089246963'.format(num,code)
data=requests.get(url=url,headers=headers,timeout=0.3).json()['data']
for i in range(0,10,2):
if data[f'f{i+31}']!='' and data[f'f{i+31}']!='-':
if data[f'f{i+31}']>self.parent.pre_close:
self.parent.baseInformation.buy_sell_Var[f'self.sell_price{5-i//2}'].setPalette(globalVariable.pered)
elif data[f'f{i+31}']<self.parent.pre_close:
self.parent.baseInformation.buy_sell_Var[f'self.sell_price{5-i//2}'].setPalette(globalVariable.pegreen)
else:
self.parent.baseInformation.buy_sell_Var[f'self.sell_price{5-i//2}'].setPalette(globalVariable.peblack)
self.parent.baseInformation.buy_sell_Var[f'self.sell_price{5-i//2}'].setText(str(data['f'+str(i+31)]))
else:
self.parent.baseInformation.buy_sell_Var[f'self.sell_price{5-i//2}'].setText('')
if data[f'f{i+11}']!='' and data[f'f{i+11}']!='-':
if data[f'f{i+11}']>self.parent.pre_close:
self.parent.baseInformation.buy_sell_Var[f'self.buy_price{5-i//2}'].setPalette(globalVariable.pered)
elif data[f'f{i+11}']<self.parent.pre_close:
self.parent.baseInformation.buy_sell_Var[f'self.buy_price{5-i//2}'].setPalette(globalVariable.pegreen)
else:
self.parent.baseInformation.buy_sell_Var[f'self.buy_price{5-i//2}'].setPalette(globalVariable.peblack)
self.parent.baseInformation.buy_sell_Var[f'self.buy_price{5-i//2}'].setText(str(data['f'+str(i+11)]))
else:
self.parent.baseInformation.buy_sell_Var[f'self.buy_price{5-i//2}'].setText('')
sc=''
if data[f'f{i+32}']!='' and data[f'f{i+32}']!='-':
sc=str(round(data[f'f{i+32}']))
self.parent.baseInformation.buy_sell_Var[f'self.sell_num{5-i//2}'].setText(sc)
sc=''
if data[f'f{i+12}']!='' and data[f'f{i+12}']!='-':
sc=str(round(data[f'f{i+12}']))
self.parent.baseInformation.buy_sell_Var[f'self.buy_num{5-i//2}'].setText(sc)
self.parent.baseInformation.base_info['self.Issued_Capital_data'].setText(globalVariable.format_conversion(data['f84']))
self.parent.baseInformation.base_info['self.Negotiable_Capital_data'].setText(globalVariable.format_conversion(data['f85']))
self.parent.baseInformation.base_info['self.total_captial_data'].setText(globalVariable.format_conversion(data['f116']))
self.parent.baseInformation.base_info['self.pe_data'].setText(str(data['f162']))
else:
for i in range(0,10,2):
self.parent.baseInformation.buy_sell_Var[f'self.sell_price{5-i//2}'].setText('')
self.parent.baseInformation.buy_sell_Var[f'self.sell_num{5-i//2}'].setText('')
self.parent.baseInformation.buy_sell_Var[f'self.buy_price{5-i//2}'].setText('')
self.parent.baseInformation.buy_sell_Var[f'self.buy_num{5-i//2}'].setText('')
def run(self):
self.isFlaseRealTime=False
try:
self.get_time_share_tick_data()
self.flash_buy_sell_and_capital()
self.deal_with_time_share_tick_data()
except Exception as e:
print('Reason:', e)
self._signal.emit()
self.isFlaseRealTime=True