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