来自Fidelity的实时行情脚本

时间:2013-12-22 16:22:16

标签: excel vba excel-vba

通过使用宏记录器,然后进行一些修改,我已经设置了一个宏来导航到多个报价的Fidelity股票报价页面,并下载延迟报价。这很好用。但是,如果我登录到Fidelity并手动访问同一页面,我可以获得实时报价。 “登录”可以在我访问页面之前或之后进行。我无法弄清楚如何将登录过程添加到处理其他所有事情的VBA例程中。在我选择“从Web获取外部数据”选项之前启动的宏录制器在访问该页面以获取实时报价时不会选择登录过程。

以下是实际调用股票报价页面并请求报价的函数部分:

With Worksheets("fidotemp").QueryTables.Add(Connection:=Connection, _
    Destination:=Range("a1"))
    .name = Symbol
    .FieldNames = True
    .RowNumbers = False
    .FillAdjacentFormulas = False
    .PreserveFormatting = True
    .RefreshOnFileOpen = False
    .BackgroundQuery = True
    .RefreshStyle = xlInsertDeleteCells
    .SavePassword = False
    .SaveData = True
    .AdjustColumnWidth = True
    .RefreshPeriod = 0
    .WebSelectionType = xlSpecifiedTables
    .WebFormatting = xlWebFormattingNone
    .WebTables = IIf(bSymbol, "12", "9")
    .WebPreFormattedTextToColumns = True
    .WebConsecutiveDelimitersAsOne = True
    .WebSingleBlockTextImport = False
    .WebDisableDateRecognition = False
    .WebDisableRedirections = False
    .Refresh BackgroundQuery:=False
    End With

名为Connection的Connection参数是包含特定页面的字符串,以及用于获取一个或多个引号的格式正确的字符串。当请求多个引号时,股票代码符号被“+”分隔并限制为100个字符长度。

示例连接字符串可能如下所示:

URL;https://fastquote.fidelity.com/webxpress/get_quote?QUOTE_TYPE=D&SID_VALUE_ID=VIP+SAN+E+MT+BCE+RAI+SNP+BP+MO+PTR&submit=Quote

1 个答案:

答案 0 :(得分:0)

从www.fidelity.com登录是否可以接受?如果是这样,这会让你进入吗?

Sub test()
' open IE, navigate to the Fidelity log-in webpage of interest and loop until fully loaded
    Set IE = CreateObject("InternetExplorer.Application")
    login_url = "https://www.fidelity.com"

    With IE
        .Visible = True
        .navigate login_url
        .Top = 50
        .Left = 130
        .Height = 800
        .Width = 800
    End With

    Do Until Not IE.Busy And IE.ReadyState = 4
        DoEvents
    Loop

' Enter your username and pw
    IE.document.getElementById("temp_id").Value = "abcde"
    IE.document.getElementById("PIN").Value = "12345"

' Click the login button
    IE.document.getElementById("logButton").disabled = False
    IE.document.getElementById("logButton").Click
End Sub