2010年7月11日 星期日

使用 C# 介接群益的報價 API (SKQuoteLib.dll)

這張程式的來源在下方
我會放在這邊是做個備份
若有不妥請通知我移除


using System.Runtime.InteropServices;

public partial class Form1 : Form
{
/*
* DLL Import:
* 0. SKQuoteLib_Initialize
* 1.0 SKQuoteLib_AttachConnectionCallBack
* 1.1 SKQuoteLib_AttachQuoteCallBack
* 1.2 SKQuoteLib_AttachTicksCallBack
* 1.3 SKQuoteLib_AttachBest5CallBack
* 2. SKQuoteLib_EnterMonitor
* 3. SKQuoteLib_GetStockByNo
* 4. SKQuoteLib_RequestTicks
* 5. SKQuoteLib_GetTick
* END. SKQuoteLib_LeaveMonitor
*/

// 0.初始使用者相關資訊
[DllImport("SKQuoteLib.dll", CallingConvention = CallingConvention.StdCall)]
extern static int SKQuoteLib_Initialize(String lpszLoginID, String lpszPassword);

// 1.0.向報價函式庫註冊接收連線狀態的 Call back 函式位址
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
delegate void FOnNotifyConnection(Int32 nKind, Int32 nCode);
static FOnNotifyConnection Delegate_AttachConnection = new FOnNotifyConnection(CBFunc_AttachConnection);

[DllImport("SKQuoteLib.dll", CallingConvention = CallingConvention.StdCall)]
extern static int SKQuoteLib_AttachConnectionCallBack(FOnNotifyConnection Delegate_AttachConnection);

// 1.1.當有索取的個股報價異動時,將透過此註冊的函式通知應用程式處理
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
delegate void FOnNotifyQuote(Int16 sMarketNo, Int16 sStockidx);
static FOnNotifyQuote Delegate_AttachQuote = new FOnNotifyQuote(CBFunc_AttachQuote);

[DllImport("SKQuoteLib.dll", CallingConvention = CallingConvention.StdCall)]
extern static int SKQuoteLib_AttachQuoteCallBack(FOnNotifyQuote Delegate_AttachQuote);

// 1.2.當有索取的個股成交明細有所異動,即透過向此註冊的 Call back 函式進行處理
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
delegate void FOnNotifyTicks(Int16 sMarketNo, Int16 sStockidx, Int32 nPtr);
static FOnNotifyTicks Delegate_AttachTicks = new FOnNotifyTicks(CBFunc_AttachTicks);

[DllImport("SKQuoteLib.dll", CallingConvention = CallingConvention.StdCall)]
extern static int SKQuoteLib_AttachTicksCallBack(FOnNotifyTicks Delegate_AttachTicks);

// 1.3.當有索取的個股五檔價格有所異動,即透過向此註冊的Call back 函式進行處理
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
delegate void FOnNotifyBest5(Int16 sMarketNo, Int16 sStockidx);
static FOnNotifyBest5 Delegate_AttachBest5 = new FOnNotifyBest5(CBFunc_AttachBest5);

[DllImport("SKQuoteLib.dll", CallingConvention = CallingConvention.StdCall)]
extern static int SKQuoteLib_AttachBest5CallBack(FOnNotifyBest5 Delegate_AttachBest5);

// 2.建立與報價伺服器的連線
[DllImport("SKQuoteLib.dll", CallingConvention = CallingConvention.StdCall)]
extern static int SKQuoteLib_EnterMonitor();

// 3.根據股票代號取得報價資訊
[DllImport("SKQuoteLib.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
extern static int SKQuoteLib_GetStockByNo(String lpszStockNo, ref TStock pStock);

// 4.要求傳送成交明細以及五檔
[DllImport("SKQuoteLib.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
extern static int SKQuoteLib_RequestTicks(ref Int16 psPageNo, String pStockNo);

// 5.取得成交明細資訊
[DllImport("SKQuoteLib.dll", CallingConvention = CallingConvention.StdCall)]
extern static int SKQuoteLib_GetTick(Int16 sMarketNo, Int16 sStockidx, Int32 nPtr, ref TTick pTick);

// END.中斷與報價伺服器的連線
[DllImport("SKQuoteLib.dll", CallingConvention = CallingConvention.StdCall)]
extern static int SKQuoteLib_LeaveMonitor();


/*
* Struct:
* TStock
* TTick
*/

// [struct] TStock
[StructLayout(LayoutKind.Sequential)]
public struct TStock
{
public Int16 m_sStockidx; // 系統自行定義的股票代碼
public Int16 m_sDecimal; // 報價小數位數
public Int16 m_sTypeNo; // 類股分類
public byte m_cMarketNo;  // 市場代號0x00上市;0x01上櫃;0x02期貨;0x03選擇權;0x04興櫃
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 20)]
public String m_caStockNo;   // 股票代號
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 10)]
public String m_caName;   // 股票名稱
public Int32 m_nOpen; // 開盤價
public Int32 m_nHigh; // 最高價
public Int32 m_nLow; // 最低價
public Int32 m_nClose; // 成交價
public Int32 m_nTickQty; // 單量
public Int32 m_nRef; // 昨收、參考價
public Int32 m_nBid; // 買價
public Int32 m_nBc; // 買量
public Int32 m_nAsk;  // 賣價
public Int32 m_nAc;  // 賣量
public Int32 m_nTBc; // 買盤量
public Int32 m_nTAc; // 賣盤量
public Int32 m_nFutureOI; // 期貨未平倉 OI
public Int32 m_nTQty; // 總量
public Int32 m_nYQty; // 昨量
public Int32 m_nUp;  // 漲停價
public Int32 m_nDown; // 跌停價
}

// [struct] TTick
[StructLayout(LayoutKind.Sequential)]
public struct TTick
{
public Int32 m_nPtr; // 資料所在的位置(Key)
public Int32 m_nTime; // 時間
public Int32 m_nBid; // 買價
public Int32 m_nAsk; // 賣價
public Int32 m_nClose; // 成交價
public Int32 m_nQty; // 成交量
}


/*
* [In,Out] ref Variable
*/

// PageNo
static short PageNo = -1;

// TStock
public static TStock Stock = new TStock();

// Tick
public static TTick Tick = new TTick();




private void Form1_Load(object sender, EventArgs e)
{
    // 0.初始使用者相關資訊
    SKQuoteLib_Initialize("身分證號", "密碼");

    // 1.0.向報價函式庫註冊接收連線狀態的 Call back 函式位址
    SKQuoteLib_AttachConnectionCallBack(Delegate_AttachConnection);
   
    // 1.1.當有索取的個股報價異動時,將透過此註冊的函式通知應用程式處理
    SKQuoteLib_AttachQuoteCallBack(Delegate_AttachQuote);
   
    // 1.2.當有索取的個股成交明細有所異動,即透過向此註冊的Call back 函式進行處理
    SKQuoteLib_AttachTicksCallBack(Delegate_AttachTicks);
   
    // 1.3.當有索取的個股五檔價格有所異動,即透過向此註冊的Call back 函式進行處理
    SKQuoteLib_AttachBest5CallBack(Delegate_AttachBest5);

    // 2.建立與報價伺服器的連線
    SKQuoteLib_EnterMonitor();
}




// 1.0
public static void CBFunc_AttachConnection(int nKind, int nCode)
{
    Console.WriteLine("[Connection] Kind: {0}, Code: {1}", nKind, nCode);
}

// 1.1
public static void CBFunc_AttachQuote(short sMarketNo, short sStockidx)
{
    Console.WriteLine("[Quoto] MarketNo: {0}, Stockidx: {1}", sMarketNo, sStockidx);
    // 在這裡做 SKQuoteLib_GetStockByIndex
}

// 1.2
public static void CBFunc_AttachTicks(short sMarketNo, short sStockidx, int nPtr)
{
    Console.WriteLine("[Ticks] MarketNo: {0}, Stockidx: {1}", sMarketNo, sStockidx);
    // 5. SKQuoteLib_GetTick
    SKQuoteLib_GetTick(sMarketNo, sStockidx, nPtr, ref Tick);
}

// 1.3
public static void CBFunc_AttachBest5(short sMarketNo, short sStockidx)
{
    Console.WriteLine("[Best5] MarketNo: {0}, Stockidx: {1}", sMarketNo, sStockidx);
    // 在這裡做 SKQuoteLib_GetBest5
}



private void button1_Click(object sender, EventArgs e)
{
    // 3. SKQuoteLib_GetStockByNo
    SKQuoteLib_GetStockByNo(textBox1.Text, ref Stock);
    // 4. SKQuoteLib_RequestTicks
    SKQuoteLib_RequestTicks(ref PageNo, textBox1.Text);
}



private void button2_Click(object sender, EventArgs e)
{
    // END. SKQuoteLib_LeaveMonitor
    SKQuoteLib_LeaveMonitor();
}

}

參考資料
http://percyy-buy.blogspot.com/2009/12/c-api-skquotelibdll.html

沒有留言: