补充资料:
如何获取所有股票历史数据:
如果要对股市进行分析,首先就要获取所有股票的历史数据,只有通过股票的历史数据,我们才能分析出股市的规律。
(以Think pad X13 锐龙版笔记本电脑,Windows 10 *** 作为例)
一、工具/原料
1、EXCEL2007或者以上版本,不能使用WPS
2、电脑1g内存1核处理器及以上配置
3、拥有较强逻辑分析能力以及少量智慧及以上的大脑一颗
方法/步骤:
第一步,获取股票代码,复制其中一部分到第一个工作表A4到A127,然后通过程序把每一个代码写入到不同的工作表A2位置,并对该工作表以该股票代码命名。程序如下:
Sub 工作表命名()
For i = 4 To 127
Sheets(i).Range("a2") = "'" &Sheets(1).Range("a" &i)
Next i
For i = 4 To Sheets.Count
Sheets(i).Name = Sheets(i).Range("a2").Value
Next
End Sub
第二步,获取股票历史数据。代码如下:
Private Function GetSource(sURL As String) As String
Dim oXHTTP As Object
Set oXHTTP = CreateObject("MSXML2.XMLHTTP")
oXHTTP.Open "GET", sURL, False
oXHTTP.Send
GetSource = oXHTTP.responsetext
Set oXHTTP = Nothing
End Function
Sub 历史数据()
Dim objXML As Object
Dim txtContent As String
Dim i As Integer
Dim strCode As String
Dim gp As String
Dim kaishihang
Dim arr, arr1, arr2, arr3, arr4, arr5, arr6, arr7, arr8, arr9, arr10, arr11
On Error Resume Next
EndRow = Range("a65536").End(xlUp).Row
startRow = 4
If startRow Range(Cells(startRow, 1), Cells(EndRow, 11)).Value = ""
Else
Exit Sub
End If
Set objXML = CreateObject("Microsoft.XMLHTTP")
gp = [A2]
For h = 1 To 4
For m = 1 To 4
kaishihang = [A65535].End(xlUp).Row
nian = Replace(Str(Year(Now) + 1 - h), " ", "")
jidu = Replace(Str(4 + 1 - m), " ", "")
With objXML
.Open "GET", "http://quotes.money.163.com/trade/lsjyj_" + gp + ".html?year=" + nian + "&season=" + jidu + "", False
.Send
If objXML.Status = 200 Then
txtContent = .responsetext
arr = Split(txtContent, "'>
For i = 1 To UBound(arr)
arr1 = Split(arr(i), "
arr2 = Split(arr1(1), Chr(60))
Cells(i + kaishihang, 2) = Mid(arr2(0), InStr(arr2(0), ">") + 1)
arr3 = Split(arr1(2), Chr(60))
Cells(i + kaishihang, 3) = Mid(arr3(0), InStr(arr3(0), ">") + 1)
arr4 = Split(arr1(3), Chr(60))
Cells(i + kaishihang, 4) = Mid(arr4(0), InStr(arr4(0), ">") + 1)
arr5 = Split(arr1(4), Chr(60))
Cells(i + kaishihang, 5) = Mid(arr5(0), InStr(arr5(0), ">") + 1)
arr6 = Split(arr1(5), Chr(60))
Cells(i + kaishihang, 6) = Mid(arr6(0), InStr(arr6(0), ">") + 1)
arr7 = Split(arr1(6), Chr(60))
Cells(i + kaishihang, 7) = Mid(arr7(0), InStr(arr7(0), ">") + 1)
arr8 = Split(arr1(7), Chr(60))
Cells(i + kaishihang, 8) = Mid(arr8(0), InStr(arr8(0), ">") + 1)
arr9 = Split(arr1(8), Chr(60))
Cells(i + kaishihang, 9) = Mid(arr9(0), InStr(arr9(0), ">") + 1)
arr10 = Split(arr1(9), Chr(60))
Cells(i + kaishihang, 10) = Mid(arr10(0), InStr(arr10(0), ">") + 1)
arr11 = Split(arr1(10), Chr(60))
Cells(i + kaishihang, 11) = Mid(arr11(0), InStr(arr11(0), ">") + 1)
Next i
End If
End With
Next m
Next h
Set objXML = Nothing
End Sub
第三步,获取上证历史数据,并获取所有股票的历史数据。程序如下:
Sub 所有股票历史数据获取()
Application.ScreenUpdating = False
Dim s As String, gp As String, nian As String, jidu As String, s1 As String
Dim arr, arr1, arr2, arr3, arr4, arr5, arr6, arr7, arr8, arr9
Dim i, h As Long
Dim kaishihang
Dim LastRow As Long, r As Long
On Error Resume Next
EndRow = Sheet2.Range("a65536").End(xlUp).Row
startRow = 4
If startRow Sheet2.Range(Sheet2.Cells(startRow, 1), Sheet2.Cells(EndRow, 9)).Value = ""
Else
Exit Sub
End If
For h = 1 To 5
For m = 1 To 4
kaishihang = Sheet2.[A65535].End(xlUp).Row
nian = Replace(Str(Year(Now) + 1 - h), " ", "")
jidu = Replace(Str(4 + 1 - m), " ", "")
s1 = "http://quotes.money.163.com/trade/lsjysj_zhishu_000001.html?year=" + nian + "&season=" + jidu + ""
s = GetSource(s1)
arr = Split(s, "'>
For i = 1 To UBound(arr)
arr1 = Split(arr(i), "
arr2 = Split(arr1(1), Chr(60))
Sheet2.Cells(i + kaishihang, 2) = Mid(arr2(0), InStr(arr2(0), ">") + 1)
arr3 = Split(arr1(2), Chr(60))
Sheet2.Cells(i + kaishihang, 3) = Mid(arr3(0), InStr(arr3(0), ">") + 1)
arr4 = Split(arr1(3), Chr(60))
Sheet2.Cells(i + kaishihang, 4) = Mid(arr4(0), InStr(arr4(0), ">") + 1)
arr5 = Split(arr1(4), Chr(60))
Sheet2.Cells(i + kaishihang, 5) = Mid(arr5(0), InStr(arr5(0), ">") + 1)
arr6 = Split(arr1(5), Chr(60))
Sheet2.Cells(i + kaishihang, 6) = Mid(arr6(0), InStr(arr6(0), ">") + 1)
arr7 = Split(arr1(6), Chr(60))
Sheet2.Cells(i + kaishihang, 7) = Mid(arr7(0), InStr(arr7(0), ">") + 1)
arr8 = Split(arr1(7), Chr(60))
Sheet2.Cells(i + kaishihang, 8) = Mid(arr8(0), InStr(arr8(0), ">") + 1)
arr9 = Split(arr1(8), Chr(60))
Sheet2.Cells(i + kaishihang, 9) = Mid(arr9(0), InStr(arr9(0), ">") + 1)
Next i
Next m
Next h
Application.ScreenUpdating = True
n = Worksheets.Count
For i = 4 To n
Worksheets(i).Activate
历史数据
Next
End Sub
第四步,保存工作簿,并对该工作簿命名为1,然后复制该工作簿27份,把所有股票代码写入每一个工作簿第一个工作表的A3到A127列。然后根据第一步对工作簿内的工作表命名,并在每一个工作表A2单元格内写入股票代码。
第五步,把这些工作簿存放到炒股文件夹下的数据库文件下。然后在炒股文件夹下新建一个工作簿,命名为“一键更新表格”,写入如下代码:
Sub 所有股票历史数据更新()
Dim wb As Workbook
For i = 1 To 27
Set wb = Workbooks.Open(ThisWorkbook.Path &"\数据库\" &i &"".xlsb"")
Application.Run ""'"" &wb.Path &""\"" &i &"".xlsb'!所有股票历史数据获取""
wb.Save
wb.Close
Next i
End sub
当启动这一段代码以后,程序会自动一个一个的打开对应的工作簿然后获取里面所有股票的历史数据,存放在对应的工作表当中。
第六步,对相应的程序添加按钮,这样,我们在启动对应程序时,只需要点一下对应的按钮就可以实现了。这样,我们通过多个工作簿就实现了获取所有股票历史数据的方法。而后面,我们只需要通过获取对应股票每一天的历史数据,我们就能够在任意一天很快速的能够拥有所有股票的历史数据了。
注意事项
1、在后续需要对这些数据进行分析,里面会加入很多计算,所以启动所有历史数据更新时,我们应该要关闭自动更新。
2、因为历史数据很大,所以这里只获取四年的数据,想要获取更多数据,可以更改程序里面 “For h = 1 To 5”的那个5即可。
3、历史数据的获取需要较长的时间,要提前做好准备。
4、如果你想要弄清楚用EXCEL建立炒股系统的整套方法,请看后续内容。" 回答于 2021-11-11 抢首赞 已踩 0 下一条回答 下一条
长安十二时辰你看懂了吗
入乡随俗,一起探访唐朝衣食住行大小事
16个问题231,534人浏览
— 为你推荐更多精彩内容 — 正在加载 加载失败 点击重新加载微信
微博
QQ空间
答案纠错
举报
取消 赞赏答主 5 10 50 100 200已赞赏0财富值
合计:0 财富值
登录后赞赏 选择举报类型 侵犯版权 色情低俗 涉嫌违法犯罪 时政信息不实 垃圾广告 低质灌水 工作人员会在48小时内处理,处理结果请关注系统通知,感谢您对百度知道的支持。 确定 void function(a,b,c,d,e,f){function g(b){a.attachEvent?a.attachEvent("onload",b,!1):a.addEventListener&&a.addEventListener("load",b)}function h(a,c,d){d=d||15var e=new Datee.setTime((new Date).getTime()+1e3*d),b.cookie=a+"="+escape(c)+"path=/expires="+e.toGMTString()}function i(a){var c=b.cookie.match(new RegExp("(^| )"+a+"=([^]*)(|$)"))return null!=c?unescape(c[2]):null}function j(){var a=i("PMS_JT")if(a){h("PMS_JT","",-1)try{a=a.match(/{["']s["']:(\d+),["']r["']:["']([\s\S]+)["']}/),a=a&&a[1]&&a[2]?{s:parseInt(a[1]),r:a[2]}:{}}catch(c){a={}}a.r&&b.referrer.replace(/#.*/,"")!=a.r||alog("speed.set","wt",a.s)}}if(a.alogObjectConfig){var k=a.alogObjectConfig.sample,l=a.alogObjectConfig.randd="https:"===a.location.protocol?"https://fex.bdstatic.com"+d:"http://fex.bdstatic.com"+d,k&&l&&l>k||(g(function(){alog("speed.set","lt",+new Date),e=b.createElement(c),e.async=!0,e.src=d+"?v="+~(new Date/864e5)+~(new Date/864e5),f=b.getElementsByTagName(c)[0],f.parentNode.insertBefore(e,f)}),j())}}(window,document,"script","/hunter/alog/dp.mobile.min.js") window.tt = 1679946931欢迎分享,转载请注明来源:内存溢出
评论列表(0条)