JKorf / Bittrex.Net

A C# .Net wrapper for the Bittrex web API including all features easily accessible and usable
MIT License
141 stars 62 forks source link

Synced order book sampled #171

Closed gabrieligbastos closed 4 years ago

gabrieligbastos commented 4 years ago

Hi, I'm trying to create a sample, that just mantain the order book synced. I've been checking to see if it is same as website, but It starts ok, but than start to frozen some missed orders.

Not being able to find out a solution, does any one have an public example or find some error on the code? Thanks,


var orderSubscription = await client.SubscribeToOrderBookUpdatesAsync("BTC-USD", 25, async update =>
{
                    Console.Clear();

                    orderbook.Ask = SubmitToOrderBook(orderbook.Ask.ToList(), update.AskDeltas.ToList(), false);
                    orderbook.Bid = SubmitToOrderBook(orderbook.Bid.ToList(), update.BidDeltas.ToList(), true);  

                   PrintMyOrderbookSynced();
}

private static ImmutableSortedSet<BittrexOrderBookEntryV3> SubmitToOrderBook(IList<BittrexOrderBookEntryV3> orders, IList<BittrexOrderBookEntryV3> deltaOrders, bool isBid)
        {
            var first = orders.First();
            foreach (var deltaOrder in deltaOrders)
            {
                var foundOrder = orders.FirstOrDefault(o => o.Rate == deltaOrder.Rate);
                if (foundOrder != null)
                {
                    foundOrder.Quantity = deltaOrder.Quantity;
                    Console.WriteLine($"Updated quantity of {deltaOrder.Rate} from isBid = {isBid} book");
                }
                else
                {
                    orders.Add(deltaOrder);
                    Console.WriteLine($"Added {deltaOrder.Rate} to isBid = {isBid} book");
                }      
            }

            return orders.Where(o => o.Quantity > 0)
                .ToImmutableSortedSet((isBid) ? OrderComparer.DescBidComparer() : OrderComparer.DescAskComparer());
        }
    }

    public class OrderComparer : Comparer<BittrexOrderBookEntryV3>
    {
        //-1 for Bid comparer
        //1 for Ask comparer
        readonly int _priceComparisonCoeff;

        OrderComparer(int priceComparisonCoeff)
        {
            _priceComparisonCoeff = priceComparisonCoeff;
        }

        //!! counterintuitive but we need SortedSets to be in descending order. Instead of calling reverse     
        //all the time, we implement the behavior in the comparer
        //Returns: -1 if x is better than y
        //          0 if x is equivalent to y
        //          1 if x is worse than y    
        //        
        //         bids:> most expensive wins  
        //         asks:> cheapest wins,
        //
        //ASSUMPTION: x and y are on the same side (BID or ASK).
        public override int Compare(BittrexOrderBookEntryV3 x, BittrexOrderBookEntryV3 y)
        {
            //two limit orders
            if (x.Rate.CompareTo(y.Rate) != 0)
            {
                return _priceComparisonCoeff * x.Rate.CompareTo(y.Rate);
            }

            //they have the same characteristics. not necessary same ID
            //not good because we are not supposed to have two equivalent orders in the orderbook
            return 0;
        }

        public static OrderComparer DescBidComparer()
        {
             return new OrderComparer(-1);
        }

        public static OrderComparer DescAskComparer()
        {
            return new OrderComparer(1);
        }
    }
JKorf commented 4 years ago

Have you had a look at the BittrexSymbolOrderBook implementation? It maintains a synced order book for Bittrex.

standimj commented 4 years ago

I am also looking at the order book and believe it is still using the old socket client. Any plans to update to v3? Thank you.

pwnzya commented 4 years ago

I tried to implement BittrexSymbolOrderBookV3 myself -> #173.

JKorf commented 4 years ago

BittrexSymbolOrderBook now uses V3 API. Let me know if you're having any problems.