-
Notifications
You must be signed in to change notification settings - Fork 0
/
warnform.pas
403 lines (335 loc) · 9.13 KB
/
warnform.pas
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
unit WarnForm;
{$mode ObjFPC}{$H+}
interface
uses
Classes,
{$if defined(windows)}
Windows,
{$endif}
{$ifdef darwin}
Process,
{$endif}
SysUtils, StrUtils, Forms, Controls, Graphics, Dialogs, ComCtrls, StdCtrls,
ExtCtrls, Menus, IdHTTP;
type
RWarnMessage = record
warnType : string;
warnMessage : string;
suggustion : string;
end;
PRWarnMessage = ^RWarnMessage;
RStockPrice = record
stockName : string;
price : string;
fd : string;
end;
PRStockPrice = ^RStockPrice;
{ TFrmWarn }
TFrmWarn = class(TForm)
btnClear: TButton;
Button1: TButton;
IdHTTP1: TIdHTTP;
il1: TImageList;
lv1: TListView;
lvStockPrice: TListView;
MenuItemStockConfig: TMenuItem;
MenuItemClockConfig: TMenuItem;
PopupMenuConfig: TPopupMenu;
tmr1: TTimer;
procedure btnClearClick(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure MenuItemClockConfigClick(Sender: TObject);
procedure MenuItemStockConfigClick(Sender: TObject);
private
public
procedure addMsg(msgType: string; msg: string; suggestion: string);
procedure ShowPrice(stockname, price, fd: string);
procedure ClearPriceInfo;
end;
{ TMyThread }
TMyThread = class(TThread)
private
warnMessageList : TThreadList;
stockPriceList : TThreadList;
procedure ShowWarnMessage;
procedure ShowPrice;
procedure AddWarnMessage(warnType, warnMessage, suggestion: string);
procedure AddStockPriceRecord(stockName, price, fd: string);
protected
procedure GetPrice(stockCode: string; var price: string; var fd: string);
procedure Execute; override;
public
constructor Create(CreateSuspended: boolean);
end;
var
FrmWarn: TFrmWarn;
implementation
uses
Utils, MainForm, Stock;
{$R *.lfm}
{ TFrmWarn }
procedure TFrmWarn.Button1Click(Sender: TObject);
begin
//self.PopupMenuConfig.PopUp;
FrmMain.Show;
end;
procedure TFrmWarn.btnClearClick(Sender: TObject);
begin
self.lv1.Clear;
end;
procedure TFrmWarn.FormCreate(Sender: TObject);
var
MyThread : TMyThread;
begin
MyThread := TMyThread.Create(True); // With the True parameter it doesn't start automatically
if Assigned(MyThread.FatalException) then
raise MyThread.FatalException;
MyThread.Start;
end;
procedure TFrmWarn.MenuItemClockConfigClick(Sender: TObject);
begin
//FrmClock.Show;
end;
procedure TFrmWarn.MenuItemStockConfigClick(Sender: TObject);
begin
FrmMain.Show;
end;
procedure TFrmWarn.ClearPriceInfo;
begin
self.lvStockPrice.Clear;
end;
procedure TFrmWarn.addMsg(msgType: string; msg: string; suggestion: string);
var
AItem: TListItem;
begin
with self.lv1 do
begin
AItem := Items.Add;
AItem.Caption := msgType;
if msgType = '^' then begin
//AItem.ImageIndex := 1;
end else if msgType = 'WARN' then
AItem.ImageIndex := 0;
AItem.SubItems.Add(msg);
AItem.SubItems.Add(suggestion);
end;
end;
procedure TFrmWarn.ShowPrice(stockname, price, fd: string);
var
AItem: TListItem;
i: integer;
begin
for i := 0 to self.lvStockPrice.Items.Count - 1 do
begin
if lvStockPrice.Items[i].Caption = stockname then
begin
lvStockPrice.Items[i].SubItems[0] := price;
lvStockPrice.Items[i].SubItems[1] := fd;
exit;
end;
end;
with self.lvStockPrice do
begin
AItem := Items.Add;
AItem.Caption := stockname;
AItem.SubItems.Add(price);
AItem.SubItems.Add(fd);
end;
end;
{ TMyThread }
procedure TMyThread.ShowWarnMessage;
var
//F:FLASHWINFO;
pRecord : PRWarnMessage;
i: Integer;
s: string;
warntype : string;
begin
//f.cbSize:=sizeof(FLASHWINFO);
//f.hwnd:=application.Handle;
//f.dwFlags:=FLASHW_ALL or FLASHW_STOP;
//f.uCount:=4;
//f.dwTimeout:=250;
//FlashWindowEx(@f);
pRecord := self.warnMessageList.LockList.First;
if pRecord <> nil then begin
warntype := pRecord^.warnType;
FrmWarn.addMsg(pRecord^.warnType, pRecord^.warnMessage, pRecord^.suggustion);
self.warnMessageList.Remove(pRecord);
Dispose(pRecord);
end;
self.warnMessageList.UnlockList;
if frmwarn.lv1.Items.Count > 200 then
begin
for i:=0 to 100 do
frmwarn.lv1.Items.Delete(0);
end;
{$if defined(windows)}
flashwindow(application.Handle, True);
{$endif}
{$ifdef darwin}
if warntype = '^' then begin
RunCommand('/usr/bin/osascript', ['-e', 'display notification "WooHoo!" with title "Attention Please:"'], s);
end else begin
if warntype = 'WARN' then begin
RunCommand('/usr/bin/osascript', ['-e', 'display notification "Warn **************" with title "Attention Please:"'], s);
end;
end;
{$endif}
end;
procedure TMyThread.AddWarnMessage(warnType, warnMessage, suggestion: string);
var
pRecord : PRWarnMessage;
begin
New(pRecord);
FillChar(pRecord^, SizeOf(RWarnMessage), 0);
pRecord^.warnType:= warnType;
pRecord^.warnMessage:= warnMessage;
pRecord^.suggustion:= suggestion;
self.warnMessageList.add(pRecord);
Synchronize(@ShowWarnMessage);
end;
procedure TMyThread.AddStockPriceRecord(stockName, price, fd: string);
var
pRecord : PRStockPrice;
begin
New(pRecord);
FillChar(pRecord^, SizeOf(RStockPrice), 0);
pRecord^.stockName:= stockName;
pRecord^.price:= price;
pRecord^.fd := fd;
self.stockPriceList.add(pRecord);
Synchronize(@ShowPrice);
end;
procedure TMyThread.ShowPrice;
var
pRecord : PRStockPrice;
begin
pRecord := self.stockPriceList.LockList.First;
if pRecord <> nil then begin
FrmWarn.ShowPrice(pRecord^.stockname, pRecord^.price, pRecord^.fd);
self.stockPriceList.Remove(pRecord);
Dispose(pRecord);
end;
self.stockPriceList.UnlockList;
end;
procedure TMyThread.GetPrice(stockCode: string; var price: string; var fd: string);
var
httpClient: TIdHTTP;
responseStream: TStringStream;
strResponse: string;
index, i, startIndex, endIndex: integer;
prifix: string;
begin
httpClient := TIdHTTP.Create(nil);
responseStream := TStringStream.Create('');
try
if stockCode = '000001' then
begin
prifix := 'sh';
end
else
begin
case stockCode[1] of
'3', '0':
prifix := 'sz';
'6':
prifix := 'sh';
else
prifix := 'sz';
end;
end;
httpClient.Get('http://qt.gtimg.cn/q=' + prifix + stockCode, responseStream);
strResponse := responseStream.DataString;
// price
index := 0;
for i := 1 to 3 do
index := PosEx('~', strResponse, index + 1);
startIndex := index + 1;
endIndex := PosEx('~', strResponse, startIndex);
price := Copy(strResponse, startIndex, endIndex - startIndex);
DebugOut(price);
// fd
index := 0;
for i := 1 to 32 do
index := PosEx('~', strResponse, index + 1);
startIndex := index + 1;
endIndex := PosEx('~', strResponse, startIndex);
fd := Copy(strResponse, startIndex, endIndex - startIndex);
DebugOut(fd);
finally
httpClient.Free;
responseStream.Free;
end;
end;
procedure TMyThread.Execute;
var
fd, price: string;
j: integer;
code, Name, strNow: string;
stockInfoArr: TStockInfoArray;
begin
fd := '';
price := '';
while (not Terminated) do
begin
strNow := FormatDateTime('hh:nn:ss', Now);
if not (((strNow > '09:30:00') and (strNow < '11:31:00')) or
((strNow > '13:00:00') and (strNow < '15:01:00'))) then
begin
sleep(1000);
continue;
end;
stockInfoArr := g_stockManager.GetStockConfig();
for j := 0 to Length(stockInfoArr) - 1 do
begin
try
code := stockInfoArr[j].code;
Name := stockInfoArr[j].Name;
self.GetPrice(code, price, fd);
DebugOut(price);
DebugOut(fd);
if (length(stockInfoArr[j].HighWaterLevel) > 0) and
(StrToFloat(stockInfoArr[j].HighWaterLevel) > 0.1) and
(StrToFloat(price) > StrToFloat(stockInfoArr[j].HighWaterLevel)) then
begin
self.AddWarnMessage('^', Name + ':' + price, '考虑获利了结');
end
else if (length(stockInfoArr[j].LowWaterLevel) > 0) and
(StrToFloat(stockInfoArr[j].LowWaterLevel) > 0.1) and
(StrToFloat(price) < StrToFloat(stockInfoArr[j].LowWaterLevel)) then
begin
self.AddWarnMessage('WARN', Name + ':' + price, '及时止损!');
end;
if (stockInfoArr[j].UpRatio <> '') and
(StrToFloat(fd) > StrToFloat(stockInfoArr[j].UpRatio)) then
begin
self.AddWarnMessage('^', Name + ':' + fd + '%', '请关注');
end
else if (stockInfoArr[j].downRatio <> '') and
(StrToFloat(fd) < -StrToFloat(stockInfoArr[j].downRatio)) then
begin
self.AddWarnMessage('WARN', Name + ':' + fd + '%', '请关注');
end;
self.AddStockPriceRecord(Name, price, fd);
except
on E: Exception do
begin
//self.AddWarnMessage('ERROR', E.Message);
end;
end;
sleep(1000);
end;
end;
self.warnMessageList.free;
self.stockPriceList.free;
end;
constructor TMyThread.Create(CreateSuspended: boolean);
begin
FreeOnTerminate := True;
self.stockPriceList := TThreadList.Create;
self.warnMessageList := TThreadList.Create;
inherited Create(CreateSuspended);
end;
end.