1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
Imports YahooFinanceApi Imports System.Windows.Forms.DataVisualization.Charting 'https://github.com/lppkarl/YahooFinanceApi Public Class Form1 Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load Refresh() Timer1.Interval = 15000 Timer1.Enabled = True End Sub Private Async Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Refresh() End Sub Private Async Sub Refresh() Dim QueryString As String = Nothing For Each MyItem In ComboBox1.Items QueryString &= MyItem & "," Next QueryString = QueryString.Remove(QueryString.Length - 1, 1) Debug.WriteLine(QueryString) Dim securities As IReadOnlyDictionary(Of String, Security) = Await Yahoo.Symbols(QueryString).Fields(Field.Symbol, Field.RegularMarketPrice, Field.RegularMarketTime, Field.Currency, Field.LongName).QueryAsync() Debug.WriteLine(securities) If lstResults.Items.Count > 0 Then lstResults.Items.Add(String.Empty) For Each objSecurity As Security In securities.Values Dim strCurrency As String Try 'for some reason, the currency is not always available! strCurrency = objSecurity.Currency Catch strCurrency = "(unknown)" End Try Dim strLongName As String Try strLongName = objSecurity.LongName Catch strLongName = "(unknown)" End Try Dim dtLatestPrice As DateTime = UnixTimeStampToDateTime(objSecurity.RegularMarketTime) lstResults.Items.Add($"{objSecurity.Symbol} @ {objSecurity.RegularMarketPrice} {strCurrency} - {dtLatestPrice} - {strLongName}") Next lstResults.SelectedIndex = lstResults.Items.Count - 1 End Sub Public Shared Function UnixTimeStampToDateTime(ByVal unixTimeStamp As Long) As DateTime Dim dtDateTime As DateTime = New DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc) dtDateTime = dtDateTime.AddSeconds(unixTimeStamp).ToLocalTime() Return dtDateTime End Function Private Sub ComboBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles ComboBox1.KeyDown If e.KeyData = Keys.Enter Then If Not ComboBox1.Items.Contains(ComboBox1.Text) Then ComboBox1.Items.Add(ComboBox1.Text) End If End If If e.KeyData = Keys.Delete Then ComboBox1.Items.RemoveAt(ComboBox1.SelectedIndex) End If End Sub Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged ShowHistory() End Sub Private Async Sub ShowHistory() Dim history = Await Yahoo.GetHistoricalAsync(ComboBox1.SelectedItem, DateTimePicker1.Value.ToShortDateString(), DateTimePicker2.Value.ToShortDateString(), Period.Daily) Dim s As Series Chart2.Series.Clear() If Not Chart1.Series.IsUniqueName(ComboBox1.SelectedItem) Then Chart1.Series.Item(ComboBox1.SelectedItem).Points.Clear() s = Chart1.Series.Item(ComboBox1.SelectedItem) Else s = New Series s.Name = ComboBox1.SelectedItem s.ChartType = SeriesChartType.Line End If For Each candle In history s.Points.AddXY(candle.DateTime.ToShortDateString, {candle.Close}) RichTextBox1.AppendText($"DateTime: {candle.DateTime}, Open: {candle.Open}, High: {candle.High}, Low: {candle.Low}, Close: {candle.Close}, Volume: {candle.Volume}, AdjustedClose: {candle.AdjustedClose}" & vbCrLf) Next If Chart1.Series.IsUniqueName(ComboBox1.SelectedItem) Then Chart1.Series.Add(s) End If Chart2.Series.Add(s) End Sub Private Sub BackgroundWorker1_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) End Sub Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick lstResults.Items.Clear() Refresh() End Sub End Class |