Beispiel #1
0
 public OnConnectArgs(IntPtr pApi, ref CZQThostFtdcRspUserLoginField pRspUserLogin, ConnectionStatus result)
 {
     this.pApi = pApi;
     this.pRspUserLogin = pRspUserLogin;
     this.result = result;
 }
Beispiel #2
0
        private void OnConnect_callback(IntPtr pApi, ref CZQThostFtdcRspUserLoginField pRspUserLogin, ConnectionStatus result)
        {
            _bMdConnected = (ConnectionStatus.E_logined == result);

            if (null != OnConnect)
            {
                OnConnect(this, new OnConnectArgs(pApi, ref pRspUserLogin, result));
            }
        }
        private void UpdateLocalTime(SetTimeMode _SetLocalTimeMode,CZQThostFtdcRspUserLoginField pRspUserLogin)
        {
            string strNewTime;
            switch (_SetLocalTimeMode)
            {
                case SetTimeMode.None:
                    return;
                case SetTimeMode.LoginTime:
                    strNewTime = pRspUserLogin.LoginTime;
                    break;
                case SetTimeMode.SHFETime:
                    strNewTime = pRspUserLogin.SHFETime;
                    break;
                case SetTimeMode.DCETime:
                    strNewTime = pRspUserLogin.DCETime;
                    break;
                case SetTimeMode.CZCETime:
                    strNewTime = pRspUserLogin.CZCETime;
                    break;
                case SetTimeMode.FFEXTime:
                    strNewTime = pRspUserLogin.FFEXTime;
                    break;
                default:
                    return;
            }

            try
            {
                int HH = int.Parse(strNewTime.Substring(0, 2));
                int mm = int.Parse(strNewTime.Substring(3, 2));
                int ss = int.Parse(strNewTime.Substring(6, 2));

                DateTime _dateTime = new DateTime(_yyyy, _MM, _dd, HH, mm, ss);
                DateTime _newDateTime = _dateTime.AddMilliseconds(AddMilliseconds);
                Console.WriteLine("SetLocalTime:Return:{0},{1}",
                    WinAPI.SetLocalTime(_newDateTime),
                    _newDateTime.ToString("yyyy-MM-dd HH:mm:ss.fff"));
            }
            catch (System.Exception ex)
            {
                Console.WriteLine("{0}不能解析",strNewTime);
            }
        }
        private void OnConnect(IntPtr pApi, ref CZQThostFtdcRspUserLoginField pRspUserLogin, ConnectionStatus result)
        {
            if (m_pMdApi == pApi)//行情
            {
                _bMdConnected = false;
                if (ConnectionStatus.E_logined == result)
                {
                    _bMdConnected = true;

                    //只登录行情时得得更新行情时间,但行情却可以隔夜不断,所以要定时更新
                    if (!_bWantTdConnect)
                    {
                        _yyyy = DateTime.Now.Year;
                        _MM = DateTime.Now.Month;
                        _dd = DateTime.Now.Day;
                    }

                    Console.WriteLine("MdApi:LocalTime:{0},LoginTime:{1},SHFETime:{2},DCETime:{3},CZCETime:{4},FFEXTime:{5}",
                        DateTime.Now.ToString("HH:mm:ss.fff"), pRspUserLogin.LoginTime, pRspUserLogin.SHFETime,
                        pRspUserLogin.DCETime, pRspUserLogin.CZCETime, pRspUserLogin.FFEXTime);
                }
                //这也有个时间,但取出的时间无效
                if (OutputLogToConsole)
                {
                    Console.WriteLine("MdApi:{0},{1},{2}", Clock.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"), result, pRspUserLogin.LoginTime);
                }
            }
            else if (m_pTdApi == pApi)//交易
            {
                _bTdConnected = false;

                if (ConnectionStatus.E_logined == result)
                {
                    _RspUserLogin = pRspUserLogin;

                    //用于行情记算时简化时间解码
                    int _yyyyMMdd = int.Parse(pRspUserLogin.TradingDay);
                    _yyyy = _yyyyMMdd / 10000;
                    _MM = (_yyyyMMdd % 10000) / 100;
                    _dd = _yyyyMMdd % 100;

                    Console.WriteLine("TdApi:LocalTime:{0},LoginTime:{1},SHFETime:{2},DCETime:{3},CZCETime:{4},FFEXTime:{5}",
                        DateTime.Now.ToString("HH:mm:ss.fff"), pRspUserLogin.LoginTime, pRspUserLogin.SHFETime,
                        pRspUserLogin.DCETime, pRspUserLogin.CZCETime, pRspUserLogin.FFEXTime);

                    UpdateLocalTime(SetLocalTimeMode,pRspUserLogin);
                //}
                //else if (ConnectionStatus.E_logined == result)
                //{
                    _bTdConnected = true;
                    //请求查询资金
                    TraderApi.TD_ReqQryTradingAccount(m_pTdApi);

                    //请求查询全部持仓
                    TraderApi.TD_ReqQryInvestorPosition(m_pTdApi, null);

                    //请求查询合约
                    _dictInstruments.Clear();
                    TraderApi.TD_ReqQryInstrument(m_pTdApi, null);
                }
                if (OutputLogToConsole)
                {
                    Console.WriteLine("TdApi:{0},{1},{2}", Clock.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"), result, pRspUserLogin.LoginTime);
                }
            }

            if (
                (_bMdConnected && _bTdConnected)//都连上
                || (!_bWantMdConnect && _bTdConnected)//只用分析交易连上
                || (!_bWantTdConnect && _bMdConnected)//只用分析行情连上
                )
            {
                ChangeStatus(ProviderStatus.LoggedIn);
                EmitConnectedEvent();
            }
        }
        private void OnConnect(IntPtr pApi, ref CZQThostFtdcRspUserLoginField pRspUserLogin, ConnectionStatus result)
        {
            //用于行情记算时简化时间解码
            //try
            //{
            //    int _yyyyMMdd = int.Parse(pRspUserLogin.TradingDay);
            //    _yyyy = _yyyyMMdd / 10000;
            //    _MM = (_yyyyMMdd % 10000) / 100;
            //    _dd = _yyyyMMdd % 100;
            //}
            //catch (Exception)
            {
                _yyyy = DateTime.Now.Year;
                _MM = DateTime.Now.Month;
                _dd = DateTime.Now.Day;
            }

            if (m_pMdApi == pApi)//行情
            {
                _bMdConnected = false;
                if (ConnectionStatus.E_logined == result)
                {
                    _bMdConnected = true;

                    mdlog.Info("TradingDay:{0},LoginTime:{1},SHFETime:{2},DCETime:{3},CZCETime:{4},FFEXTime:{5}",
                        pRspUserLogin.TradingDay, pRspUserLogin.LoginTime, pRspUserLogin.SHFETime,
                        pRspUserLogin.DCETime, pRspUserLogin.CZCETime, pRspUserLogin.FFEXTime);

                    // 如果断线重连是使用的重新新建对象的方式,则要重新订阅
                    if (_dictAltSymbol2Instrument.Count > 0)
                    {
                        mdlog.Info("行情列表数{0},全部重新订阅", _dictAltSymbol2Instrument.Count);
                        foreach (DataRecord record in _dictAltSymbol2Instrument.Values)
                        {
                            MdApi.MD_Subscribe(m_pMdApi, record.Symbol, record.Exchange);
                        }
                    }
                }
                //这也有个时间,但取出的时间无效
                mdlog.Info("{0},{1}", result, pRspUserLogin.LoginTime);
            }
            else if (m_pTdApi == pApi)//交易
            {
                _bTdConnected = false;
                if (ConnectionStatus.E_logined == result)
                {
                    _RspUserLogin = pRspUserLogin;

                    tdlog.Info("TradingDay:{0},LoginTime:{1},SHFETime:{2},DCETime:{3},CZCETime:{4},FFEXTime:{5}",
                        pRspUserLogin.TradingDay, pRspUserLogin.LoginTime, pRspUserLogin.SHFETime,
                        pRspUserLogin.DCETime, pRspUserLogin.CZCETime, pRspUserLogin.FFEXTime);

                    UpdateLocalTime(SetLocalTimeMode, pRspUserLogin);
                    //}
                    //else if (ConnectionStatus.E_confirmed == result)
                    //{
                    _bTdConnected = true;
                    //请求查询资金
                    TraderApi.TD_ReqQryTradingAccount(m_pTdApi);

                    //请求查询全部持仓
                    _dbInMemInvestorPosition.Clear();
                    TraderApi.TD_ReqQryInvestorPosition(m_pTdApi, null);

                    //请求查询合约
                    _dictInstruments.Clear();
                    TraderApi.TD_ReqQryInstrument(m_pTdApi, null);

                    _dictCommissionRate.Clear();

                    timerAccount.Enabled = true;
                    timerPonstion.Enabled = true;
                }

                tdlog.Info("{0},{1}", result, pRspUserLogin.LoginTime);
            }

            if (
                (_bMdConnected && _bTdConnected)//都连上
                || (!_bWantMdConnect && _bTdConnected)//只用分析交易连上
                || (!_bWantTdConnect && _bMdConnected)//只用分析行情连上
                )
            {
                timerConnect.Enabled = true;
                timerDisconnect.Enabled = false;//都连接上了,用不着定时断
                ChangeStatus(ProviderStatus.LoggedIn);
                isConnected = true;
                EmitConnectedEvent();
            }
        }