Imports MasterlinkAPI
Imports System.ComponentModel

Public Class Form1
    Private connFlag As Boolean = False
    Private WithEvents bsCur As BindingSource = New BindingSource()
    Private WithEvents bsOrder As BindingSource = New BindingSource()
    Private WithEvents bsDeal As BindingSource = New BindingSource()
    Private WithEvents orderList As BindingList(Of QuoteData) = New BindingList(Of QuoteData)
    Private WithEvents dealList As BindingList(Of QuoteData) = New BindingList(Of QuoteData)
    Private WithEvents curList As BindingList(Of QuoteData) = New BindingList(Of QuoteData)

    Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
        RichTextBox1.Text = String.Empty

    End Sub

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        If connFlag Then
            MessageBox.Show("連線已建立,不需再建立連線")
            Return
        End If
        gTradeMain.instance.receiveMsg = AddressOf receMsg
        gTradeMain.instance.statusMsg = AddressOf statusMsg
        gTradeMain.instance.OrderStatusMsg = AddressOf orderStatusMsg
        gTradeMain.instance.Log = AddressOf Log
        gTradeMain.instance.init(".\Profile.ini", "TOUCHANCE", "Masterlink")
        gTradeMain.instance.Securities1 = 1 '這裡改成0,下單不會出現確認視窗
        gTradeMain.instance.Securities2 = 1 '這裡改成0,刪單不會出現確認視窗
        dataTableInit()
    End Sub
    Sub receMsg(ByVal rt As String)
        RichTextBox1.AppendText(rt + vbNewLine)
    End Sub
    Sub statusMsg(ByVal rt As status)
        RichTextBox1.AppendText(rt.txt + vbNewLine)
        connFlag = rt.tag
    End Sub
    Sub orderStatusMsg(ByVal rt As QuoteData)
        Console.WriteLine(rt._toString())
        compList(rt)
    End Sub
    Sub Log(ByVal s As String)
        RichTextBox1.AppendText(s + vbNewLine)
    End Sub

    Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) Handles Button3.Click
        'gTradeMain.instance.sendOrders("MTX.201703", 1, 9500, "B")
        Dim comm As String = txtComm.Text
        Dim qty As Int32 = Convert.ToInt32(txtQty.Text)
        Dim price As Double = Convert.ToDouble(txtPrice.Text)
        Dim buySell As String = txtBuySell.Text
        gTradeMain.instance.sendOrders(comm, qty, price, buySell)
    End Sub
    Private Sub dataTableInit()
        dGVCur.AutoGenerateColumns = False
        dGVCur.AllowUserToAddRows = False
        dGVCur.RowHeadersVisible = False
        dGVCur.Invalidate()
        bsCur.DataSource = curList
        bsCur.ResumeBinding()
        dGVCur.DataSource = bsCur
        dGVOrder.AutoGenerateColumns = False
        dGVOrder.AllowUserToAddRows = False
        dGVOrder.RowHeadersVisible = False
        dGVOrder.Invalidate()
        bsOrder.DataSource = orderList
        bsOrder.ResumeBinding()
        dGVOrder.DataSource = bsOrder
        dGVDeals.AutoGenerateColumns = False
        dGVDeals.AllowUserToAddRows = False
        dGVDeals.RowHeadersVisible = False
        dGVDeals.Invalidate()
        bsDeal.DataSource = dealList
        bsDeal.ResumeBinding()
        dGVDeals.DataSource = bsDeal
    End Sub
    Private Sub compList(ByVal qd As QuoteData)
        Dim order = From odr In orderList
                        Where odr.userKey = qd.userKey
                        Select odr
        If order.Count = 0 Then
            orderList.Add(qd)
        Else
            'order.ToList<QuoteData>()[0].update(qd);
            order.ToList(0).update(qd)
        End If
        If qd.dealQty > 0 Then
            Dim deals = From deal In dealList
                    Where deal.userKey = qd.userKey
                    Select deal
            If deals.Count = 0 Then
                dealList.Add(qd)
            End If
        End If
        If qd.qty = 0 Then
            Dim curr = From cur In curList
                       Where cur.userKey = qd.userKey
                       Select cur
            If curr.Count > 0 Then
                curList.Remove(curr.ToList(0))
                'curList.Remove(curr.ToList<QuoteData>()[0]);
            End If
        ElseIf qd.qty > 0 Then
            Dim curr = From cur In curList
                            Where cur.userKey = qd.userKey
                            Select cur
            If curr.Count = 0 Then
                curList.Add(qd)
            Else
                curr.ToList(0).update(qd)
            End If
        End If
    End Sub

    Private Sub Button4_Click(sender As System.Object, e As System.EventArgs) Handles Button4.Click
        Dim cc = (From c In curList
                     Where c.del = True
                     Select c).ToList()
        For Each q As QuoteData In cc
            If Not q.del Then
                Continue For
            End If
            Dim o As OrderData = New OrderData
            o.orderCategory = "R" '0:委託類別, R:減量, M:改價
            o.account = q.account '1:帳號
            o.symbolID = q.symbolID '2:商品ID
            o.orderType = q.orderType '3:委託別
            o.buySell = q.buySell '4:買賣
            o.priceFlag = q.priceFlag '5:價格別
            o.price = q.price '6:委託價格
            o.qty = q.qty '7:委託數量
            o.marketType = String.Empty '8:市場別(證券)
            o.marketAP = String.Empty '9:盤別(證券)
            o.dayTrade = q.dayTrade '10:當沖 Y/N
            o.userKey = q.userKey '11 UserKey
            o.marketNo = String.Empty '12交易所代碼
            o.stopPrice = 0 '14:停損價格
            o.openClose = q.openClose '15:倉別
            o.orderno = q.orderno '18:委託書號
            Dim rtnCode As Int32 = gTradeMain.instance.Order(o)
        Next
    End Sub
End Class
