Exemplo n.º 1
0
        //登录
        async private void btnLogin_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                //禁用
                this.btnLogin.IsEnabled  = false;
                this.btnCancel.IsEnabled = true;
                //API初始化
                this._trdApi = new CTradeApi(this.cbxInvestorID.Text, this.cbxPassword.Text, this.cbxBrokerID.Text, "tcp://" + cbxTradeIP.Text + ":" + cbxTradePort.Text);
                this._mktApi = new CMdApi(this.cbxInvestorID.Text, this.cbxPassword.Text, this.cbxBrokerID.Text, "tcp://" + cbxQuoteIP.Text + ":" + cbxQuotePort.Text);
                Body.trdApi  = this._trdApi;
                Body.mktApi  = this._mktApi;

                #region 绑定后台Body
                this._mktApi.OnTick  += Body.Body_OnTick;
                this._trdApi.OnTrade += Body.Body_OnRtnTrade;
                //this._trdApi.OnDetailPosition += Body.Body_OnDetailPosition;
                //this._trdApi.OnPosition += Body.Body_OnPosition;
                this._trdApi.OnOrder          += Body.Body_OnOrderField;
                this._trdApi.OnErrorOrder     += Body.Body_OnErrorOrder;
                this._trdApi.OnUpdate         += Body.Body_OnUpdate;
                this._trdApi.OnTradingAccount += Body.Body_OnTradingAccount;
                #endregion

                //连接
                tbStatus.Text = "正在连接行情端...";
                await this._mktApi.AsyncConnect(_cts.Token);

                tbStatus.Text      = "行情端已连接!";
                progressBar.Value += 10;
                tbStatus.Text      = "正在连接交易端...";
                await _trdApi.AsyncConnect(_cts.Token);

                tbStatus.Text      = "交易端已连接!";
                progressBar.Value += 10;
                //登陆
                tbStatus.Text = "正在登陆行情端...";
                await _mktApi.AsyncLogin(_cts.Token);

                tbStatus.Text      = "行情端已登陆!";
                progressBar.Value += 10;
                tbStatus.Text      = "正在登陆交易端...";
                await _trdApi.AsyncLogin(_cts.Token);

                tbStatus.Text      = "交易端已登陆!";
                progressBar.Value += 10;
                //如果是今天第一次登陆,则要确认结算单
                if (IsFirstTimeLogin() || (bool)this.chxIsConfirmSettlementInfo.IsChecked)
                {
                    tbStatus.Text = "正在查询结算信息...";
                    _trdApi.StackQuery.Push(new Tuple <EnumReqCmdType, object>(EnumReqCmdType.QrySettlementInfo, string.Empty));
                    await _trdApi.ExecStackQuery();

                    MessageBox.Show(_trdApi.SettlementInfoContent);
                    tbStatus.Text = "正在确认结算信息...";
                    _trdApi.StackQuery.Push(new Tuple <EnumReqCmdType, object>(EnumReqCmdType.SettlementInfoConfirm, string.Empty));
                    await _trdApi.ExecStackQuery();

                    tbStatus.Text      = "已确认结算单!";
                    progressBar.Value += 10;
                }
                //查询合约
                tbStatus.Text = "正在查询合约...";
                _trdApi.StackQuery.Push(new Tuple <EnumReqCmdType, object>(EnumReqCmdType.QryInstrument, string.Empty));
                await _trdApi.ExecStackQuery();

                tbStatus.Text      = "合约查询完毕!";
                progressBar.Value += 10;
                //查询持仓细节
                tbStatus.Text = "正在查询持仓细节...";
                _trdApi.StackQuery.Push(new Tuple <EnumReqCmdType, object>(EnumReqCmdType.QryInvestorPositionDetail, string.Empty));
                await _trdApi.ExecStackQuery();

                tbStatus.Text      = "持仓细节查询完毕!";
                progressBar.Value += 10;
                //查询持仓
                tbStatus.Text = "正在查询持仓...";
                _trdApi.StackQuery.Push(new Tuple <EnumReqCmdType, object>(EnumReqCmdType.QryInvestorPosition, string.Empty));
                await _trdApi.ExecStackQuery();

                tbStatus.Text      = "持仓查询完毕!";
                progressBar.Value += 10;
                //查询交易账户信息
                tbStatus.Text = "正在交易账户...";
                _trdApi.StackQuery.Push(new Tuple <EnumReqCmdType, object>(EnumReqCmdType.QryTradingAccount, string.Empty));
                await _trdApi.ExecStackQuery();

                tbStatus.Text      = "交易账户查询完毕!";
                progressBar.Value += 10;
                //订阅行情数据
                tbStatus.Text = "正在订阅行情数据...";
                _mktApi.ReqSubscribeMarketData(_trdApi.DicInstrumentField.Keys.ToArray());
                progressBar.Value += 10;

                //调用主交易窗口
                Hide();
                MainWindow mainWindow = new MainWindow();
                Body.mainWindow = mainWindow;
                mainWindow.ShowDialog();
            }
            catch (Exception ex)
            {
                //this._mktApi.ReqUserLogout();
                //this._trdApi.ReqUserLogout();
                MessageBox.Show(ex.Message);
                this.btnCancel.IsEnabled = false;
                this.btnLogin.IsEnabled  = true;
                this.progressBar.Value   = 0;
            }
            finally
            {
                //关闭登录窗口
                Close();
            }
        }