/// <summary> /// Retrieves recent transactions from the exchange to calculate recent candles /// </summary> /// <param name="candlesDurationInMin"></param> /// <returns></returns> private static IList <OHLC> GetRecentCandlesFromTransactions(DateTime from, int candlesDurationInMin, CurrencyPair pair) { try { var proxy = ExchangeProxyFactory.GetProxy(pair.Exchange.InternalCode); var tradesRes = proxy.GetTransactions(false, pair); if (!tradesRes.Success) { return(new List <OHLC>()); } var trades = tradesRes.Result.Transactions; trades.Reverse(); // Create trade list (required to calculate OHLC) IList <BitcoinCharts.Models.Trade> list = (from trade in trades select new BitcoinCharts.Models.Trade() { Datetime = trade.Date, Price = trade.Price, Quantity = trade.Amount, Symbol = pair.Item2 }).ToList(); // Calculate trades IList <OHLC> candles = CandlesProvider.CalculateOHLCFromTrades(list, candlesDurationInMin, TradeSource.Bitstamp).Where(c => c.Date >= from).ToList(); return(candles); } catch (Exception ex) { Logger.Log(ex); MessageBox.Show(ex.ToString()); } return(new List <OHLC>()); }
/// <summary> /// Returns candles from Bitcoincharts (for Bitcoins only) /// </summary> /// <param name="minValue">The from date</param> /// <param name="candlesDurationInMinutes">The candles' duration in miutes</param> /// <param name="market">The exchange</param> /// <param name="currency">The currency</param> /// <returns></returns> private static IList <OHLC> GetCandlesFromBitcoincharts(DateTime minValue, int candlesDurationInMinutes, string market, string currency) { lock (_obj) { var client = new BitcoinChartsClient(); IList <OHLC> aggregate = new List <OHLC>(); DateTime retrieveFrom = minValue; while (retrieveFrom.AddMinutes(candlesDurationInMinutes) < new DateTimeOffset(DateTime.Now)) { Task <BitcoinCharts.Models.Trades> tradeFetcher; if (retrieveFrom != DateTime.MinValue) { tradeFetcher = client.GetTradesAsync(new DateTimeOffset(retrieveFrom), market, currency); } else { tradeFetcher = client.GetTradesAsync(market, currency); } if (tradeFetcher.Status == TaskStatus.Faulted) { break; } try { tradeFetcher.Wait(); } catch (Exception ex) { Logger.Log(ex); MessageBox.Show(ex.ToString()); } if (tradeFetcher.Status == TaskStatus.Faulted) { throw new Exception("Failed to get values from Bitcoincharts"); } if (tradeFetcher.Result != null && tradeFetcher.Result.Count > 0) { IList <OHLC> candles = CandlesProvider.CalculateOHLCFromTrades(tradeFetcher.Result, candlesDurationInMinutes, TradeSource.BitcoinCharts).Where(x => x.Date >= minValue).ToList();; if (candles.Count() == 0 || candles.Last().Date == retrieveFrom) { break; } else { if (aggregate.Count > 0) { aggregate.RemoveAt(aggregate.Count - 1); } aggregate = aggregate.Union(candles).ToList(); retrieveFrom = candles.Last().Date; } } else { break; } } //while return(aggregate.OrderBy(i => i.Date).ToList()); } //lock }
/// <summary> /// Updates a list of candles with the most recent data from a given exchange (Called when ticker refreshes) /// </summary> /// <param name="from">The from date</param> /// <param name="candlesDurationInMin">The period' duration in minutes</param> /// <param name="existingCandles">A list of existing candles</param> /// <returns>True if the update was successful</returns> public static bool UpdateCandlesWithLiveData(DateTime from, int candlesDurationInMin, IList <OHLC> existingCandles, CurrencyPair pair) { try { var proxy = ExchangeProxyFactory.GetProxy(pair.Exchange.InternalCode); //We first get the transactions for the last minute var transactionsRes = proxy.GetTransactions(true, pair);// if (!transactionsRes.Success) { return(false); } var transactions = transactionsRes.Result.Transactions; transactions.Reverse();//Make sure they are in ASC order transactions = transactions.Where(t => t.Date >= from).ToList(); // Create trade list (required to calculate OHLC) IList <BitcoinCharts.Models.Trade> list = (from trade in transactions select new BitcoinCharts.Models.Trade() { Datetime = trade.Date, Price = trade.Price, Quantity = trade.Amount, Symbol = pair.Item2 }).ToList(); //Check if we need to create another candle var lastCandle = existingCandles.LastOrDefault(); if (list.Count == 0 && lastCandle != null) { if (lastCandle.Date.Subtract(DateTime.MinValue).TotalMinutes / candlesDurationInMin != DateTime.Now.Subtract(DateTime.MinValue).TotalMinutes / candlesDurationInMin) { list.Add(new BitcoinCharts.Models.Trade { Datetime = lastCandle.Date.AddMilliseconds(candlesDurationInMin), Price = lastCandle.Close, Quantity = 0, Symbol = pair.Item2 }); } } // Calculate trades IList <OHLC> recentCandles = CandlesProvider.CalculateOHLCFromTrades(list, candlesDurationInMin, TradeSource.Bitstamp); foreach (OHLC newCandle in recentCandles) { var existingCandle = existingCandles.FirstOrDefault(e => e.Date == newCandle.Date); if (existingCandle != null) { existingCandle.High = newCandle.High > existingCandle.High ? newCandle.High : existingCandle.High; existingCandle.Low = newCandle.Low < existingCandle.Low ? newCandle.Low : existingCandle.Low; existingCandle.Close = newCandle.Close; existingCandle.TradeSource = TradeSource.Bitstamp; //recentCandles = recentCandles.Skip(1).ToList(); } else { existingCandles.Add(newCandle); } } return(recentCandles.Count() > 0); } catch (Exception ex) { Logger.Log(ex); MessageBox.Show(ex.ToString()); } return(false); }