using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.IO;
using System.Collections;

namespace TouchancegTradeAPISample
{
    public partial class Form1 : Form
    {
        //cccc D
        [DllImport("kernel32")]
        static extern int GetPrivateProfileSectionNames(byte[] lpszReturnBuffer, int nSize, string lpFileName);

        private const int LOG_TYPE_ERROR = 0;
        private const int LOG_TYPE_INFO = 1; 
        private const int LOG_TYPE_HINT = 2;
        private const int LOG_TYPE_MISC	= 3;

        private const int ORDER_INFO_TYPE = 0;
        private const int ORDER_INFO_ACCOUNT = 1;
        private const int ORDER_INFO_SYMBOL = 2;
        private const int ORDER_INFO_ORDER_TYPE = 3;
        private const int ORDER_INFO_BUY_SELL = 4;
        private const int ORDER_INFO_PRICE_FLAG = 5;
        private const int ORDER_INFO_PRICE = 6;
        private const int ORDER_INFO_QTY = 7;
        private const int ORDER_INFO_MARKET_TYPE = 8;
        private const int ORDER_INFO_STOCK_TYPE	= 9;
        private const int ORDER_INFO_DAY_TRADE = 10;
        private const int ORDER_INFO_USER_KEY = 11;
        private const int ORDER_INFO_EXCHANGE = 12;
        private const int ORDER_INFO_MONTH = 13;
        private const int ORDER_INFO_STOP_PRICE = 14;
        private const int ORDER_INFO_OPEN_CLOSE = 15;
        private const int ORDER_INFO_CALL_PUT = 16;
        private const int ORDER_INFO_STRIKE_PRICE = 17;
        private const int ORDER_INFO_ORDER_NO = 18;
        private const int ORDER_INFO_SYMBOL2 = 20;
        private const int ORDER_INFO_BUY_SELL2 = 21;
        private const int ORDER_INFO_MONTH2 = 22;
        private const int ORDER_INFO_CALL_PUT2 = 23;
        private const int ORDER_INFO_STRIKE_PRICE2 = 24;
        private const int ORDER_INFO_DISPLAY = 25;

        ArrayList m_OrderReport = new ArrayList();
        ArrayList m_DealReport = new ArrayList();

        int m_OrderRow;
        int m_DealRow;

        public Form1()
        {
            InitializeComponent();
        }

        private void axICE_gTrade1_LoginReady(object sender, AxICE_GTRADELib._DICE_gTradeEvents_LoginReadyEvent e)
        {
            WriteLog("Login Ready on [" + e.strSession + "]");

            m_OrderRow = 0;
            m_DealRow = 0;

            int nCount = axICE_gTrade1.GetStatementCount(0);
            for (int i = 0; i < nCount; i++)
            {
                WriteLog(axICE_gTrade1.GetStatementString(0, i, 0) + " " + axICE_gTrade1.GetStatementString(0, i, 1) + " " + axICE_gTrade1.GetStatementString(0, i, 2));
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text != "")
            {
                axICE_gTrade1.TouchanceLoginY(textBox1.Text);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (textBox2.Text != "")
            {
                axICE_gTrade1.TouchanceLogin(textBox2.Text, "TOUCHANCE");
                ReloadSessions();
            }
            else
            {
                WriteLog("Fail to login TOUCHANCE service.");
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            //login
            string strSession = comboBox1.Text;
            string path = textBox2.Text;
            if (strSession != "" && path != "")
            {
                int nRet = axICE_gTrade1.Login(path, strSession);
                WriteLog("Login nRet = " + nRet + " Session = " + strSession);
            }
        }

        private void ReloadSessions()
        {
            string path = textBox2.Text;
            if (path != "")            
            {
                byte[] buffer = new byte[1024];
                GetPrivateProfileSectionNames(buffer, buffer.Length, path);
                string allSections = System.Text.Encoding.Default.GetString(buffer);
                string[] sectionNames = allSections.Split('\0');
                foreach (string sectionName in sectionNames)
                {
                    if (sectionName != string.Empty)
                        comboBox1.Items.Add(sectionName);
                }
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            //Logout
            string strSession = comboBox1.Text;
            if (strSession != "")
            {
                axICE_gTrade1.Logout(strSession);
                WriteLog("Logout Session = " + strSession);
            }       
        }

        private void button5_Click(object sender, EventArgs e)
        {//期貨下單
            axICE_gTrade1.ClearOrder();
            axICE_gTrade1.SetOrderInfoString(ORDER_INFO_ACCOUNT, textBox_FutAcc.Text);
            axICE_gTrade1.SetOrderInfoString(ORDER_INFO_SYMBOL, textBox7.Text);
            axICE_gTrade1.SetOrderInfoString(ORDER_INFO_ORDER_TYPE, comboBox2.Text);
            axICE_gTrade1.SetOrderInfoString(ORDER_INFO_BUY_SELL, comboBox4.Text);
            axICE_gTrade1.SetOrderInfoString(ORDER_INFO_PRICE_FLAG, comboBox6.Text);
            Double dbPrice = 0;
            Double.TryParse(textBox9.Text, out dbPrice);
            axICE_gTrade1.SetOrderInfoValue(ORDER_INFO_PRICE, dbPrice);
            long nQty = 0;
            long.TryParse(textBox11.Text, out nQty);
            axICE_gTrade1.SetOrderInfoValue(ORDER_INFO_QTY, nQty);
            axICE_gTrade1.SetOrderInfoString(ORDER_INFO_DAY_TRADE, comboBox8.Text);
            axICE_gTrade1.SetOrderInfoString(ORDER_INFO_USER_KEY, textBox13.Text);
            axICE_gTrade1.SetOrderInfoString(ORDER_INFO_EXCHANGE, textBox15.Text);
            axICE_gTrade1.SetOrderInfoString(ORDER_INFO_MONTH, textBox17.Text);
            Double dbStopPrice = 0;
            Double.TryParse(textBox19.Text, out dbStopPrice);
            axICE_gTrade1.SetOrderInfoValue(ORDER_INFO_STOP_PRICE, dbStopPrice);
            axICE_gTrade1.SetOrderInfoString(ORDER_INFO_OPEN_CLOSE, comboBox10.Text);
            if(textBox24.Text == "")
            {//單式期貨
                axICE_gTrade1.SetOrderInfoString(ORDER_INFO_TYPE, "F");
            }
            else
            {            
                axICE_gTrade1.SetOrderInfoString(ORDER_INFO_TYPE, "F2");
                axICE_gTrade1.SetOrderInfoString(ORDER_INFO_SYMBOL2, textBox24.Text);
                axICE_gTrade1.SetOrderInfoString(ORDER_INFO_BUY_SELL2, comboBox13.Text);
                axICE_gTrade1.SetOrderInfoString(ORDER_INFO_MONTH2, textBox16.Text);
            }
            int nRet = axICE_gTrade1.ProcessOrder();
            WriteLog("Process Fut Order Ret = " + nRet);
        }

        private void WriteLog(string strlog)
        {
            listBox1.Items.Add(strlog);
            EventLog.Write(strlog);
        }

        private void axICE_gTrade1_LoginFail(object sender, AxICE_GTRADELib._DICE_gTradeEvents_LoginFailEvent e)
        {
            WriteLog("Login Failed on [" + e.strSession + "] Error = " + e.strErrorMsg);
        }

        private void axICE_gTrade1_ConnectionLost(object sender, AxICE_GTRADELib._DICE_gTradeEvents_ConnectionLostEvent e)
        {
            WriteLog("Connection Lost on ["+ e.strSession + "]");
            axICE_gTrade1.Logout(e.strSession);
        }

        private void axICE_gTrade1_Log(object sender, AxICE_GTRADELib._DICE_gTradeEvents_LogEvent e)
        {
            string strLogType;
            switch (e.nLogType)
            {
                case LOG_TYPE_ERROR:
                    strLogType = "ERROR";
                    break;
                case LOG_TYPE_INFO:
                    strLogType = "INFO";
                    break;
                case LOG_TYPE_HINT:
                    strLogType = "HINT";
                    break;
                case LOG_TYPE_MISC:
                    strLogType = "MISC";
                    break;
                default:
                    strLogType = "UNDEFINED";
                    break;
            }
            WriteLog("[" + e.strSession + "]" + strLogType +">"+ e.strLog);
        }

        private void axICE_gTrade1_OrderStatus(object sender, AxICE_GTRADELib._DICE_gTradeEvents_OrderStatusEvent e)
        {
            WriteLog("OrderStatus[" + e.nIndex + "] memo = " + axICE_gTrade1.GetOrderString(e.nIndex, 24));

            int ncount = axICE_gTrade1.GetOrderCount();


            int Row = -1;
            foreach (TRptData tmpRptData in m_OrderReport)
            {
                if (tmpRptData.nIndex == e.nIndex)
                {//更新
                    Row = tmpRptData.nGridNum;
                    dataGridView1.Rows[Row].Cells[0].Value = axICE_gTrade1.GetOrderString(e.nIndex, 0); //帳號
                    dataGridView1.Rows[Row].Cells[1].Value = axICE_gTrade1.GetOrderString(e.nIndex, 1); //委託書號
                    dataGridView1.Rows[Row].Cells[2].Value = axICE_gTrade1.GetOrderString(e.nIndex, 2); //商品名稱
                    dataGridView1.Rows[Row].Cells[3].Value = axICE_gTrade1.GetOrderString(e.nIndex, 3); //類別
                    dataGridView1.Rows[Row].Cells[4].Value = axICE_gTrade1.GetOrderString(e.nIndex, 4); //委託類別
                    dataGridView1.Rows[Row].Cells[5].Value = axICE_gTrade1.GetOrderString(e.nIndex, 5); //價格別   
                    dataGridView1.Rows[Row].Cells[6].Value = axICE_gTrade1.GetOrderString(e.nIndex, 6); //買賣別 
                    dataGridView1.Rows[Row].Cells[7].Value = axICE_gTrade1.GetOrderValue(e.nIndex, 7);//委託價
                    dataGridView1.Rows[Row].Cells[8].Value = axICE_gTrade1.GetOrderValue(e.nIndex, 8);//有效委託
                    dataGridView1.Rows[Row].Cells[9].Value = axICE_gTrade1.GetOrderValue(e.nIndex, 9); //已成交
                    dataGridView1.Rows[Row].Cells[10].Value = axICE_gTrade1.GetOrderValue(e.nIndex, 10);//成交均價             
                    dataGridView1.Rows[Row].Cells[11].Value = axICE_gTrade1.GetOrderString(e.nIndex, 11); //狀態
                    dataGridView1.Rows[Row].Cells[12].Value = axICE_gTrade1.GetOrderString(e.nIndex, 12); //時間
                    dataGridView1.Rows[Row].Cells[13].Value = axICE_gTrade1.GetOrderString(e.nIndex, 13); //來源別
                    dataGridView1.Rows[Row].Cells[14].Value = axICE_gTrade1.GetOrderString(e.nIndex, 14); //User Key
                    dataGridView1.Rows[Row].Cells[15].Value = axICE_gTrade1.GetOrderString(e.nIndex, 20); //是否有效 
                    dataGridView1.Rows[Row].Cells[16].Value = axICE_gTrade1.GetOrderString(e.nIndex, 21); //可增量
                    dataGridView1.Rows[Row].Cells[17].Value = axICE_gTrade1.GetOrderString(e.nIndex, 22); //可減量 
                    dataGridView1.Rows[Row].Cells[18].Value = axICE_gTrade1.GetOrderString(e.nIndex, 23); //可改價 
                    dataGridView1.Rows[Row].Cells[19].Value = axICE_gTrade1.GetOrderString(e.nIndex, 16); //交易所 
                }
            };
            if (Row == -1)//新回報
            {
                TRptData tmpRptData = new TRptData();
                tmpRptData.nIndex = e.nIndex;
                tmpRptData.nGridNum = m_OrderRow;

                dataGridView1.Rows.Add();
                dataGridView1.Rows[m_OrderRow].Cells[0].Value = axICE_gTrade1.GetOrderString(e.nIndex, 0); //帳號
                dataGridView1.Rows[m_OrderRow].Cells[1].Value = axICE_gTrade1.GetOrderString(e.nIndex, 1); //委託書號
                dataGridView1.Rows[m_OrderRow].Cells[2].Value = axICE_gTrade1.GetOrderString(e.nIndex, 2); //商品名稱
                dataGridView1.Rows[m_OrderRow].Cells[3].Value = axICE_gTrade1.GetOrderString(e.nIndex, 3); //類別
                dataGridView1.Rows[m_OrderRow].Cells[4].Value = axICE_gTrade1.GetOrderString(e.nIndex, 4); //委託類別
                dataGridView1.Rows[m_OrderRow].Cells[5].Value = axICE_gTrade1.GetOrderString(e.nIndex, 5); //價格別   
                dataGridView1.Rows[m_OrderRow].Cells[6].Value = axICE_gTrade1.GetOrderString(e.nIndex, 6); //買賣別 
                dataGridView1.Rows[m_OrderRow].Cells[7].Value = axICE_gTrade1.GetOrderValue(e.nIndex, 7);//委託價
                dataGridView1.Rows[m_OrderRow].Cells[8].Value = axICE_gTrade1.GetOrderValue(e.nIndex, 8);//有效委託
                dataGridView1.Rows[m_OrderRow].Cells[9].Value = axICE_gTrade1.GetOrderValue(e.nIndex, 9); //已成交
                dataGridView1.Rows[m_OrderRow].Cells[10].Value = axICE_gTrade1.GetOrderValue(e.nIndex, 10);//成交均價             
                dataGridView1.Rows[m_OrderRow].Cells[11].Value = axICE_gTrade1.GetOrderString(e.nIndex, 11); //狀態
                dataGridView1.Rows[m_OrderRow].Cells[12].Value = axICE_gTrade1.GetOrderString(e.nIndex, 12); //時間
                dataGridView1.Rows[m_OrderRow].Cells[13].Value = axICE_gTrade1.GetOrderString(e.nIndex, 13); //來源別
                dataGridView1.Rows[m_OrderRow].Cells[14].Value = axICE_gTrade1.GetOrderString(e.nIndex, 14); //User Key
                dataGridView1.Rows[m_OrderRow].Cells[15].Value = axICE_gTrade1.GetOrderString(e.nIndex, 20); //是否有效 
                dataGridView1.Rows[m_OrderRow].Cells[16].Value = axICE_gTrade1.GetOrderString(e.nIndex, 21); //可增量
                dataGridView1.Rows[m_OrderRow].Cells[17].Value = axICE_gTrade1.GetOrderString(e.nIndex, 22); //可減量 
                dataGridView1.Rows[m_OrderRow].Cells[18].Value = axICE_gTrade1.GetOrderString(e.nIndex, 23); //可改價 
                dataGridView1.Rows[m_OrderRow].Cells[19].Value = axICE_gTrade1.GetOrderString(e.nIndex, 16); //交易所 
                m_OrderReport.Add(tmpRptData);
                m_OrderRow++;
            }
        }

        private void axICE_gTrade1_FillOrder(object sender, AxICE_GTRADELib._DICE_gTradeEvents_FillOrderEvent e)
        {
            WriteLog("FillOrder[" + e.nIndex + "]");

            int Row = -1;
            foreach (TRptData tmpRptData in m_DealReport)
            {
                if (tmpRptData.nIndex == e.nIndex)
                {//更新
                    Row = tmpRptData.nGridNum;
                    dataGridView2.Rows[Row].Cells[0].Value = axICE_gTrade1.GetFillOrderString(e.nIndex, 0); //帳號
                    dataGridView2.Rows[Row].Cells[1].Value = axICE_gTrade1.GetFillOrderString(e.nIndex, 1); //委託書號
                    dataGridView2.Rows[Row].Cells[2].Value = axICE_gTrade1.GetFillOrderString(e.nIndex, 2); //商品名稱
                    dataGridView2.Rows[Row].Cells[3].Value = axICE_gTrade1.GetFillOrderString(e.nIndex, 3); //類別
                    dataGridView2.Rows[Row].Cells[4].Value = axICE_gTrade1.GetFillOrderString(e.nIndex, 6); //買賣別 
                    dataGridView2.Rows[Row].Cells[5].Value = axICE_gTrade1.GetFillOrderValue(e.nIndex, 9); //成交量
                    dataGridView2.Rows[Row].Cells[6].Value = axICE_gTrade1.GetFillOrderValue(e.nIndex, 10);//成交均價             
                    dataGridView2.Rows[Row].Cells[7].Value = axICE_gTrade1.GetFillOrderString(e.nIndex, 12); //時間
                    dataGridView2.Rows[Row].Cells[8].Value = axICE_gTrade1.GetFillOrderString(e.nIndex, 13); //來源別
                    dataGridView2.Rows[Row].Cells[9].Value = axICE_gTrade1.GetFillOrderString(e.nIndex, 14); //User Key
                    dataGridView2.Rows[Row].Cells[10].Value = axICE_gTrade1.GetFillOrderString(e.nIndex, 15); //倉別 
                    dataGridView2.Rows[Row].Cells[11].Value = axICE_gTrade1.GetFillOrderString(e.nIndex, 16); //交易所 
                }
            };
            if (Row == -1)//新回報
            {
                TRptData tmpRptData = new TRptData();
                tmpRptData.nIndex = e.nIndex;
                tmpRptData.nGridNum = m_DealRow;

                dataGridView2.Rows.Add();
                dataGridView2.Rows[m_DealRow].Cells[0].Value = axICE_gTrade1.GetFillOrderString(e.nIndex, 0); //帳號
                dataGridView2.Rows[m_DealRow].Cells[1].Value = axICE_gTrade1.GetFillOrderString(e.nIndex, 1); //委託書號
                dataGridView2.Rows[m_DealRow].Cells[2].Value = axICE_gTrade1.GetFillOrderString(e.nIndex, 2); //商品名稱
                dataGridView2.Rows[m_DealRow].Cells[3].Value = axICE_gTrade1.GetFillOrderString(e.nIndex, 3); //類別
                dataGridView2.Rows[m_DealRow].Cells[4].Value = axICE_gTrade1.GetFillOrderString(e.nIndex, 6); //買賣別 
                dataGridView2.Rows[m_DealRow].Cells[5].Value = axICE_gTrade1.GetFillOrderValue(e.nIndex, 9); //成交量
                dataGridView2.Rows[m_DealRow].Cells[6].Value = axICE_gTrade1.GetFillOrderValue(e.nIndex, 10);//成交均價             
                dataGridView2.Rows[m_DealRow].Cells[7].Value = axICE_gTrade1.GetFillOrderString(e.nIndex, 12); //時間
                dataGridView2.Rows[m_DealRow].Cells[8].Value = axICE_gTrade1.GetFillOrderString(e.nIndex, 13); //來源別
                dataGridView2.Rows[m_DealRow].Cells[9].Value = axICE_gTrade1.GetFillOrderString(e.nIndex, 14); //User Key
                dataGridView2.Rows[m_DealRow].Cells[10].Value = axICE_gTrade1.GetFillOrderString(e.nIndex, 15); //倉別 
                dataGridView2.Rows[m_DealRow].Cells[11].Value = axICE_gTrade1.GetFillOrderString(e.nIndex, 16); //交易所 
                m_DealReport.Add(tmpRptData);
                m_DealRow ++;
            }
        }

        private void button6_Click(object sender, EventArgs e)
        {//選擇權下單
            axICE_gTrade1.ClearOrder();
            axICE_gTrade1.SetOrderInfoString(ORDER_INFO_ACCOUNT, textBox_OptAcc.Text);
            axICE_gTrade1.SetOrderInfoString(ORDER_INFO_SYMBOL, textBox8.Text);
            axICE_gTrade1.SetOrderInfoString(ORDER_INFO_ORDER_TYPE, comboBox3.Text);
            axICE_gTrade1.SetOrderInfoString(ORDER_INFO_BUY_SELL, comboBox5.Text);
            axICE_gTrade1.SetOrderInfoString(ORDER_INFO_PRICE_FLAG, comboBox7.Text);
            Double dbPrice = 0;
            Double.TryParse(textBox10.Text, out dbPrice);
            axICE_gTrade1.SetOrderInfoValue(ORDER_INFO_PRICE, dbPrice);
            long nQty = 0;
            long.TryParse(textBox12.Text, out nQty);
            axICE_gTrade1.SetOrderInfoValue(ORDER_INFO_QTY, nQty);
            axICE_gTrade1.SetOrderInfoString(ORDER_INFO_DAY_TRADE, comboBox9.Text);
            axICE_gTrade1.SetOrderInfoString(ORDER_INFO_USER_KEY, textBox14.Text);
            axICE_gTrade1.SetOrderInfoString(ORDER_INFO_EXCHANGE, textBox26.Text);
            axICE_gTrade1.SetOrderInfoString(ORDER_INFO_MONTH, textBox18.Text);
            Double dbStopPrice = 0;
            Double.TryParse(textBox20.Text, out dbStopPrice);
            axICE_gTrade1.SetOrderInfoValue(ORDER_INFO_STOP_PRICE, dbStopPrice);
            axICE_gTrade1.SetOrderInfoString(ORDER_INFO_OPEN_CLOSE, comboBox11.Text);
            axICE_gTrade1.SetOrderInfoString(ORDER_INFO_CALL_PUT, comboBox12.Text);
            Double dbStrikePrice = 0;
            Double.TryParse(textBox23.Text, out dbStrikePrice);
            axICE_gTrade1.SetOrderInfoValue(ORDER_INFO_STRIKE_PRICE, dbStrikePrice);
            if (textBox25.Text == "")
            {//單式期貨
                axICE_gTrade1.SetOrderInfoString(ORDER_INFO_TYPE, "O");
            }
            else
            {
                axICE_gTrade1.SetOrderInfoString(ORDER_INFO_TYPE, "O2");
                axICE_gTrade1.SetOrderInfoString(ORDER_INFO_SYMBOL2, textBox25.Text);
                axICE_gTrade1.SetOrderInfoString(ORDER_INFO_BUY_SELL2, comboBox14.Text);
                axICE_gTrade1.SetOrderInfoString(ORDER_INFO_MONTH2, textBox27.Text);
                axICE_gTrade1.SetOrderInfoString(ORDER_INFO_CALL_PUT2, comboBox15.Text);
                Double dbStrikePrice2 = 0;
                Double.TryParse(textBox30.Text, out dbStrikePrice2);
                axICE_gTrade1.SetOrderInfoValue(ORDER_INFO_STRIKE_PRICE2, dbStrikePrice2);
            }
            int nRet = axICE_gTrade1.ProcessOrder();
            WriteLog("Process Opt Order Ret = " + nRet);
        }

        private void button7_Click(object sender, EventArgs e)
        {//
            axICE_gTrade1.ClearOrder();
            axICE_gTrade1.SetOrderInfoString(ORDER_INFO_TYPE, "R");
            axICE_gTrade1.SetOrderInfoString(ORDER_INFO_ORDER_NO, textBox4.Text);
            long nQty = 0;
            long.TryParse(textBox5.Text, out nQty);
            axICE_gTrade1.SetOrderInfoValue(ORDER_INFO_QTY, nQty);
            int nRet = axICE_gTrade1.ProcessOrder();

            WriteLog("Process ReduceOrder Ret = " + nRet);
        }

        private void button8_Click(object sender, EventArgs e)
        {
            int nSel = comboBox16.SelectedIndex;
            axICE_gTrade1.QueryStatement(nSel);
        }

        private void axICE_gTrade1_StatementUpdate(object sender, AxICE_GTRADELib._DICE_gTradeEvents_StatementUpdateEvent e)
        {
            /*Querystatement會去查詢所有帳號的帳務資料
             每個帳號查詢完成後會藉由OnStatementUpdate來通知查詢完成
             EX:登入兩個帳號,
             1.當查詢(期貨保證金/期貨部位)
            會去查(A帳號國內/A帳號國外/B帳號國內/B帳號國外)共4次
            所以會觸發4次OnStatementUpdate
             2.當查詢(選擇權部位)
            會去查(A帳號國內/B帳號國內)共2次
            所以會觸發2次OnStatementUpdate
            */
            int nCount = axICE_gTrade1.GetStatementCount(e.nInfoType);
            string strlog = "";
            switch (e.nInfoType)
            {
                case 2://期貨保證金
                {
                    for (int i = 0; i < nCount; i++)
                    {
                        strlog = "";
                        for (int j = 0; j < 21; j++)
                        {
                            strlog = strlog + j + ":" + axICE_gTrade1.GetStatementString(e.nInfoType, i, j) + "\t";
                        }
                        WriteLog(strlog);
                    }
                    break;
                }
                case 4://期貨部位
                {
                    for (int i = 0; i < nCount; i++)
                    {
                        strlog = "";
                        for (int j = 0; j < 12; j++)
                        {
                            strlog = strlog + j + ":" + axICE_gTrade1.GetStatementString(e.nInfoType, i, j) + "\t";
                        }
                        WriteLog(strlog);
                    }
                    break;
                }
                case 5://選擇權部位
                {
                    for (int i = 0; i < nCount; i++)
                    {
                        strlog = "";
                        for (int j = 0; j < 12; j++)
                        {
                            strlog = strlog + j + ":" + axICE_gTrade1.GetStatementString(e.nInfoType, i, j) + "\t";
                        }
                        WriteLog(strlog);
                    }
                    break;
                }
                default:
                    break;
            }
        }

        private void button9_Click(object sender, EventArgs e)
        {
            int nSel = comboBox16.SelectedIndex;
            int nCount = axICE_gTrade1.GetStatementCount(nSel);
            WriteLog("Statement "+ nSel + " count = " + nCount);

            string strlog = "";
            switch (nSel)
            {
                case 0://可用帳號
                    {
                        for (int i = 0; i < nCount; i++)
                        {
                            strlog = "";
                            for (int j = 0; j < 12; j++)
                            {
                                strlog = strlog + j + ":" + axICE_gTrade1.GetStatementString(nSel, i, j) + "\t";
                            }
                            WriteLog(strlog);
                        }
                        break;
                    }
                case 2://期貨保證金
                    {
                        for (int i = 0; i < nCount; i++)
                        {
                            strlog = "";
                            for (int j = 0; j < 21; j++)
                            {
                                strlog = strlog + j + ":" + axICE_gTrade1.GetStatementString(nSel, i, j) + "\t";
                            }
                            WriteLog(strlog);
                        }
                        break;
                    }
                case 4://期貨部位
                    {
                        for (int i = 0; i < nCount; i++)
                        {
                            strlog = "";
                            for (int j = 0; j < 12; j++)
                            {
                                strlog = strlog + j + ":" + axICE_gTrade1.GetStatementString(nSel, i, j) + "\t";
                            }
                            WriteLog(strlog);
                        }
                        break;
                    }
                case 5://選擇權部位
                    {
                        for (int i = 0; i < nCount; i++)
                        {
                            strlog = "";
                            for (int j = 0; j < 12; j++)
                            {
                                strlog = strlog + j + ":" + axICE_gTrade1.GetStatementString(nSel, i, j) + "\t";
                            }
                            WriteLog(strlog);
                        }
                        break;
                    }
                default:
                    break;
            }
        }
    }

    public class TRptData
    {
        public int nIndex;
        public int nGridNum;
        public String nOrderNo;
    }

    public static class EventLog
    {
        public static string FilePath { get; set; }

        public static void Write(string format, params object[] arg)
        {
            Write(string.Format(format, arg));
        }

        public static void Write(string message)
        {
            if (string.IsNullOrEmpty(FilePath))
            {
                FilePath = Directory.GetCurrentDirectory();
            }
            string filename = FilePath +
                string.Format("\\{0:yyyy}\\{0:MM}\\{0:yyyy-MM-dd}.txt", DateTime.Now);
            FileInfo finfo = new FileInfo(filename);
            if (finfo.Directory.Exists == false)
            {
                finfo.Directory.Create();
            }
            string writeString = string.Format("{0:yyyy/MM/dd HH:mm:ss.fff} {1}",
                DateTime.Now, message) + Environment.NewLine;
            File.AppendAllText(filename, writeString, Encoding.Unicode);
        }
    }

}
