Exemple #1
0
        public static IOrderBook Order(this IOrderBook orderBook)
        {
            orderBook.Prices = orderBook.IsBuy
                ? orderBook.Prices.OrderByDescending(x => x.Price).ToList()
                : orderBook.Prices.OrderBy(x => x.Price).ToList();

            return(orderBook);
        }
Exemple #2
0
        private static void ScanFiles(object state)
        {
            string[]       dirs       = (string[])((object[])state)[0];
            ArrayList      old_files  = (ArrayList)((object[])state)[1];
            IOrderBook     order_book = (OrderBook)((object[])state)[2];
            AutoResetEvent quit       = (AutoResetEvent)((object[])state)[3];

            // dirs[0] = input file directory
            if (!Directory.Exists(dirs[0]))
            {
                PrintErrorMessage("Can not locate input file directory: '" + dirs[0] + "'");
                quit.Set();
                return;
            }

            // dirs[1] = output file directory
            if (!Directory.Exists(dirs[1]))
            {
                PrintErrorMessage("Can not locate output file directory: '" + dirs[1] + "'");
                quit.Set();
                return;
            }

            // synchronise to make ArrayList thread-safe
            ArrayList sync_files = ArrayList.Synchronized(old_files);

            string[] input_files = Directory.GetFiles(dirs[0], "*.json", SearchOption.TopDirectoryOnly);

            List <string> new_files = new List <string>();

            // lock the sync_files list while we read newly found files
            lock (sync_files.SyncRoot)
            {
                foreach (string in_file in input_files)
                {
                    if (sync_files.Contains(in_file))
                    {
                        continue;
                    }

                    new_files.Add(in_file);
                    sync_files.Add(in_file);
                }
            }

            foreach (string file in new_files)
            {
                string file_text = File.ReadAllText(file);
                // ignore the first name that appears in file 'cos this is not necessarily unique for: orders, order_items, or order_shipments
                file_text = file_text.Substring(file_text.IndexOf('['));
                JArray jarray = JArray.Parse(file_text.Substring(0, file_text.LastIndexOf(']') + 1));

                foreach (var item in jarray.Children())
                {
                    order_book.ParseNewInfo(item);
                }
            }
        }
Exemple #3
0
        public static IOrderBook Invert(this IOrderBook orderBook)
        {
            foreach (var price in orderBook.Prices)
            {
                price.Volume = price.Volume * price.Price * -1;
            }

            return(orderBook);
        }
        private void SetOrderBookClear(IOrderBook orderBook)
        {
            askListView.BeginInvoke(new Action(() =>
            {
                askListView.BeginUpdate();
                try
                {
                    var selectedPrice = GetSelectedPrice(askListView);
                    askListView.Items.Clear();

                    if (orderBook == null)
                    {
                        return;
                    }

                    lock (_askLock)
                    {
                        FillListView(orderBook.Asks, askListView);
                        HighlightLargePositions(orderBook.LargeAsksIndexes, askListView, Color.IndianRed);
                        FocusLastItem(askListView);
                        SelectPriceIfPossible(selectedPrice, askListView);
                    }
                }
                finally
                {
                    askListView.EndUpdate();
                }
            }));

            bidListView.BeginInvoke(new Action(() =>
            {
                bidListView.BeginUpdate();
                try
                {
                    var selectedPrice = GetSelectedPrice(bidListView);
                    bidListView.Items.Clear();

                    if (orderBook == null)
                    {
                        return;
                    }

                    lock (_bidLock)
                    {
                        FillListView(orderBook.Bids, bidListView);
                        HighlightLargePositions(orderBook.LargeBidsIndexes, bidListView, Color.ForestGreen);
                        FocusFirstItem(bidListView);
                        SelectPriceIfPossible(selectedPrice, bidListView);
                    }
                }
                finally
                {
                    bidListView.EndUpdate();
                }
            }));
        }
        /// <summary>
        /// Insert or replace orders from a given OrderBook.
        /// It is assumed that all the orders in the incoming OrderBook are from the same Exchange (identified by ExchangeID).
        /// Any order from the same Exchange will be removed before the incoming OrderBook is inserted
        /// </summary>
        /// <param name="orderBook">orderbook containing the orders which should be inserted in the aggregated orderbook.</param>
        /// <returns>AggregatedOrderBook where order from the same Exchange as orderBook as replaced with the ones in orderBook</returns>
        public AggregatedOrderBook InsertBook(IOrderBook orderBook)
        {
            int exchangeID             = orderBook.ExchangeID;
            var modifiedAggregatedBids = Bids;
            var modifiedAggregatedAsks = Asks;

            var correspondingBids = Bids.Where(a => a.ExchangeID == exchangeID);

            modifiedAggregatedBids = Bids.Except(correspondingBids).Union(orderBook.Bids);

            var correspondingAsks = Asks.Where(a => a.ExchangeID == exchangeID);

            modifiedAggregatedAsks = Asks.Except(correspondingAsks).Union(orderBook.Asks);

            return(new AggregatedOrderBook(modifiedAggregatedBids, modifiedAggregatedAsks));
        }
Exemple #6
0
        public double FindBestPrice(CurrencyPair currencyPair, OrderType orderType, double?amountQuote = null)
        {
            IOrderBook orders = GetOpenOrders(currencyPair, 20);

            double pricePerCoin = (from order in orders.BuyOrders
                                   where order.AmountBase > MIN_ORDER_AMOUNT && order.AmountBase != amountQuote
                                   select order.PricePerCoin).FirstOrDefault();

            if (orderType == OrderType.Buy)
            {
                // Ne pas prendre les ordres de montants ridicules
                // Ajouter un % à la meilleure offre pour être au top
                return(Utilities.TruncateDouble(pricePerCoin + (pricePerCoin * BEST_PRICE_MULTIPLICATOR)));
            }
            else // OrderType.Sell
            {
                // Ne pas prendre les ordres de montants ridicules
                // Ajouter un % à la meilleure offre pour être au top
                return(Utilities.TruncateDouble(pricePerCoin - (pricePerCoin * BEST_PRICE_MULTIPLICATOR)));
            }
        }
Exemple #7
0
            // Order Data
            public static Trading.OrderLive[] PullOrderHistory(CurrencyPair pair)
            {
                IOrderBook orderBook = ClientManager.client.Markets.GetOpenOrdersAsync(pair).Result;

                if (orderBook == null)
                {
                    return(null);
                }

                List <Trading.OrderLive> list = new List <Trading.OrderLive>();

                if (orderBook.BuyOrders != null)
                {
                    for (int i = 0; i < orderBook.BuyOrders.Count; i++)
                    {
                        Trading.OrderLive order = new Trading.OrderLive(
                            Trading.OrderLiveType.Modify,
                            Trading.MarketAction.Buy,
                            orderBook.BuyOrders[i].AmountBase,
                            orderBook.BuyOrders[i].PricePerCoin);
                        list.Add(order);
                    }
                }
                if (orderBook.SellOrders != null)
                {
                    for (int i = 0; i < orderBook.SellOrders.Count; i++)
                    {
                        Trading.OrderLive order = new Trading.OrderLive(
                            Trading.OrderLiveType.Modify,
                            Trading.MarketAction.Sell,
                            orderBook.SellOrders[i].AmountBase,
                            orderBook.SellOrders[i].PricePerCoin);
                        list.Add(order);
                    }
                }

                return(list.ToArray());
            }
Exemple #8
0
        public static void Validate(IOrderBook book)
        {
            if (book.Asks.Count + book.Bids.Count == 0)
            {
                return;
            }

            if (book.Asks.Count > book.Depth)
            {
                throw new ApplicationException("Asks count exceeds book depth in " + book);
            }

            for (var i = 1; i < book.Asks.Count; i++)
            {
                if (book.Asks[i - 1].Price == book.Asks[i].Price)
                {
                    //throw new ApplicationException("Asks are duplicated in " + book.ToString());
                    if (book.Asks[i - 1].Price > book.Asks[i].Price)
                    {
                        throw new ApplicationException("Asks are unordered in " + book);
                    }
                }
            }

            if (book.Bids.Count > book.Depth)
            {
                throw new ApplicationException("Bids count exceeds book depth in " + book);
            }

            for (var i = 1; i < book.Bids.Count; i++)
            {
                if (book.Bids[i - 1].Price == book.Bids[i].Price)
                {
                    //throw new ApplicationException("Bids are duplicated in " + book.ToString());
                    if (book.Bids[i - 1].Price < book.Bids[i].Price)
                    {
                        throw new ApplicationException("Bids are unordered in " + book);
                    }
                }
            }
        }
Exemple #9
0
 public OrdersController(IOrderValidator orderValidator, IOrderRouter orderRouter, IOrderBook orderBook)
 {
     _orderValidator = orderValidator;
     _orderRouter    = orderRouter;
     _orderBook      = orderBook;
 }
 public void SetOrderBook(IOrderBook orderBook)
 {
     SetOrderBookClear(orderBook);
     //SetOrderBookByItem(orderBook);
 }
        private void SetOrderBookByItem(IOrderBook orderBook)
        {
            askListView.BeginInvoke(new Action(() =>
            {
                askListView.BeginUpdate();
                if (orderBook == null)
                {
                    askListView.Items.Clear();
                }
                else
                {
                    var asks = orderBook.Asks.ToList();
                    var diff = asks.Count - askListView.Items.Count;
                    if (diff < 0)
                    {
                        for (var i = 0; i < Math.Abs(diff); i++)
                        {
                            askListView.Items.RemoveAt(0);
                        }
                    }
                    else if (diff > 0)
                    {
                        /*for (var i = 0; i < Math.Abs(diff); i++)
                         * {
                         *  var item = new ListViewItem {Text = ask.Price.ToString(CultureInfo.CurrentCulture)};
                         *  item.SubItems.Add(ask.Quantity.ToString(CultureInfo.CurrentCulture));
                         *  askListView.Items.Add(item);
                         * }*/
                    }
                    else
                    {
                        for (var i = 0; i < asks.Count; i++)
                        {
                            var ask = asks[asks.Count - i - 1];
                            askListView.Items[i].Text             = ask.Price.ToString(CultureInfo.CurrentCulture);
                            askListView.Items[i].SubItems[0].Text = ask.Quantity.ToString(CultureInfo.CurrentCulture);
                        }
                    }

                    if (askListView.Items.Count > 0)
                    {
                        askListView.FocusedItem = askListView.Items[askListView.Items.Count - 1];
                        askListView.Items[askListView.Items.Count - 1].EnsureVisible();
                    }
                }

                askListView.EndUpdate();
            }));

            bidListView.BeginInvoke(new Action(() =>
            {
                bidListView.BeginUpdate();
                if (orderBook == null)
                {
                    bidListView.Items.Clear();
                }
                else
                {
                    var bids = orderBook.Bids.ToList();
                    var diff = bids.Count - bidListView.Items.Count;
                    if (diff < 0)
                    {
                        for (var i = 0; i < Math.Abs(diff); i++)
                        {
                            bidListView.Items.RemoveAt(diff);
                        }
                    }
                    else if (diff > 0)
                    {
                        for (var i = 0; i < bidListView.Items.Count; i++)
                        {
                            var bid = bids[i];
                            bidListView.Items[i].Text             = bid.Price.ToString(CultureInfo.CurrentCulture);
                            bidListView.Items[i].SubItems[0].Text = bid.Quantity.ToString(CultureInfo.CurrentCulture);
                        }
                        var count = bidListView.Items.Count;
                        for (var i = 0; i < Math.Abs(diff); i++)
                        {
                            var bid  = bids[i + count];
                            var item = new ListViewItem {
                                Text = bid.Price.ToString(CultureInfo.CurrentCulture)
                            };
                            item.SubItems.Add(bid.Quantity.ToString(CultureInfo.CurrentCulture));
                            bidListView.Items.Add(item);
                        }
                    }
                    else
                    {
                        for (var i = 0; i < bids.Count; i++)
                        {
                            var bid     = bids[i];
                            var newText = bid.Price.ToString(CultureInfo.CurrentCulture);
                            if (newText != bidListView.Items[i].Text)
                            {
                                bidListView.Items[i].Text = bid.Price.ToString(CultureInfo.CurrentCulture);
                            }

                            newText = bid.Quantity.ToString(CultureInfo.CurrentCulture);
                            if (newText != bidListView.Items[i].SubItems[0].Text)
                            {
                                bidListView.Items[i].SubItems[0].Text = bid.Quantity.ToString(CultureInfo.CurrentCulture);
                            }
                        }
                    }

                    if (bidListView.Items.Count > 0)
                    {
                        bidListView.FocusedItem = bidListView.Items[0];
                        bidListView.Items[0].EnsureVisible();
                    }
                }

                bidListView.EndUpdate();
            }));
        }
 private void OnChanged(IOrderBook orderBook)
 {
     Changed?.Invoke(this, orderBook);
 }
        public void SetOrderBook(IOrderBook orderBook)
        {
            _orderBook = orderBook;

            ClearCache();
        }
 private void Model_OrderBookChanged(IOrderBook orderBook)
 {
     View.SetOrderBook(orderBook);
 }
Exemple #15
0
 public static double GetPrice(this IOrderBook src)
 {
     return(src.IsBuy
         ? src.Prices.Max(item => item.Price)
         : src.Prices.Min(item => item.Price));
 }
Exemple #16
0
 public void SetOrderBook(IOrderBook orderBook)
 {
     OrderBookPartView.SetOrderBook(orderBook);
 }
Exemple #17
0
 private void OrderBookUpdater_Changed(object sender, IOrderBook orderBook)
 {
     OrderBook = orderBook;
     OrderBookAdapter.SetOrderBook(OrderBook);
     OnOrderBookChanged();
 }