private async Task UpdateCurrencyCycles() { IExchange exchange = this.exchange; if (!exchange.Connected) { await exchange.ConnectReadOnly(); } this.tickers = await exchange.GetTicker(); Matrix <ArbitragePath> arbitrage1 = Matrix <Ticker> .FromArray(NullRing <Ticker> .Instance, this.tickers) .Map(new ArbitragePathSemiring(false), (y, x, value) => { decimal multiplier = 0; if (value.LowestAskPrice != 0) { multiplier = 1m / value.LowestAskPrice * (1m - exchange.FeePercentage * 0.01m); } if (x == y) { return(new ArbitragePath(multiplier, new List <string> { exchange.Currencies[(int)x] })); } else { return(new ArbitragePath(multiplier, new List <string> { exchange.Currencies[(int)x], exchange.Currencies[(int)y] })); } }); Matrix <ArbitragePath> arbitrage = arbitrage1; Matrix <ArbitragePath> arbitrageN = arbitrage1; for (int tradeCount = 1; tradeCount < this.maximumTradeCount; tradeCount++) { arbitrageN = arbitrage1 * arbitrageN; arbitrage = arbitrage + arbitrageN; } await this.Dispatcher.InvokeAsync(() => { if (this.CurrencyCyclesListView.Items.Count != exchange.Currencies.Count) { this.CurrencyCyclesListView.Items.Clear(); for (int i = 0; i < exchange.Currencies.Count; i++) { this.CurrencyCyclesListView.Items.Add(new CurrencyCycleListViewItem { Name = exchange.Currencies[i], Exchange = exchange, ArbitragePath = new ArbitragePath(0, new List <string>()) }); } } this.suppressExecutionUpdate = true; int selectedIndex = this.CurrencyCyclesListView.SelectedIndex; for (int i = 0; i < exchange.Currencies.Count; i++) { ArbitragePath arbitragePath = arbitrage[(uint)i, (uint)i]; this.CurrencyCyclesListView.Items[i] = new CurrencyCycleListViewItem { Name = exchange.Currencies[i], Exchange = exchange, ArbitragePath = arbitragePath }; } this.CurrencyCyclesListView.SelectedIndex = selectedIndex; this.suppressExecutionUpdate = false; }); this.executionCycle = (CurrencyCycleListViewItem?)this.CurrencyCyclesListView.SelectedItem; await this.UpdateExecution(); }
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(); } }