Example #1
0
 public bool Equals(AssetPair other)
 {
     return(Id == other.Id);
 }
Example #2
0
 public void SetAssetPair(AssetPair assetPair)
 {
     AssetPair = assetPair;
 }
Example #3
0
        public static SynthOrderBook FromOrderBooks(OrderBook first, OrderBook second, OrderBook third, AssetPair target)
        {
            Debug.Assert(first != null);
            Debug.Assert(first.AssetPair != null && first.AssetPair.IsValid());
            Debug.Assert(second != null);
            Debug.Assert(second.AssetPair != null && second.AssetPair.IsValid());
            Debug.Assert(third != null);
            Debug.Assert(third.AssetPair != null && third.AssetPair.IsValid());
            Debug.Assert(target != null && target.IsValid());

            var result = new SynthOrderBook(target, GetOrdered(new List <OrderBook> {
                first, second, third
            }, target));

            return(result);
        }
Example #4
0
        public static IReadOnlyList <AssetPair> GetChained(IReadOnlyCollection <OrderBook> orderBooks, AssetPair target)
        {
            Debug.Assert(orderBooks != null);
            Debug.Assert(orderBooks.Any());
            Debug.Assert(target != null);
            Debug.Assert(target.IsValid());

            var result = new List <AssetPair>();

            var @base = target.Base;
            var quote = target.Quote;

            var first = orderBooks.Single(x => x.AssetPair.ContainsAsset(@base.Id)).AssetPair;

            if (first.Quote.Id == @base.Id)
            {
                first = first.Invert();
            }
            result.Add(first);

            if (orderBooks.Count == 1)
            {
                Debug.Assert(first.Quote.Id == quote.Id);
                return(result);
            }

            var nextAssetId = first.GetOtherAssetId(@base.Id);
            var second      = orderBooks.Single(x => x.AssetPair.ContainsAsset(nextAssetId) && !x.AssetPair.EqualOrInverted(first)).AssetPair;

            if (second.Quote.Id == nextAssetId)
            {
                second = second.Invert();
            }
            result.Add(second);

            if (orderBooks.Count == 2)
            {
                Debug.Assert(second.Quote.Id == quote.Id);
                return(result);
            }

            nextAssetId = second.GetOtherAssetId(nextAssetId);
            var third = orderBooks.Single(x => x.AssetPair.ContainsAsset(nextAssetId) && x.AssetPair.ContainsAsset(quote.Id)).AssetPair;

            if (third.Quote.Id == nextAssetId)
            {
                third = third.Invert();
            }
            result.Add(third);

            Debug.Assert(third.Quote.Id == quote.Id);
            return(result);
        }
Example #5
0
        public static IDictionary <AssetPair, OrderBook> PrepareForEnumeration(IReadOnlyCollection <OrderBook> orderBooks, AssetPair target)
        {
            var result = new Dictionary <AssetPair, OrderBook>();

            var chainedAssetPairs = GetChained(orderBooks, target);
            var orderedOrderBooks = GetOrdered(orderBooks, target);

            Debug.Assert(orderBooks.Count == orderedOrderBooks.Count && orderedOrderBooks.Count == chainedAssetPairs.Count);

            for (var i = 0; i < orderBooks.Count; i++)
            {
                result.Add(chainedAssetPairs[i], orderedOrderBooks[i]);
            }

            return(result);
        }
Example #6
0
        public static IReadOnlyCollection <SynthOrderBook> GetSynthsFrom2(AssetPair target,
                                                                          IReadOnlyCollection <OrderBook> sourceOrderBooks, IReadOnlyCollection <OrderBook> allOrderBooks)
        {
            var result = new List <SynthOrderBook>();

            // Trying to find base asset in current source's asset pair
            var withBaseOrQuoteOrderBooks = sourceOrderBooks.Where(x => x.AssetPair.ContainsAsset(target.Base.Id) ||
                                                                   x.AssetPair.ContainsAsset(target.Quote.Id)).ToList();

            foreach (var withBaseOrQuoteOrderBook in withBaseOrQuoteOrderBooks)
            {
                var withBaseOrQuoteAssetPair = withBaseOrQuoteOrderBook.AssetPair;

                // Get intermediate asset
                var intermediateId = withBaseOrQuoteAssetPair.GetOtherAssetId(target.Base.Id)
                                     ?? withBaseOrQuoteAssetPair.GetOtherAssetId(target.Quote.Id);

                // 1. If current is target or inverted then just use it
                if (intermediateId == target.Base.Id || intermediateId == target.Quote.Id)
                {
                    continue; // The pairs are the same or inverted (it is from 1 order book)
                }
                // 1. If current is base&intermediate then find quote&intermediate
                if (withBaseOrQuoteAssetPair.ContainsAsset(target.Base.Id))
                {
                    var baseAndIntermediate = withBaseOrQuoteOrderBook;
                    // Trying to find quote/intermediate or intermediate/quote pair (quote&intermediate)
                    var intermediateQuoteOrderBooks = allOrderBooks
                                                      .Where(x => x.AssetPair.ContainsAsset(intermediateId) && x.AssetPair.ContainsAsset(target.Quote.Id))
                                                      .ToList();

                    foreach (var intermediateQuoteOrderBook in intermediateQuoteOrderBooks)
                    {
                        if (!baseAndIntermediate.Asks.Any() && !baseAndIntermediate.Bids.Any() ||
                            !intermediateQuoteOrderBook.Asks.Any() && !intermediateQuoteOrderBook.Bids.Any())
                        {
                            continue;
                        }

                        var synthOrderBook = FromOrderBooks(baseAndIntermediate, intermediateQuoteOrderBook, target);
                        result.Add(synthOrderBook);
                    }
                }

                // 2. If current is quote&intermediate then find base&intermediate
                if (withBaseOrQuoteAssetPair.ContainsAsset(target.Quote.Id))
                {
                    var quoteAndIntermediate = withBaseOrQuoteOrderBook;
                    // Trying to find base/intermediate or intermediate/base pair (base&intermediate)
                    var intermediateBaseOrderBooks = allOrderBooks
                                                     .Where(x => x.AssetPair.ContainsAsset(intermediateId) && x.AssetPair.ContainsAsset(target.Base.Id))
                                                     .ToList();

                    foreach (var intermediateBaseOrderBook in intermediateBaseOrderBooks)
                    {
                        if (!intermediateBaseOrderBook.Asks.Any() && !intermediateBaseOrderBook.Bids.Any() ||
                            !quoteAndIntermediate.Asks.Any() && !quoteAndIntermediate.Bids.Any())
                        {
                            continue;
                        }

                        var synthOrderBook = FromOrderBooks(intermediateBaseOrderBook, quoteAndIntermediate, target);
                        result.Add(synthOrderBook);
                    }
                }
            }

            return(result);
        }