【目标】

使用vba程序获取新浪股票api的实时行情数据、K线,以及macd等技术指标数据。

【新浪api介绍】

  1.  获取实时行情数据接口,http://hq.sinajs.cn/list=sz002208,sh601318
  2. 对于股票的K线图,日线图等的获取可以通过请求:
    http://image.sinajs.cn/…./…/.gif
    此URL获取,其中
    代表股票代码,详见如下:
    查看日K线图: http://image.sinajs.cn/newchart/daily/n/sh601318.gif
    分时线的查询: http://image.sinajs.cn/newchart/min/n/sh601318.gif
    日K线查询: http://image.sinajs.cn/newchart/daily/n/sh601318.gif
    周K线查询: http://image.sinajs.cn/newchart/weekly/n/sh601318.gif
    月K线查询: http://image.sinajs.cn/newchart/monthly/n/sh601318.gif

  3. 对于股票的macd等技术指标图,可以通过请求:
    http://image.sinajs.cn/…./…/.gif
    此URL获取,其中
    代表股票代码,详见如下:
    macd: http://image.sinajs.cn/newchart/macd/sh601318.gif
    kdj: http://image.sinajs.cn/newchart/kdj/sh601318.gif
    rsi: http://image.sinajs.cn/newchart/rsi/sh601318.gif
    bias: http://image.sinajs.cn/newchart/bias/sh601318.gif
    等等,其他雷同

【示例代码1.查询实时行情】

  获取股票名称、现价、涨幅、涨跌额、昨收、今开、最高、最低、振幅、总手、总额等,编写vba程序,并使用按钮宏关联此vba。

Public indexType As String
Public caption_Name As String
Public stockCode As String
Public url As String
Public i, j As Integer
Public Const stockCodeColumn = 1                    '股票代码所在列
Public Const startRow = 3                           '开始行1表示是从第一行开始的数据,否则表示前面有注释类信息
Public Const arrSrartColumn = 3                     '排列开始的列
Public Const arrEndColumn = arrSrartColumn + 15     '排列结束的列
Public Const extSrartColumn = 17                    '扩展信息开始的列
Public Const url1 = "http://image.sinajs.cn/newchart/"
Public Const url2 = "http://image2.sinajs.cn/newchart/"Sub GetData_click()Dim originalData As String, arrAllData() As String, recordData() As String, recordHeader() As String
Dim stocks As StringEndRow = Range("A65536").End(xlUp).Row'关闭屏幕刷新
Application.ScreenUpdating = False'跳过错误
On Error Resume Next'合并股票代码
For i = startRow To EndRowIf i = startRow Thenstocks = LCase(Cells(startRow, 1))Elsestocks = stocks + "," + LCase(Cells(i, 1))End If
Next istocks = stocks + "," + "sh000001" + "," + "sz399001"'取得实时数据
originalData = GetHttp("http://hq.sinajs.cn/list=" + stocks)arrAllData = Split(originalData, ";")iCount = UBound(arrAllData)'排列所取得的数据
For i = startRow To iCount + startRow - 1 - 2KK = Cells(i, arrSrartColumn + 2).ValuerecordData = Split(arrAllData(i - startRow), ",")If UBound(recordData) <= 1 ThenCells(i, stockCodeColumn + 1) = "无此代码"ElserecordHeader = Split(recordData(0), Chr(34))Cells(i, stockCodeColumn + 1) = recordHeader(1)       '股票名称End IfCells(i, arrSrartColumn + 0) = recordData(3)     '现价Cells(i, arrSrartColumn + 1) = Round((recordData(3) - recordData(2)) / recordData(2) * 100, 2) & "%"    '涨跌幅Cells(i, arrSrartColumn + 2) = Round(recordData(3) - recordData(2), 3)    '涨跌Cells(i, arrSrartColumn + 3) = recordData(2)     '昨收Cells(i, arrSrartColumn + 4) = recordData(1)     '今开Cells(i, arrSrartColumn + 5) = recordData(4)     '最高Cells(i, arrSrartColumn + 6) = recordData(5)     '最低Cells(i, arrSrartColumn + 7) = Round((recordData(4) - recordData(5)) / recordData(2) * 100, 2) & "%"     '振幅Cells(i, arrSrartColumn + 8) = Round(recordData(8) / 100, 0)             '成交量由股变为手Cells(i, arrSrartColumn + 9) = Round(recordData(9) / 100000000, 2)       '成交额由元变亿元If recordData(3) = 0 ThenCells(i, arrSrartColumn + 1) = "0%"                                      '现价为0时涨跌幅Cells(i, arrSrartColumn + 2) = "0"                                       '现价为0时涨跌Cells(i, arrSrartColumn + 7) = "0%"                                      '现价为0时振幅End IfNext i'打开屏幕刷新
Application.ScreenUpdating = TrueEnd Sub'清除数据
Sub Clean_ArrArea()
On Error Resume Next
EndRow = Range("A65536").End(xlUp).RowIf startRow <= EndRow ThenRange(Cells(startRow, arrSrartColumn - 1), Cells(EndRow, arrEndColumn + 3)).Value = ""
ElseExit Sub
End IfEnd Sub

【运行效果1.】

【示例代码2.查询日K线】

 获取日K线的gif图片,其他5分钟、15分钟、macd、kdj等逻辑都是一样的,展示图片而已。

Sub A01_mink5()MyRow = ActiveCell.RowstockCode = LCase(Cells(MyRow, stockCodeColumn).Value)url = url1caption_Name = "5分钟K线"indexType = "mink5"If Len(stockCode) <> 8 ThenMsgBox "不是有效的股票代码。", 16, "错误信息"ElseGet_Photo.ShowEnd IfEnd SubSub A02_mink15()url = url1MyRow = ActiveCell.RowstockCode = LCase(Cells(MyRow, stockCodeColumn).Value)caption_Name = "15分钟K线"indexType = "mink15"If Len(stockCode) <> 8 ThenMsgBox "不是有效的股票代码。", 16, "错误信息"ElseGet_Photo.ShowEnd IfEnd SubSub A03_mink30()url = url1MyRow = ActiveCell.RowstockCode = LCase(Cells(MyRow, stockCodeColumn).Value)caption_Name = "30分钟K线"indexType = "mink30"If Len(stockCode) <> 8 ThenMsgBox "不是有效的股票代码。", 16, "错误信息"ElseGet_Photo.ShowEnd IfEnd SubSub A04_daily()url = url1MyRow = ActiveCell.RowstockCode = LCase(Cells(MyRow, stockCodeColumn).Value)caption_Name = "日K线"indexType = "daily"If Len(stockCode) <> 8 ThenMsgBox "不是有效的股票代码。", 16, "错误信息"ElseGet_Photo.ShowEnd IfEnd SubSub A05_weekly()url = url1MyRow = ActiveCell.RowstockCode = LCase(Cells(MyRow, stockCodeColumn).Value)caption_Name = "周K线"indexType = "weekly"If Len(stockCode) <> 8 ThenMsgBox "不是有效的股票代码。", 16, "错误信息"ElseGet_Photo.ShowEnd IfEnd SubSub A06_monthly()url = url1MyRow = ActiveCell.RowstockCode = LCase(Cells(MyRow, stockCodeColumn).Value)caption_Name = "月K线"indexType = "monthly"If Len(stockCode) <> 8 ThenMsgBox "不是有效的股票代码。", 16, "错误信息"ElseGet_Photo.ShowEnd IfEnd SubSub A07_min()url = url1MyRow = ActiveCell.RowstockCode = LCase(Cells(MyRow, stockCodeColumn).Value)caption_Name = "分时线"indexType = "min"If Len(stockCode) <> 8 ThenMsgBox "不是有效的股票代码。", 16, "错误信息"ElseGet_Photo.ShowEnd IfEnd SubSub B01_macd()url = url1MyRow = ActiveCell.RowstockCode = LCase(Cells(MyRow, stockCodeColumn).Value)caption_Name = "MACD"indexType = "macd"If Len(stockCode) <> 8 ThenMsgBox "不是有效的股票代码。", 16, "错误信息"ElseGet_Photo.ShowEnd IfEnd SubSub B02_trix()url = url1MyRow = ActiveCell.RowstockCode = LCase(Cells(MyRow, stockCodeColumn).Value)caption_Name = "TRIX"indexType = "trix"If Len(stockCode) <> 8 ThenMsgBox "不是有效的股票代码。", 16, "错误信息"ElseGet_Photo.ShowEnd IfEnd SubSub B03_dmi()url = url1MyRow = ActiveCell.RowstockCode = LCase(Cells(MyRow, stockCodeColumn).Value)caption_Name = "DMI"indexType = "dmi"If Len(stockCode) <> 8 ThenMsgBox "不是有效的股票代码。", 16, "错误信息"ElseGet_Photo.ShowEnd IfEnd SubSub B04_expma()url = url1MyRow = ActiveCell.RowstockCode = LCase(Cells(MyRow, stockCodeColumn).Value)caption_Name = "EXPMA"indexType = "expma"If Len(stockCode) <> 8 ThenMsgBox "不是有效的股票代码。", 16, "错误信息"ElseGet_Photo.ShowEnd IfEnd SubSub B05_brar()url = url1MyRow = ActiveCell.RowstockCode = LCase(Cells(MyRow, stockCodeColumn).Value)caption_Name = "BRAR"indexType = "brar"If Len(stockCode) <> 8 ThenMsgBox "不是有效的股票代码。", 16, "错误信息"ElseGet_Photo.ShowEnd IfEnd SubSub B06_cr()url = url1MyRow = ActiveCell.RowstockCode = LCase(Cells(MyRow, stockCodeColumn).Value)caption_Name = "CR"indexType = "cr"If Len(stockCode) <> 8 ThenMsgBox "不是有效的股票代码。", 16, "错误信息"ElseGet_Photo.ShowEnd IfEnd SubSub B07_vr()url = url1MyRow = ActiveCell.RowstockCode = LCase(Cells(MyRow, stockCodeColumn).Value)caption_Name = "VR"indexType = "vr"If Len(stockCode) <> 8 ThenMsgBox "不是有效的股票代码。", 16, "错误信息"ElseGet_Photo.ShowEnd IfEnd SubSub B08_bias()url = url1MyRow = ActiveCell.RowstockCode = LCase(Cells(MyRow, stockCodeColumn).Value)caption_Name = "BIAS"indexType = "bias"If Len(stockCode) <> 8 ThenMsgBox "不是有效的股票代码。", 16, "错误信息"ElseGet_Photo.ShowEnd IfEnd SubSub B09_psy()url = url1MyRow = ActiveCell.RowstockCode = LCase(Cells(MyRow, stockCodeColumn).Value)caption_Name = "PSY"indexType = "psy"If Len(stockCode) <> 8 ThenMsgBox "不是有效的股票代码。", 16, "错误信息"ElseGet_Photo.ShowEnd IfEnd SubSub B10_obv()url = url1MyRow = ActiveCell.RowstockCode = LCase(Cells(MyRow, stockCodeColumn).Value)caption_Name = "OBV"indexType = "obv"If Len(stockCode) <> 8 ThenMsgBox "不是有效的股票代码。", 16, "错误信息"ElseGet_Photo.ShowEnd IfEnd SubSub B11_boll()url = url1MyRow = ActiveCell.RowstockCode = LCase(Cells(MyRow, stockCodeColumn).Value)caption_Name = "BOLL"indexType = "boll"If Len(stockCode) <> 8 ThenMsgBox "不是有效的股票代码。", 16, "错误信息"ElseGet_Photo.ShowEnd IfEnd SubSub B12_mike()url = url1MyRow = ActiveCell.RowstockCode = LCase(Cells(MyRow, stockCodeColumn).Value)caption_Name = "MIKE"indexType = "mike"If Len(stockCode) <> 8 ThenMsgBox "不是有效的股票代码。", 16, "错误信息"ElseGet_Photo.ShowEnd IfEnd SubSub B13_asi()url = url1MyRow = ActiveCell.RowstockCode = LCase(Cells(MyRow, stockCodeColumn).Value)caption_Name = "ASI"indexType = "asi"If Len(stockCode) <> 8 ThenMsgBox "不是有效的股票代码。", 16, "错误信息"ElseGet_Photo.ShowEnd IfEnd SubSub B14_emv()url = url1MyRow = ActiveCell.RowstockCode = LCase(Cells(MyRow, stockCodeColumn).Value)caption_Name = "EMV"indexType = "emv"If Len(stockCode) <> 8 ThenMsgBox "不是有效的股票代码。", 16, "错误信息"ElseGet_Photo.ShowEnd IfEnd SubSub B15_cci()url = url1MyRow = ActiveCell.RowstockCode = LCase(Cells(MyRow, stockCodeColumn).Value)caption_Name = "CCI"indexType = "cci"If Len(stockCode) <> 8 ThenMsgBox "不是有效的股票代码。", 16, "错误信息"ElseGet_Photo.ShowEnd IfEnd SubSub B16_rsi()url = url1MyRow = ActiveCell.RowstockCode = LCase(Cells(MyRow, stockCodeColumn).Value)caption_Name = "RSI"indexType = "rsi"If Len(stockCode) <> 8 ThenMsgBox "不是有效的股票代码。", 16, "错误信息"ElseGet_Photo.ShowEnd IfEnd SubSub B17_wr()url = url1MyRow = ActiveCell.RowstockCode = LCase(Cells(MyRow, stockCodeColumn).Value)caption_Name = "WR"indexType = "wr"If Len(stockCode) <> 8 ThenMsgBox "不是有效的股票代码。", 16, "错误信息"ElseGet_Photo.ShowEnd IfEnd SubSub B18_kdj()url = url1MyRow = ActiveCell.RowstockCode = LCase(Cells(MyRow, stockCodeColumn).Value)caption_Name = "KDJ"indexType = "kdj"If Len(stockCode) <> 8 ThenMsgBox "不是有效的股票代码。", 16, "错误信息"ElseGet_Photo.ShowEnd IfEnd SubSub B19_roc()url = url1MyRow = ActiveCell.RowstockCode = LCase(Cells(MyRow, stockCodeColumn).Value)caption_Name = "ROC"indexType = "roc"If Len(stockCode) <> 8 ThenMsgBox "不是有效的股票代码。", 16, "错误信息"ElseGet_Photo.ShowEnd IfEnd SubSub B20_dma()url = url1MyRow = ActiveCell.RowstockCode = LCase(Cells(MyRow, stockCodeColumn).Value)caption_Name = "DMA"indexType = "dma"If Len(stockCode) <> 8 ThenMsgBox "不是有效的股票代码。", 16, "错误信息"ElseGet_Photo.ShowEnd IfEnd Sub

【运行效果】

【量化投资】02.数据接口初探——excel读取新浪股票api数据相关推荐

  1. 小工具 - 新浪股票API数据实时展示插件

    效果 应用之前的数据: 应用之后的展示: 主要功能 [名][当前价格][当前涨幅]实时的展示在浏览器的标签上(嘿嘿) 页面上增加详细的开盘,收盘,最高,最低等价格 页面上增加搜索跳转方便查看其他股票 ...

  2. php股票价格实时刷新,使用ajax技术无刷新动态调用新浪股票实时数据

    由于最近网速慢的缘故,查看股票信息时网页老是打不开.这几天一直在研究ajax,于是用jquery自己做了一个自动读取新浪股票实时数据的页面 新浪的财金频道一直感觉做得很好.但由于最近网速慢的缘故,查看 ...

  3. 新浪股票行情数据接口有什么作用?

    通过新浪股票行情数据接口可以让投资者在实际交易当中能够更加精准的洞悉盘口变化.该接口可以说是目前最好用的免费股票行情数据接口了,虽然一直并未公开,但暂时使用良好.大家用浏览器访问新浪的股票行情数据接口 ...

  4. 股票详情接口 html5,新浪股票API接口

    新浪股票API处理 如果想实时获得某支股票的实时数据,可访问:http://hq.sinajs.cn/list=sz002307,sh600928 其中sz002307,sh600928为股票代码加沪 ...

  5. 获取同花顺数据接口_如何获取深沪股票 LEVEL2 数据接口?

    根据上交所2017年5月公告的文件显示,拿到L1普通行情数据授权的有112家公司,L2授权的有25家,我这里只贴出L2数据的厂家,如果想看L1的授权公司或者深交所的授权情况可以自己去查,太多了就不贴了 ...

  6. php 获取新浪股票行情数据,从新浪获取股票历史数据

    转自:http://blog.163.com/fluxray_sensor/blog/static/2965101520085213574929/ 这几天在网上找股票的历史数据想研究研究,最后找到一个 ...

  7. php 获取新浪股票行情数据,python 抓取新浪财经股票数据

    新浪并未提供API,但我们可以通过抓包来获取实时或历史行情数据. 实时行情 比如我们可以通过浏览器访问: ?== 来获取证券代码为的实时行情数据,可以看到内容为: ="华泰证券,,,,20. ...

  8. android股票sdk,新浪股票接口AndroidSDK

    昨天想到一个点子,需要访问股票行情.于是在网上搜了一下免费的股市行情的接口.发现新浪股票的数据接口比较稳定,于是就用它了. 网上对于新浪股票的数据接口介绍比较详细,并且实现也很简单,所以花了一下午就基 ...

  9. 新浪股票接口AndroidSDK

    昨天想到一个点子,需要访问股票行情.于是在网上搜了一下免费的股市行情的接口.发现新浪股票的数据接口比较稳定,于是就用它了. 网上对于新浪股票的数据接口介绍比较详细,并且实现也很简单,所以花了一下午就基 ...

最新文章

  1. tp5实现Redis的简单使用
  2. Netsharp下微信菜单以及OAuth
  3. #error使用分析
  4. 转载:如何在 SQL Server 中使用配置选项调整内存使用量
  5. OSChina 周六乱弹 —— 有人骂你神经病怎么办?
  6. Mysql数据库安全管理配置
  7. 在package.json里面的script设置环境变量,区分开发及生产环境。注意mac与windows的设置方式不一样...
  8. 前端学习(2968):实现路由跳转的两种方式
  9. 中国科技大学计算机学院叶辉,中国科技大学计算机科学与技术学院导师教师师资介绍简介-黄文超...
  10. virmach主机购买和使用
  11. setState如何知道该做什么?
  12. 浙江大华 研发类试题
  13. python 日志框架_按日期打印Python的Tornado框架中的日志的方法
  14. OpenGLGamma校正
  15. HttpPrinter共享打印机 网络打印
  16. # 关于Dran,Cran,CloundRan ,的区别
  17. 我的叔叔精通计算机英语翻译,人教小学英语精通版 三年级下册Unit3 课文翻译...
  18. GMARK设计奖申报
  19. html修改文字颜色代码
  20. html自动刷新倒计时,javascript – 倒计时结束时刷新页面

热门文章

  1. wincc画面c语言脚本在哪里,wincc画面切换C脚本
  2. 学习分享:Windows下大型文件 CRC 校验
  3. docker中文管理神器Portainer一键安装
  4. 所有浏览器都有mozilla的原因
  5. 机器翻译:引入注意力机制的Encoder-Decoder深度神经网络训练实战中英文互译(完结篇)
  6. 【面试题记录】在mysql中查询10万条数据找到第50000到51000条数据,你会怎么做?
  7. java定义数组_java定义数组的方法有哪些
  8. 熊猫猪新系统測试之四:Ubuntu 14.04
  9. Paddle深度学习快速入门
  10. JavaScript实现倒计时案例