Exemplo n.º 1
0
        public int ProcessTrade(TradeReportPacket p)
        {
            Logger.WriteLine($"processing {p}");
            int qtyToSell = 0;

            Volume += p.Size;
            if (Current.Update(p))
            {
                P1      = Current;
                Current = new Candle()
                {
                    time = DateTime.Now, High = Double.MinValue, Low = Double.MaxValue
                };
                Current.Update(p);
                //Console.WriteLine($"{p.Symbol} {P1}");
            }
            if (OpenShares > 0 && (DateTime.Now - ExecutionTime).TotalSeconds > 10) //TEST ONLY should be 75
            {
                if (P1 != null)
                {
                    if (P1.Low != Double.MaxValue)
                    {
                        double how = P1.Low - (ATR * .03);
                        if (how > Stop)
                        {
                            Logger.WriteLine($"{p.Symbol} stop moving from {Stop} to {how}");
                            Stop = how;
                        }
                        else
                        {
                            if (p.Price < Stop)
                            {
                                qtyToSell  = OpenShares;
                                OpenShares = 0;
                            }
                        }
                    }
                    else
                    {
                        Logger.WriteLine($"P1 HAD NO TRADES {p.Symbol}");
                    }
                }
                else
                {
                    Logger.WriteLine($"P1 was NULL {p.Symbol}");
                }
            }
            return(qtyToSell);
        }
Exemplo n.º 2
0
            public bool Update(TradeReportPacket p)
            {
                bool closed = false;

                if (p.Time.Minute != time.Minute && p.Time.Hour != time.Hour)
                {
                    closed = true;
                }
                else
                {
                    if (p.Price > High)
                    {
                        High = p.Price;
                    }
                    if (p.Price < Low)
                    {
                        Low = p.Price;
                    }
                }
                return(closed);
            }
Exemplo n.º 3
0
        public static Packet GetPacket(Stream str)
        {
            byte[] lbuff  = new byte[2];
            int    count  = 0;
            ushort length = 0;

            byte[] pl = null;
            do
            {
                int lc = 0;
                do
                {
                    int c = str.Read(lbuff, lc, 2 - lc);
                    if (audit != null)
                    {
                        audit.Write(lbuff, lc, 2 - lc);
                    }
                    lc += c;
                } while (lc < 2);

                length = (ushort)(BitConverter.ToUInt16(lbuff, 0) - 2);
                int offset = 0;
                pl    = new byte[length];
                count = 0;
                while (count < length)
                {
                    int c = str.Read(pl, offset, length - count);
                    if (audit != null)
                    {
                        audit.Write(pl, offset, length - count);
                    }
                    count += c;
                    offset = count;
                }
                if (length < 2)
                {
                    Console.WriteLine("dropped packet");
                    //for(;;)
                    //{
                    //    Console.
                    //}
                }
            } while (length < 2);
            ushort pt = BitConverter.ToUInt16(pl, 0);

//            Console.WriteLine((PacketType)pt);
            if (pt != 501 && pt != 505 && pt != 504)
            {
                //Console.WriteLine((PacketType)pt);
            }
            Packet rv = null;

            // packetType = (PacketType)pt;
            switch ((PacketType)pt)
            {
            case PacketType.M_RESP_REFRESH_SYMBOL:
                rv = new RespRefreshSymbolPacket(length, pt, pl);
                break;

            case PacketType.M_LEVEL1:
                rv = new L1Packet(length, pt, pl);
                break;

            case PacketType.M_BOOK_MODIFY_QUOTE:
                rv = new BookModifyQuotePacket(length, pt, pl);
                break;

            case PacketType.M_BOOK_NEW_QUOTE:
                rv = new BookNewQuotePacket(length, pt, pl);
                break;

            case PacketType.M_BOOK_DELETE_QUOTE:
                rv = new BookDeleteQuotePacket(length, pt, pl);
                break;

            case PacketType.M_STOCK_IMBALANCE_INDICATOR:
                rv = new StockImbalanceIndicator(length, pt, pl);
                break;

            case PacketType.M_BOOK_ORDER_CANCELED:
                rv = new BookOrderCancelledPacket(length, pt, pl);
                break;

            case PacketType.M_BOOK_ORDER_EXECUTED:
                rv = new BookOrderExecutedPacket(length, pt, pl);
                break;

            case PacketType.M_TRADE_REPORT:
                rv = new TradeReportPacket(length, pt, pl);
                break;

            default:
                return(new Packet(length, pt, pl));
            }

            return(rv);
        }