Imports System.Net
Imports System.IO
'''''' <summary>
'''''' Managed den Quotes-Download von YahooFinance
'''''' </summary>
'''''' <remarks></remarks>
Public Class YahooFinanceQuotes
Public Event DownloadCompleted(ByVal values() As QuoteData)
Public Event Timeout()
Private WithEvents m_WC As WebClient
Private WithEvents m_Timeout As Timer
'''''' <summary>
'''''' Startet den Download von YahooFinance
'''''' </summary>
'''''' <param name="idList">Eine Auflistung der Yahoo-IDs der verschiedenen Unternehmen</param>
'''''' <param name="timeout">Zeitspanne bis der Download abgebrochen wird in ms</param>
'''''' <remarks></remarks>
Public Sub StartDownload(ByVal idList() As String, Optional ByVal timeout As Integer = 10000)
''URL zusammen setzen
Dim url As String = "http://finance.yahoo.com/d/quotes.csv?s="
''IDs hinzufügen
For Each s As String In idList
url &= s & "+"
Next
url &= "&f="
''Das Wichtigste: Die Symbolkombination
''Alle Symbole z.B. unter "http://www.gummy-stuff.org/Yahoo-data.htm"
url &= "nl1poc1p2ghjkva2"
''Download starten
m_WC = New WebClient
m_Timeout = New Timer
m_Timeout.Interval = timeout
m_Timeout.Start()
m_WC.OpenReadAsync(New Uri(url))
End Sub
Private Sub m_Quotes_OpenReadCompleted(ByVal sender As Object, ByVal e As System.Net.OpenReadCompletedEventArgs) Handles m_WC.OpenReadCompleted
Try
m_Timeout.Stop()
m_Timeout.Dispose()
Dim quotes As New List(Of QuoteData)
Dim sr As New StreamReader(e.Result)
While sr.Peek <> -1
''Row aufteilen
Dim rowStr() As String = sr.ReadLine.Split(Char.Parse(","))
Dim values As New List(Of Double)
Dim sd As New QuoteData
''Liste mit Werten der Row erstellen
For i As Integer = 0 To rowStr.Count - 1
rowStr(i) = rowStr(i).ToString.Replace("""", "")
rowStr(i) = rowStr(i).ToString.Replace("%", "")
If i = 0 Then
sd.Name = rowStr(i)
Else
If rowStr(i).ToString = "N/A" Then
''"N/A" sendet Yahoo, wenn kein Wert zur Verfügung steht
values.Add(0)
Else
''Die Daten liegen im englischen Format vor
Dim ci As New Globalization.CultureInfo("en-US")
values.Add(Double.Parse(rowStr(i).ToString, Globalization.NumberStyles.Currency, ci))
End If
End If
Next
''Wertzuweisung in Abstimmung zur Symbolkombination
sd.Value = values(0)
sd.Close = values(1)
sd.Open = values(2)
sd.ChangeNominal = values(3)
sd.ChangePercent = values(4)
sd.LowDay = values(5)
sd.HighDay = values(6)
sd.LowYear = values(7)
sd.HighYear = values(8)
sd.Volume = values(9)
sd.VolumeO = values(10)
quotes.Add(sd)
End While
''Event auslösen
RaiseEvent DownloadCompleted(quotes.ToArray)
e.Result.Close()
m_WC.Dispose()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Private Sub m_Timeout_Tick() Handles m_Timeout.Tick
''Download bei Erreichen des Timeouts unterbrechen
If m_WC IsNot Nothing AndAlso m_WC.IsBusy Then
m_WC.CancelAsync()
End If
m_Timeout.Stop()
m_Timeout.Dispose()
RaiseEvent Timeout()
End Sub
End Class
Public Structure QuoteData
Public Name As String
Public Value As Double
Public Close As Double
Public Open As Double
Public ChangeNominal As Double
Public ChangePercent As Double
Public LowDay As Double
Public HighDay As Double
Public LowYear As Double
Public HighYear As Double
Public Volume As Double
Public VolumeO As Double
End Structure
'''''' <summary>
'''''' Beispielanwendung: Form mit einem ListView(5 Columns: Name, Value, Open, High, Low) und einem Button
'''''' </summary>
'''''' <remarks></remarks>
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim y As New YahooFinanceQuotes
AddHandler y.DownloadCompleted, AddressOf DownloadCompleted
''Die IDs für Adidas, Alianz und BASF
Dim id() As String = New String(2) {"ADS.DE", "ALV.DE", "BAS.DE"}
''oder DAX, TECDAX und MDAX
''Dim id() As String = New String(2) {"^GDAXI", "^TECDAX", "^MDAXI"}
y.StartDownload(id)
End Sub
Private Sub DownloadCompleted(ByVal quotes() As QuoteData)
For Each q As QuoteData In quotes
ListView1.Items.Add(New ListViewItem(New String(4) {q.Name, q.Value.ToString("n"), q.Open.ToString("n"), q.HighDay.ToString("n"), q.LowDay.ToString("n")}))
Next
End Sub
End Class