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); } } } }
//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); } } }