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

【目标】

           使用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 String

EndRow = Range("A65536").End(xlUp).Row

'关闭屏幕刷新
Application.ScreenUpdating = False

'跳过错误
On Error Resume Next

'合并股票代码
For i = startRow To EndRow
    If i = startRow Then
        stocks = LCase(Cells(startRow, 1))
    Else
        stocks = stocks + "," + LCase(Cells(i, 1))
    End If
Next i

stocks = stocks + "," + "sh000001" + "," + "sz399001"

'取得实时数据
originalData = GetHttp("http://hq.sinajs.cn/list=" + stocks)

arrAllData = Split(originalData, ";")

iCount = UBound(arrAllData)

'排列所取得的数据
For i = startRow To iCount + startRow - 1 - 2

    KK = Cells(i, arrSrartColumn + 2).Value

    recordData = Split(arrAllData(i - startRow), ",")

    If UBound(recordData) <= 1 Then
        Cells(i, stockCodeColumn + 1) = "无此代码"
    Else
        recordHeader = Split(recordData(0), Chr(34))
        Cells(i, stockCodeColumn + 1) = recordHeader(1)       '股票名称
 
        End If
           
             Cells(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 Then
             Cells(i, arrSrartColumn + 1) = "0%"                                      '现价为0时涨跌幅
             Cells(i, arrSrartColumn + 2) = "0"                                       '现价为0时涨跌
             Cells(i, arrSrartColumn + 7) = "0%"                                      '现价为0时振幅
                 

        End If

Next i


'打开屏幕刷新
Application.ScreenUpdating = True

End Sub

'清除数据
Sub Clean_ArrArea()
On Error Resume Next
EndRow = Range("A65536").End(xlUp).Row

If startRow <= EndRow Then
    Range(Cells(startRow, arrSrartColumn - 1), Cells(EndRow, arrEndColumn + 3)).Value = ""
Else
    Exit Sub
End If

End Sub

【运行效果1.】

【量化投资】02.数据接口初探——excel读取新浪股票api数据_第1张图片

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

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

Sub A01_mink5()

    MyRow = ActiveCell.Row
    stockCode = LCase(Cells(MyRow, stockCodeColumn).Value)

    url = url1
    caption_Name = "5分钟K线"
    indexType = "mink5"

    If Len(stockCode) <> 8 Then
        MsgBox "不是有效的股票代码。", 16, "错误信息"
    Else
        Get_Photo.Show
    End If

End Sub

Sub A02_mink15()

    url = url1

    MyRow = ActiveCell.Row
    stockCode = LCase(Cells(MyRow, stockCodeColumn).Value)

    caption_Name = "15分钟K线"
    indexType = "mink15"

    If Len(stockCode) <> 8 Then
        MsgBox "不是有效的股票代码。", 16, "错误信息"
    Else
        Get_Photo.Show
    End If

End Sub

Sub A03_mink30()

    url = url1

    MyRow = ActiveCell.Row
    stockCode = LCase(Cells(MyRow, stockCodeColumn).Value)

    caption_Name = "30分钟K线"
    indexType = "mink30"

    If Len(stockCode) <> 8 Then
        MsgBox "不是有效的股票代码。", 16, "错误信息"
    Else
        Get_Photo.Show
    End If

End Sub

Sub A04_daily()

    url = url1

    MyRow = ActiveCell.Row
    stockCode = LCase(Cells(MyRow, stockCodeColumn).Value)

    caption_Name = "日K线"
    indexType = "daily"

    If Len(stockCode) <> 8 Then
        MsgBox "不是有效的股票代码。", 16, "错误信息"
    Else
        Get_Photo.Show
    End If

End Sub

Sub A05_weekly()

    url = url1

    MyRow = ActiveCell.Row
    stockCode = LCase(Cells(MyRow, stockCodeColumn).Value)

    caption_Name = "周K线"
    indexType = "weekly"

    If Len(stockCode) <> 8 Then
        MsgBox "不是有效的股票代码。", 16, "错误信息"
    Else
        Get_Photo.Show
    End If

End Sub

Sub A06_monthly()

    url = url1

    MyRow = ActiveCell.Row
    stockCode = LCase(Cells(MyRow, stockCodeColumn).Value)

    caption_Name = "月K线"
    indexType = "monthly"

    If Len(stockCode) <> 8 Then
        MsgBox "不是有效的股票代码。", 16, "错误信息"
    Else
        Get_Photo.Show
    End If

End Sub

Sub A07_min()

    url = url1

    MyRow = ActiveCell.Row
    stockCode = LCase(Cells(MyRow, stockCodeColumn).Value)

    caption_Name = "分时线"
    indexType = "min"

    If Len(stockCode) <> 8 Then
        MsgBox "不是有效的股票代码。", 16, "错误信息"
    Else
        Get_Photo.Show
    End If

End Sub

Sub B01_macd()

    url = url1

    MyRow = ActiveCell.Row
    stockCode = LCase(Cells(MyRow, stockCodeColumn).Value)

    caption_Name = "MACD"
    indexType = "macd"

    If Len(stockCode) <> 8 Then
        MsgBox "不是有效的股票代码。", 16, "错误信息"
    Else
        Get_Photo.Show
    End If

End Sub

Sub B02_trix()

    url = url1

    MyRow = ActiveCell.Row
    stockCode = LCase(Cells(MyRow, stockCodeColumn).Value)

    caption_Name = "TRIX"
    indexType = "trix"

    If Len(stockCode) <> 8 Then
        MsgBox "不是有效的股票代码。", 16, "错误信息"
    Else
        Get_Photo.Show
    End If

End Sub

Sub B03_dmi()

    url = url1

    MyRow = ActiveCell.Row
    stockCode = LCase(Cells(MyRow, stockCodeColumn).Value)

    caption_Name = "DMI"
    indexType = "dmi"

    If Len(stockCode) <> 8 Then
        MsgBox "不是有效的股票代码。", 16, "错误信息"
    Else
        Get_Photo.Show
    End If

End Sub

Sub B04_expma()

    url = url1

    MyRow = ActiveCell.Row
    stockCode = LCase(Cells(MyRow, stockCodeColumn).Value)

    caption_Name = "EXPMA"
    indexType = "expma"

    If Len(stockCode) <> 8 Then
        MsgBox "不是有效的股票代码。", 16, "错误信息"
    Else
        Get_Photo.Show
    End If

End Sub

Sub B05_brar()

    url = url1

    MyRow = ActiveCell.Row
    stockCode = LCase(Cells(MyRow, stockCodeColumn).Value)

    caption_Name = "BRAR"
    indexType = "brar"

    If Len(stockCode) <> 8 Then
        MsgBox "不是有效的股票代码。", 16, "错误信息"
    Else
        Get_Photo.Show
    End If

End Sub

Sub B06_cr()

    url = url1

    MyRow = ActiveCell.Row
    stockCode = LCase(Cells(MyRow, stockCodeColumn).Value)

    caption_Name = "CR"
    indexType = "cr"

    If Len(stockCode) <> 8 Then
        MsgBox "不是有效的股票代码。", 16, "错误信息"
    Else
        Get_Photo.Show
    End If

End Sub

Sub B07_vr()

    url = url1

    MyRow = ActiveCell.Row
    stockCode = LCase(Cells(MyRow, stockCodeColumn).Value)

    caption_Name = "VR"
    indexType = "vr"

    If Len(stockCode) <> 8 Then
        MsgBox "不是有效的股票代码。", 16, "错误信息"
    Else
        Get_Photo.Show
    End If

End Sub

Sub B08_bias()

    url = url1

    MyRow = ActiveCell.Row
    stockCode = LCase(Cells(MyRow, stockCodeColumn).Value)

    caption_Name = "BIAS"
    indexType = "bias"

    If Len(stockCode) <> 8 Then
        MsgBox "不是有效的股票代码。", 16, "错误信息"
    Else
        Get_Photo.Show
    End If

End Sub

Sub B09_psy()

    url = url1

    MyRow = ActiveCell.Row
    stockCode = LCase(Cells(MyRow, stockCodeColumn).Value)

    caption_Name = "PSY"
    indexType = "psy"

    If Len(stockCode) <> 8 Then
        MsgBox "不是有效的股票代码。", 16, "错误信息"
    Else
        Get_Photo.Show
    End If

End Sub

Sub B10_obv()

    url = url1

    MyRow = ActiveCell.Row
    stockCode = LCase(Cells(MyRow, stockCodeColumn).Value)

    caption_Name = "OBV"
    indexType = "obv"

    If Len(stockCode) <> 8 Then
        MsgBox "不是有效的股票代码。", 16, "错误信息"
    Else
        Get_Photo.Show
    End If

End Sub

Sub B11_boll()

    url = url1

    MyRow = ActiveCell.Row
    stockCode = LCase(Cells(MyRow, stockCodeColumn).Value)

    caption_Name = "BOLL"
    indexType = "boll"

    If Len(stockCode) <> 8 Then
        MsgBox "不是有效的股票代码。", 16, "错误信息"
    Else
        Get_Photo.Show
    End If

End Sub

Sub B12_mike()

    url = url1

    MyRow = ActiveCell.Row
    stockCode = LCase(Cells(MyRow, stockCodeColumn).Value)

    caption_Name = "MIKE"
    indexType = "mike"

    If Len(stockCode) <> 8 Then
        MsgBox "不是有效的股票代码。", 16, "错误信息"
    Else
        Get_Photo.Show
    End If

End Sub

Sub B13_asi()

    url = url1

    MyRow = ActiveCell.Row
    stockCode = LCase(Cells(MyRow, stockCodeColumn).Value)

    caption_Name = "ASI"
    indexType = "asi"

    If Len(stockCode) <> 8 Then
        MsgBox "不是有效的股票代码。", 16, "错误信息"
    Else
        Get_Photo.Show
    End If

End Sub

Sub B14_emv()

    url = url1

    MyRow = ActiveCell.Row
    stockCode = LCase(Cells(MyRow, stockCodeColumn).Value)

    caption_Name = "EMV"
    indexType = "emv"

    If Len(stockCode) <> 8 Then
        MsgBox "不是有效的股票代码。", 16, "错误信息"
    Else
        Get_Photo.Show
    End If

End Sub

Sub B15_cci()

    url = url1

    MyRow = ActiveCell.Row
    stockCode = LCase(Cells(MyRow, stockCodeColumn).Value)

    caption_Name = "CCI"
    indexType = "cci"

    If Len(stockCode) <> 8 Then
        MsgBox "不是有效的股票代码。", 16, "错误信息"
    Else
        Get_Photo.Show
    End If

End Sub

Sub B16_rsi()

    url = url1

    MyRow = ActiveCell.Row
    stockCode = LCase(Cells(MyRow, stockCodeColumn).Value)

    caption_Name = "RSI"
    indexType = "rsi"

    If Len(stockCode) <> 8 Then
        MsgBox "不是有效的股票代码。", 16, "错误信息"
    Else
        Get_Photo.Show
    End If

End Sub

Sub B17_wr()

    url = url1

    MyRow = ActiveCell.Row
    stockCode = LCase(Cells(MyRow, stockCodeColumn).Value)

    caption_Name = "WR"
    indexType = "wr"

    If Len(stockCode) <> 8 Then
        MsgBox "不是有效的股票代码。", 16, "错误信息"
    Else
        Get_Photo.Show
    End If

End Sub

Sub B18_kdj()

    url = url1

    MyRow = ActiveCell.Row
    stockCode = LCase(Cells(MyRow, stockCodeColumn).Value)

    caption_Name = "KDJ"
    indexType = "kdj"

    If Len(stockCode) <> 8 Then
        MsgBox "不是有效的股票代码。", 16, "错误信息"
    Else
        Get_Photo.Show
    End If

End Sub

Sub B19_roc()

    url = url1

    MyRow = ActiveCell.Row
    stockCode = LCase(Cells(MyRow, stockCodeColumn).Value)

    caption_Name = "ROC"
    indexType = "roc"

    If Len(stockCode) <> 8 Then
        MsgBox "不是有效的股票代码。", 16, "错误信息"
    Else
        Get_Photo.Show
    End If

End Sub

Sub B20_dma()

    url = url1

    MyRow = ActiveCell.Row
    stockCode = LCase(Cells(MyRow, stockCodeColumn).Value)

    caption_Name = "DMA"
    indexType = "dma"

    If Len(stockCode) <> 8 Then
        MsgBox "不是有效的股票代码。", 16, "错误信息"
    Else
        Get_Photo.Show
    End If

End Sub

【运行效果】

【量化投资】02.数据接口初探——excel读取新浪股票api数据_第2张图片

你可能感兴趣的:(股票,股票,vba,实时行情)