Exemplo n.º 1
0
 //登出
 public void Logout()
 {
     mTrader.ReqUserLogout();
 }
Exemplo n.º 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();
        }