Exemple #1
0
        private async Task UpdateExecution()
        {
            if (this.executionCycle != null)
            {
                this.updatingExecution = true;

                int selectedIndex = this.CurrencyCyclesListView.SelectedIndex;
                CurrencyCycleListViewItem item = executionCycle.Value;

                ArbitragePath arbitragePath = item.ArbitragePath;

                Ticker[,] tickers = this.tickers;

                decimal sourceQuantity = 0.1m;

                List <ExecutionListViewItem> items = new List <ExecutionListViewItem>();
                for (int i = 1; i < arbitragePath.Currencies.Count; i++)
                {
                    string sourceCurrency           = arbitragePath.Currencies[i - 1];
                    string destinationCurrency      = arbitragePath.Currencies[i];
                    int    sourceCurrencyIndex      = item.Exchange.Currencies.IndexOf(sourceCurrency);
                    int    destinationCurrencyIndex = item.Exchange.Currencies.IndexOf(destinationCurrency);

                    TradeType type;
                    (string, string)tradingPair;
                    switch (item.Exchange.TradingPairs[item.Exchange.Currencies.IndexOf(destinationCurrency), item.Exchange.Currencies.IndexOf(sourceCurrency)])
                    {
                    case TradingPairType.Buy:
                        type        = TradeType.Buy;
                        tradingPair = (destinationCurrency, sourceCurrency);
                        break;

                    case TradingPairType.Sell:
                        type        = TradeType.Sell;
                        tradingPair = (sourceCurrency, destinationCurrency);
                        break;

                    case TradingPairType.Invalid:
                    default:
                        throw new InvalidOperationException();
                    }

                    Ticker    ticker    = tickers[item.Exchange.Currencies.IndexOf(tradingPair.Item1), item.Exchange.Currencies.IndexOf(tradingPair.Item2)];
                    OrderBook orderBook = await item.Exchange.GetOrderBook(tradingPair);

                    decimal        destinationQuantity;
                    OrderBookEntry buyOrder  = orderBook.Bids[0];
                    OrderBookEntry sellOrder = orderBook.Asks[0];
                    if (type == TradeType.Buy)
                    {
                        // destinationQuantity = sourceQuantity / ticker.LowestAskPrice * (1m - item.Exchange.FeePercentage * 0.01m);
                        // sellOrder.Price = ticker.LowestAskPrice;
                        (destinationQuantity, sellOrder) = orderBook.ComputeBuyQuantity(sourceQuantity).Value;
                        ticker.LowestAskPrice            = sellOrder.Price;
                    }
                    else
                    {
                        // destinationQuantity = sourceQuantity * ticker.HighestBidPrice * (1m - item.Exchange.FeePercentage * 0.01m);
                        // buyOrder.Price = ticker.HighestBidPrice;
                        (destinationQuantity, buyOrder) = orderBook.ComputeSellCost(sourceQuantity).Value;
                        ticker.HighestBidPrice          = buyOrder.Price;
                    }

                    items.Add(new ExecutionListViewItem
                    {
                        Exchange            = item.Exchange,
                        TradingPair         = tradingPair,
                        Type                = type,
                        Price               = type == TradeType.Buy ? ticker.LowestAskPrice : ticker.HighestBidPrice,
                        Ticker              = ticker,
                        OrderBook           = orderBook,
                        BuyOrder            = buyOrder,
                        SellOrder           = sellOrder,
                        SourceQuantity      = sourceQuantity,
                        DestinationQuantity = destinationQuantity
                    });

                    sourceQuantity = destinationQuantity;
                }

                if (this.CurrencyCyclesListView.SelectedIndex == selectedIndex)
                {
                    await this.Dispatcher.InvokeAsync(() =>
                    {
                        this.ExecutionListView.Items.Clear();
                        items.ForEach(x => this.ExecutionListView.Items.Add(x));
                    });
                }

                this.updatingExecution = false;
            }
            else
            {
                this.ExecutionListView.Items.Clear();
            }
        }