C#

範例

spdOrderAPI 使用範例 (testSpdOrderAPI)

tspdOrderAPI 使用範例 (testSpdOrderAPI)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using megaSpeedyAPI;

namespace testPyOrderAPI
{
    class Program
    {
        static void Main(string[] args)
        {
            Program program = new Program();
            program.MainLoop();
            Console.WriteLine("Bye!");
            return;
        }

        private spdOrderAPI orderAPI = null;
        private bool exit = false;
        private bool FIsLogon = false;

        public void OnConnected()
        {
            Console.WriteLine("-------------- OnConnected() --------------");
              /*
              登入證券
              orderAPI.LogonProxy('身分證字號(大寫字母)', '電子登入密碼', '證券帳號七位數如:[0023456]')

            登入期貨
            orderAPI.LogonProxy('身分證字號(大寫字母)', '電子登入密碼', '期貨帳號七位數如:[0123457]')

            登入多市場逗號分隔
            orderAPI.LogonProxy('身分證字號(大寫字母)', '電子登入密碼', '證券帳號七位數如:[0023456],期貨帳號七位數如:[0123457]')
            */
            if (!orderAPI.LogonProxy("Z323456789", "1234", "0023456"))
            {
                string msg = orderAPI.GetLastErrorMsg();
                Console.WriteLine($"登入驗證失敗! {msg}");
            }

        }

        public void OnDisconnected()
        {
            Console.WriteLine("-------------- OnDisConnected() --------------");
            FIsLogon = false;
            exit = true;
        }

        public void OnLogonResponse(bool isLogonOK, string replyMsg)
        {
            Console.WriteLine("-------------- OnLogonResponse() --------------");
            Console.WriteLine($"--- IsSucceed:{isLogonOK} ReplyString:{replyMsg}");
            if (isLogonOK)
            {
                FIsLogon = true;
                //期貨設定下單帳號
                // orderAPI.SetAccount( 'TAIFEX', 'F030000', '期貨帳號七位數')
                orderAPI.SetAccount("TAIFEX", "F030000", "0123457");
                //證券設定下單帳號
                //orderAPI.SetAccount( 'TWSE', '分公司別四位數', '證券帳號七位數')
                orderAPI.SetAccount("TWSE", "7000", "0023456");
                //海外證券設定下單帳號
                //orderAPI.SetAccount( 'US', '分公司別四位數', '證券帳號六位數')
                orderAPI.SetAccount("US", "7000", "002345");
            }
            else
                Console.WriteLine($"登入失敗. {replyMsg}");
        }

        public void OnReplyNewOrder(Int64 nid, string UDD, string Symbol, double Price, string Side, Int32 OrderQty, string OrderType, string TimeinForce, string OrderID)
        {
            Console.WriteLine("-------------- OnReplyNewOrder() --------------");
            Console.WriteLine($"--- NID:{nid} UDD:{UDD} Symbol:{Symbol} Price:{Price} Side:{Side}");
            Console.WriteLine($"--- OrderQty:{OrderQty} OrderType:{OrderType} TimeInForce:{TimeinForce} OrderID:{OrderID}");
        }

        public void OnReplyCancelOrder(Int64 nid, string UDD, string Symbol, double Price, string Side, string OrderID)
        {
            Console.WriteLine("-------------- OnReplyCancelOrder() --------------");
            Console.WriteLine($"--- NID:{nid} UDD:{UDD} Symbol:{Symbol} Price:{Price} Side:{Side} OrderID:{OrderID}");
        }

        public void OnReplyReplaceOrder(Int64 nid, string UDD, string Symbol, double Price, string Side, Int32 OrderQty, string OrderType, string TimeInForce, string OrderID)
        {
            Console.WriteLine("-------------- OnReplyReplaceOrder() --------------");
            Console.WriteLine($"--- NID:{nid} UDD:{UDD} Symbol:{Symbol} Price:{Price} Side:{Side}");
            Console.WriteLine($"--- OrderQty:{OrderQty} OrderType:{OrderType} TimeInForce:{TimeInForce} OrderID:{OrderID}");
        }

        public void OnOrderReject(Int64 nid, string UDD, string ActionFrom, string ErrCode, string ErrMsg)
        {
            Console.WriteLine("-------------- OnOrderReject() --------------");
            Console.WriteLine($"--- NID:{nid} UDD:{UDD} Actionfrom:{ActionFrom} ErrCode:{ErrCode} ErrMsg:{ErrMsg}");
        }

        public void OnOrderFill(Int64 nid, string UDD, string OrderID, Int32 ReportSeq, double FillPrice, Int32 FillQty, string FillTime)
        {
            Console.WriteLine("-------------- OnOrderFill() --------------");
            Console.WriteLine($"--- NID:{nid} UDD:{UDD} OrderID:{OrderID} ReportSequence:{ReportSeq}");
            Console.WriteLine($"--- FillPrice:{FillPrice} FillQty:{FillQty} FillTime:{FillTime}");
        }

        public void MainLoop()
        {

            orderAPI = new spdOrderAPI();

            orderAPI.evOnConnected = new OnOrderConnected(this.OnConnected);
            orderAPI.evOnDisconnected = new OnOrderDisconnected(this.OnDisconnected);
            orderAPI.evOnLogonResponse = new OnOrderLogonResponse(this.OnLogonResponse);
            orderAPI.evOnNewOrder = new OnOrderReplyNewOrder(this.OnReplyNewOrder);
            orderAPI.evOnOrderCanceled = new OnOrderReplyCancelOrder(this.OnReplyCancelOrder);
            orderAPI.evOnOrderReplaced = new OnOrderReplyReplaceOrder(this.OnReplyReplaceOrder);
            orderAPI.evOnOrderRejected = new OnOrderRejectOrder(this.OnOrderReject);
            orderAPI.evOnOrderFilled = new OnOrderFill(this.OnOrderFill);

            exit = false;

            //orderAPI.EnableMEGACA("憑證檔路徑","身分證字號(大寫字母)","憑證密碼")
            if (orderAPI.EnableMEGACA("Z323456789.pfx", "Z323456789", "Z323456789"))
                Console.WriteLine("已成功設定簽章憑證");
            else
            {
                string msg = orderAPI.GetLastErrorMsg();
                Console.WriteLine(msg);
                return;
            }

            //orderAPI.Connect('主機位置', 56789, 設定連線逾時[秒,如設定5為5秒])
            orderAPI.Connect("spapitest.emega.com.tw", 56789, 10);

            while (!FIsLogon && !exit)
                Thread.Sleep(1000);

            while(!exit)
            {
                Console.WriteLine("[Select function]");
                Console.WriteLine(" 1:新單");
                Console.WriteLine(" 2:刪單委託");
                Console.WriteLine(" 3:改單");
                Console.WriteLine(" 4:複委託新單");
                Console.WriteLine(" 5:複委託刪單");
                Console.WriteLine(" 6.Stock-即時帳務查詢");
                Console.WriteLine(" 7.Stock-委託查詢");
                Console.WriteLine(" 8.Stock-成交查詢");
                Console.WriteLine(" 9.Future-委託查詢");
                Console.WriteLine("10.Future-成交查詢");
                Console.WriteLine("11.Future-即時未平倉查詢");
                Console.WriteLine("12.Future-即時權益暨保證金查詢");
                Console.WriteLine("13.密碼變更");
                Console.WriteLine("14.複委託庫存查詢");
                Console.WriteLine("15.複委託委託查詢");
                Console.WriteLine("16.複委託成交查詢");
                Console.WriteLine("17.複委託商品資料下載");
                Console.WriteLine("18.複委託幣別資料下載");
                Console.WriteLine("19.複委託市場資料下載");
                Console.Write(" 0:Quit ->");

                string selection = Console.ReadLine();
                selection = selection.TrimEnd('\n', '\r');
                int n = Convert.ToInt32(selection);
                if (n == 1)
                {
                    Console.Write("[Market] (fut:futures opt:options tse:stock otc:otc_stock) ->");
                    string Market = Console.ReadLine();
                    Market = Market.TrimEnd('\n', '\r');
                    Console.Write("[Symbol] ->");
                    string Symbol = Console.ReadLine();
                    Symbol= Symbol.TrimEnd('\n', '\r');
                    Console.Write("[Price] ->");
                    string Price = Console.ReadLine();
                    Price = Price.TrimEnd('\n', '\r');
                    Console.Write("[Quantity] ->");
                    string Qty = Console.ReadLine();
                    Qty = Qty.TrimEnd('\n', '\r');
                    Console.Write("[Side] (B:Buy S:Sell) ->");
                    string Side = Console.ReadLine();
                    Side = Side.TrimEnd('\n', '\r');
                    Console.Write("[OrderType] (L:限價 M:市價 P:Market with protection) ->");
                    string OrderType = Console.ReadLine();
                    OrderType = OrderType.TrimEnd('\n', '\r');
                    Console.Write("[TimeInForce] (R:ROD I:IOC F:FOK) ->");
                    string Tif = Console.ReadLine();
                    Tif = Tif.TrimEnd('\n', '\r');
                    Console.Write("[TadingSession] (N:普通 I:盤中零股 O:盤後零股 A:盤後定價) ->");
                    string TS = Console.ReadLine();
                    TS = TS.TrimEnd('\n', '\r');
                    Console.Write("[PositionEffect] (A:Auto(證券用A、期貨自動) D:DayTrade(期貨當沖)) ->");
                    string PE = Console.ReadLine();
                    PE = PE.TrimEnd('\n', '\r');
                    Console.Write("[TWSEOrdTyp] (空白:期貨下單 0:證券一般 3:證券融資 4:證券融劵 A:證券當沖) ->");
                    string TO = Console.ReadLine();
                    TO = TO.TrimEnd('\n', '\r');

                    Int64 rt = orderAPI.SendNewOrderEx(Market, "1A2B34C", Symbol, Convert.ToDouble(Price), Side, Convert.ToInt32(Qty), OrderType, Tif, TS, PE, TO);
                    if (rt == 0)
                    {
                        string err_msg = orderAPI.GetLastErrorMsg();
                        Console.WriteLine(err_msg);
                    }
                }
                else if (n == 2)
                {
                    Console.Write("[Market] (fut:futures opt:options tse:stock otc:otc_stock) ->");
                    string Market = Console.ReadLine();
                    Market = Market.TrimEnd('\n', '\r');
                    Console.Write("[Symbol] ->");
                    string Symbol = Console.ReadLine();
                    Symbol = Symbol.TrimEnd('\n', '\r');
                    Console.Write("[Price] ->");
                    string Price = Console.ReadLine();
                    Price = Price.TrimEnd('\n', '\r');
                    Console.Write("[Side] (B:Buy S:Sell) ->");
                    string Side = Console.ReadLine();
                    Side = Side.TrimEnd('\n', '\r');
                    Console.Write("[TadingSession] (N:普通 I:盤中零股 O:盤後零股 A:盤後定價) ->");
                    string TS = Console.ReadLine();
                    Console.Write("[OrderID] ->");
                    string OrderID = Console.ReadLine();
                    OrderID = OrderID.TrimEnd('\n', '\r');
                    TS = TS.TrimEnd('\n', '\r');
                    Console.Write("[TWSEOrdTyp] (空白:期貨下單 0:證券一般 3:證券融資 4:證券融劵 A:證券當沖) ->");
                    string TO = Console.ReadLine();
                    TO = TO.TrimEnd('\n', '\r');
                    Int64 rt = orderAPI.SendCancelOrderEx(Market, "testUDD_Cancel", Symbol, Convert.ToDouble(Price), Side, OrderID, TS, TO);
                    if (rt == 0)
                    {
                        string err_msg = orderAPI.GetLastErrorMsg();
                        Console.WriteLine(err_msg);
                    }
                }
                else if (n == 3)
                {
                    Console.Write("[Market] (fut:futures opt:options tse:stock otc:otc_stock) ->");
                    string Market = Console.ReadLine();
                    Market = Market.TrimEnd('\n', '\r');
                    Console.Write("[Symbol] ->");
                    string Symbol = Console.ReadLine();
                    Symbol = Symbol.TrimEnd('\n', '\r');
                    Console.Write("[Price] ->");
                    string Price = Console.ReadLine();
                    Price = Price.TrimEnd('\n', '\r');
                    Console.Write("[Quantity] ->");
                    string Qty = Console.ReadLine();
                    Qty = Qty.TrimEnd('\n', '\r');
                    Console.Write("[Side] (B:Buy S:Sell) ->");
                    string Side = Console.ReadLine();
                    Side = Side.TrimEnd('\n', '\r');
                    Console.Write("[OrderID] ->");
                    string OrderID = Console.ReadLine();
                    OrderID = OrderID.TrimEnd('\n', '\r');
                    Console.Write("[OrderType] (L:限價 M:市價 P:Market with protection) ->");
                    string OrderType = Console.ReadLine();
                    OrderType = OrderType.TrimEnd('\n', '\r');
                    Console.Write("[TimeInForce] (R:ROD I:IOC F:FOK) ->");
                    string Tif = Console.ReadLine();
                    Tif = Tif.TrimEnd('\n', '\r');
                    Console.Write("[TadingSession] (N:普通 I:盤中零股 O:盤後零股 A:盤後定價) ->");
                    string TS = Console.ReadLine();
                    TS = TS.TrimEnd('\n', '\r');
                    Console.Write("[PositionEffect] (A:Auto(證券用A、期貨自動) D:DayTrade(期貨當沖)) ->");
                    string PE = Console.ReadLine();
                    PE = PE.TrimEnd('\n', '\r');
                    Console.Write("[TWSEOrdTyp] (空白:期貨下單 0:證券一般 3:證券融資 4:證券融劵 A:證券當沖) ->");
                    string TO = Console.ReadLine();
                    TO = TO.TrimEnd('\n', '\r');
                    Int64 rt = orderAPI.SendReplaceOrderEx(Market, "testUDD_Replace", Symbol, OrderID, Side, Convert.ToDouble(Price), Convert.ToInt32(Qty), OrderType, Tif, TS, TO);
                    if (rt == 0)
                    {
                        string err_msg = orderAPI.GetLastErrorMsg();
                        Console.WriteLine(err_msg);
                    }
                }
                else if (n == 4)
                {
                    Console.Write("[Exchange] ->");
                    string Exchange = Console.ReadLine();
                    Exchange = Exchange.TrimEnd('\n', '\r');
                    Console.Write("[Symbol] ->");
                    string Symbol = Console.ReadLine();
                    Symbol = Symbol.TrimEnd('\n', '\r');
                    Console.Write("[Price] ->");
                    string Price = Console.ReadLine();
                    Price = Price.TrimEnd('\n', '\r');
                    Console.Write("[StopPrice] ->");
                    string StopPrice = Console.ReadLine();
                    StopPrice = StopPrice.TrimEnd('\n', '\r');
                    Console.Write("[Quantity] ->");
                    string Qty = Console.ReadLine();
                    Qty = Qty.TrimEnd('\n', '\r');
                    Console.Write("[Side] (B:Buy S:Sell) ->");
                    string Side = Console.ReadLine();
                    Side = Side.TrimEnd('\n', '\r');
                    Console.WriteLine("[OrderType]");
                    Console.WriteLine("    L: 限價");
                    Console.WriteLine("    M: 市價");
                    Console.WriteLine("   SL: 停損限價");
                    Console.WriteLine("   OL: 開盤LMT");
                    Console.WriteLine("   CL: 收盤LMT");
                    Console.WriteLine("   OM: 開盤MKT");
                    Console.WriteLine("   CM: 收盤MKT");
                    Console.WriteLine("   VL: VWAP LMT");
                    Console.WriteLine("   TL: TWAP LMT");
                    Console.WriteLine("   VM: VWAP MKT");
                    Console.WriteLine("   TM: TWAP MKT");
                    Console.Write(" ->");
                    string OrderType = Console.ReadLine();
                    OrderType = OrderType.TrimEnd('\n', '\r');
                    Console.Write("[TimeInForce] (R:ROD I:IOC F:FOK) ->");
                    string Tif = Console.ReadLine();
                    Tif = Tif.TrimEnd('\n', '\r');
                    Console.Write("[Currency] (1:TWD 2:NON-TWD ->");
                    string Currency = Console.ReadLine();
                    Currency = Currency.TrimEnd('\n', '\r');
                    Console.Write("[ExecInst] (G:All or None) ->");
                    string ExecInst = Console.ReadLine();
                    ExecInst = ExecInst.TrimEnd('\n', '\r');

                    Int64 rt = orderAPI.SendNewForeignOrder(Exchange, "1A2B34C", Symbol, Convert.ToDouble(Price), Convert.ToDouble(StopPrice), Side, Convert.ToInt32(Qty), OrderType, Tif, Currency, ExecInst);
                    if (rt == 0)
                    {
                        string err_msg = orderAPI.GetLastErrorMsg();
                        Console.WriteLine(err_msg);
                    }
                }
                else if (n == 5)
                {
                    Console.Write("[Exchange] ->");
                    string Exchange = Console.ReadLine();
                    Exchange = Exchange.TrimEnd('\n', '\r');
                    Console.Write("[Symbol] ->");
                    string Symbol = Console.ReadLine();
                    Symbol = Symbol.TrimEnd('\n', '\r');
                    Console.Write("[Side] (B:Buy S:Sell) ->");
                    string Side = Console.ReadLine();
                    Side = Side.TrimEnd('\n', '\r');
                    Console.Write("[OrderID] ->");
                    string OrderID = Console.ReadLine();
                    OrderID = OrderID.TrimEnd('\n', '\r');
                    Int64 rt = orderAPI.SendCancelForeignOrder(Exchange, "CxllForeign", Symbol, Side, OrderID);
                    if (rt == 0)
                    {
                        string err_msg = orderAPI.GetLastErrorMsg();
                        Console.WriteLine(err_msg);
                    }
                }
                else if (n == 6)
                {
                    Console.Write("[branch_id] ->");
                    string branch_id = Console.ReadLine();
                    branch_id = branch_id.TrimEnd('\n', '\r');
                    Console.Write("[cust_id] ->");
                    string cust_id = Console.ReadLine();
                    cust_id = cust_id.TrimEnd('\n', '\r');
                    Dictionary<string, string> query_param = new Dictionary<string, string>()
                    {
                        {"branch_id", branch_id },
                        {"cust_id", cust_id }
                    };

                    string s_json = orderAPI.MakeStockAccountInquriy(query_param);
                    Console.WriteLine(s_json);
                }
                else if (n == 7)
                {
                    // TODO ... 
                    Console.Write("[branch_id] ->");
                    string branch_id = Console.ReadLine();
                    branch_id = branch_id.TrimEnd('\n', '\r');
                    Console.Write("[cust_id] ->");
                    string cust_id = Console.ReadLine();
                    cust_id = cust_id.TrimEnd('\n', '\r');
                    Console.Write("[apcode] ->");
                    string apcode = Console.ReadLine();
                    apcode = apcode.TrimEnd('\n', '\r');
                    Console.Write("[market] ->");
                    string market = Console.ReadLine();
                    market = market.TrimEnd('\n', '\r');
                    Console.Write("[qry_type] ->");
                    string qry_type = Console.ReadLine();
                    qry_type = qry_type.TrimEnd('\n', '\r');
                    Dictionary<string, string> query_param = new Dictionary<string, string>()
                    {
                        {"branch_id", branch_id },
                        {"cust_id", cust_id },
                        {"apcode", apcode },
                        {"market",market },
                        {"qry_type",qry_type }
                    };

                    string s_json = orderAPI.QueryStkOrder(query_param);
                    Console.WriteLine(s_json);
                }
                else if (n == 8)
                {
                    Console.Write("[branch_id] ->");
                    string branch_id = Console.ReadLine();
                    branch_id = branch_id.TrimEnd('\n', '\r');
                    Console.Write("[cust_id] ->");
                    string cust_id = Console.ReadLine();
                    cust_id = cust_id.TrimEnd('\n', '\r');
                    Console.Write("[qry_type] ->");
                    string qry_type = Console.ReadLine();
                    qry_type = qry_type.TrimEnd('\n', '\r');
                    Dictionary<string, string> query_param = new Dictionary<string, string>()
                    {
                        {"branch_id", branch_id },
                        {"cust_id", cust_id },
                        {"qry_type", qry_type }
                    };

                    string s_json = orderAPI.QueryStkMatch(query_param);
                    Console.WriteLine(s_json);
                }
                else if (n == 9)
                {
                    // TODO ... 
                    Console.Write("[branch_id] ->");
                    string branch_id = Console.ReadLine();
                    branch_id = branch_id.TrimEnd('\n', '\r');
                    Console.Write("[cust_id] ->");
                    string cust_id = Console.ReadLine();
                    cust_id = cust_id.TrimEnd('\n', '\r');
                    Console.Write("[type] ->");
                    string type = Console.ReadLine();
                    type = type.TrimEnd('\n', '\r');
                    Console.Write("[apcode] ->");
                    string apcode = Console.ReadLine();
                    apcode = apcode.TrimEnd('\n', '\r');
                    Dictionary<string, string> query_param = new Dictionary<string, string>()
                    {
                        {"branch_id", branch_id },
                        {"cust_id", cust_id },
                        {"type", type },
                        {"apcode", apcode }
                    };

                    string s_json = orderAPI.QueryFutOrder(query_param);
                    Console.WriteLine(s_json);
                }
                else if (n == 10)
                {
                    Console.Write("[branch_id] ->");
                    string branch_id = Console.ReadLine();
                    branch_id = branch_id.TrimEnd('\n', '\r');
                    Console.Write("[cust_id] ->");
                    string cust_id = Console.ReadLine();
                    cust_id = cust_id.TrimEnd('\n', '\r');
                    Dictionary<string, string> query_param = new Dictionary<string, string>()
                    {
                        {"branch_id", branch_id },
                        {"cust_id", cust_id }
                    };

                    string s_json = orderAPI.QueryFutMatch(query_param);
                    Console.WriteLine(s_json);
                }
                else if (n == 11)
                {
                    Console.Write("[branch_id] ->");
                    string branch_id = Console.ReadLine();
                    branch_id = branch_id.TrimEnd('\n', '\r');
                    Console.Write("[cust_id] ->");
                    string cust_id = Console.ReadLine();
                    cust_id = cust_id.TrimEnd('\n', '\r');
                    Dictionary<string, string> query_param = new Dictionary<string, string>()
                    {
                        {"branch_id", branch_id },
                        {"cust_id", cust_id }
                    };

                    string s_json = orderAPI.QueryFutUncoverRT(query_param);
                    Console.WriteLine(s_json);
                }
                else if (n == 12)
                {
                    Console.Write("[branch_id] ->");
                    string branch_id = Console.ReadLine();
                    branch_id = branch_id.TrimEnd('\n', '\r');
                    Console.Write("[cust_id] ->");
                    string cust_id = Console.ReadLine();
                    cust_id = cust_id.TrimEnd('\n', '\r');
                    Console.Write("[type] ->");
                    string type = Console.ReadLine();
                    type = type.TrimEnd('\n', '\r');
                    Dictionary<string, string> query_param = new Dictionary<string, string>()
                    {
                        {"branch_id", branch_id },
                        {"cust_id", cust_id },
                        {"type", type }
                    };

                    string s_json = orderAPI.QueryMargin(query_param);
                    Console.WriteLine(s_json);
                }
                else if (n ==13)
                {
                    Console.Write("[cust_id] ->");
                    string cust_id = Console.ReadLine();
                    cust_id = cust_id.TrimEnd('\n', '\r');
                    Console.Write("[func] ->");
                    string func = Console.ReadLine();
                    func = func.TrimEnd('\n', '\r');
                    Dictionary<string, string> query_param = new Dictionary<string, string>()
                    {
                        {"cust_id", cust_id },
                        {"func", func }
                    };

                    Console.Write("[oldpwd] ->");
                    string oldpwd = Console.ReadLine();
                    oldpwd = oldpwd.TrimEnd('\n', '\r');
                    Console.Write("[newpwd] ->");
                    string newpwd = Console.ReadLine();
                    newpwd = newpwd.TrimEnd('\n', '\r');

                    query_param.Add("oldpwd", oldpwd);
                    query_param.Add("newpwd", newpwd);

                    string s_json = orderAPI.ChangePassword(query_param);
                    Console.WriteLine(s_json);
                }
                else if (n == 14)
                {
                    Console.Write("[cust_id] ->");
                    string cust_id = Console.ReadLine();
                    cust_id = cust_id.TrimEnd('\n', '\r');
                    Dictionary<string, string> query_param = new Dictionary<string, string>()
                    {
                        {"cust_id", cust_id }
                    };

                    string s_json = orderAPI.QueryForeignStockInventory(query_param);
                    Console.WriteLine(s_json);
                }
                else if (n == 15)
                {
                    Console.Write("[branch_id] ->");
                    string branch_id = Console.ReadLine();
                    branch_id = branch_id.TrimEnd('\n', '\r');
                    Console.Write("[cust_id] ->");
                    string cust_id = Console.ReadLine();
                    cust_id = cust_id.TrimEnd('\n', '\r');
                    Console.WriteLine("[qry_kind]");
                    Console.WriteLine("0:全部委託單");
                    Console.WriteLine("1:委託處理中");
                    Console.WriteLine("2:委託成功");
                    Console.WriteLine("3:全部成交");
                    Console.WriteLine("4:部分成交");
                    Console.WriteLine("5:刪單成功");
                    Console.WriteLine("6:刪單處理中");
                    Console.WriteLine("7:預約單");
                    Console.WriteLine("8:委託失敗");
                    Console.Write("->");
                    string qry_kind = Console.ReadLine();
                    qry_kind = qry_kind.TrimEnd('\n', '\r');
                    Dictionary<string, string> query_param = new Dictionary<string, string>()
                    {
                        {"branch_id", branch_id },
                        {"cust_id", cust_id },
                        {"qry_kind", qry_kind }
                    };

                    string s_json = orderAPI.QueryForeignStockOrder(query_param);
                    Console.WriteLine(s_json);
                }
                else if (n == 16)
                {
                    Console.Write("[branch_id] ->");
                    string branch_id = Console.ReadLine();
                    branch_id = branch_id.TrimEnd('\n', '\r');
                    Console.Write("[cust_id] ->");
                    string cust_id = Console.ReadLine();
                    cust_id = cust_id.TrimEnd('\n', '\r');
                    Dictionary<string, string> query_param = new Dictionary<string, string>()
                    {
                        {"branch_id", branch_id },
                        {"cust_id", cust_id }
                    };

                    string s_json = orderAPI.QueryForeignStockFilled(query_param);
                    Console.WriteLine(s_json);
                }
                else if (n == 17)
                {
                    Console.Write("[cust_id] ->");
                    string cust_id = Console.ReadLine();
                    cust_id = cust_id.TrimEnd('\n', '\r');
                    Console.Write("[keyword] ->");
                    string keyword = Console.ReadLine();
                    keyword = keyword.TrimEnd('\n', '\r');
                    Console.Write("[gz file name] ->");
                    string gzfname_prefix = Console.ReadLine();
                    gzfname_prefix = gzfname_prefix.TrimEnd('\n', '\r');
                    Dictionary<string, string> query_param = new Dictionary<string, string>()
                    {
                        {"cust_id", cust_id },
                        {"keyword", keyword }
                    };

                    string s_json = orderAPI.DownloadForeignStockProductData(query_param, gzfname_prefix);
                    Console.WriteLine(s_json);
                }
                else if (n == 18)
                {                    
                    Console.Write("[cust_id] ->");
                    string cust_id = Console.ReadLine();
                    cust_id = cust_id.TrimEnd('\n', '\r');
                    Dictionary<string, string> query_param = new Dictionary<string, string>()
                    {
                        {"cust_id", cust_id }
                    };

                    string s_json = orderAPI.DownloadForeignStockCurrencyData(query_param);
                    Console.WriteLine(s_json);
                }
                else if (n == 19)
                {
                    Console.Write("[cust_id] ->");
                    string cust_id = Console.ReadLine();
                    cust_id = cust_id.TrimEnd('\n', '\r');
                    Dictionary<string, string> query_param = new Dictionary<string, string>()
                    {
                        {"cust_id", cust_id }
                    };

                    string s_json = orderAPI.DownloadForeignStockMarketData(query_param);
                    Console.WriteLine(s_json);
                }
                else if (n == 0)
                {
                    break;
                }                
            }              
        }
    }
}
  

spdQuoteAPI 使用範例 (testSpdQuoteAPI)

spdQuoteAPI 使用範例 (testSpdQuoteAPI)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using megaSpeedyAPI;

namespace testPyQuoteAPI
{
    class Program
    {
        static void Main(string[] args)
        {
            Program program = new Program();
            program.MainLoop();
            Console.WriteLine("Bye!");
            return;
        }

        private spdQuoteAPI quoteAPI = null;
        private bool exit = false;

        Program()
        {

        }

        public void OnConnected()
        {
            Console.WriteLine("-------------- OnConnected() --------------");
        }

        public void OnDisconnected()
        {
            Console.WriteLine("-------------- OnDisConnected() --------------");
            exit = true;
        }

        public void OnOrderBook(string Exchange, string Symbol, string MsgTime, MsgOrderBook OrderBook)
        {
            Console.WriteLine("-------------- OnOrderBook() --------------");
            Console.WriteLine($"--- Exchange[{Exchange}]  Symbol[{Symbol}]  MsgTime[{MsgTime}]");
            Console.WriteLine($"--- Bid1={OrderBook.BidPrice1}({OrderBook.BidQty1})  Bid2={OrderBook.BidPrice2}({OrderBook.BidQty2})  Bid3={OrderBook.BidPrice3}({OrderBook.BidQty3})  Bid4={OrderBook.BidPrice4}({OrderBook.BidQty4})  Bid5={OrderBook.BidPrice5}({OrderBook.BidQty5})");
            Console.WriteLine($"--- Ask1={OrderBook.AskPrice1}({OrderBook.AskQty1})  Ask2={OrderBook.AskPrice2}({OrderBook.AskQty2})  Ask3={OrderBook.AskPrice3}({OrderBook.AskQty3})  Ask4={OrderBook.AskPrice4}({OrderBook.AskQty4})  Ask5={OrderBook.AskPrice5}({OrderBook.AskQty5})");
            Console.WriteLine($"--- DerivedBid={OrderBook.DerivedBidPrice}({OrderBook.DerivedBidQty})  DerivedAsk={OrderBook.DerivedAskPrice}({OrderBook.DerivedAskQty})");
            Console.WriteLine($"--- IsTestMatch={OrderBook.IsTestMatch}");
        }

        public void OnTrade(string Exchange, string Symbol, string MatchTime, double MatchPrice, int MatchQty, bool isTestMatch)
        {
            Console.WriteLine("--------OnTrade---------------");
            Console.WriteLine($"---Exchange[{Exchange}] symbol[{Symbol}]");
            Console.WriteLine($"---MatchTime={MatchTime} MatchPrice={MatchPrice} MatchQty={MatchQty}");
            Console.WriteLine($"---isTestMatch={isTestMatch}");
        }

        public void OnLogonResponse(bool isLogonOK, string ReplyMsg)
        {
            Console.WriteLine("-------------- OnLogonResponse() --------------");
            Console.WriteLine($"--- IsSucceed:{isLogonOK} ReplyString:{ReplyMsg}");
        }

        public void OnContractDownloadComplete(int TotalContracts)
        {
            Console.WriteLine($"-------------- OnContractDownloadComplete({TotalContracts}) --------------");
            Console.WriteLine($"已下載股票商品{quoteAPI.Stocks.Count}筆");
            Console.WriteLine($"已下載期貨商品{quoteAPI.Futures.Count}筆");
            Console.WriteLine($"已下載選擇權商品{quoteAPI.Options.Count}筆");

            if (quoteAPI.Stocks.ContainsKey("2330"))
                quoteAPI.SubscribeContract(quoteAPI.Stocks["2330"]);
            if (quoteAPI.Futures.ContainsKey("MXFE4"))
                quoteAPI.SubscribeContract(quoteAPI.Futures["MXFE4"]);
            if (quoteAPI.Futures.ContainsKey("TXFE4"))
                quoteAPI.SubscribeContract(quoteAPI.Futures["TXFE4"]);
        }

        public void MainLoop()
        {
            quoteAPI = new spdQuoteAPI();

            quoteAPI.evOnConnected = new OnQuoteConnected(this.OnConnected);
            quoteAPI.evOnDisconnected = new OnQuoteDisconnected(this.OnDisconnected);
            quoteAPI.evOnLogonResponse = new OnQuoteLogonResponse(this.OnLogonResponse);
            quoteAPI.evOnContractDownloadComplete = new OnQuoteContractDownloadComplete(this.OnContractDownloadComplete);
            quoteAPI.evOnOrderBook = new OnQuoteOrderBook(this.OnOrderBook);
            quoteAPI.evOnTrade = new OnQuoteTrade(this.OnTrade);

            exit = false;

            //訂閱檔數上限20檔
            //quoteAPI.Logon('IP', 34567, '身分證字號(大寫字母)', '電子登入密碼', True )
            //取消訂閱商品:UnsubscribeAll()
            quoteAPI.Logon("IP", 34567, "Z323456789", "1234", true);

            while (!exit)
            {
                Thread.Sleep(1000);
            }
        }
    }
}