public CryptoCoinSummary(CryptoExchangeBase exchange, CryptoCoin coin) { Symbol = coin.Symbol; ExchangeName = exchange.Name; HighestBid = coin.HighestBid; LowestAsk = coin.LowestAsk; }
private void StartSubscription(CryptoExchangeBase exchange, TeleSubscription sub) { var subscription = new TelegramSubscription(exchange, sub); subscription.Changed += SendSubscriptionReply; subscription.Changed += async(s, o, n) => await UpdateSubscriptionInDb(s, n).ConfigureAwait(false); exchange.Subscribe(subscription); lock (subscriptionLock) Subscriptions.Add(subscription); }
private async Task SendSubscriptionReply(long id, CryptoExchangeBase ex, CryptoCoin oldValue, CryptoCoin newValue) { var change = newValue - oldValue; var builder = new StringBuilder( ); builder .AppendLine($"{ex.Name,-14} {newValue.Symbol}") .AppendLine($"Current Price: {ex[newValue.Symbol].Average:C}") .AppendLine($"Change: {change.Value.ToCurrency ( )}") .AppendLine($"Change %: {change.Percentage:P}") .AppendLine($"in {change.TimeDiff:dd\\:hh\\:mm\\:ss}"); SaveSubscriptions( ); await bot.SendTextMessageAsync(id, $"```\n{builder}\n```", ParseMode.Markdown); }
public TelegramSubscription( CryptoExchangeBase exchange, TeleSubscription subscription) : base(exchange) { Id = subscription.Id; ChatId = subscription.ChatId; UserName = subscription.UserName; ExchangeId = exchange.Id; Threshold = subscription.Threshold; LastSignificantPrice = subscription.LastSignificantPrice.ToDictionary( x => x.Key, x => new CryptoCoin(x.Value) ); Coins = subscription.Coins.Select(c => c.Id).ToImmutableHashSet( ); }
public TelegramSubscription( CryptoExchangeBase exchange, int id, long chatId, string userName, decimal threshold, IEnumerable <CryptoCoinId> coins, IDictionary <CryptoCoinId, CryptoCoin> lastSignificantPrice = null ) : base(exchange) { Id = id; ChatId = chatId; UserName = userName; ExchangeId = exchange.Id; Threshold = threshold; LastSignificantPrice = new ConcurrentDictionary <CryptoCoinId, CryptoCoin> ( lastSignificantPrice ?? exchange.ExchangeData ); Coins = coins.ToImmutableHashSet( ); }
private async Task HandleBestAllBetweenExchanges(Message message, CryptoExchangeBase param = null) { // var (from, to, first, second, profit, fees) = Ctb.CompareTable.GetBest(); var bestall = Ctb.CompareTable.GetBestAll(); if (bestall.Count == 0) { await SendBlockText(message, "ERROR: Not enough data received.").ConfigureAwait(false); return; } var tables = BuildBestTables(bestall, Exchanges, param); Logger.Info($"Sending best pair data to {message.From.Username}"); foreach (var table in tables) { await SendBlockText(message, table).ConfigureAwait(false); } }
private static IList <string> BuildBestTables(List <BestCoinModel> bestAll, IDictionary <CryptoExchangeId, CryptoExchangeBase> exchanges , CryptoExchangeBase param) { var tables = new List <string>(); foreach (var item in bestAll) { var table = new StringBuilder(); if (item.first == CryptoCoinId.NULL || item.second == CryptoCoinId.NULL) { continue; } if (param != null && param.ToString().Substring(0, param.ToString().IndexOf(" ")).ToLower() != item.from.ToString().ToLower()) { continue; } var fromExchange = exchanges[item.from]; var toExchange = exchanges[item.to]; var minInvestment = item.fees / item.profit; var reply = $"Buy {item.first} From: {fromExchange.Name,-12} @ {fromExchange[item.first].BuyPrice:C}\n" + $"Sell {item.first} To: {toExchange.Name,-12} @ {toExchange[item.first].SellPrice:C}\n" + $"Buy {item.second} From: {toExchange.Name,-12} @ {toExchange[item.second].BuyPrice:C}\n" + $"Sell {item.second} To: {fromExchange.Name,-12} @ {fromExchange[item.second].SellPrice:C}\n" + $"Expected profit: {item.profit:P}\n" + $"Estimated fees: {item.fees:C}\n" + $"Minimum Investment: {minInvestment:C}"; table.AppendLine(reply); table.AppendLine(); tables.Add(table.ToString()); } return(tables); }
private async Task AddSubscription( Message message, CryptoExchangeBase exchange, decimal threshold, IList <CryptoCoinId> coinIds ) { var ids = coinIds.ToList( ); var sub = UnitOfWork.Get(unit => { var subscription = unit.Subscriptions.Add( exchange.Id, message.Chat.Id, message.From.Username, threshold, ids ); foreach ( var coin in exchange.ExchangeData.Values.Where(x => coinIds.Contains(x.Id)) ) { unit.Subscriptions.UpdateCoin( subscription.Id, coin.ToCryptoCoinValue(exchange.Id) ); } return(subscription); } ); StartSubscription(exchange, sub); await SendBlockText( message, $"Subscribed to {exchange.Name} at a threshold of {threshold:P}\n" + $"For coins: {ids.Join ( ", " )}" ).ConfigureAwait(false); }
private static void UpdateExchangeLastUpdateInDb(CryptoExchangeBase exchange, CryptoCoin coin) => UnitOfWork.Do(u => u.Exchanges.UpdateExchange(exchange.Id, lastUpdate: coin.Time.ToUniversalTime( )));
private static void StoreCoinValueInDb(CryptoExchangeBase exchange, CryptoCoin coin) => UnitOfWork.Do(u => u.CoinValues.Add(coin.ToCryptoCoinValue(exchange.Id)));
private void CoinValueOnNextHandler(CryptoExchangeBase e, CryptoCoin c) { Send("CoinValueUpdate", new CryptoCoinSummary(e, c)); }
public void AddExchange(CryptoExchangeBase exchange) => Exchanges[exchange.Id] = exchange;