Imports AxICE_GTRADELib

Public Class gTradeMain : Inherits SingletonBase(Of gTradeMain)
    Friend WithEvents gTrade1 As AxICE_gTrade
    Public count As Int32
    Public statusMsg As Action(Of status)
    Public Log As Action(Of String)
    Public receiveMsg As Action(Of String)
    Public OrderStatusMsg As Action(Of QuoteData)
    Dim status As status = New status
    Public Property Securities1 As Int16 = 1
    Public Property Securities2 As Int16 = 1
    Private account As String
    Public Sub init(f As String, s As String, s1 As String)
        gTrade1 = New AxICE_gTrade()
        Dim host As New System.Windows.Forms.Integration.WindowsFormsHost()
        host.Child = gTrade1
        gTrade1.Handle.GetHashCode()
        count = gTrade1.TouchanceLogin(f, s)
        If (statusMsg = Nothing Or receiveMsg = Nothing Or Log = Nothing) Then
            Return
        End If
        If (count = 0) Then
            status.tag = False
            status.txt = "取得授權失敗"
            statusMsg(status)
            Return
        End If
        gTrade1.TouchanceLoginY(s)
        count = gTrade1.Login(f, s1)
        If (count <> 3) Then
            status.tag = False
            status.txt = rtnMsg.instance.getLoginMsg(count)
            statusMsg(status)
            Return
        End If
        status.tag = True
        status.txt = "登入成功"
        statusMsg(status)
    End Sub
    Private Sub gTrade1_OrderStatus(sender As System.Object, e As AxICE_GTRADELib._DICE_gTradeEvents_OrderStatusEvent) Handles gTrade1.OrderStatus
        Dim qd As QuoteData = New QuoteData
        'Dim nCnt As Int32 = e.nIndex
        qd.account = gTrade1.GetOrderString(e.nIndex, 0) '//0帳號
        qd.orderno = gTrade1.GetOrderString(e.nIndex, 1) '//1委託書號
        qd.symbolID = gTrade1.GetOrderString(e.nIndex, 2) '2商品ID
        qd.orderCategory = gTrade1.GetOrderString(e.nIndex, 3) '3類別
        qd.orderType = gTrade1.GetOrderString(e.nIndex, 4) '4委託類別
        qd.priceFlag = gTrade1.GetOrderString(e.nIndex, 5) '5價格別
        qd.buySell = gTrade1.GetOrderString(e.nIndex, 6) '6買賣
        qd.dayTrade = gTrade1.GetOrderString(e.nIndex, 7) '7當沖
        qd.price = gTrade1.GetOrderValue(e.nIndex, 7) '7(數值)委託價格
        qd.qty = gTrade1.GetOrderValue(e.nIndex, 8)  '8(數值)有效委託
        'Console.WriteLine("第" + e.nIndex + "筆, 有效委託數量:" + gTrade1.GetOrderValue(e.nIndex, 8))
        qd.dealQty = gTrade1.GetOrderValue(e.nIndex, 9) '9(數值)已成交數量
        qd.udateTime = gTrade1.GetOrderString(e.nIndex, 12) '12更新時間
        qd.status = gTrade1.GetOrderString(e.nIndex, 11) '12狀態
        qd.source = gTrade1.GetOrderString(e.nIndex, 13) '13來源別
        qd.userKey = gTrade1.GetOrderString(e.nIndex, 14) '14userKey
        qd.canData = gTrade1.GetOrderString(e.nIndex, 20) '20可見資料
        qd.memo = gTrade1.GetOrderString(e.nIndex, 24) '24備註
        Dim ss As String = gTrade1.GetOrderString(e.nIndex, 31)
        If gTrade1.GetOrderString(e.nIndex, 25).Length > 8 Then '25QuoteID
            qd.gQuoteID = gTrade1.GetOrderString(e.nIndex, 25).Substring(8)
        End If
        qd.symbolID2 = gTrade1.GetOrderString(e.nIndex, 29) '29商品名稱2
        qd.buySell2 = gTrade1.GetOrderString(e.nIndex, 30) '30買賣2
        If gTrade1.GetOrderString(e.nIndex, 31).Length > 13 Then '31gQuoteID2
            qd.gQuoteID2 = gTrade1.GetOrderString(e.nIndex, 31).Substring(8)
        End If
        OrderStatusMsg(qd)
    End Sub
    Private Sub gTrade1_ConnectionLost(sender As System.Object, e As AxICE_GTRADELib._DICE_gTradeEvents_ConnectionLostEvent) Handles gTrade1.ConnectionLost
        status.tag = False
        status.txt = "ConnectionLost"
        statusMsg(status)
    End Sub
    Private Sub gTrade1_LoginFail(sender As System.Object, e As AxICE_GTRADELib._DICE_gTradeEvents_LoginFailEvent) Handles gTrade1.LoginFail
        status.tag = False
        status.txt = "LoginFail"
        statusMsg(status)
    End Sub
    Private Sub gTrade1_LoginReady(sender As System.Object, e As AxICE_GTRADELib._DICE_gTradeEvents_LoginReadyEvent) Handles gTrade1.LoginReady
        status.tag = True
        status.txt = "LoginReady"
        statusMsg(status)
        account = gTrade1.GetStatementString(0, 0, 0) '取出期貨帳號
    End Sub
    Private Sub gTrade1_Log(sender As System.Object, e As AxICE_GTRADELib._DICE_gTradeEvents_LogEvent) Handles gTrade1.Log
        Log(e.strLog.ToString())
    End Sub
    Public Sub sendOrders(comm As String, qty As Int16, price As Double, buySell As String)
        gTrade1.SetSecurities(1, "", Securities1) '下單跳出視窗, 1:彈出, 0:不彈出
        gTrade1.SetSecurities(2, "", Securities2) '刪單跳出視窗, 1:彈出, 0:不彈出
        gTrade1.SetOrderInfoString(0, "F") '0:委託類別,S:證券,F:期貨,O:選擇權,R:刪改單
        gTrade1.SetOrderInfoString(1, account) '1:  帳號
        gTrade1.SetOrderInfoString(2, "ICE.TWF." + comm)     '2:  商品ID
        gTrade1.SetOrderInfoString(3, "R")   '3:  委託別 : R:ROD I:IOC F:FOK
        gTrade1.SetOrderInfoString(4, buySell)      '4:  買賣別 : B S
        gTrade1.SetOrderInfoString(5, "L")   '5:  價格別 : L:限價 M:市價 P:一定範圍市價
        gTrade1.SetOrderInfoValue(6, price)        '6:  委託價格(數值)
        gTrade1.SetOrderInfoValue(7, qty)          '7:  委託數量(數值)
        gTrade1.SetOrderInfoString(10, "N")    '10: 當沖 : Y N
        gTrade1.SetOrderInfoString(11, genUserKey.instance.getKey())     '11: userKey
        gTrade1.SetOrderInfoValue(14, 0)              '14: 停損價格(數值)(0表無停損價)(只有期貨商品為必填欄位)
        gTrade1.SetOrderInfoString(15, "A")   '15: 倉別 : A:自動 O:新倉 C:平倉
        gTrade1.SetOrderInfoString(16, "")      '16: Call/Put
        gTrade1.SetOrderInfoValue(17, 0) '   17:履約價
        gTrade1.SetOrderInfoString(18, "")      '18:委託書號
        gTrade1.SetOrderInfoString(20, "")   '20:商品ID2
        gTrade1.SetOrderInfoString(21, "")   '21:買賣別2
        gTrade1.SetOrderInfoString(22, "")     '22:月份2
        gTrade1.SetOrderInfoString(23, "")    '23:CallPut2
        gTrade1.SetOrderInfoValue(24, 0) '24:履約價2
        gTrade1.ProcessOrder()
    End Sub
    Public Function Order(ByRef o As OrderData) As Int32
        gTrade1.SetSecurities(1, "", Securities1) '下單跳出視窗, 1:彈出, 0:不彈出
        gTrade1.SetSecurities(2, "", Securities2) '刪單跳出視窗, 1:彈出, 0:不彈出
        gTrade1.SetOrderInfoString(0, o.orderCategory) '0:委託類別,S:證券,F:期貨,O:選擇權,R:刪改單
        gTrade1.SetOrderInfoString(1, o.account) '1:  帳號
        gTrade1.SetOrderInfoString(2, o.symbolID)   '2:  商品ID
        gTrade1.SetOrderInfoString(3, o.orderType) '3:  委託別 : R:ROD I:IOC F:FOK
        gTrade1.SetOrderInfoString(4, o.buySell)      '4:  買賣別 : B S
        gTrade1.SetOrderInfoString(5, o.priceFlag)    '5:  價格別 : L:限價 M:市價 P:一定範圍市價
        gTrade1.SetOrderInfoValue(6, o.price)         '6:  委託價格(數值)
        gTrade1.SetOrderInfoValue(7, o.qty)           '7:  委託數量(數值)
        gTrade1.SetOrderInfoString(10, o.dayTrade)    '10: 當沖 : Y N
        gTrade1.SetOrderInfoString(11, o.userKey)     '11: userKey
        gTrade1.SetOrderInfoValue(14, 0)              '14: 停損價格(數值)(0表無停損價)(只有期貨商品為必填欄位)
        gTrade1.SetOrderInfoString(15, o.openClose)   '15: 倉別 : A:自動 O:新倉 C:平倉
        gTrade1.SetOrderInfoString(16, o.callPut)      '16: Call/Put
        gTrade1.SetOrderInfoValue(17, o.strikePrice)   '17:履約價
        gTrade1.SetOrderInfoString(18, o.orderno)      '18:委託書號
        gTrade1.SetOrderInfoString(20, o.symbolID2)   '20:商品ID2
        gTrade1.SetOrderInfoString(21, o.buySell2)    '21:買賣別2
        gTrade1.SetOrderInfoString(22, o.commYM2)     '22:月份2
        gTrade1.SetOrderInfoString(23, o.callPut2)    '23:CallPut2
        gTrade1.SetOrderInfoValue(24, o.strikePrice2) '24:履約價2
        Return gTrade1.ProcessOrder()
    End Function

End Class
