Hi
Try this version. Button2 needs to be added which is to recalculate values.
Designer
Code
Option Strict On
Option Explicit On
Imports System.Net.Http
Imports System.Runtime.InteropServices
Imports System.Text
Imports Newtonsoft.Json
Public Class Form1
Dim currASKS As String()()
Dim currBIDS As String()()
<DllImport("user32.dll", CharSet:=CharSet.Auto)>
Private Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal wMsg As Integer, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr
End Function
Private Const WM_VSCROLL As Integer = 277
Private Const SB_PAGEBOTTOM As Integer = 7
Friend Shared Sub ScrollToBottom(ByVal richTextBox As RichTextBox)
SendMessage(richTextBox.Handle, WM_VSCROLL, CType(SB_PAGEBOTTOM, IntPtr), IntPtr.Zero)
richTextBox.SelectionStart = richTextBox.Text.Length
End Sub
Public Class Result
Public Property asks As String()()
Public Property bids As String()()
Public Property seqNum As Integer
Public Property prevSeqNum As Integer
Public Property lastTs As Double
End Class
Public Class Root
Public Property [error] As Object
Public Property result As Result
Public Property id As Integer
End Class
Private URI As String = "https://api.hotbit.io/api/v1/order.depth?market=KIBA/USDT&limit=150&interval=1e-8"
Private Async Function GetJsonFromApi() As Task(Of Root)
Using http As New HttpClient(),
response As HttpResponseMessage = Await http.GetAsync(URI)
Return JsonConvert.DeserializeObject(Of Root)(Await response.Content.ReadAsStringAsync())
End Using
End Function
Private Sub OutputAsks(asks As String()())
'asks
Try
Dim contents As New StringBuilder
currASKS = asks
ReCalcASKS()
For Each ask() As String In asks
contents.AppendLine(String.Join(" - ", {ask(0), ToKMB(GetDouble(ask(1)))}))
Next
RichTextBox1.BeginInvoke(Sub()
RichTextBox1.Text = contents.ToString()
RichTextBox1.Lines = RichTextBox1.Lines.
Where(Function(x) Not String.IsNullOrEmpty(x)).
OrderByDescending(Function(x) Decimal.Parse(x.Split(" "c)(0))).ToArray
ScrollToBottom(RichTextBox1)
End Sub)
Catch ex As Exception
MessageBox.Show("ERROR in asks")
End Try
End Sub
Private Sub OutputBids(bids As String()())
'bids
Try
Dim contents As New StringBuilder
currBIDS = bids
ReCalcBIDS()
For Each Bid() As String In bids
contents.AppendLine(String.Join(" - ", {Bid(0), ToKMB(GetDouble(Bid(1)))}))
Next
RichTextBox2.BeginInvoke(Sub()
RichTextBox2.Text = contents.ToString()
End Sub)
Catch ex As Exception
MessageBox.Show("ERROR in Bids")
End Try
End Sub
Public Shared Function ToKMB(num As Double) As String
If num > 999999999 OrElse num < -999999999 Then
Return num.ToString("0,,,.###B", Globalization.CultureInfo.InvariantCulture)
ElseIf num > 999999 OrElse num < -999999 Then
Return num.ToString("0,,.##M", Globalization.CultureInfo.InvariantCulture)
ElseIf num > 999 OrElse num < -999 Then
Return num.ToString("0,.#K", Globalization.CultureInfo.InvariantCulture)
Else
Return num.ToString("#.00", Globalization.CultureInfo.InvariantCulture)
End If
End Function
Function GetDouble(s As String) As Double
Dim v As Double = 0.0D
If Double.TryParse(s, v) Then Return v
Return 0.0D
End Function
Private Async Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Dim root As Root = Await GetJsonFromApi()
OutputAsks(root.result.asks)
OutputBids(root.result.bids)
' change to a 5 second interval
Timer1.Interval = 5000
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Timer1.Start()
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
ReCalcASKS()
ReCalcASKS()
End Sub
Sub ReCalcASKS()
Label1.Text = ToKMB(Calc(currASKS, GetDouble(TextBox1.Text), GetDouble(TextBox2.Text)))
End Sub
Sub ReCalcBIDS()
Label2.Text = ToKMB(Calc(currBIDS, GetDouble(TextBox3.Text), GetDouble(TextBox4.Text)))
End Sub
Function Calc(lst As String()(), min As Double, max As Double) As Double
If lst Is Nothing Then Return 0
Dim sum As Double
For Each s As String() In lst
Dim a As Double = GetDouble(s(0))
Dim b As Double = GetDouble(s(1))
If a >= min AndAlso a <= max Then sum += b
Next
Return sum
End Function
End Class