Ejemplo n.º 1
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();
        }
Ejemplo n.º 2
0
        static void Main(string[] args)
        {
            List <string> codes = new List <string>();

            codes.AddRange(RM);
            codes.AddRange(AU);
            codes.AddRange(C);
            codes.AddRange(M);
            codes.AddRange(SR);
            codes.AddRange(AP);
            codes.AddRange(CS);
            codes.AddRange(Y);
            codes.AddRange(A);
            codes.AddRange(CF);
            codes.AddRange(OI);
            codes.AddRange(SN);
            codes.AddRange(B);
            codes.AddRange(PB);
            codes.AddRange(JD);
            codes.AddRange(SF);
            codes.AddRange(CU);
            codes.AddRange(RU);
            codes.AddRange(L);
            codes.AddRange(V);
            codes.AddRange(P);
            codes.AddRange(FG);
            codes.AddRange(ZC);
            codes.AddRange(AL);
            codes.AddRange(FU);
            codes.AddRange(AG);
            codes.AddRange(HC);
            codes.AddRange(RB);
            codes.AddRange(I);
            codes.AddRange(MA);
            codes.AddRange(TA);
            codes.AddRange(ZN);
            codes.AddRange(NI);
            codes.AddRange(J);
            codes.AddRange(JM);
            codes.AddRange(SP);
            codes.AddRange(BU);
            codes.AddRange(SC);
            codes.AddRange(PP);

            Logger.Info("code num: {0}", codes.Count);
            var qs = new QuoteSave(investor, pwd, codes.ToArray());

            qs.Run();

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

            qs.Release();
        }