Ejemplo n.º 1
0
        public bool EqualOrInverted(AssetPair other)
        {
            Debug.Assert(IsValid());
            Debug.Assert(other.IsValid());

            return(Equals(other) || IsInverted(other));
        }
Ejemplo n.º 2
0
        public bool IsInverted(AssetPair other)
        {
            Debug.Assert(IsValid());
            Debug.Assert(other.IsValid());

            return(Base.Id == other.Quote.Id && Quote.Id == other.Base.Id);
        }
Ejemplo n.º 3
0
        public static IEnumerable <LimitOrder> GetAsks(OrderBook orderBook, AssetPair target)
        {
            Debug.Assert(orderBook != null);
            Debug.Assert(target != null && target.IsValid());
            Debug.Assert(target.EqualOrInverted(orderBook.AssetPair));

            var bids = orderBook.Bids;
            var asks = orderBook.Asks;

            // Streight
            if (orderBook.AssetPair.Base.Id == target.Base.Id &&
                orderBook.AssetPair.Quote.Id == target.Quote.Id)
            {
                foreach (var ask in asks)
                {
                    yield return(ask);
                }
            }

            // Inverted
            if (orderBook.AssetPair.Base.Id == target.Quote.Id &&
                orderBook.AssetPair.Quote.Id == target.Base.Id)
            {
                foreach (var bid in bids)
                {
                    var ask = bid.Reciprocal();
                    yield return(ask);
                }
            }
        }
Ejemplo n.º 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);
        }
Ejemplo n.º 5
0
        public static SynthOrderBook FromOrderBook(OrderBook orderBook, AssetPair target)
        {
            Debug.Assert(orderBook != null);
            Debug.Assert(target != null && target.IsValid());
            Debug.Assert(target.EqualOrInverted(orderBook.AssetPair));

            var result = new SynthOrderBook(target, new List <OrderBook> {
                orderBook
            });

            return(result);
        }
Ejemplo n.º 6
0
        public static SynthOrderBook FromOrderBooks(OrderBook first, OrderBook second, 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(target != null && target.IsValid());

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

            return(result);
        }