COCO研究院

 找回密碼
 註冊
搜索
查看: 18731|回復: 78

[API] 請問關於群益報價API + VB 2012的問題

[複製鏈接]
發表於 13-3-4 12:17 | 顯示全部樓層 |閱讀模式
各位前輩好,想請問關於群益報價API + VB 2012的問題…..

小弟在win7作業系統下,使用VS2012 Desktop的VB抓取群益報價API ,
目前只抓取台指期貨TX00報價,但是,每次收報價大概4分鐘後,
報價就自動停了,也沒有任何的錯誤訊息。

懇請有經驗的前輩指導一下,謝謝。

1. 前置作業如下:
  先把config.ini、SKQuote.exe、SKQuoteLib.dll、SKQuoteLib.lib等4個檔,
  複製貼到  /bin/Debug/內。

2. 完整程式碼如下:
Imports System.Runtime.InteropServices

Public Class Form1

    ' *** DLL  Import
    '* 0. SKQuoteLib_Initialize 初始使用者相關資訊
    <DllImport("SKQuoteLib.dll", CallingConvention:=CallingConvention.StdCall)> _
    Public Shared Function SKQuoteLib_Initialize(ByVal lpszLoginID As String, ByVal lpszPassword As String) As Integer
    End Function

    '* 1. SKQuoteLib_AttachConnectionCallBack 向報價函式庫註冊接收連線狀態的 Call back 函式位址
    <UnmanagedFunctionPointer(CallingConvention.StdCall)> _
    Public Delegate Sub FOnNotifyConnection(ByVal nKind As Integer, ByVal nCode As Integer)
    Shared Delegate_AttachConnection As New FOnNotifyConnection(AddressOf OnConnectionBack)

    <DllImport("SKQuoteLib.dll", CallingConvention:=CallingConvention.StdCall)> _
    Public Shared Function SKQuoteLib_AttachConnectionCallBack(ByVal Delegate_AttachConnection As FOnNotifyConnection) As Integer
    End Function

    '* 2. SKQuoteLib_AttachQuoteCallBack 當有索取的個股報價異動時,將透過此註冊的函式通知應用程式處理
    <UnmanagedFunctionPointer(CallingConvention.StdCall)> _
    Public Delegate Sub FOnNotifyQuote(ByVal sMarketNo As Short, ByVal sStockidx As Short)
    Public Shared Delegate_AttachQuote As New FOnNotifyQuote(AddressOf OnQuoteCallBack)

    <DllImport("SKQuoteLib.dll", CallingConvention:=CallingConvention.StdCall)> _
    Public Shared Function SKQuoteLib_AttachQuoteCallBack(ByVal Delegate_AttachQuote As FOnNotifyQuote) As Integer
    End Function

    '* 3. SKQuoteLib_EnterMonitor 建立與報價伺服器的連線
    <DllImport("SKQuoteLib.dll", CallingConvention:=CallingConvention.StdCall)> _
    Public Shared Function SKQuoteLib_EnterMonitor() As Integer
    End Function

    '* 4. SKQuoteLib_RequestStocks 要求伺服器針對pStockNos內的商品代號作報價通知的動作
    <DllImport("SKQuoteLib.dll", CallingConvention:=CallingConvention.StdCall, CharSet:=CharSet.Ansi)> _
    Public Shared Function SKQuoteLib_RequestStocks(ByRef psPageNo As Short, ByVal pStockNos As String) As Integer
    End Function

    '* 5. SKQuoteLib_GetStockByIndex 根據市場別編號與系統所編的特殊代號,取回股票報價的相關資訊
    <DllImport("SKQuoteLib.dll", CallingConvention:=CallingConvention.StdCall, CharSet:=CharSet.Ansi)> _
    Public Shared Function SKQuoteLib_GetStockByIndex(ByVal sMarketNo As Short, ByVal sIndex As Short, ByRef pStock As TStock) As Integer
    End Function

    '* 6. SKQuoteLib_LeaveMonitor 中斷與報價伺服器的連線
    <DllImport("SKQuoteLib.dll", CallingConvention:=CallingConvention.StdCall)> _
    Public Shared Function SKQuoteLib_LeaveMonitor() As Integer
    End Function

    ' *** 定義TStock 結構
    <StructLayout(LayoutKind.Sequential)> _
       Public Structure TStock
        Dim Stockidx As Short
        Dim Decimal1 As Short
        Dim TypeNo As Short
        Dim MarketNo As Byte
        <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=20)> Dim StockNo As String
        <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=10)> Dim StockName As String
        Dim Open As Integer
        Dim High As Integer
        Dim Low As Integer
        Dim Close As Integer
        Dim TickQty As Integer ' 單量
        Dim Ref As Integer     ' 昨收、參考價
        Dim Bid As Integer     ' 買價
        Dim Bc As Integer      ' 買量
        Dim Ask As Integer     ' 賣價
        Dim Ac As Integer      ' 賣量
        Dim TBc As Integer     ' 買盤量
        Dim TAc As Integer     ' 賣盤量
        Dim FutureOI As Integer ' 期貨未平倉 OI
        Dim TQty As Integer     ' 單量
        Dim YQty As Integer
        Dim Up As Integer       ' 漲停
        Dim Down As Integer     ' 跌停
    End Structure

    '*** 檢視  報價連線是否登入成功
    Public Shared Sub OnConnectionBack(ByVal Kind As Integer, ByVal Code As Integer)

        If Code = 0 Then
            MsgBox(" 報價伺服器登入成功!!")
        Else
            MsgBox("  報價中斷!!")
        End If

        '*** 要求伺服器針對 pStockNos內的商品代號作報價通知的動作
        Dim Status As Integer
        Dim ComIds As String
        ComIds = "TX00"

        Status = SKQuoteLib_RequestStocks(-1, ComIds)

    End Sub

    '*** 接收報價的位置
    Public Shared Sub OnQuoteCallBack(ByVal Market As Short, ByVal Index As Short)

        On Error Resume Next
        Dim Status As Integer
        Dim Stock As New TStock

        '*** 要求報價
        Status = SKQuoteLib_GetStockByIndex(Market, Index, Stock)

        '*** 接收台指期貨 TX00 報價的項目
        Form1.txtTxBid.Text = Stock.Bid / 100
        Form1.txtBuyQty.Text = Stock.Bc
        Form1.txtTxAsk.Text = Stock.Ask / 100
        Form1.txtSellQty.Text = Stock.Ac
        Form1.txtTxClose.Text = Stock.Close / 100
        Form1.txtTxTickQty.Text = Stock.TickQty

    End Sub

    '*** 程式開啟時 呼叫 API
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        Dim Status As Integer
        Status = Status + SKQuoteLib_Initialize("身分證字號", "密碼")
        Status = Status + SKQuoteLib_AttachConnectionCallBack(AddressOf OnConnectionBack)
        Status = Status + SKQuoteLib_AttachQuoteCallBack(AddressOf OnQuoteCallBack)
        Status = SKQuoteLib_EnterMonitor()

    End Sub

    '*** 結束報價按鈕
    Private Sub btnEnd_Click(sender As Object, e As EventArgs) Handles btnEnd.Click

        SKQuoteLib_LeaveMonitor()
        MsgBox("結束報價,離開 !")

    End Sub

End Class
發表於 13-3-4 14:06 | 顯示全部樓層
據小弟使用的經驗~群益API會不定時踢帳號,我也不知道為什麼?
(我使用的是excel vba接收的)
 樓主| 發表於 13-3-4 15:52 | 顯示全部樓層
我的 Excel 2007 VBA接收報價是很正常,
但是,類似的程式架構,
轉成 VB.net 2012,卻只能收到4分鐘的報價,
真是奇怪了,
還請各位高手,多多指教,
謝謝。
發表於 13-3-5 09:53 | 顯示全部樓層
我用老老的VB6,  沒這問題ㄟ

.net ㄜ...... 懶的學
發表於 13-3-5 10:36 | 顯示全部樓層
印象上五分鐘以內要在跟server要一次時間資訊, 不然好像是5分鐘會斷線...

我自己用C#是定幾分鐘去要一次server time...
 樓主| 發表於 13-3-5 12:22 | 顯示全部樓層
pazival01 發表於 13-3-5 10:36
印象上五分鐘以內要在跟server要一次時間資訊, 不然好像是5分鐘會斷線...

我自己用C#是定幾分鐘去要一次se ...

我試著每60秒呼叫一次伺服器時間,但是還是4分鐘左右,自動停止收報價。
還是很感謝 pazival01 兄的建議。
程式碼如下:
' * 呼叫伺服器時間
    <UnmanagedFunctionPointer(CallingConvention.StdCall)>_
    Public DelegateSub FOnNotifyServerTime(ByVal tHour As Int16, ByVal tMinute As Int16, ByValtSecond As Int16, ByVal tTotal As Int16)
    SharedDelegate_ServerTime As New FOnNotifyServerTime(AddressOf CBFunc_ServerTime)
    <DllImport("SKQuoteLib.dll",CallingConvention:=CallingConvention.StdCall)> _
    Public Shared FunctionSKQuoteLib_AttchServerTimeCallBack(ByVal Delegate_ServerTime As FOnNotifyServerTime)As Integer
    End Function
    <DllImport("SKQuoteLib.dll",CallingConvention:=CallingConvention.StdCall)> _
    Public Shared FunctionSKQuoteLib_RequestServerTime() As Integer
    End Function
'*** 60 秒呼叫一次伺服器時間
    Private SubTimer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
              SKQuoteLib_RequestServerTime()
           End Sub
    Public Shared SubCBFunc_ServerTime(ByVal tHour As Int16, ByVal tMinute As Int16, ByVal tSecond AsInt16, ByVal tTotal As Int16)
              '留空()
    End Sub
發表於 13-3-5 13:23 | 顯示全部樓層
印象上 好像有兩個function, 看看要不要用另一個

--
先用SKQuoteLib_AttchServerTimeCallBack 註冊

之後固定時間用SKQuoteLib_GetServerTime

我是用這個, 目前用C#沒有遇過斷線問題
 樓主| 發表於 13-3-5 14:21 | 顯示全部樓層
pazival01 發表於 13-3-5 13:23
印象上 好像有兩個function, 看看要不要用另一個

--

了解,我明天盤中再試試,
非常感謝您的指點。
 樓主| 發表於 13-3-6 11:31 | 顯示全部樓層
剛剛試著先用SKQuoteLib_AttchServerTimeCallBack 註冊
之後固定1分鐘用timer 呼叫SKQuoteLib_GetServerTime
    固定5分鐘用timer 呼叫 SKQuoteLib_RequestServerTime()
但是…還是只能收到 約4分鐘左右的報價,就收不到報價了….

更新後的全部程式碼如下:

Imports System.Runtime.InteropServices

Public Class Form1

    ' *** DLL  Import
    '* 0. SKQuoteLib_Initialize 初始使用者相關資訊
    <DllImport("SKQuoteLib.dll", CallingConvention:=CallingConvention.StdCall)> _
    Public Shared Function SKQuoteLib_Initialize(ByVal lpszLoginID As String, ByVal lpszPassword As String) As Integer
    End Function

    '* 1. SKQuoteLib_AttachConnectionCallBack 向報價函式庫註冊接收連線狀態的 Call back 函式位址
    <UnmanagedFunctionPointer(CallingConvention.StdCall)> _
    Public Delegate Sub FOnNotifyConnection(ByVal nKind As Integer, ByVal nCode As Integer)
    Shared Delegate_AttachConnection As New FOnNotifyConnection(AddressOf OnConnectionBack)

    <DllImport("SKQuoteLib.dll", CallingConvention:=CallingConvention.StdCall)> _
    Public Shared Function SKQuoteLib_AttachConnectionCallBack(ByVal Delegate_AttachConnection As FOnNotifyConnection) As Integer
    End Function

    '* 2. SKQuoteLib_AttachQuoteCallBack 當有索取的個股報價異動時,將透過此註冊的函式通知應用程式處理
    <UnmanagedFunctionPointer(CallingConvention.StdCall)> _
    Public Delegate Sub FOnNotifyQuote(ByVal sMarketNo As Short, ByVal sStockidx As Short)
    Public Shared Delegate_AttachQuote As New FOnNotifyQuote(AddressOf OnQuoteCallBack)

    <DllImport("SKQuoteLib.dll", CallingConvention:=CallingConvention.StdCall)> _
    Public Shared Function SKQuoteLib_AttachQuoteCallBack(ByVal Delegate_AttachQuote As FOnNotifyQuote) As Integer
    End Function

    '* 3. SKQuoteLib_EnterMonitor 建立與報價伺服器的連線
    <DllImport("SKQuoteLib.dll", CallingConvention:=CallingConvention.StdCall)> _
    Public Shared Function SKQuoteLib_EnterMonitor() As Integer
    End Function

    '* 4. SKQuoteLib_RequestStocks 要求伺服器針對pStockNos內的商品代號作報價通知的動作
    <DllImport("SKQuoteLib.dll", CallingConvention:=CallingConvention.StdCall, CharSet:=CharSet.Ansi)> _
    Public Shared Function SKQuoteLib_RequestStocks(ByRef psPageNo As Short, ByVal pStockNos As String) As Integer
    End Function

    '* 5. SKQuoteLib_GetStockByIndex 根據市場別編號與系統所編的特殊代號,取回股票報價的相關資訊
    <DllImport("SKQuoteLib.dll", CallingConvention:=CallingConvention.StdCall, CharSet:=CharSet.Ansi)> _
    Public Shared Function SKQuoteLib_GetStockByIndex(ByVal sMarketNo As Short, ByVal sIndex As Short, ByRef pStock As TStock) As Integer
    End Function

    '* 6. SKQuoteLib_LeaveMonitor 中斷與報價伺服器的連線
    <DllImport("SKQuoteLib.dll", CallingConvention:=CallingConvention.StdCall)> _
    Public Shared Function SKQuoteLib_LeaveMonitor() As Integer
    End Function

    '* 7. SKQuoteLib_GetStockByNo
    <DllImport("SKQuoteLib.dll", CallingConvention:=CallingConvention.StdCall, CharSet:=CharSet.Ansi)> _
    Public Shared Function SKQuoteLib_GetStockByNo(ByVal lpszStockNo As [String], ByRef pStock As TStock) As Integer
    End Function

    '* 8. SKQuoteLib_AttchServerTimeCallBack 呼叫伺服器時間
    <UnmanagedFunctionPointer(CallingConvention.StdCall)> _
    Public Delegate Sub FOnNotifyServerTime(ByVal tHour As Int16, ByVal tMinute As Int16, ByVal tSecond As Int16, ByVal tTotal As Int16)
    Shared Delegate_ServerTime As New FOnNotifyServerTime(AddressOf CBFunc_ServerTime)

    <DllImport("SKQuoteLib.dll", CallingConvention:=CallingConvention.StdCall)> _
    Public Shared Function SKQuoteLib_AttchServerTimeCallBack(ByVal Delegate_ServerTime As FOnNotifyServerTime) As Integer
    End Function

    '*9. SKQuoteLib_RequestServerTime 要求報價主機傳送目前時間
    <DllImport("SKQuoteLib.dll", CallingConvention:=CallingConvention.StdCall)> _
    Public Shared Function SKQuoteLib_RequestServerTime() As Integer
    End Function

    '* 10 . SKQuoteLib_GetServerTime 取得查詢的主機時間
    <DllImport("SKQuoteLib.dll", CallingConvention:=CallingConvention.StdCall, CharSet:=CharSet.Ansi)> _
    Public Shared Function SKQuoteLib_GetServerTime(ByRef pFormat05 As CFormat05) As Integer
    End Function


    ' *** 定義TStock 結構
    <StructLayout(LayoutKind.Sequential)> _
       Public Structure TStock
        Dim Stockidx As Short
        Dim Decimal1 As Short
        Dim TypeNo As Short
        Dim MarketNo As Byte
        <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=20)> Dim StockNo As String
        <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=10)> Dim StockName As String
        Dim Open As Integer
        Dim High As Integer
        Dim Low As Integer
        Dim Close As Integer
        Dim TickQty As Integer ' 單量
        Dim Ref As Integer     ' 昨收、參考價
        Dim Bid As Integer     ' 買價
        Dim Bc As Integer      ' 買量
        Dim Ask As Integer     ' 賣價
        Dim Ac As Integer      ' 賣量
        Dim TBc As Integer     ' 買盤量
        Dim TAc As Integer     ' 賣盤量
        Dim FutureOI As Integer ' 期貨未平倉 OI
        Dim TQty As Integer     ' 單量
        Dim YQty As Integer
        Dim Up As Integer       ' 漲停
        Dim Down As Integer     ' 跌停
    End Structure

    ' *** 定義主機時間格式   CFormat05
    <StructLayout(LayoutKind.Sequential)> _
    Public Structure CFormat05
        Dim m_sHour As Short
        Dim m_sMinute As Short
        Dim m_sSecond As Short
        Dim m_lTotal As Integer
    End Structure

    '*** 檢視  報價連線是否登入成功
    Public Shared Sub OnConnectionBack(ByVal Kind As Integer, ByVal Code As Integer)

        If Code = 0 Then
            MsgBox(" 報價伺服器登入成功!!")
        Else
            MsgBox("  報價中斷!!")
        End If

        '*** 要求伺服器針對 pStockNos內的商品代號作報價通知的動作
        Dim Status As Integer
        Dim ComIds As String
        ComIds = "TX00"

        Status = SKQuoteLib_RequestStocks(-1, ComIds)

    End Sub

    '*** 接收報價的位置
    Public Shared Sub OnQuoteCallBack(ByVal Market As Short, ByVal Index As Short)

        On Error Resume Next
        Dim Status As Integer
        Dim Stock As New TStock

        '*** 要求報價
        Status = SKQuoteLib_GetStockByIndex(Market, Index, Stock)
     
        '*** 接收台指期貨 TX00 報價的項目
        Form1.txtTxBid.Text = Stock.Bid / 100
        Form1.txtBuyQty.Text = Stock.Bc
        Form1.txtTxAsk.Text = Stock.Ask / 100
        Form1.txtSellQty.Text = Stock.Ac
        Form1.txtTxClose.Text = Stock.Close / 100
        Form1.txtTxTickQty.Text = Stock.TickQty

    End Sub

    '*** 程式開啟時 呼叫 API
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        Dim Status As Integer
        Status = Status + SKQuoteLib_Initialize("身分證字號", "密碼")
        Status = Status + SKQuoteLib_AttachConnectionCallBack(AddressOf OnConnectionBack)
        Status = Status + SKQuoteLib_AttachQuoteCallBack(AddressOf OnQuoteCallBack)

        Status = Status + SKQuoteLib_AttchServerTimeCallBack(AddressOf CBFunc_ServerTime)

        Status = SKQuoteLib_EnterMonitor()

    End Sub

    '*** 結束報價按鈕
    Private Sub btnEnd_Click(sender As Object, e As EventArgs) Handles btnEnd.Click

        SKQuoteLib_LeaveMonitor()
        MsgBox("結束報價,離開 !")

    End Sub

    '*** 接收 SKQuoteLib_AttchServerTimeCallBack
    Public Shared Sub CBFunc_ServerTime(ByVal tHour As Int16, ByVal tMinute As Int16, ByVal tSecond As Int16, ByVal tTotal As Int16)
        ' Form1.Label7.Text = tHour
        ' Form1.Label8.Text = tMinute
        ' Form1.Label9.Text = tSecond
        ' 留空()
      
    End Sub



    '*** 5分鐘 秒呼叫一次 SKQuoteLib_RequestServerTime() 伺服器時間  
    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
      
        Try
            SKQuoteLib_RequestServerTime()
        Catch ex As Exception
        End Try

          End Sub

    '*** 1 分鐘 秒呼叫一次 SKQuoteLib_GetServerTime 查詢的主機時間時間
    Private Sub Timer2_Tick(sender As Object, e As EventArgs) Handles Timer2.Tick
      
                 Dim pFormat05 As New CFormat05
         SKQuoteLib_GetServerTime(pFormat05)
      
    End Sub

End Class
 樓主| 發表於 13-3-7 08:48 | 顯示全部樓層
pazival01 發表於 13-3-5 13:23
印象上 好像有兩個function, 看看要不要用另一個

--

透過轉換,把『群益報價api』的程式碼由VB轉成C#,
pazival01兄與各位精通C#的高手指教,
目前只能收到約4分鐘的台指期貨TX00報價,
謝謝。
C#的程式碼如下:
using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Runtime.InteropServices;
public class Form1
{
        // *** DLL  Import
        //* 0. SKQuoteLib_Initialize 初始使用者相關資訊
        [DllImport("SKQuoteLib.dll",CallingConvention = CallingConvention.StdCall)]
        public static extern intSKQuoteLib_Initialize(string lpszLoginID, string lpszPassword);
        //* 1.SKQuoteLib_AttachConnectionCallBack 向報價函式庫註冊接收連線狀態的 Call back 函式位址
        [UnmanagedFunctionPointer(CallingConvention.StdCall)]
        public delegate voidFOnNotifyConnection(int nKind, int nCode);
        static FOnNotifyConnectionDelegate_AttachConnection = new FOnNotifyConnection(OnConnectionBack);
        [DllImport("SKQuoteLib.dll",CallingConvention = CallingConvention.StdCall)]
        public static extern intSKQuoteLib_AttachConnectionCallBack(FOnNotifyConnectionDelegate_AttachConnection);
        //* 2.SKQuoteLib_AttachQuoteCallBack 當有索取的個股報價異動時,將透過此註冊的函式通知應用程式處理
        [UnmanagedFunctionPointer(CallingConvention.StdCall)]
        public delegate voidFOnNotifyQuote(short sMarketNo, short sStockidx);
        public static FOnNotifyQuoteDelegate_AttachQuote = new FOnNotifyQuote(OnQuoteCallBack);
        [DllImport("SKQuoteLib.dll",CallingConvention = CallingConvention.StdCall)]
        public static extern intSKQuoteLib_AttachQuoteCallBack(FOnNotifyQuote Delegate_AttachQuote);
        //* 3.SKQuoteLib_EnterMonitor 建立與報價伺服器的連線
        [DllImport("SKQuoteLib.dll",CallingConvention = CallingConvention.StdCall)]
        public static extern intSKQuoteLib_EnterMonitor();
        //* 4.SKQuoteLib_RequestStocks 要求伺服器針對pStockNos內的商品代號作報價通知的動作
        [DllImport("SKQuoteLib.dll",CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
        public static extern intSKQuoteLib_RequestStocks(ref short psPageNo, string pStockNos);
        //* 5.SKQuoteLib_GetStockByIndex 根據市場別編號與系統所編的特殊代號,取回股票報價的相關資訊
        [DllImport("SKQuoteLib.dll",CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
        public static extern intSKQuoteLib_GetStockByIndex(short sMarketNo, short sIndex, ref TStock pStock);
        //* 6.SKQuoteLib_LeaveMonitor 中斷與報價伺服器的連線
        [DllImport("SKQuoteLib.dll",CallingConvention = CallingConvention.StdCall)]
        public static extern intSKQuoteLib_LeaveMonitor();
        //* 7.SKQuoteLib_GetStockByNo
        [DllImport("SKQuoteLib.dll",CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
        public static extern intSKQuoteLib_GetStockByNo(String lpszStockNo, ref TStock pStock);
        //* 8.SKQuoteLib_AttchServerTimeCallBack 呼叫伺服器時間
        [UnmanagedFunctionPointer(CallingConvention.StdCall)]
        public delegate voidFOnNotifyServerTime(Int16 tHour, Int16 tMinute, Int16 tSecond, Int16 tTotal);
        static FOnNotifyServerTimeDelegate_ServerTime = new FOnNotifyServerTime(CBFunc_ServerTime);
        [DllImport("SKQuoteLib.dll",CallingConvention = CallingConvention.StdCall)]
        public static extern intSKQuoteLib_AttchServerTimeCallBack(FOnNotifyServerTime Delegate_ServerTime);
        //*9.SKQuoteLib_RequestServerTime 要求報價主機傳送目前時間
        [DllImport("SKQuoteLib.dll",CallingConvention = CallingConvention.StdCall)]
        public static extern intSKQuoteLib_RequestServerTime();
        //* 10 .SKQuoteLib_GetServerTime 取得查詢的主機時間
        [DllImport("SKQuoteLib.dll",CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
        public static extern intSKQuoteLib_GetServerTime(ref CFormat05 pFormat05);
        // *** 定義TStock 結構
        [StructLayout(LayoutKind.Sequential)]
        public struct TStock
        {
                 public short Stockidx;
                 public shortDecimal1;
                 public short TypeNo;
                 public byteMarketNo;
                 [MarshalAs(UnmanagedType.ByValTStr,SizeConst = 20)]
                 public stringStockNo;
                 [MarshalAs(UnmanagedType.ByValTStr,SizeConst = 10)]
                 public stringStockName;
                 public int Open;
                 public int High;
                 public int Low;
                 public int Close;
                         // 單量
                 public int TickQty;
                         // 昨收、參考價
                 public int Ref;
                         // 買價
                 public int Bid;
                         // 買量
                 public int Bc;
                         // 賣價
                 public int Ask;
                         // 賣量
                 public int Ac;
                         // 買盤量
                 public int TBc;
                         // 賣盤量
                 public int TAc;
                         // 期貨未平倉 OI
                 public int FutureOI;
                         // 單量
                 public int TQty;
                 public int YQty;
                         // 漲停
                 public int Up;
                         // 跌停
                 public int Down;
        }
        // *** 定義主機時間格式   CFormat05
        [StructLayout(LayoutKind.Sequential)]
        public struct CFormat05
        {
                 public shortm_sHour;
                 public shortm_sMinute;
                 public shortm_sSecond;
                 public int m_lTotal;
        }
        //*** 檢視  報價連線是否登入成功
        public static voidOnConnectionBack(int Kind, int Code)
        {
                 if (Code == 0) {
                         Interaction.MsgBox("報價伺服器登入成功!!");
                 } else {
                         Interaction.MsgBox("  報價中斷!!");
                 }
                 //*** 要求伺服器針對 pStockNos內的商品代號作報價通知的動作
                 int Status = 0;
                 string ComIds =null;
                 ComIds ="TX00";
                 Status =SKQuoteLib_RequestStocks(ref -1, ComIds);
        }
        //*** 接收報價的位置
        public static voidOnQuoteCallBack(short Market, short Index)
        {
                  // ERROR: Not supported in C#:OnErrorStatement
                 int Status = 0;
                 TStock Stock = newTStock();
                 //*** 要求報價
                 Status =SKQuoteLib_GetStockByIndex(Market, Index, ref Stock);
                 //*** 接收台指期貨 TX00 報價的項目
                 Form1.txtTxBid.Text= Stock.Bid / 100;
                 Form1.txtBuyQty.Text= Stock.Bc;
                 Form1.txtTxAsk.Text= Stock.Ask / 100;
                 Form1.txtSellQty.Text= Stock.Ac;
                 Form1.txtTxClose.Text= Stock.Close / 100;
                 Form1.txtTxTickQty.Text= Stock.TickQty;
        }
        //*** 程式開啟時 呼叫 API
        private voidForm1_Load(object sender, EventArgs e)
        {
                 int Status = 0;
                 Status = Status +SKQuoteLib_Initialize("身分證字號", "密碼");
                 Status = Status +SKQuoteLib_AttachConnectionCallBack(OnConnectionBack);
                 Status = Status +SKQuoteLib_AttachQuoteCallBack(OnQuoteCallBack);
                 Status = Status +SKQuoteLib_AttchServerTimeCallBack(CBFunc_ServerTime);
                 Status =SKQuoteLib_EnterMonitor();
        }
        //*** 結束報價按鈕
        private voidbtnEnd_Click(object sender, EventArgs e)
        {
                 SKQuoteLib_LeaveMonitor();
                 Interaction.MsgBox("結束報價,離開 !");
        }
        //*** 接收SKQuoteLib_AttchServerTimeCallBack
        public static voidCBFunc_ServerTime(Int16 tHour, Int16 tMinute, Int16 tSecond, Int16 tTotal)
        {
                 // Form1.Label7.Text= tHour
                 // Form1.Label8.Text= tMinute
                 // Form1.Label9.Text= tSecond
                 // 留空()
        }
        //*** 5分鐘 秒呼叫一次SKQuoteLib_RequestServerTime() 伺服器時間  
        private voidTimer1_Tick(object sender, EventArgs e)
        {
                 try {
                         SKQuoteLib_RequestServerTime();
                 } catch (Exceptionex) {
                 }
        }
        //*** 1 分鐘 秒呼叫一次SKQuoteLib_GetServerTime 查詢的主機時間時間
        private voidTimer2_Tick(object sender, EventArgs e)
        {
                 CFormat05 pFormat05= new CFormat05();
                 SKQuoteLib_GetServerTime(refpFormat05);
        }
        public Form1()
        {
                 Load += Form1_Load;
        }
}
發表於 13-3-7 10:18 | 顯示全部樓層
我看了一下我的code

我的程式在SKQuoteLib_AttachQuoteCallBack 之後

還有加上

//2.3當有索取的個股成交明細有所異動,即透過向此註冊的 Call back 函式進行處理
SKQuoteLib_AttachTicksCallBack(  QuoteAttachTicks );

之後我又加了一大堆我看不懂的

//2.4
SKQuoteLib_AttachBest5CallBack(QuoteAttachBest5 );
SKQuoteLib_AttachStrikePricesCallBack(QuoteAttachStrikePrices );
SKQuoteLib_AttachKLineDataCallBack(QuoteAttachKLineData );

在這之後我就是下
QuoteEnterMonitor();

SKQuoteLib_AttchServerTimeCallBack(QuoteAttachServerTime);
SKQuoteLib_GetServerTime(ref CFtime);

----

之後就是每隔幾分鐘下 SKQuoteLib_GetServerTime 命令..

希望對你有幫助.
 樓主| 發表於 13-3-7 12:20 | 顯示全部樓層
pazival01 發表於 13-3-7 10:18
我看了一下我的code

我的程式在SKQuoteLib_AttachQuoteCallBack 之後

感謝pazival01兄指點,我再試看看。
 樓主| 發表於 13-3-7 12:23 | 顯示全部樓層
在修改過程中,發現一個奇怪的現象,

1. 原接收台指期貨TX00,買價、買量、賣價、賣量、成交價、單筆量,
    等6個報價選項時,約可收到4分鐘報價。
2. 若是接收台指期貨TX00的項目,縮減為買量、賣量,
   等2個報價選項時,約可收到12分鐘報價。

煩請 高手們指教,幫幫忙,
謝謝。
發表於 13-3-7 13:33 | 顯示全部樓層
我看了一下我的code, 我好像沒使用SKQuoteLib_RequestStocks 這個function

--
接收台指期貨 TX00 報價 我是這樣用

SKQuoteLib_GetStockByNo("TX00", ref Stock);

SKQuoteLib_RequestTicks(ref PageNo, "TX00");

SKQuoteLib_QuoteGetKLine("TX00", 0);

也許你可以這樣試試看,

--
也說不定是我哪裡誤用了, 拿到是錯誤的數據~ @@..
...
 樓主| 發表於 13-3-7 13:44 | 顯示全部樓層
pazival01 發表於 13-3-7 13:33
我看了一下我的code, 我好像沒使用SKQuoteLib_RequestStocks 這個function

--

感謝pazival01兄指點,我再試看看。
您需要登錄後才可以回帖 登錄 | 註冊

本版積分規則

手機版|Archiver|站長信箱|廣告洽詢|COCO研究院

GMT+8, 24-11-22 21:28

Powered by Discuz! X3.4

Copyright © 2001-2023, Tencent Cloud.

快速回復 返回頂部 返回列表
理財討論網站 |