-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathfGetUSstock.m
More file actions
515 lines (405 loc) · 18.8 KB
/
fGetUSstock.m
File metadata and controls
515 lines (405 loc) · 18.8 KB
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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
classdef fGetUSstock < handle
%% fGetUSstock
% by LiYang_faruto
% Email:farutoliyang@foxmail.com
% 2015/6/1
%% properties
properties
Exchange = 'NASDAQ';
Code = 'AAPL';
StartDate = 'All';
EndDate = datestr(today,'yyyymmdd');
isSave = 0;
isPlot = 0;
isTicToc = 0;
% sina ifeng
ListSource = 'sina';
end
%% properties(SetAccess = private, GetAccess = public)
properties(SetAccess = private, GetAccess = public)
end
%% properties(Access = protected)
properties(Access = protected)
end
%% properties(Access = private)
properties(Access = private)
end
%% methods
methods
%% fGetUSstock()
function obj = fGetUSstock( varargin )
end
%% GetList()
function OutputData = GetList(obj)
OutputData = [];
% %===输入参数检查 开始===
Flag = ParaCheck(obj);
if 0 == Flag
str = ['请检查输入参数是否正确!'];
disp(str)
return;
end
% %===输入参数检查 完毕===
if strcmpi(obj.ListSource, 'sina')
URL = 'http://vip.stock.finance.sina.com.cn/usstock/ustotal.php';
URLchar = urlread(URL,'Charset','gb2312');
URLString = java.lang.String(URLchar);
expr = ['<a href="http://stock.finance.sina.com.cn/usstock/quotes/','.*?',...
'rel="suggest" title="','.*?>','(.*?)','</a>'];
tData = regexpi(URLchar, expr,'tokens');
Len = length(tData);
OutputData = cell(Len,2);
for i = 1:Len
tD = tData{1,i};
while iscell(tD)
tD = tD{1,1};
end
ind = find( tD == '(' );
tName = tD(1:ind-1);
tCode = tD(ind+1:end-1);
OutputData(i,:) = [{tCode},{tName}];
end
end
if strcmpi(obj.ListSource, 'ifeng')
URL_Pattern = 'http://app.finance.ifeng.com/list/usstock.php?first=all&page=%s';
Page = 1;
tURL = sprintf(URL_Pattern, num2str(Page));
[TableTotalNum,TableCell] = GetTableFromWeb(tURL, 'utf-8');
if iscell(TableCell)
TableCell = TableCell{1,1};
end
Len = size(TableCell,1);
while Page <= 500
if ~isempty(TableCell) && Len>1
temp = TableCell(2:end,[2,1]);
OutputData = [OutputData;temp];
end
Page = Page+1;
tURL = sprintf(URL_Pattern, num2str(Page));
if mod(Page,5) == 0
% % 频繁访问的话,会获取数据失败,设置N秒暂停
pause(5);
end
[TableTotalNum,TableCell] = GetTableFromWeb(tURL, 'utf-8');
if iscell(TableCell)
TableCell = TableCell{1,1};
end
Len = size(TableCell,1);
if isempty(TableCell)
Page = Page - 1;
pause(30);
end
end
end
% % % Save
if 1 == obj.isSave && ~isempty(OutputData)
tOutputData = OutputData;
% for i = 1:Len
%
% tCode = OutputData{i,1};
% tCode = ['''',tCode];
% tOutputData{i,1} = tCode;
% end
FolderStr = ['./DataBaseTemp/USstock'];
if ~isdir( FolderStr )
mkdir( FolderStr );
end
FileName = ['USstockList_',obj.ListSource];
FileString = [FolderStr,'/',FileName,'.xlsx'];
FileExist = 0;
if exist(FileString, 'file') == 2
FileExist = 1;
end
if 1 == FileExist
FileString = [FolderStr,'/',FileName,'(1)','.xlsx'];
end
Headers = {'代码','名称'};
FileName = FileString;
ColNamesCell = Headers;
Data = tOutputData;
[Status, Message] = SaveData2File(Data, FileName, ColNamesCell);
str = ['USstockList数据已保存至',FileString];
disp(str);
end
end
%% GetHistQuote()
function [OutputData,Headers] = GetHistQuote(obj)
% % 新浪的数据有的最低价为0 需要注意!
OutputData = [];
Headers = {'Date', 'Open', 'High', 'Low', 'Close', 'Volume', 'AdjClose'};
% %===输入参数检查 开始===
Flag = ParaCheck(obj);
if 0 == Flag
str = ['请检查输入参数是否正确!'];
disp(str)
return;
end
% %===输入参数检查 完毕===
if obj.isTicToc == 1
tic;
end
if strcmpi(obj.StartDate, 'all')
obj.StartDate = '20000101';
end
if strcmpi(obj.EndDate, 'all')
obj.StartDate = datestr(today,'yyyymmdd');
end
tickers = obj.Code;
dateFormat = 'dd/mm/yyyy';
startDate = datestr( datenum(obj.StartDate,'yyyymmdd'),dateFormat );
endDate = datestr( datenum(obj.EndDate,'yyyymmdd'),dateFormat );
YHdata = obj.getYahooDailyData(tickers, startDate, endDate, dateFormat);
OutputData = table2array(YHdata);
% % %数据处理,将为0的数据替换为NaN
OutputData(OutputData==0) = NaN;
if obj.isTicToc == 1
str = ['数据获取完毕!共耗时:'];
disp(str);
toc;
end
% % % Save
if 1 == obj.isSave && ~isempty(OutputData)
if obj.isTicToc == 1
tic;
end
tOutputData = OutputData;
FolderStr = ['./DataBaseTemp/USstock'];
if ~isdir( FolderStr )
mkdir( FolderStr );
end
if strcmpi(obj.StartDate, 'all') || strcmpi(obj.EndDate, 'all')
FileName = [obj.Code,'.US_All'];
else
tdatefrom = obj.StartDate;
tdateto = obj.EndDate;
FileName = [obj.Code,'.US_',tdatefrom,'-',tdateto];
end
FileString = [FolderStr,'/',FileName,'.csv'];
FileExist = 0;
if exist(FileString, 'file') == 2
FileExist = 1;
end
if 1 == FileExist
FileString = [FolderStr,'/',FileName,'(1)','.csv'];
end
FileName = FileString;
ColNamesCell = Headers;
Data = tOutputData;
[Status, Message] = SaveData2File(Data, FileName, ColNamesCell);
str = [obj.Code,'.US数据已保存至',FileString];
disp(str);
if obj.isTicToc == 1
str = ['数据保存完毕!共耗时:'];
disp(str);
toc;
end
end
% % % Plot
if 1 == obj.isPlot && ~isempty(OutputData)
if obj.isTicToc == 1
tic;
end
scrsz = get(0,'ScreenSize');
figure('Position',[scrsz(3)*1/4 scrsz(4)*1/6 scrsz(3)*4/5 scrsz(4)]*3/4);
subplot(3,1,1:2);
FAdjFactor = OutputData(:,5)./OutputData(:,7);
tOutputData = OutputData;
for i = 2:5
tOutputData(:,i) = tOutputData(:,i)./FAdjFactor;
end
OHLC = tOutputData(:,2:5);
KplotNew(OHLC);
% 横轴时间轴设定
Dates = OutputData(:,1);
Style = 0;
XTRot = [];
LabelSet(gca, Dates, [], [], Style,XTRot);
str = [obj.Code,'.US(前复权) K线图'];
title(str,'FontWeight','Bold');
subplot(3,1,3);
Vol = OutputData(:,6);
bar(Vol);
str = ['成交量'];
ylabel(str,'FontWeight','Bold');
Dates = OutputData(:,1);
Style = 0;
XTRot = [];
LabelSet(gca, Dates, [], [], Style,XTRot);
xlim([0,1+length(Dates)]);
if obj.isTicToc == 1
str = ['数据展示完毕!共耗时:'];
disp(str);
toc;
end
end
end
%% getYahooDailyData()
function data = getYahooDailyData(obj,tickers, startDate, endDate, dateFormat)
% GETYAHOODAILYDATA scrapes the Yahoo! Finance website for one or more
% ticker symbols and returns OHLC, volume, and adjusted close information
% for the date range specified.
%
% Inputs:
% tickers: Either a character string or cell array of strings listing one
% or more (Yahoo!-formatted) ticker symbols
% startDate, endDate: Either MATLAB datenums or character strings listing
% the date range desired (inclusive)
% dateFormat (optional): If startDate and endDate are strings, then this
% indicates their format. (See the 'formatIn' argument of DAMENUM.)
%
% Outputs:
% data: A structure containing fields for each ticker requested. The
% fields are named by genvarname(ticker). Each field is an N-by-7 table
% listing the dates, open, high, low, close, volume, and adjusted close
% for all N of the trading days between startDate and endDate and in
% increasing order of date. NOTE: If the version of MATLAB is less than
% 8.2 (R2013b), then data returns a structure of dataset arrays
% (Statistics Toolbox required).
%
% EXAMPLE:
% data = getYahooDailyData({'MSFT', 'ML.PA'}, ...
% '01/01/2010', '01/01/2013', 'dd/mm/yyyy');
%% 1. Input parsing
% Check to see if a single ticker was provided as a string; if so, make it
% a cell array to better fit the later logic.
if ischar(tickers)
tickers = {tickers};
end
% If dateFormat is provided, use it. Otherwise, assume datenums were
% given.
if nargin == 5
startDate = datenum(startDate, dateFormat);
endDate = datenum( endDate, dateFormat);
end
url1 = 'http://ichart.finance.yahoo.com/table.csv?s=';
[sY, sM, sD] = datevec(startDate);
[eY, eM, eD] = datevec(endDate);
url2 = ['&a=' num2str(sM-1, '%02u') ...
'&b=' num2str(sD) ...
'&c=' num2str(sY) ...
'&d=' num2str(eM-1, '%02u') ...
'&e=' num2str(eD) ...
'&f=' num2str(eY) '&g=d&ignore=.csv'];
% Determine if we're using tables or datasets:
isBeforeR2013b = verLessThan('matlab', '8.2');
%% 2. Load Data in a loop
for iTicker = 1:length(tickers)
try
str = urlread([url1 tickers{iTicker} url2]);
catch
% Special behaviour if str cannot be found: this means that no
% price info was returned. Error and say which asset is invalid:
error('getYahooDailyData:invalidTicker', ...
['No data returned for ticker ''' tickers{iTicker} ...
'''. Is this a valid symbol? Do you have an internet connection?'])
end
c = textscan(str, '%s%f%f%f%f%f%f', 'HeaderLines', 1, 'Delimiter', ',');
if isBeforeR2013b
ds = dataset(c{1}, c{2}, c{3}, c{4}, c{5}, c{6}, c{7}, 'VarNames', ...
{'Date', 'Open', 'High', 'Low', 'Close', 'Volume', 'AdjClose'});
else
ds = table(c{1}, c{2}, c{3}, c{4}, c{5}, c{6}, c{7}, 'VariableNames', ...
{'Date', 'Open', 'High', 'Low', 'Close', 'Volume', 'AdjClose'});
end
% ds.Date = datenum(ds.Date, 'yyyy-mm-dd');
temp = datenum(ds.Date, 'yyyy-mm-dd');
temp = str2num(datestr(temp,'yyyymmdd'));
ds.Date = temp;
ds = flipud(ds);
% data.(genvarname(tickers{iTicker})) = ds;
data = ds;
end
end
%% GetProfile()
function [OutputData] = GetProfile(obj)
OutputData = [];
URL_Pattern = ['http://stock.finance.sina.com.cn/hkstock/info/%s.html'];
tSymbol = obj.Code;
tURL = sprintf(URL_Pattern, tSymbol);
% Content = urlread(tURL);
[TableTotalNum,TableCell] = GetTableFromWeb(tURL,'gbk');
if TableTotalNum>=1
OutputData = TableCell{1,1};
else
disp('获取数据失败,请检查输入参数!');
return;
end
% % % Save
if 1 == obj.isSave && ~isempty(OutputData)
tOutputData = OutputData;
FolderStr = ['./DataBaseTemp/HKstock'];
if ~isdir( FolderStr )
mkdir( FolderStr );
end
FileName = [obj.Code,'.HK_Profile'];
FileString = [FolderStr,'/',FileName,'.xlsx'];
FileExist = 0;
if exist(FileString, 'file') == 2
FileExist = 1;
end
if 1 == FileExist
FileString = [FolderStr,'/',FileName,'(1)','.xlsx'];
end
FileName = FileString;
ColNamesCell = [];
Data = tOutputData;
[Status, Message] = SaveData2File(Data, FileName, ColNamesCell);
str = [obj.Code,'.HK公司资料数据已保存至',FileString];
disp(str);
end
end
%% GetDividends()
function [OutputData,Headers] = GetDividends(obj)
OutputData = [];
Headers = {'Ex/Eff Date(除息日)','Type(类型)','Cash Amount(现金金额)','Declaration Date(宣布日)','Record Date(登记日)','Payment Date(支付日)'};
URL_Pattern = ['http://www.nasdaq.com/symbol/%s/dividend-history'];
tSymbol = obj.Code;
tURL = sprintf(URL_Pattern, tSymbol);
[TableTotalNum,TableCell] = GetTableFromWeb(tURL,'utf-8');
if TableTotalNum>=5 && size(TableCell{5,1},2)>1
Ind = 5;
OutputData = TableCell{Ind,1};
else
disp('获取数据失败或数据不存在,请检查输入参数!');
return;
end
% % % Save
if 1 == obj.isSave && ~isempty(OutputData)
tOutputData = OutputData;
FolderStr = ['./DataBaseTemp/USstock'];
if ~isdir( FolderStr )
mkdir( FolderStr );
end
FileName = [obj.Code,'.US_Dividends'];
FileString = [FolderStr,'/',FileName,'.csv'];
FileExist = 0;
if exist(FileString, 'file') == 2
FileExist = 1;
end
if 1 == FileExist
FileString = [FolderStr,'/',FileName,'(1)','.csv'];
end
FileName = FileString;
ColNamesCell = [];
Data = tOutputData;
[Status, Message] = SaveData2File(Data, FileName, ColNamesCell);
str = [obj.Code,'.US分红派息数据已保存至',FileString];
disp(str);
end
end
%% 输入参数检查函数
function Flag = ParaCheck(obj, varargin )
Flag = 1;
% %===输入参数检查 开始===
% checkflag = ismember( lower(obj.DownUpSampling),lower(obj.DownUpSampling_ParaList) );
% if checkflag ~= 1
% str = ['DownUpSampling参数输入错误请检查!可选的参数列表为(大小写都行):'];
% disp(str);
% ParaList = obj.DownUpSampling_ParaList
% Flag = 0;
% return;
% end
% %===输入参数检查 完毕===
end
end
end