This script uses a WebServiceX.net hosted Web Service to fetch a stock quote for any symbol. The Web Service returns 16 different values as XML marked-up data.

StockQuote

I start by defining the Windows Form (WinForm) used for input and output GUI display. I added the Input TextBox and Label pair and well as one TextBox/Label pair for each output value.

Add-Type -AssemblyName System.Windows.Forms

# Main Form 
$mainForm = New-Object System.Windows.Forms.Form
$mainForm.Location = "200, 200"
$mainForm.Font = "Comic Sans MS,9"
$mainForm.FormBorderStyle = "FixedDialog"
$mainForm.Text = " Stock Quote"
$mainForm.ForeColor = "White"
$mainForm.BackColor = "SteelBlue"
$mainForm.Width = 460
$mainForm.Height = 550

# Quote Input Label
$quoteInputLabel = New-Object System.Windows.Forms.Label
$quoteInputLabel.Location = "45,10"
$quoteInputLabel.Height = 22
$quoteInputLabel.Width = 110
$quoteInputLabel.Text = "Stock Symbol"
$mainForm.Controls.Add($quoteInputLabel)

# Quote Input TextBox
$quoteTextBox = New-Object System.Windows.Forms.TextBox
$quoteTextBox.Location = "180,10"
$quoteTextBox.Size = "50,20"
$quoteTextBox.ForeColor = "MediumBlue"
$quoteTextBox.BackColor = "White"
$mainForm.Controls.Add($quoteTextBox)

# Name Output Label
$nameLabel = New-Object System.Windows.Forms.Label
$nameLabel.Location = "45,40"
$nameLabel.Height = 22
$nameLabel.Width = 110
$nameLabel.Text = "Name"
$mainForm.Controls.Add($nameLabel)

# Name Output TextBox
$nameTextBox = New-Object System.Windows.Forms.TextBox
$nameTextBox.Location = "180,40"
$nameTextBox.Size = "130,20"
$nameTextBox.ForeColor = "MediumBlue"
$nameTextBox.BackColor = "White"
$mainForm.Controls.Add($nameTextBox)

Next I add a Button used to call the QuoteLookup Function as well as an Exit Button.

# Lookup Button
$lookupButton = New-Object System.Windows.Forms.Button 
$lookupButton.Location = "350,10"
$lookupButton.Size = "75,28"
$lookupButton.ForeColor = "DarkBlue"
$lookupButton.BackColor = "White"
$lookupButton.Text = "Lookup"
$lookupButton.add_Click({QuoteLookup})
$mainForm.Controls.Add($lookupButton)

# Exit Button 
$exitButton = New-Object System.Windows.Forms.Button
$exitButton.Location = "350,40"
$exitButton.Size = "75,28"
$exitButton.ForeColor = "Red"
$exitButton.BackColor = "White"
$exitButton.Text = "Exit"
$exitButton.add_Click({$mainForm.close()})
$mainForm.Controls.Add($exitButton)

Finally added is the QuoteLookup Function to call the Web Service and process the returned XML Data. In that function I setup a New-WebServiceProxy for the WebserviceX.net URL pointing to the StockQuote WSDL schema. A SOAP call returns the XML which is then parsed with a Select statement. The previously initialized values are then set for display in the various TextBoxes.

Function QuoteLookup {
    $lastTextBox.Text = " "
    $dateTextBox.Text = " "
    $timeTextBox.Text = " "
    $changeTextBox.Text = " "
    $openTextBox.Text = " "
    $highTextBox.Text = " "
    $lowTextBox.Text = " "
    $volumeTextBox.Text = " "
    $mktCapTextBox.Text = " "
    $previousCloseTextBox.Text = " "
    $PercentageChangeTextBox.Text = " "
    $annRangeTextBox.Text = " "
    $earnsTextBox.Text = " "
    $PETextBox.Text = " "
    $nameTextBox.Text = " "
    $mainForm.Refresh()

    $stockQuote = New-WebServiceProxy -uri "http://www.webservicex.net/stockquote.asmx?WSDL"
    $quote = $stockQuote.GetQuote($quoteTextBox.Text)
    $quoteXML = [xml]$quote
    $selection  = $quoteXML.StockQuotes.Stock | Select Last, Date, Time, Change, Open, High, Low, Volume, MktCap, PreviousClose, PercentageChange, AnnRange, Earns, Name
    $PE = $quoteXML.StockQuotes.Stock | Select  -ExpandProperty P-E
    $lastTextBox.Text = $selection.Last
    $dateTextBox.Text = $selection.Date
    $timeTextBox.Text = $selection.Time
    $changeTextBox.Text = $selection.Change
    $openTextBox.Text = $selection.Open
    $highTextBox.Text = $selection.High
    $lowTextBox.Text = $selection.Low
    $volumeTextBox.Text = "{0:N0}" -f [int]$selection.Volume
    $mktCapTextBox.Text = $selection.MktCap
    $previousCloseTextBox.Text = $selection.PreviousClose
    $PercentageChangeTextBox.Text = $selection.PercentageChange
    $annRangeTextBox.Text = $selection.AnnRange
    $earnsTextBox.Text = $selection.Earns
    $PETextBox.Text = $PE
    $nameTextBox.Text = $selection.Name
}

This is an example of the returned XML from the Web Service.

<StockQuotes>
    <Stock>
        <Symbol>C</Symbol>
        <Last>48.89</Last>
        <Date>9/27/2013</Date>
        <Time>4:00pm</Time>
        <Change>-0.04</Change>
        <Open>48.76</Open>
        <High>49.20</High>
        <Low>48.69</Low>
        <Volume>20248714</Volume>
        <MktCap>148.7B</MktCap>
        <PreviousClose>48.93</PreviousClose>
        <PercentageChange>-0.08%</PercentageChange>
        <AnnRange>32.70 - 53.56</AnnRange>
        <Earns>3.113</Earns>
        <P-E>15.72</P-E>
        <Name>Citigroup</Name>
    </Stock>
</StockQuotes>

Download the complete script here – StockQuote.ps1

Advertisements