|
不說明!自已看,直接複制成class就可以直接使用
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
namespace StockAPI
{
[StructLayout(LayoutKind.Sequential)]
public struct STOCK
{
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; // 跌停價
}
[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; // 成交量
}
[StructLayout(LayoutKind.Sequential)]
public struct DataItem
{
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 13)]
public string caKeyNo; // 委託序號
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 2)]
public string caMarketType; // TS:證券 TA:盤後 TL:零股 TF:期貨 TO:選擇權
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 1)]
public string cType; // N:委託 C:取消 U:改量 D:成交 Q:報價
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 1)]
public string cOrderErr; // Y:失敗 T:逾時 N:正常
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 7)]
public string caBroker; // TS,TA,TL: 分公司代號 unit no , TF,TO: IB 代號 broker id
[MarshalAs(UnmanagedType.AnsiBStr, SizeConst = 7)]
public string caCustNo; // 交易帳號
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 3)]
public string caBuySell; // 證[0] B/S 買/賣,[1,2] 00 現股,01 代資,02 代券,03 融資,04 融券,20 零股,40 拍賣現股
// 期[0] B/S 買/賣,[1] Y 當沖, [2] I/R/F IOC / ROD / FOK
// 權[0] B/S 買/賣,[1] N/O 新倉 / 平倉,[2] I/R/F IOC / ROD / FOK
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 7)]
public string caExchangeID; // 交易所
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 20)]
public string caComId; // 前13碼商品代號 一個空白 後六碼商品年月
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 13)]
public string caStrikePrice; // 履約價 七位整數
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 5)]
public string caOrderNo; // 委託書號
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 13)]
public String caPrice; // 證券兩位小數, 期貨三位小數,
// 期貨委託:9999999999999為市價,
// 海外期貨,八位整數五位小數
// 其餘為根據 Type 種類,「委託」為委託價;「成交」為成交價
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 5)]
public string caNumerator; // 海外期貨回報用,分子,3位整數,2位小數
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 4)]
public string caDenominator; // 海外期貨回報用,分母 4位整數
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 13)]
public string caPrice1; // 八位整數五位小數 委託:觸發價 成交:0000000000000
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 5)]
public string caNumerator1; // 海外期貨回報用,分子 三位整數二位小數
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 4)]
public string caDenominator1; // 海外期貨回報用,分母 四位整數
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 13)]
public string caPrice2; // 八位整數五位小數 委託:0000000000000 成交:0000000000000
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 5)]
public string caNumerator2; // 海外期貨回報用,分子 三位整數二位小數
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 4)]
public string caDenominator2; // 海外期貨回報用,分母 四位整數
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 8)]
public string caQty; // TS TA張數/ TL股數/ TF TO 口數
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 4)]
public string caBeforeQty; // 參考欄位,異動變更前量
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 4)]
public string caAfterQty; // 參考欄位,異動變更後量
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 8)]
public string caDate; // 交易日期
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 6)]
public string caTime; // 交易時間
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 8)]
public string caOkSeq; // 成交序號
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 7)]
public string caSubID; // 子帳帳號
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 4)]
public string caSaleNo; // 營業員編號
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 1)]
public string cAgent; // 委託介面
};
public class SKStockAPI
{
#region 事件宣告
public delegate void ErrorMessage(string msg);
/// <summary>
/// 錯誤回報事件
/// </summary>
public event ErrorMessage OnErrorMessage;
public delegate void OnReplayConnectedEvent(string MSG, int Code);
/// <summary>
/// 連線交易回報伺服器事件
/// </summary>
public static event OnReplayConnectedEvent OnReplayConnected;
public delegate void OnReplayDisconnectEvent(string MSG, int Code);
/// <summary>
/// 交易回報伺服器斷線事件
/// </summary>
public static event OnReplayConnectedEvent OnReplayDisconnect;
public delegate void OnReplayDataEvent(ref DataItem Item);
/// <summary>
/// 交易回報資料接收事件
/// </summary>
public static event OnReplayDataEvent OnReplayDataReceive;
public delegate void OnOrderAccountEvent(string Data);
/// <summary>
/// 回傳帳戶資料事件,由執行GetUserAccount()後回傳
/// </summary>
public static event OnOrderAccountEvent OnOrderAccount;
public delegate void OnOrderOpenInterestEvent(string Data);
/// <summary>
/// 回傳未平倉內容事件,由執行 GetOpenInterest()後回傳
/// </summary>
public static event OnOrderOpenInterestEvent OnOrderOpenInterest;
public delegate void OnRealBalanceReportEvent(string Data);
/// <summary>
/// 回傳證劵庫存內容事件,由執行GetRealBalanceReport()後回傳
/// </summary>
public static event OnRealBalanceReportEvent OnRealBalanceReport;
public delegate void OnQuoteAttachConnectionEvent(int Kind, int Code);
/// <summary>
/// 連線報價伺服器事件
/// </summary>
public static event OnQuoteAttachConnectionEvent OnQuoteAttachConnection;
public delegate void OnQuoteAttachQuoteEvent(int MarketNo, int StockIex);
/// <summary>
/// 當所選取的商品報價有變動時的事件
/// </summary>
public static event OnQuoteAttachQuoteEvent OnQuoteAttachQuote;
public delegate void OnQuoteAttachTicksEvent(int MarketNo, int Stockidx, int Ptr);
/// <summary>
/// 當所選取的商品交易資料有變動時的事件
/// </summary>
public static event OnQuoteAttachTicksEvent OnQuoteAttachTicks;
public delegate void OnQuoteAttachBest5Event(int MarketNo, int Stockidx);
/// <summary>
/// 當所選取的五檔商品報價有變動時資料
/// </summary>
public static event OnQuoteAttachBest5Event OnQuoteAttachBest5;
public delegate void OnQuoteAttachStrikePricesEvent(string BProduct, string BName, string BCall, string BPut, int StrikePrice, int YearMonth);
/// <summary>
/// 當選擇權商品交易資料有變動時事件
/// </summary>
public static event OnQuoteAttachStrikePricesEvent OnQuoteAttachStrikePrices;
public delegate void OnQuoteAttachKLineDataEvent(string caStockNo, string caData);
/// <summary>
/// 當所選取的商品回傳K線技術資料時的事件
/// </summary>
public static event OnQuoteAttachKLineDataEvent OnQuoteAttachKLineData;
public delegate void OnQuoteAttachMarketTotEvent(char cMarketNo, int sPrt, long lTime, long lTotv, long lTots, long lTotc);
/// <summary>
/// 回傳大盤成交張筆資料事件
/// </summary>
public static event OnQuoteAttachMarketTotEvent OnQuoteAttachMarketTot;
public delegate void OnQuoteAttachMarketBuySellEvent(char cMarketNo, int sPrt, long lTime, long lBc, long lSc, long lBs, long lSs);
/// <summary>
/// 回傳大盤買賣張筆數事件
/// </summary>
public static event OnQuoteAttachMarketBuySellEvent OnQuoteAttachMarketBuySell;
public delegate void OnQuoteAttachMarketHighLowEvent(char cMarketNo, int sPrt, long lTime, short sUp, short sDown, short sHigh, short sLow, short sNoChange);
/// <summary>
/// 回傳大盤上漲下跌家數資料事件
/// </summary>
public static event OnQuoteAttachMarketHighLowEvent OnQuoteAttachMarketHighLow;
#endregion
#region APIs 呼叫
const string OrderDll = "SKOrderLib.dll";
const string QuoteDll = "SKQuoteLib.dll";
const string ReplyDll = "SKReplyLib.dll";
#region 下單API
//CallBack 指標
delegate void OrderAccountCallback(string Data);
delegate void OrderOpenInterestCallBack(string Data);
delegate void OrderOnRealBalanceReportCallBack(string Data);
//API
[DllImport(OrderDll, EntryPoint = "SKOrderLib_Initialize")]
private static extern int SKOrderLib_Initialize(string UserName, string PassWord);
[DllImport(OrderDll, EntryPoint = "SKOrderLib_ReadCertByID")]
private static extern int SKOrderLib_ReadCertByID(string UserID);
[DllImport(OrderDll, EntryPoint = "SendStockOrder")]
private static extern int SendStockOrder(string Account, string StockNo, int nPeriod, int nFlag, int nBuySell, string Price, int nQty,string Msg, ref int nBufferSize);
[DllImport(OrderDll, EntryPoint = "SendStockOrderAsync")]
private static extern int SendStockOrderAsync(string Account, string StockNo, int nPeriod, int nFlag, int nBuySell, string Price, int nQty);
[DllImport(OrderDll, EntryPoint = "SendFutureOrder")]
private static extern int SendFutureOrder(string Account, string StockNo, int nTradeType, int nDayTrade, int nBuySell, string Price, int nQty, string Msg, int nBufferSize);
[DllImport(OrderDll, EntryPoint = "SendFutureOrderAsync")]
private static extern int SendFutureOrderAsync(string Account, string StockNo, int nTradeType, int nDayTrade, int nBuySell, string Price, int nQty, string Msg, int nBufferSize);
[DllImport(OrderDll, EntryPoint = "SendOptionOrder")]
private static extern int SendOptionOrder(string Account, string StockNo, int nTradeType, int nNewClose, int nBuySell, string Price, int nQty, string Msg, int nBufferSize);
[DllImport(OrderDll, EntryPoint = "SendOptionOrderAsync")]
private static extern int SendOptionOrderAsync(string Account, string StockNo, int nTradeType, int nNewClose, int nBuySell, string Price, int nQty, string Msg, int nBufferSize);
[DllImport(OrderDll, EntryPoint = "CancelOrderBySeqNo")]
private static extern int CancelOrderBySeqNo(string Account, string SegNo);
[DllImport(OrderDll, EntryPoint = "CancelOrderByStockNo")]
private static extern int CancelOrderByStockNo(string Account , string StockNo);
[DllImport(OrderDll, EntryPoint = "DecreaseOrderBySeqNo")]
private static extern int DecreaseOrderBySeqNo(string SeqNo, int DecreaseQty);
[DllImport(OrderDll, EntryPoint = "GetUserAccount")]
private static extern int GetUserAccount();
[DllImport(OrderDll, EntryPoint = "GetOpenInterest")]
private static extern int GetOpenInterest(string Account);
[DllImport(OrderDll, EntryPoint = "GetRealBalanceReport")]
private static extern int GetRealBalanceReport(string Account);
[DllImport(OrderDll, EntryPoint = "RegisterOnAccountCallBack")]
private static extern int RegisterOnAccountCallBack(OrderAccountCallback cb);
[DllImport(OrderDll, EntryPoint = "RegisterOnOpenInterestCallBack")]
private static extern int RegisterOnOpenInterestCallBack(OrderOpenInterestCallBack cb);
[DllImport(OrderDll, CallingConvention = CallingConvention.StdCall, EntryPoint = "RegisterOnRealBalanceReportCallBack")]
private static extern int RegisterOnRealBalanceReportCallBack(OrderOnRealBalanceReportCallBack cb);
OrderAccountCallback OrderAccountDel = new OrderAccountCallback(OrderAccount);
OrderOpenInterestCallBack OrderOpenInterestDel = new OrderOpenInterestCallBack(OrderOpenInterest);
OrderOnRealBalanceReportCallBack OrderRealBalanceReportDel = new OrderOnRealBalanceReportCallBack(OrderRealBalanceReport);
#endregion
#region 回報API
// CallBack 指標
delegate void ReplayConnectCallBack(string MSG, int Code);
delegate void ReplayDisconnectCallBack(string MSG, int Code);
delegate void ReplayDataCallBack(ref DataItem Item);
//API 呼叫
[DllImport(ReplyDll, EntryPoint = "SKReplyLib_Initialize")]
private static extern int SKReplyLib_Initialize(string UserName, string PassWord);
[DllImport(ReplyDll, EntryPoint = "SKReplyLib_Connect")]
private static extern int SKReplyLib_Connect();
[DllImport(ReplyDll, EntryPoint = "SKReplyLib_Connect")]
private static extern int SKReplyLib_ConnectByID(string Account);
[DllImport(ReplyDll, EntryPoint = "SKReplyLib_Close")]
private static extern int SKReplyLib_Close();
[DllImport(ReplyDll, EntryPoint = "SKReplyLib_IsConnected")]
private static extern int SKReplyLib_IsConnected();
[DllImport(ReplyDll, EntryPoint = "RegisterOnConnectCallBack")]
private static extern int RegisterOnConnectCallBack(ReplayConnectCallBack cb);
[DllImport(ReplyDll, CallingConvention = CallingConvention.StdCall, EntryPoint = "RegisterOnDataCallBack")]
private static extern int RegisterOnDataCallBack(ReplayDataCallBack cb);
[DllImport(ReplyDll, EntryPoint = "RegisterOnDisconnectCallBack")]
private static extern int RegisterOnDisconnectCallBack(ReplayDisconnectCallBack cb);
static ReplayConnectCallBack ReplayConnectDel = new ReplayConnectCallBack(ReplayConnect);
static ReplayDisconnectCallBack ReplayDisconnectDel = new ReplayDisconnectCallBack(ReplayDisconnect);
static ReplayDataCallBack ReplayDataDel = new ReplayDataCallBack(ReplayData);
#endregion
#region 報價API
//Call Back 指標
delegate void QuoteAttachConnectionCallBack(int Kind, int Code);
delegate void QuoteAttachQuoteCallBack(int MarketNo, int StockIex);
delegate void QuoteAttachTicksCallBack(int MarketNo, int Stockidx, int Ptr);
delegate void QuoteAttachBest5CallBack(int MarketNo, int Stockidx);
delegate void QuoteAttachStrikePricesCallBack([Out, MarshalAs(UnmanagedType.LPTStr)] string BProduct, [Out, MarshalAs(UnmanagedType.LPTStr)] string BName, [Out, MarshalAs(UnmanagedType.LPTStr)] string BCall, [Out, MarshalAs(UnmanagedType.LPTStr)]string BPut, int StrikePrice, int YearMonth);
delegate void QuoteAttachKLineDataCallBack(string caStockNo, string caData);
delegate void QuoteAttachMarketTotCallBack(char cMarketNo, int sPrt, long lTime, long lTotv, long lTots, long lTotc);
delegate void QuoteAttachMarketBuySellCallBack(char cMarketNo, int sPrt, long lTime, long lBc, long lSc, long lBs, long lSs);
delegate void QuoteAttachMarketHighLowCallBack(char cMarketNo, int sPrt, long lTime, short sUp, short sDown, short sHigh, short sLow, short sNoChange);
//API
[DllImport(QuoteDll, EntryPoint = "SKQuoteLib_Initialize")]
private static extern int SKQuoteLib_Initialize(string UserName, string PassWord);
[DllImport(QuoteDll, EntryPoint = "SKQuoteLib_EnterMonitor")]
private static extern int SKQuoteLib_EnterMonitor();
[DllImport(QuoteDll, EntryPoint = "SKQuoteLib_LeaveMonitor")]
private static extern int SKQuoteLib_LeaveMonitor();
[DllImport(QuoteDll, EntryPoint = "SKQuoteLib_RequestStocks")]
private static extern int SKQuoteLib_RequestStocks(ref int PageNo, string StockNos);
[DllImport(QuoteDll, EntryPoint = "SKQuoteLib_RequestTicks")]
private static extern int SKQuoteLib_RequestTicks(ref int PageNo, string StockNos);
[DllImport(QuoteDll, EntryPoint = "SKQuoteLib_GetStockByIndex")]
private static extern int SKQuoteLib_GetStockByIndex(int MarketNo, int Index, ref STOCK pStock);
[DllImport(QuoteDll, EntryPoint = "SKQuoteLib_GetStockByNo")]
private static extern int SKQuoteLib_GetStockByNo(string StockNo, ref STOCK pStock);
[DllImport(QuoteDll, EntryPoint = "SKQuoteLib_GetTick")]
private static extern int SKQuoteLib_GetTick(int MarketNo, int StockIndex, int nPtr,ref TTick pTick);
[DllImport(QuoteDll, EntryPoint = "SKQuoteLib_GetBest5")]
private static extern int SKQuoteLib_GetBest5(int MarketNo, int StockIndex,ref BEST5 pBest5);
[DllImport(QuoteDll, EntryPoint = "SKQuoteLib_GetStrikePrices")]
private static extern int SKQuoteLib_GetStrikePrices();
[DllImport(QuoteDll, EntryPoint = "SKQuoteLib_GetKLine")]
private static extern int SKQuoteLib_GetKLine(string StockNo, int KLineType);
[DllImport(QuoteDll, EntryPoint = "SKQuoteLib_GetMarketTot")]
private static extern int SKQuoteLib_GetMarketTot(int Index, char MarketNo, ref int nPtr, ref long lTime, ref long lTotv, ref long lTotc);
[DllImport(QuoteDll, EntryPoint = "SKQuoteLib_GetMarketBuySell")]
private static extern int SKQuoteLib_GetMarketBuySell(int Index, string MarketNo, ref int nPtr, ref long lTime, ref long lBc, ref long lSc, ref long lBs, ref long lSs);
[DllImport(QuoteDll, EntryPoint = "SKQuoteLib_GetMarketHighLow")]
private static extern int SKQuoteLib_GetMarketHighLow(int Index, string MarketNo, ref int nPtr, ref long lTime, ref short sUp, ref short sDown, ref short sHigh, ref short sLow, ref short sNoChange);
[DllImport(QuoteDll, EntryPoint = "SKQuoteLib_AttachConnectionCallBack")]
private static extern int SKQuoteLib_AttachConnectionCallBack(QuoteAttachConnectionCallBack cb);
[DllImport(QuoteDll, CallingConvention = CallingConvention.StdCall, EntryPoint = "SKQuoteLib_AttachQuoteCallBack")]
private static extern int SKQuoteLib_AttachQuoteCallBack(QuoteAttachQuoteCallBack cb);
[DllImport(QuoteDll, EntryPoint = "SKQuoteLib_AttachTicksCallBack")]
private static extern int SKQuoteLib_AttachTicksCallBack(QuoteAttachTicksCallBack cb);
[DllImport(QuoteDll, EntryPoint = "SKQuoteLib_AttachBest5CallBack")]
private static extern int SKQuoteLib_AttachBest5CallBack(QuoteAttachBest5CallBack cb);
[DllImport(QuoteDll, EntryPoint = "SKQuoteLib_AttachStrikePricesCallBack")]
private static extern int SKQuoteLib_AttachStrikePricesCallBack(QuoteAttachStrikePricesCallBack cb);
[DllImport(QuoteDll, EntryPoint = "SKQuoteLib_AttachKLineDataCallBack")]
private static extern int SKQuoteLib_AttachKLineDataCallBack(QuoteAttachKLineDataCallBack cb);
[DllImport(QuoteDll, EntryPoint = "SKQuoteLib_AttachMarketTotCallBack")]
private static extern int SKQuoteLib_AttachMarketTotCallBack(QuoteAttachMarketTotCallBack cb);
[DllImport(QuoteDll, EntryPoint = "SKQuoteLib_AttachMarketBuySellCallBack")]
private static extern int SKQuoteLib_AttachMarketBuySellCallBack(QuoteAttachMarketBuySellCallBack cb);
[DllImport(QuoteDll, EntryPoint = "SKQuoteLib_AttachMarketHighLowCallBack")]
private static extern int SKQuoteLib_AttachMarketHighLowCallBack(QuoteAttachMarketHighLowCallBack cb);
QuoteAttachConnectionCallBack QuoteAttachConnectionDel = new QuoteAttachConnectionCallBack(QuoteAttachConnection);
QuoteAttachQuoteCallBack QuoteAttachQuoteDel = new QuoteAttachQuoteCallBack(QuoteAttachQuote);
QuoteAttachTicksCallBack QuoteAttachTicksDel = new QuoteAttachTicksCallBack(QuoteAttachTicks);
QuoteAttachBest5CallBack QuoteAttachBest5Del = new QuoteAttachBest5CallBack(QuoteAttachBest5);
QuoteAttachStrikePricesCallBack QuoteAttachStrikePricesDel = new QuoteAttachStrikePricesCallBack(QuoteAttachStrikePrices);
QuoteAttachKLineDataCallBack QuoteAttachKLineDataDel = new QuoteAttachKLineDataCallBack(QuoteAttachKLineData);
QuoteAttachMarketTotCallBack QuoteAttachMarketTotDel = new QuoteAttachMarketTotCallBack(QuoteAttachMarketTot);
QuoteAttachMarketBuySellCallBack QuoteAttachMarketBuySellDel = new QuoteAttachMarketBuySellCallBack(QuoteAttachMarketBuySell);
QuoteAttachMarketHighLowCallBack QuoteAttachMarketHighLowDel = new QuoteAttachMarketHighLowCallBack(QuoteAttachMarketHighLow);
#endregion
#endregion
private string UserName; //帳號
private string PassWord; //密碼
/// <summary>
/// 建構式
/// </summary>
/// <param name="UserName">帳號名稱(身份證字號)</param>
/// <param name="PassWord">帳號密碼</param>
public SKStockAPI(string UserName, String PassWord)
{
this.UserName = UserName;
this.PassWord = PassWord;
}
/// <summary>
/// 解構式
/// </summary>
~SKStockAPI()
{
}
#region 公用
/// <summary>
/// 初始化Stock API
/// </summary>
public void Init()
{
try
{
//初始化API
int OrderMSG = SKOrderLib_Initialize(UserName, PassWord);
if (OrderMSG != (int)MessageType.SK_SUCCESS)
{
OnErrorMessage("下單API初始化錯誤 CODE=" + OrderMSG.ToString());
return;
}
int ReplayMSG = SKReplyLib_Initialize(UserName, PassWord);
if (ReplayMSG != (int)MessageType.SK_SUCCESS)
{
OnErrorMessage("回報API初始化錯誤 CODE=" + ReplayMSG.ToString());
return;
}
int QuoteMSG = SKQuoteLib_Initialize(UserName, PassWord);
if (QuoteMSG != (int)MessageType.SK_SUCCESS)
{
OnErrorMessage("報價API初始化錯誤 CODE=" + QuoteMSG.ToString());
return;
}
int ReadCertMSG = SKOrderLib_ReadCertByID(UserName);
if (ReadCertMSG != (int)MessageType.SK_SUCCESS)
{
OnErrorMessage("讀取交易憑證錯誤 CODE =" + ReadCertMSG.ToString());
}
//註冊Replay CallBack
RegisterOnConnectCallBack(ReplayConnectDel);
RegisterOnDisconnectCallBack(ReplayDisconnectDel);
RegisterOnDataCallBack(ReplayDataDel);
//註冊Order CallBack
RegisterOnAccountCallBack(OrderAccountDel);
RegisterOnOpenInterestCallBack(OrderOpenInterestDel);
RegisterOnRealBalanceReportCallBack(OrderRealBalanceReportDel);
//註冊Quote CallBack
SKQuoteLib_AttachConnectionCallBack(QuoteAttachConnectionDel);
SKQuoteLib_AttachQuoteCallBack(QuoteAttachQuoteDel);
SKQuoteLib_AttachTicksCallBack(QuoteAttachTicksDel);
SKQuoteLib_AttachBest5CallBack(QuoteAttachBest5Del);
SKQuoteLib_AttachStrikePricesCallBack(QuoteAttachStrikePricesDel);
SKQuoteLib_AttachKLineDataCallBack(QuoteAttachKLineDataDel);
//SKQuoteLib_AttachMarketTotCallBack(QuoteAttachMarketTotDel);
//SKQuoteLib_AttachMarketBuySellCallBack(QuoteAttachMarketBuySellDel);
//SKQuoteLib_AttachMarketHighLowCallBack(QuoteAttachMarketHighLowDel);
}
catch (Exception ex)
{
OnErrorMessage("初始化錯誤!" + ex.Message);
}
}
#region PUBLIC Quote API Function
/// <summary>
/// 連線報價伺服器
/// </summary>
/// <returns></returns>
public int QuoteEnterMonitor()
{
return SKQuoteLib_EnterMonitor();
}
/// <summary>
/// 離線報價伺服器
/// </summary>
/// <returns></returns>
public int QuoteLeaveMonitor()
{
return SKQuoteLib_LeaveMonitor();
}
/// <summary>
/// 要求傳送報價
/// </summary>
/// <param name="PageNo">頁碼</param>
/// <param name="StockNos">股票代碼</param>
/// <returns></returns>
public int RequestStocks(ref int PageNo, string StockNos)
{
return SKQuoteLib_RequestStocks(ref PageNo, StockNos);
}
/// <summary>
/// 要求傳送成交明細資料
/// </summary>
/// <param name="PageNo">頁碼</param>
/// <param name="StockNo">股票代碼</param>
/// <returns></returns>
public int RequestTicks(ref int PageNo, string StockNo)
{
return SKQuoteLib_RequestTicks(ref PageNo, StockNo);
}
/// <summary>
/// 依股票編號取得股票報價資料
/// </summary>
/// <param name="StockNo">股票編號</param>
/// <param name="pSTock">SSTOCK 物件</param>
/// <returns></returns>
public int GetStockByNo(string StockNo, ref STOCK pSTock)
{
return SKQuoteLib_GetStockByNo(StockNo, ref pSTock);
}
/// <summary>
/// 依索引取得股票報價資料
/// </summary>
/// <param name="MarketNo">市場編號</param>
/// <param name="Index">索引</param>
/// <param name="pStock">Stock 物件</param>
/// <returns></returns>
public int GetStockByIndex(int MarketNo, int Index, ref STOCK pStock)
{
return SKQuoteLib_GetStockByIndex(MarketNo, Index, ref pStock);
}
/// <summary>
/// 取得成交明細資料
/// </summary>
/// <param name="MarketNo">市場編號</param>
/// <param name="Index">索引</param>
/// <param name="nPtr">指標</param>
/// <param name="pTick">TTICK物件</param>
/// <returns></returns>
public int GetTick(int MarketNo, int Index, int nPtr,ref TTick pTick)
{
return SKQuoteLib_GetTick(MarketNo, Index, nPtr, ref pTick);
}
/// <summary>
/// 取得五檔名細
/// </summary>
/// <param name="MarketNo">市場編號</param>
/// <param name="StockIndex">商品索引編號</param>
/// <param name="pBase5">BASE5 物件</param>
/// <returns></returns>
public int QuoteGetBest5(int MarketNo,int StockIndex,ref BEST5 pBase5)
{
return SKQuoteLib_GetBest5(MarketNo, StockIndex, ref pBase5);
}
/// <summary>
/// 取得選擇權報價
/// </summary>
/// <returns></returns>
public int QuoteGetStrikePrices()
{
return SKQuoteLib_GetStrikePrices();
}
/// <summary>
/// 取得商品k線資訊
/// </summary>
/// <param name="StockNo">商品編號</param>
/// <param name="KLineType">0 = 1分鐘線 1 = 5分鐘線。2 = 30分鐘線。3 =日線288天。4 =完整日線。5 =週線。 6 =月線。</param>
/// <returns></returns>
public int QuoteGetKLine(string StockNo,int KLineType)
{
return SKQuoteLib_GetKLine(StockNo,KLineType);
}
#endregion
#region Public Order API Function
/// <summary>
/// 送出證劵委託單
/// </summary>
/// <param name="Account">證券帳號,分公司四碼+帳號7碼</param>
/// <param name="StockNo">委託股票代號</param>
/// <param name="nPeriod">0:盤中 1:盤後 2:零股</param>
/// <param name="nFlag">0:現股 1:融資 2:融券</param>
/// <param name="nByeSell">0:買進 1:賣出</param>
/// <param name="Price">委託價格,「M」表示參考價(昨收價)</param>
/// <param name="nQty">整股交易為張數,如果是零股則為股數</param>
/// <param name="Msg">回覆訊息,如果回傳值為 0表示委託成功,訊息內容則為13碼的委託序號</param>
/// <param name="nBufferSize"> Buffer 寬度,如果傳入的 Buffer 寬度不足,則訊息內容將會被截斷以符合 Buffer 的大小,回傳會回覆真正填入 Buffer 的訊息長度</param>
/// <returns></returns>
public int SendStockOrders(string Account,string StockNo,int nPeriod,int nFlag,int nBuySell,string Price,int nQty,string Msg,ref int nBufferSize)
{
return SendStockOrder(Account, StockNo, nPeriod, nFlag, nBuySell, Price, nQty, Msg, ref nBufferSize);
}
/// <summary>
/// 送出證劵非同步委託單
/// </summary>
/// <param name="Account">證券帳號,分公司四碼+帳號7碼</param>
/// <param name="StockNo">委託股票代號</param>
/// <param name="nPeriod">0:盤中 1:盤後 2:零股</param>
/// <param name="nFlag">0:現股 1:融資 2:融券</param>
/// <param name="nByeSell">0:買進 1:賣出</param>
/// <param name="Price">委託價格,「M」表示參考價(昨收價)</param>
/// <param name="nQty">整股交易為張數,如果是零股則為股數</param>
/// <param name="Msg">回覆訊息,如果回傳值為 0表示委託成功,訊息內容則為13碼的委託序號</param>
/// <param name="nBufferSize"> Buffer 寬度,如果傳入的 Buffer 寬度不足,則訊息內容將會被截斷以符合 Buffer 的大小,回傳會回覆真正填入 Buffer 的訊息長度</param>
/// <returns></returns>
public int SendStockOrdersAsync(string Account, string StockNo, int nPeriod, int nFlag, int nBuySell, string Price, int nQty)
{
return SendStockOrderAsync(Account, StockNo, nPeriod, nFlag, nBuySell, Price, nQty);
}
/// <summary>
/// 傳送期貨委託單
/// </summary>
/// <param name="Account">期貨帳號,IB+帳號</param>
/// <param name="StockNo">委託期貨代號</param>
/// <param name="nTradeType">0:ROD 1:IOC 2:FOK</param>
/// <param name="nDayTrade">當沖0:否 1:是,可當沖商品請參考交易所規定。</param>
/// <param name="nBuySell">買賣別,0:買進 1:賣出</param>
/// <param name="Price">委託價格,「M」表示市價</param>
/// <param name="nQty">交易口數</param>
/// <param name="Msg">回覆訊息,如果回傳值為 0表示委託成功,訊息內容則為13碼的委託序號</param>
/// <param name="nBufferSize"> Buffer 寬度,如果傳入的 Buffer 寬度不足,則訊息內容將會被截斷以符合 Buffer 的大小,回傳會回覆真正填入 Buffer 的訊息長度</param>
/// <returns></returns>
public int SendFutureOrders(string Account, string StockNo, int nTradeType, int nDayTrade, int nBuySell, string Price, int nQty, string Msg, int nBufferSize)
{
return SendFutureOrder(Account, StockNo, nTradeType, nDayTrade, nBuySell, Price, nQty, Msg, nBufferSize);
}
/// <summary>
/// 取得帳戶資料
/// </summary>
/// <returns></returns>
public int OrderGetUserAccount()
{
return GetUserAccount();
}
/// <summary>
/// 取得證劵即時庫存資料
/// </summary>
/// <param name="Account">分公司+帳號</param>
/// <returns></returns>
public int OrderGetRealBalanceReport(string Account)
{
return GetRealBalanceReport(Account);
}
public int GetOpenInterestReport(string Account)
{
return GetOpenInterest(Account);
}
/// <summary>
/// 取消下單
/// </summary>
/// <param name="StockNo">股票代號</param>
/// <returns></returns>
public int CancelOrder(string Account , string StockNo)
{
return CancelOrderByStockNo(Account, StockNo);
}
#endregion
#region Public Replay API Function
/// <summary>
/// 連接回報伺服器
/// </summary>
/// <returns></returns>
public int ReplyConnect()
{
return SKReplyLib_Connect();
}
/// <summary>
/// 依帳戶名稱連線回報伺服器
/// </summary>
/// <param name="Account"></param>
/// <returns></returns>
public int ReplyConnectByID(string Account)
{
return SKReplyLib_ConnectByID(Account);
}
/// <summary>
/// 關閉回報伺服器
/// </summary>
/// <returns></returns>
public int ReplyClose()
{
return SKReplyLib_Close();
}
/// <summary>
/// 取得回報伺服器是否連線,回傳值0尚未連線,回傳值1已連線
/// </summary>
/// <returns></returns>
public int ReplyIsConnected()
{
return SKReplyLib_IsConnected();
}
#endregion
#endregion
#region 內部回呼
private static void ReplayConnect(string MSG, int Code)
{
if (OnReplayConnected != null) OnReplayConnected(MSG,Code);
}
private static void ReplayDisconnect(string MSG, int Code)
{
if (OnReplayDisconnect != null) OnReplayDisconnect(MSG,Code);
}
private static void ReplayData(ref DataItem Item)
{
if (OnReplayDataReceive != null) OnReplayDataReceive(ref Item);
}
private static void OrderAccount(string Data)
{
if (OnOrderAccount != null) OnOrderAccount(Data);
}
private static void OrderOpenInterest(string Data)
{
if (OnOrderOpenInterest != null) OnOrderOpenInterest(Data);
}
private static void OrderRealBalanceReport(string Data)
{
if (OnRealBalanceReport != null) OnRealBalanceReport(Data);
}
private static void QuoteAttachConnection(int Kind, int Code)
{
if (OnQuoteAttachConnection != null) OnQuoteAttachConnection(Kind, Code);
}
private static void QuoteAttachQuote(int MarketNo, int StockIex)
{
if (OnQuoteAttachQuote != null) OnQuoteAttachQuote(MarketNo, StockIex);
}
private static void QuoteAttachTicks(int MarketNo, int Stockidx, int Ptr)
{
if (OnQuoteAttachTicks != null) OnQuoteAttachTicks(MarketNo, Stockidx, Ptr);
}
private static void QuoteAttachBest5(int MarketNo, int Stockidx)
{
if (OnQuoteAttachBest5 != null) OnQuoteAttachBest5(MarketNo, Stockidx);
}
private static void QuoteAttachStrikePrices(string BProduct, string BName, string BCall, string BPut, int StrikePrice, int YearMonth)
{
if (OnQuoteAttachStrikePrices != null) OnQuoteAttachStrikePrices(BProduct, BName, BCall, BPut, StrikePrice, YearMonth);
}
private static void QuoteAttachKLineData(string caStockNo, string caData)
{
if (OnQuoteAttachKLineData != null) OnQuoteAttachKLineData(caStockNo, caData);
}
private static void QuoteAttachMarketTot(char cMarketNo, int sPrt, long lTime, long lTotv, long lTots, long lTotc)
{
if (OnQuoteAttachMarketTot != null) OnQuoteAttachMarketTot(cMarketNo, sPrt, lTime, lTotv, lTots, lTotc);
}
private static void QuoteAttachMarketBuySell(char cMarketNo, int sPrt, long lTime, long lBc, long lSc, long lBs, long lSs)
{
if (OnQuoteAttachMarketBuySell != null) OnQuoteAttachMarketBuySell(cMarketNo, sPrt, lTime, lBc, lSc, lBs, lSs);
}
private static void QuoteAttachMarketHighLow(char cMarketNo, int sPrt, long lTime, short sUp, short sDown, short sHigh, short sLow, short sNoChange)
{
if (OnQuoteAttachMarketHighLow != null) OnQuoteAttachMarketHighLow(cMarketNo, sPrt, lTime, sUp, sDown, sHigh, sLow, sNoChange);
}
#endregion
}
}
|
評分
-
查看全部評分
|