示例#1
0
        //登陆
        public void Login(Account account)
        {
            if (account == null)
            {
                return;
            }
            mAccount = account;

            if (mTrader != null && mTrader.IsLogin)
            {
                return;
            }

            //初始化
            mTrader = new CTPTrade();

            //连接
            mTrader.OnFrontConnected += (object sender, EventArgs e) => {
                mTrader.ReqUserLogin(mAccount.Investor, mAccount.Password, mAccount.Broker);
                LogUtils.EnginLog("ctptd:OnFrontConnected");
            };
            //登入
            mTrader.OnRspUserLogin += (object sender, IntEventArgs e) => {
                if (e.Value != 0)
                {
                    Logout();
                    mTrader = null;
                }
                LogUtils.EnginLog("ctptd:OnRspUserLogin:"******"ctptd:OnRspUserLogout");
            };
            //订单回报
            mTrader.OnRtnOrder += _OnRtnOrder;
            //报单错误回报
            mTrader.OnRtnErrOrder += _OnRtnErrOrder;
            //交易回报
            mTrader.OnRtnTrade += _OnRtnTrade;
            //撤单回报
            mTrader.OnRtnCancel += _OnRtnCancel;
            //撤单错误
            mTrader.OnRtnErrCancel      += _OnRtnErrCancel;
            mTrader.OnRtnExchangeStatus += _OnRtnExchangeStatus;
            //开始连接
            mTrader.ReqConnect(mAccount.Server);
        }
示例#2
0
        static void Main(string[] args)
        {
            //var msg = "CTP:登录成功。您的密码为弱密码,密码长度不可少于6位";
            //Console.WriteLine(Encoding.Default.BodyName);
            //var gb = CodePagesEncodingProvider.Instance.GetEncoding("GB2312");

            //var bytes = Encoding.Default.GetBytes(msg);

            //Console.WriteLine(msg.Length);
            //Console.WriteLine(bytes.Length);

            //Console.WriteLine(gb.GetBytes(msg).Length);

            //return;

            // Encoding.RegisterProvider(CodePagesEncodingProvider.Instance.GetEncoding("GB2312"));
            //var codePage  = System.Globalization.CultureInfo.CurrentCulture.TextInfo.ANSICodePage;
            //Console.WriteLine(codePage);
            //// CTP:登录成功。您的密码为弱密码,密码长度不可少于6位
            //var codeGB2312 = CodePagesEncodingProvider.Instance.GetEncoding("GB2312");
            //Console.WriteLine(codeGB2312.CodePage);

            //var bodyName = Encoding.Default.BodyName;
            //Console.WriteLine(bodyName);

            Logger.Info("start2.");

            QuoteSave qs = null;

            var t = new CTPTrade("ctp_trade");

            System.AppDomain.CurrentDomain.UnhandledException += (sender, e) =>
            {
                if (sender != null)
                {
                    Logger.Error("UnhandledException sender= {0}", sender.ToString());
                }

                if (e.ExceptionObject != null)
                {
                    var ex = e.ExceptionObject as Exception;
                    if (ex != null)
                    {
                        Logger.Error("UnhandledException ex ", ex.StackTrace);
                    }
                    else
                    {
                        Logger.Error("UnhandledException errorObj {0}", e.ExceptionObject.ToString());
                    }
                }

                Logger.Info("IsTerminating : {0}", e.IsTerminating);
            };

            // 登录交易账号,获得当前所有正在交易的期货代码
            t.OnFrontConnected += (sender, e) =>
            {
                t.ReqUserLogin(investor, pwd, "9999");
                t.OnRspUserLogin += (sender2, e2) =>
                {
                    if (e2.Value == 0)
                    {
                        Logger.Info("Login success");

                        List <string> codes = new List <string>();

                        foreach (var instrument in t.DicInstrumentField.Values)
                        {
                            codes.Add(instrument.InstrumentID);
                        }

                        Logger.Info("code num: {0}", codes.Count);

                        qs = new QuoteSave(investor, pwd, codes.ToArray());
                        qs.Run();
                    }
                    else
                    {
                        Console.WriteLine("登录失败!ret=" + e2.Value);
                    }
                };
            };

            t.ReqConnect("tcp://180.168.146.187:10000");

            bool isWaitConsole = true;

            if (args.Length == 1 && args[0] == "linux")
            {
                isWaitConsole = false;
                Logger.Info("not wait console input.");
            }

            while (true)
            {
                if (isWaitConsole)
                {
                    var key = Console.ReadKey();
                    if (key.Key == ConsoleKey.Escape ||
                        key.Key == ConsoleKey.Spacebar ||
                        key.Key == ConsoleKey.Enter)
                    {
                        break;
                    }
                }
                Logger.Info("wait");
                Thread.Sleep(500);
            }

            qs?.Release();
            t?.ReqUserLogout();
        }