Exemplo n.º 1
0
        static void ReceiveServerMessages()
        {
            byte[]   buff = new byte[1000];
            EndPoint rep  = new IPEndPoint(IPAddress.Any, 0);
            int      n;

            for (;;)
            {
                n = socket.ReceiveFrom(buff, ref rep);
                string message = ASCIIEncoding.ASCII.GetString(buff, 0, n);
                if (message.StartsWith("POS "))
                {
                    string[] toks = message.Split(' ');
                    ExistingPosition.Add(toks[1], Int32.Parse(toks[2]));
                }
                if (!message.StartsWith("message type="))
                {
                    Console.WriteLine(message);
                }
                if (message.StartsWith("TM_NEW_EXECUTION"))
                {
                    string[] toks    = message.Split('|');
                    string   symbol  = toks[1];
                    double   price   = double.Parse(toks[2]);
                    int      execQty = Int32.Parse(toks[3]);
                    int      posQty  = Int32.Parse(toks[4]);
                    if (dict.ContainsKey(symbol))
                    {
                        QuoteHolder qh = dict[symbol];
                        qh.PostExec(symbol, execQty, price);
                    }
                }
            }
        }
Exemplo n.º 2
0
        static void Main(string[] args)
        {
            string[] symbols = config.GetSymbols();
            //symbols = (from s in symbols select s).Skip(2500).Take(1000).ToArray();
            File.WriteAllLines(@"e:\symbs.txt", symbols);
            Packet.audit = File.OpenWrite(@"e:\audit" + DateTime.Now.ToString("HHmmss") + ".bin");
            TcpClient cl = new TcpClient();

            cl.Connect(config.quoteServerEP);
            //cl.Connect(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 5555));
            StreamWriter sw = new StreamWriter(cl.GetStream());

            sw.AutoFlush = true;
            Stream str = cl.GetStream();

            byte[] key = { 0x08, 0x00, 0x0eb, 0x03, 0x03, 0x00, 0x00, 0x00 };
            str.Write(key, 0, 8);
            str.Flush();
            //str.Write(connect, 0, connect.Length);
            str.Write(config.logonRequest, 0, config.logonRequest.Length);
            str.Flush();
            Packet.GetPacket(str);
            //str.Write(config.zeroRequest, 0, config.zeroRequest.Length);
            //z = true;
            //Packet.GetPacket(str);
            str.Flush();

            byte[] last = null;
            InitSocket();
            new Thread(MessageLoop).Start(str);
            new Thread(ReceiveServerMessages).Start();
            dict["BG"] = new QuoteHolder("BG 1.21 183869 61.75");
            foreach (string s in symbols)
            {
                int         c      = 0;
                QuoteHolder qh     = new QuoteHolder(s);
                string      symbol = s.Split(' ')[0];
                dict[symbol] = qh;
                //byte[] req = { 0x10, 0x00, 0xbd, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
                byte[] req = new byte[config.symbolRequest.Length];
                Array.Copy(config.symbolRequest, req, req.Length);
                Array.Copy(ASCIIEncoding.ASCII.GetBytes(symbol), 0, req, 4, symbol.Length);
                str.Write(req, 0, req.Length);
                //Console.WriteLine("subscribing " + symbol);
                str.Flush();
                lock (outstanding) {
                    c = outstanding.Count;
                }
                while (c > 50)
                {
                    Thread.Sleep(100);
                    Console.WriteLine("sleeping");
                }
            }
            Console.WriteLine("all symbols sent");
        }
Exemplo n.º 3
0
 private static void ExecuteOPG()
 {
     Console.WriteLine($"window open {DateTime.Now}");
     T92750 = true;
     foreach (string sym in dict.Keys)
     {
         QuoteHolder qhx = dict[sym];
         double      Bid = 0;
         double      Ask = 0;
         if (qhx.Resp.Count > 0 && qhx.Resp[0].Snapshot != null)
         {
             RespRefreshSymbolPacket.SnapShot snap = qhx.Resp[0].Snapshot;
             if (qhx.L1 == null)
             {
                 Bid = snap.Bid;
                 Ask = snap.Ask;
             }
             else
             {
                 Bid = qhx.L1.Bid;
                 Ask = qhx.L1.Ask;
             }
             //if (qhx.Volume < 100 && Ask > snap.Close)
             if (true && !ExistingPosition.ContainsKey(sym)) //TESTING USE ABOVE LINE
             {
                 double TargetPrice = snap.Close - (qhx.ATR * .25);
                 TargetPrice = Ask + .03;                                                                                                                                     //TESTING DELETE THIS LINE
                 string m = $"B|{sym}|{snap.Close}|{qhx.Volume}|{qhx.ATR.ToString("0.00")}|{TargetPrice.ToString("0.00")}|{Bid.ToString("0.00")}|{Ask.ToString("0.00")}|DAY"; //TESTING = shour be OPG
                 //qhx.PostExec(sym, 100, TargetPrice);
                 Console.WriteLine(m);
                 sendMessage(m);
             }
         }
     }
     Console.WriteLine($"window closed {DateTime.Now}");
 }
Exemplo n.º 4
0
        static void Main(string[] args)
        {
            string[] symbols = config.GetSymbols();
            foreach (string s in symbols)
            {
                QuoteHolder qh     = new QuoteHolder(s);
                string      symbol = s.Split(' ')[0];
                dict[symbol] = qh;
            }
            dict["SPY"] = new QuoteHolder("SPY 1.21 183869 61.75");
            //symbols = (from s in symbols select s).Skip(2500).Take(1000).ToArray();
            //File.WriteAllLines(@"e:\symbs.txt", symbols);
            //Packet.audit = File.OpenWrite(@"e:\audit" + DateTime.Now.ToString("HHmmss") + ".bin");
            TcpClient cl = new TcpClient();

            cl.Connect(config.quoteServerEP);
            //cl.Connect(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 5555));
            StreamWriter sw = new StreamWriter(cl.GetStream());

            sw.AutoFlush = true;
            Stream str = cl.GetStream();

            byte[] key = { 0x08, 0x00, 0x0eb, 0x03, 0x03, 0x00, 0x00, 0x00 };
            str.Write(key, 0, 8);
            str.Flush();
            //str.Write(connect, 0, connect.Length);
            str.Write(config.logonRequest, 0, config.logonRequest.Length);
            str.Flush();
            Packet.GetPacket(str);
            //str.Write(config.zeroRequest, 0, config.zeroRequest.Length);
            //z = true;
            //Packet.GetPacket(str);
            str.Flush();

            byte[] last = null;
            new Thread(MessageLoop).Start(str);

            new Thread(() => {
                Thread.Sleep(1000);
                lock (outstanding)
                {
                    foreach (string k in outstanding.Keys)
                    {
                        if ((DateTime.Now - outstanding[k]).Seconds > 2)
                        {
                            Console.WriteLine($"remove {k}");
                            outstanding.Remove(k);
                        }
                    }
                }
            }).Start();
            //new Thread(() => {
            //    for (;;)
            //    {
            //        string s = Console.ReadLine();
            //        Console.WriteLine(s);
            //        watching = s;

            //        if (dict.ContainsKey(s))
            //        {
            //            QuoteHolder qh = dict[s];
            //            double bid = qh.Resp.Count > 0 ? qh.Resp[0].Bid : 0;
            //            Console.WriteLine($"bid={bid}");
            //            string l1 = qh.L1 == null ? "XX" : qh.L1.ToString();
            //            Console.WriteLine($"s ={l1}");
            //        }
            //    }
            //}).Start();
            foreach (string s in symbols)
            {
                int    c      = 0;
                string symbol = s.Split(' ')[0];
                //QuoteHolder qh = new QuoteHolder(s);
                //dict[symbol] = qh;
                //byte[] req = { 0x10, 0x00, 0xbd, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
                byte[] req = new byte[config.symbolRequest.Length];
                Array.Copy(config.symbolRequest, req, req.Length);
                Array.Copy(ASCIIEncoding.ASCII.GetBytes(symbol), 0, req, 4, symbol.Length);
                str.Write(req, 0, req.Length);
                //Console.WriteLine("subscribing " + symbol);
                str.Flush();
                lock (outstanding)
                {
                    c = outstanding.Count;
                }
                while (c > 50)
                {
                    Thread.Sleep(100);
                    Console.WriteLine("sleeping");
                }
            }
        }
Exemplo n.º 5
0
        //static DateTime dtt = DateTime.Now.AddSeconds(30);

        private static void MessageLoop(object o)
        {
            Console.WriteLine(dtt);
            Stream str = (Stream)o;

            sendMessage($"Window will open at {dtt}");
            int         packetCount    = 0;
            int         packetInterval = 0;
            int         l1             = 0;
            int         resp           = 0;
            int         trade          = 0;
            DateTime    dt             = DateTime.Now;
            DateTime    ldt            = DateTime.Now;
            QuoteHolder qh;

            for (;;)
            {
                //Packet p = new Packet(str);
                Packet p = Packet.GetPacket(str);
                if (!T92750 && DateTime.Now > dtt)
                {
                    Console.WriteLine($"window open {DateTime.Now}");
                    T92750 = true;
                    foreach (string sym in dict.Keys)
                    {
                        QuoteHolder qhx = dict[sym];
                        double      Ask = -1;
                        if (qhx.L1 == null)
                        {
                            if (qhx.Resp.Count > 0)
                            {
                                if (qhx.Resp[0].rawPayload[14] == 15)
                                {
                                    Ask = qhx.Resp[0].Ask;
                                }
                            }
                        }
                        else
                        {
                            Ask = qhx.L1.Ask;
                        }
                        if (qhx.Volume < 100 && Ask > qhx.PrevClose)
                        {
                            double TargetPrice = qhx.PrevClose - (qhx.ATR * .25);
                            string m           = $"B {sym} Ask={Ask} PrevClose={qhx.PrevClose} vol={qhx.Volume} atr={qhx.ATR} buyprice={TargetPrice.ToString("0.00")}";
                            sendMessage(m);
                        }
                    }
                }
                try
                {
                    packetCount++;
                    packetInterval++;
                    if ((DateTime.Now - ldt).TotalSeconds > 10)
                    {
                        Console.WriteLine("total = " + packetCount / 1000000 + " interval = " + packetInterval + " l1=" + l1 + " resp=" + resp + " trade=" + trade + " symbols = " + dict.Keys.Count);
                        l1  = 0; trade = 0; packetInterval = 0;
                        ldt = DateTime.Now;
                        //Packet.audit.Flush();
                    }
                    switch (p.packetType)
                    {
                    case Packet.PacketType.M_RESP_REFRESH_SYMBOL:
                        //Console.WriteLine("resp");
                        resp++;
                        RespRefreshSymbolPacket rp = (RespRefreshSymbolPacket)p;
                        lock (outstanding)
                        {
                            if (outstanding.ContainsKey(rp.Symbol))
                            {
                                outstanding.Remove(rp.Symbol);
                            }
                        }
                        if (dict.ContainsKey(rp.Symbol))
                        {
                            qh = dict[rp.Symbol];
                            qh.Resp.Add(rp);
                            qh.Volume = qh.Resp[0].Volume;
                        }
                        break;

                    case Packet.PacketType.M_TRADE_REPORT:
                        trade++;
                        TradeReportPacket tp = (TradeReportPacket)p;
                        string            ts = tp.Symbol;
                        qh         = dict[ts];
                        qh.Volume += tp.Size;
                        //Console.WriteLine(p);
                        if (watching == ts)
                        {
                            Console.WriteLine("TR + " + qh.L1 == null ? "xx" : qh.L1 + " " + qh.Volume + " " + tp.Size);
                        }
                        break;

                    case Packet.PacketType.M_BOOK_TRADE:
                        //trade++;
                        //Console.WriteLine(p);
                        break;

                    case Packet.PacketType.M_LEVEL1:
                        l1++;
                        L1Packet q  = (L1Packet)p;
                        string   qs = q.Symbol;
                        try
                        {
                            qh = dict[qs];
                            //double pct = .2;
                            //double targetPrice = qh.PrevClose + (qh.ATR * pct);
                            ////if (watching == qs)
                            //if (!qh.hasPosition)
                            //{
                            //    if (q.Bid > targetPrice)
                            //    {
                            //        string s = $"{qs} prevClose={qh.PrevClose} targetPrice={targetPrice}  atr={qh.ATR} bid={q.Bid} pct={pct.ToString("0.00")}";
                            //        Console.WriteLine(s);
                            //        qh.hasPosition = true;
                            //        Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
                            //        IPAddress send_to_address = IPAddress.Parse("127.0.0.1");
                            //        IPEndPoint sending_end_point = new IPEndPoint(send_to_address, 5555);
                            //        socket.SendTo(ASCIIEncoding.ASCII.GetBytes(s), sending_end_point);
                            //    }
                            //    //Console.WriteLine($"{q} {qh.Volume} {qh.PrevClose} {qh.ATR}");
                            //}
                            qh.L1 = (L1Packet)p;
                        }
                        catch (Exception)
                        {
                            Console.WriteLine("unsolicited pack");
                        }
                        break;

                    case Packet.PacketType.M_BOOK_NEW_QUOTE:
                    case Packet.PacketType.M_BOOK_MODIFY_QUOTE:
                    case Packet.PacketType.M_BOOK_DELETE_QUOTE:
                    case Packet.PacketType.M_STOCK_IMBALANCE_INDICATOR:
                    case Packet.PacketType.M_BOOK_ORDER_CANCELED:
                        break;

                    default:
                        //Console.WriteLine(p);
                        break;
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                }
            }
        }
Exemplo n.º 6
0
 public virtual void ProcessTrade(QuoteHolder qh)
 {
 }
Exemplo n.º 7
0
 public virtual void ProcessExecution(string exec, QuoteHolder qh)
 {
 }
Exemplo n.º 8
0
        static void Main(string[] args)
        {
            string[] symbols = config.GetSymbols();
            //symbols = (from s in symbols select s).Skip(2500).Take(1000).ToArray();
            File.WriteAllLines(@"e:\symbs.txt", symbols);
            Packet.audit = File.OpenWrite(@"e:\audit" + DateTime.Now.ToString("HHmmss") + ".bin");
            TcpClient cl = new TcpClient();

            cl.Connect(config.quoteServerEP);
            //cl.Connect(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 5555));
            StreamWriter sw = new StreamWriter(cl.GetStream());

            sw.AutoFlush = true;
            Stream str = cl.GetStream();

            byte[] key = { 0x08, 0x00, 0x0eb, 0x03, 0x03, 0x00, 0x00, 0x00 };
            str.Write(key, 0, 8);
            str.Flush();
            //str.Write(connect, 0, connect.Length);
            str.Write(config.logonRequest, 0, config.logonRequest.Length);
            str.Flush();
            Packet.GetPacket(str);
            //str.Write(config.zeroRequest, 0, config.zeroRequest.Length);
            //z = true;
            //Packet.GetPacket(str);
            str.Flush();

            byte[] last = null;
            new Thread(MessageLoop).Start(str);

            new Thread(() => {
                Thread.Sleep(1000);
                lock (outstanding)
                {
                    foreach (string k in outstanding.Keys)
                    {
                        if ((DateTime.Now - outstanding[k]).Seconds > 2)
                        {
                            Console.WriteLine($"remove {k}");
                            outstanding.Remove(k);
                        }
                    }
                }
            }).Start();
            dict["BG"] = new QuoteHolder("BG 1.21 183869 61.75");
            for (;;)
            {
                string      sy     = Console.ReadLine();
                string      s      = $"{sy} 1.21 183869 61.75";
                int         c      = 0;
                QuoteHolder qh     = new QuoteHolder(s);
                string      symbol = s.Split(' ')[0];
                dict[symbol] = qh;
                //byte[] req = { 0x10, 0x00, 0xbd, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
                byte[] req = new byte[config.symbolRequest.Length];
                Array.Copy(config.symbolRequest, req, req.Length);
                Array.Copy(ASCIIEncoding.ASCII.GetBytes(symbol), 0, req, 4, symbol.Length);
                str.Write(req, 0, req.Length);
                //Console.WriteLine("subscribing " + symbol);
                str.Flush();
                lock (outstanding)
                {
                    c = outstanding.Count;
                }
                while (c > 50)
                {
                    Thread.Sleep(100);
                    Console.WriteLine("sleeping");
                }
            }
            //for (;;)
            //{
            //    string symbol = Console.ReadLine();
            //    if (!symbol.EndsWith("-"))
            //    {
            //        config.updateSymbol(symbol);
            //        str.Write(config.symbolRequest, 0, config.symbolRequest.Length);
            //        str.Flush();
            //    }
            //    else
            //    {
            //        symbol = symbol.TrimEnd('-');
            //        unsubscribe(str, symbol);
            //        Console.WriteLine("sent unsubscribe " + symbol);
            //    }
            //}
        }
Exemplo n.º 9
0
        //static DateTime dtt = DateTime.Now.AddMinutes(2.5);
        //static DateTime dtt = DateTime.Now.AddSeconds(30);

        private static void MessageLoop(object o)
        {
            Console.WriteLine(dtt);
            Stream str = (Stream)o;

            sendMessage($"Window will open at {dtt}");
            int         packetCount    = 0;
            int         packetInterval = 0;
            int         l1             = 0;
            int         resp           = 0;
            int         trade          = 0;
            DateTime    dt             = DateTime.Now;
            DateTime    ldt            = DateTime.Now;
            QuoteHolder qh;

            QuoteHolder.Logger = new StreamWriter($@"e:\QHL{DateTime.Now.ToString("MMddyyyy-HHmmss")}.txt");
            for (int i = 0; ; i++)
            {
                if (i % 10000 == 0)
                {
                    QuoteHolder.Logger.Flush();
                }
                //Packet p = new Packet(str);
                Packet p = Packet.GetPacket(str);
                if (!T92750 && DateTime.Now > dtt)
                {
                    Console.WriteLine($"window open {DateTime.Now}");
                    T92750 = true;
                    foreach (string sym in dict.Keys)
                    {
                        QuoteHolder qhx = dict[sym];
                        if (qhx.Resp.Count > 0 && qhx.Resp[0].Snapshot != null)
                        {
                            RespRefreshSymbolPacket.SnapShot snap = qhx.Resp[0].Snapshot;
                            Double Ask = 0;
                            Double Bid = 0;
                            if (qhx.L1 == null)
                            {
                                Ask = snap.Ask;
                                Bid = snap.Bid;
                            }
                            else
                            {
                                Ask = qhx.L1.Ask;
                                Bid = qhx.L1.Bid;
                            }
                            if (qhx.Volume < 100 && Ask > qhx.PrevClose && qhx.L1 != null) //TESTCLIENT
                            //if (qhx.Volume < 100000 && Ask > qhx.PrevClose && qhx.L1 != null) //TESTING
                            {
                                double TargetPrice = qhx.PrevClose - (qhx.ATR * .25);
                                string m           = $"B {sym} Ask={Ask} PrevClose={qhx.PrevClose} vol={qhx.Volume} atr={qhx.ATR} buyprice={TargetPrice.ToString("0.00")}";
                                qhx.PostExec(sym, 100, TargetPrice);
                                Console.WriteLine(m);
                                sendMessage(m);
                            }
                        }
                    }
                }
                try
                {
                    packetCount++;
                    packetInterval++;
                    if ((DateTime.Now - ldt).TotalSeconds > 10)
                    {
                        Console.WriteLine("total = " + packetCount / 1000000 + " interval = " + packetInterval + " l1=" + l1 + " resp=" + resp + " trade=" + trade + " symbols = " + dict.Keys.Count);
                        l1  = 0; trade = 0; packetInterval = 0;
                        ldt = DateTime.Now;
                        Packet.audit.Flush();
                    }
                    switch (p.packetType)
                    {
                    case Packet.PacketType.M_RESP_REFRESH_SYMBOL:
                        //Console.WriteLine("resp");
                        resp++;
                        RespRefreshSymbolPacket rp = (RespRefreshSymbolPacket)p;
                        lock (outstanding)
                        {
                            if (outstanding.ContainsKey(rp.Symbol))
                            {
                                outstanding.Remove(rp.Symbol);
                            }
                        }
                        if (dict.ContainsKey(rp.Symbol))
                        {
                            qh = dict[rp.Symbol];
                            qh.Resp.Add(rp);
                            qh.Volume = qh.Resp[0].Volume;
                            const byte SRS_LEVEL1 = 1 << 2;
                            if ((rp.Steps & SRS_LEVEL1) != 0)
                            {
                                Console.WriteLine(rp.Snapshot);
                            }
                        }
                        //for(int i = 42;i < p.length; i += 32)
                        //{
                        //    string t = ASCIIEncoding.ASCII.GetString(p.rawPayload, i, 4);
                        //    Console.WriteLine(t);
                        //}
                        //string symbol = ASCIIEncoding.ASCII.GetString(p.rawPayload, 6, 10).TrimEnd((Char)0);
                        //if (last != null)
                        //{
                        //    for (int i = 16; i < 42; i++)
                        //    {
                        //        if (last[i] != p.rawPayload[i])
                        //        {
                        //            Console.WriteLine("i=" + i + " l=" + last[i] + " cu=" + p.rawPayload[i]);
                        //        }
                        //    }
                        //    for (int i = 16; i < 42; i++)
                        //    {
                        //        Console.WriteLine(i + " " + BitConverter.ToInt16(p.rawPayload,i));
                        //    }
                        //    for (int i = 16; i < 42; i++)
                        //    {
                        //        Console.WriteLine(i + " " + BitConverter.ToInt32(p.rawPayload, i));
                        //    }
                        //}
                        //last = p.rawPayload;
                        break;

                    case Packet.PacketType.M_TRADE_REPORT:
                        trade++;
                        TradeReportPacket tp = (TradeReportPacket)p;
                        string            ts = tp.Symbol;
                        qh = dict[ts];
                        qh.ProcessTrade(tp);
                        //Console.WriteLine(p);
                        if (watching == ts)
                        {
                            Console.WriteLine("TR + " + qh.L1 == null ? "xx" : qh.L1 + " " + qh.Volume + " " + tp.Size);
                        }
                        break;

                    case Packet.PacketType.M_BOOK_TRADE:
                        //trade++;
                        //Console.WriteLine(p);
                        break;

                    case Packet.PacketType.M_LEVEL1:
                        l1++;
                        L1Packet q  = (L1Packet)p;
                        string   qs = q.Symbol;
                        try
                        {
                            qh = dict[qs];
                            //double pct = .2;
                            //double targetPrice = qh.PrevClose + (qh.ATR * pct);
                            ////if (watching == qs)
                            //if (!qh.hasPosition)
                            //{
                            //    if (q.Bid > targetPrice)
                            //    {
                            //        string s = $"{qs} prevClose={qh.PrevClose} targetPrice={targetPrice}  atr={qh.ATR} bid={q.Bid} pct={pct.ToString("0.00")}";
                            //        Console.WriteLine(s);
                            //        qh.hasPosition = true;
                            //        Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
                            //        IPAddress send_to_address = IPAddress.Parse("127.0.0.1");
                            //        IPEndPoint sending_end_point = new IPEndPoint(send_to_address, 5555);
                            //        socket.SendTo(ASCIIEncoding.ASCII.GetBytes(s), sending_end_point);
                            //    }
                            //    //Console.WriteLine($"{q} {qh.Volume} {qh.PrevClose} {qh.ATR}");
                            //}
                            qh.L1 = (L1Packet)p;
                        }
                        catch (Exception)
                        {
                            Console.WriteLine("unsolicitied pack");
                        }
                        break;

                    case Packet.PacketType.M_BOOK_NEW_QUOTE:
                    case Packet.PacketType.M_BOOK_MODIFY_QUOTE:
                    case Packet.PacketType.M_BOOK_DELETE_QUOTE:
                    case Packet.PacketType.M_STOCK_IMBALANCE_INDICATOR:
                    case Packet.PacketType.M_BOOK_ORDER_CANCELED:
                        break;

                    default:
                        //Console.WriteLine(p);
                        break;
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                }
            }
        }
Exemplo n.º 10
0
        static DateTime dtt = DateTime.Now.AddSeconds(20); //TESTING USE ABOVE LINE

        private static void MessageLoop(object o)
        {
            Console.WriteLine(dtt);
            Stream str = (Stream)o;

            //sendMessage($"Window will open at {dtt}");
            Console.WriteLine($"Window will open at {dtt}");
            int         packetCount    = 0;
            int         packetInterval = 0;
            int         l1             = 0;
            int         resp           = 0;
            int         trade          = 0;
            DateTime    dt             = DateTime.Now;
            DateTime    ldt            = DateTime.Now;
            QuoteHolder qh;

            QuoteHolder.Logger = new StreamWriter($@"e:\Q{DateTime.Now.ToString("HHmmss")}.txt");
            new Thread(t => {
                Thread.Sleep(1000);
                QuoteHolder.Logger.Flush();
            }).Start();
            for (int i = 0;; i++)
            {
                if (i % 10000 == 0)
                {
                    QuoteHolder.Logger.Flush();
                }
                //Packet p = new Packet(str);
                Packet p = Packet.GetPacket(str);
                if (!T92750 && DateTime.Now > dtt)
                {
                    ExecuteOPG();
                }
                try
                {
                    packetCount++;
                    packetInterval++;
                    if ((DateTime.Now - ldt).TotalSeconds > 10)
                    {
                        Console.WriteLine("total = " + packetCount / 1000000 + " interval = " + packetInterval + " l1=" + l1 + " resp=" + resp + " trade=" + trade + " symbols = " + dict.Keys.Count);
                        l1  = 0; trade = 0; packetInterval = 0;
                        ldt = DateTime.Now;
                        Packet.audit.Flush();
                    }
                    switch (p.packetType)
                    {
                    case Packet.PacketType.M_RESP_REFRESH_SYMBOL:
                        //Console.WriteLine("resp");
                        resp++;
                        RespRefreshSymbolPacket rp = (RespRefreshSymbolPacket)p;
                        lock (outstanding)
                        {
                            if (outstanding.ContainsKey(rp.Symbol))
                            {
                                outstanding.Remove(rp.Symbol);
                            }
                        }
                        if (dict.ContainsKey(rp.Symbol))
                        {
                            qh = dict[rp.Symbol];
                            qh.Resp.Add(rp);
                            //qh.Volume = qh.Resp[0].Volume;
                            const byte SRS_LEVEL1 = 1 << 2;
                            if ((rp.Steps & SRS_LEVEL1) != 0)
                            {
                                qh.Volume = rp.Snapshot.Volume;
                                //Console.WriteLine(rp.Snapshot.ToString());
                            }
                        }

                        break;

                    case Packet.PacketType.M_TRADE_REPORT:
                        trade++;
                        TradeReportPacket tp = (TradeReportPacket)p;
                        string            ts = tp.Symbol;
                        qh = dict[ts];
                        int QtyToSell = qh.ProcessTrade(tp);
                        Console.WriteLine($"{tp.Symbol} process Trade returned {QtyToSell}");
                        if (QtyToSell > 0 && !qh.Closing && !ExistingPosition.ContainsKey(ts))
                        {
                            QuoteHolder qhx = dict[tp.Symbol];
                            RespRefreshSymbolPacket.SnapShot snap = qhx.Resp[0].Snapshot;     //DANGEROUS
                            //double TargetPrice = qhx.Resp[0].Snapshot.Close - (qhx.ATR * .25);
                            double Ask         = qhx.L1 != null ? qhx.L1.Ask : snap.Ask;
                            double Bid         = qhx.L1 != null ? qhx.L1.Bid : snap.Bid;
                            double TargetPrice = Bid - .03;                                                                                                                                              //TESTING DELETE THIS LINE
                            string m           = $"S|{tp.Symbol}|{snap.Close}|{qhx.Volume}|{qhx.ATR.ToString("0.00")}|{TargetPrice.ToString("0.00")}|{Bid.ToString("0.00")}|{Ask.ToString("0.00")}|DAY"; //TESTING = shour be OPG
                            //qhx.PostExec(sym, 100, TargetPrice);
                            Console.WriteLine(m);
                            qh.Closing = true;
                            sendMessage(m);
                        }
                        if (watching == ts)
                        {
                            Console.WriteLine("TR + " + qh.L1 == null ? "xx" : qh.L1 + " " + qh.Volume + " " + tp.Size);
                        }
                        break;

                    case Packet.PacketType.M_BOOK_TRADE:
                        //trade++;
                        //Console.WriteLine(p);
                        break;

                    case Packet.PacketType.M_LEVEL1:
                        l1++;
                        L1Packet q  = (L1Packet)p;
                        string   qs = q.Symbol;
                        try
                        {
                            qh    = dict[qs];
                            qh.L1 = (L1Packet)p;
                        }
                        catch (Exception)
                        {
                            Console.WriteLine("unsolicitied pack");
                        }
                        break;

                    case Packet.PacketType.M_BOOK_NEW_QUOTE:
                    case Packet.PacketType.M_BOOK_MODIFY_QUOTE:
                    case Packet.PacketType.M_BOOK_DELETE_QUOTE:
                    case Packet.PacketType.M_STOCK_IMBALANCE_INDICATOR:
                    case Packet.PacketType.M_BOOK_ORDER_CANCELED:
                        break;

                    default:
                        //Console.WriteLine(p);
                        break;
                    }
                } catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                }
            }
        }