public void AddRangeReversed(ResizeableArray <T> data) { for (int i = data.Count - 1; i >= 0; i--) { Add(data[i]); } }
public static ResizeableArray <TimeBaseValue> CalculateByTime <T>(ResizeableArray <T> array, string timeField, TimeSpan interval) { if (array.Count == 0) { return(new ResizeableArray <TimeBaseValue>()); } DateTime min = GetMinTime(array, timeField); DateTime max = GetMaxTime(array, timeField); int count = (int)((max - min).TotalMilliseconds / interval.TotalMilliseconds + 0.5); ResizeableArray <TimeBaseValue> list = new ResizeableArray <TimeBaseValue>(count + 1); PropertyInfo pi = array[0].GetType().GetProperty(timeField, BindingFlags.Instance | BindingFlags.Public); double k = 1.0 / interval.TotalMilliseconds; for (int i = 0; i < count + 1; i++) { list[i] = new TimeBaseValue() { Time = min.AddMilliseconds((int)i * interval.TotalMilliseconds) }; } for (int i = 0; i < array.Count; i++) { DateTime time = (DateTime)pi.GetValue(array[i]); int index = (int)((time - min).TotalMilliseconds * k + 0.5); list[index].Value++; } return(list); }
public void AddRange(ResizeableArray <T> data) { for (int i = 0; i < data.Count; i++) { Add(data[i]); } }
public static ArgumentValue[] Calculate(ResizeableArray <TimeBaseValue> data, double clasterizationWidth) { double minX = GetMin(data); double maxX = GetMax(data); int count = (int)((maxX - minX) / clasterizationWidth) + 1; return(Calculate(data, count)); }
private static DateTime GetMinTime <T>(ResizeableArray <T> dataSource, string field) { if (dataSource.Count == 0) { return(DateTime.MinValue); } PropertyInfo pi = dataSource[0].GetType().GetProperty(field, BindingFlags.Instance | BindingFlags.Public); return(dataSource.Min(i => (DateTime)pi.GetValue(i))); }
public static ResizeableArray <T> FromList(List <T> list) { ResizeableArray <T> res = new ResizeableArray <T>(list.Count); foreach (T item in list) { res.Add(item); } return(res); }
public static ArgumentValue[] Calculate(ResizeableArray <object> dataSource, string valueField, double clasterizationWidth) { double minX = GetMin(dataSource, valueField); double maxX = GetMax(dataSource, valueField); int count = 100; if (clasterizationWidth != 0) { count = (int)((maxX - minX) / clasterizationWidth) + 1; } return(Calculate((ResizeableArray <object>)dataSource, valueField, count)); }
public static ResizeableArray <ArgumentValue> Calculate(ResizeableArray <object> data, string valueField, int count) { if (data.Count == 0) { return(null); } double minX = GetMin(data, valueField); double maxX = GetMax(data, valueField); double step = (maxX - minX) / count; minX = ((int)(minX / step)) * step; maxX = ((int)(maxX / step + 0.5)) * step; count = (int)((maxX - minX) / step); ResizeableArray <ArgumentValue> res = new ResizeableArray <ArgumentValue>(count + 1); for (int i = 0; i < count + 1; i++) { res[i] = new ArgumentValue() { X = minX + i * step }; } PropertyInfo pi = data[0].GetType().GetProperty(valueField, BindingFlags.Instance | BindingFlags.Public); foreach (var item in data) { double val = (double)pi.GetValue(item); if (double.IsNaN(val)) { continue; } int index = (int)((val - minX) / step + 0.5); if (index >= res.Count) { continue; } res[index].Y += 1.0; } double del = 100.0 / data.Count; for (int i = 0; i < count; i++) { res[i].Y *= del; } return(res); }
private static double GetMax(ResizeableArray <TimeBaseValue> dataSource) { if (dataSource.Count == 0) { return(0); } double min = double.NaN; for (int i = 0; i < dataSource.Count; i++) { if (double.IsNaN(min) || min < dataSource[i].Value) { min = dataSource[i].Value; } } return(min); }
private static double GetMax(ResizeableArray <object> dataSource, string valueField) { if (dataSource.Count == 0) { return(0); } PropertyInfo pi = dataSource[0].GetType().GetProperty(valueField, BindingFlags.Instance | BindingFlags.Public); return(dataSource.Max(i => { double val = (double)pi.GetValue(i); if (double.IsNaN(val)) { return double.MinValue; } return val; })); }
private void CalculateTrendInfo(ResizeableArray <TickerTradeHistoryInfoItem> list, int index) { DateTime startTime = list[index].Time; DateTime endTime = startTime.AddMinutes(TrendPeriodMin); double maxDelta = 0; double edgePrice = 0; for (int i = index + 1; i < list.Count && list[i].Time < endTime; i++) { double delta = Math.Abs(list[i].Close - list[index].Close); if (delta > maxDelta) { edgePrice = list[i].Close; maxDelta = delta; } } list[index].EdgePrice = edgePrice; }
public bool Calculate() { if (Exchange == null) { LogManager.Default.Error("Exchange not specified"); return(false); } if (!Exchange.Connect()) { LogManager.Default.Error("Exchange not connected"); return(false); } foreach (string baseCurr in BaseCurrencies) { List <Ticker> tickers = Exchange.Tickers.Where(t => t.BaseCurrency == baseCurr && (MarketCurrencies == null || MarketCurrencies.Contains(t.MarketCurrency))).ToList(); foreach (Ticker ticker in tickers) { ResizeableArray <CandleStickData> data = Utils.DownloadCandleStickData(ticker, CandleStickPeriodMin); if (data == null) { LogManager.Default.Error("Cannot download candlesticks for " + ticker.Name); continue; } LogManager.Default.Success("Downloaded candlesticks for " + ticker.Name); ticker.CandleStickData.AddRange(data); //foreach(var t in data) // ticker.CandleStickData.Add(t); //data.ForEach(i => ticker.CandleStickData.Add(i)); TickerVolatilityInfo info = new TickerVolatilityInfo() { Ticker = ticker }; info.Calculate(); info.CalculateMath(); info.CalculateDev(); info.CalcHistogramm(HistogrammBarCount); Result.Add(info); Items.Add(info); RaiseTickerAdded(info); } } return(true); }
public static bool DownloadCandlesticksHistory(Exchange e, string[] baseCurrFilter, string[] marketCurrency, int candleStickPeriodMin) { if (!e.Connect()) { return(false); } foreach (string baseCurr in baseCurrFilter) { List <Ticker> tickers = e.Tickers.Where(t => t.BaseCurrency == baseCurr && (marketCurrency == null || marketCurrency.Contains(t.MarketCurrency))).ToList(); foreach (Ticker ticker in tickers) { ResizeableArray <CandleStickData> data = DownloadCandleStickData(ticker, candleStickPeriodMin); if (data == null) { return(false); } } } return(true); }
public static ResizeableArray <ArgumentValue> Calculate(ResizeableArray <TimeBaseValue> data, int count) { double minX = GetMin(data); double maxX = GetMax(data); if (double.IsNaN(minX) || double.IsNaN(maxX)) { return(new ResizeableArray <ArgumentValue>()); } double step = (maxX - minX) / count; minX = ((int)(minX / step)) * step; maxX = ((int)(maxX / step + 0.5)) * step; count = (int)((maxX - minX) / step); ResizeableArray <ArgumentValue> res = new ResizeableArray <ArgumentValue>(count + 1); for (int i = 0; i < count + 1; i++) { res[i] = new ArgumentValue() { X = minX + i * step }; } foreach (var item in data) { if (double.IsNaN(item.Value)) { continue; } int index = (int)((item.Value - minX) / step + 0.5); res[index].Y += 1.0; } double del = 100.0 / data.Count; for (int i = 0; i < count; i++) { res[i].Y *= del; } return(res); }
public TickerTradeHistoryInfo DownloadItem(Ticker ticker) { SimulationStrategyDataProvider provider = new SimulationStrategyDataProvider(); provider.DownloadProgressChanged += DownloadProgressChanged; TickerInputInfo info = new TickerInputInfo() { Exchange = ticker.Exchange.Type, KlineIntervalMin = 5, Ticker = ticker, TickerName = ticker.Name }; ResizeableArray <CandleStickData> kline = provider.DownloadCandleStickData(info); if (kline == null) { LogManager.Default.Error("Cannot download candlesticks for " + ticker.Name); return(null); } LogManager.Default.Success("Downloaded candlesticks for " + ticker.Name); ticker.CandleStickData.AddRange(kline); ResizeableArray <TradeInfoItem> trades = provider.DownloadTradeHistory(info, ticker.CandleStickData.First().Time); if (trades == null) { LogManager.Default.Error("Cannot download trade history for " + ticker.Name); return(null); } LogManager.Default.Success("Downloaded trade history for " + ticker.Name); ticker.TradeHistory.AddRange(trades); TickerTradeHistoryInfo tradeInfo = new TickerTradeHistoryInfo() { Ticker = ticker }; tradeInfo.HistogrammIntervalSec = HistogrammIntervalSec; return(tradeInfo); }
public static ResizeableArray <CandleStickData> DownloadCandleStickData(TickerInputInfo info) { CachedCandleStickData savedData = new CachedCandleStickData() { Exchange = info.Exchange, TickerName = info.TickerName, IntervalMin = info.KlineIntervalMin }; CachedCandleStickData cachedData = CachedCandleStickData.FromFile(CachedCandleStickData.Directory + "\\" + ((ISupportSerialization)savedData).FileName); if (cachedData != null) { return(cachedData.Items); } DateTime start = DateTime.UtcNow; int intervalInSeconds = info.KlineIntervalMin * 60; int candleStickCount = 10000; start = start.AddSeconds(-intervalInSeconds * candleStickCount); ResizeableArray <CandleStickData> res = new ResizeableArray <CandleStickData>(); List <ResizeableArray <CandleStickData> > splitData = new List <ResizeableArray <CandleStickData> >(); int deltaCount = 500; for (int i = 0; i < candleStickCount / deltaCount; i++) { ResizeableArray <CandleStickData> data = info.Ticker.GetCandleStickData(info.KlineIntervalMin, start, intervalInSeconds * deltaCount); if (data == null || data.Count == 0) { continue; } res.AddRange(data); Thread.Sleep(300); start = start.AddSeconds(intervalInSeconds * deltaCount); } cachedData = savedData; cachedData.Items = res; cachedData.Save(); return(res); }
public TickerDownloadData DownloadItem(TickerInputInfo info, bool downloadCandle) { Ticker ticker = info.Ticker; SimulationStrategyDataProvider provider = new SimulationStrategyDataProvider(); provider.DownloadProgressChanged += DownloadProgressChanged; if (downloadCandle) { ResizeableArray <CandleStickData> kline = provider.DownloadCandleStickData(info); if (kline == null) { LogManager.Default.Error("Cannot download candlesticks for " + ticker.Name); return(null); } LogManager.Default.Success("Downloaded candlesticks for " + ticker.Name); ticker.CandleStickData.AddRange(kline); } ResizeableArray <TradeInfoItem> trades = provider.DownloadTradeHistory(info, info.StartDate); if (trades == null) { LogManager.Default.Error("Cannot download trade history for " + ticker.Name); return(null); } LogManager.Default.Success("Downloaded trade history for " + ticker.Name); ticker.TradeHistory.AddRange(trades); TickerDownloadData tradeInfo = new TickerDownloadData() { Ticker = ticker }; tradeInfo.HistogrammIntervalSec = HistogrammIntervalSec; return(tradeInfo); }
public static ResizeableArray <ArgumentValue> Calculate(double[] data, int count) { double minX = data.Min(); double maxX = data.Max(); double step = (maxX - minX) / count; minX = ((int)(minX / step)) * step; maxX = ((int)(maxX / step + 0.5)) * step; count = (int)((maxX - minX) / step); ResizeableArray <ArgumentValue> res = new ResizeableArray <ArgumentValue>(count + 1); for (int i = 0; i < count + 1; i++) { res[i] = new ArgumentValue() { X = minX + i * step }; } for (int i = 0; i < data.Length; i++) { if (double.IsNaN(data[i])) { continue; } int index = (int)((data[i] - minX) / step + 0.5); res[index].Y += 1.0; } double del = 100.0 / data.Length; for (int i = 0; i < count; i++) { res[i].Y *= del; } return(res); }
public StackEnumeratorObject(ResizeableArray <T> owner) { Owner = owner; CurrentIndex = -1; }
ResizeableArray <TickerTradeHistoryInfoItem> CalcItems() { if (TradeHistory.Count == 0) { return(new ResizeableArray <TickerTradeHistoryInfoItem>()); } DateTime min = TradeHistory[0].Time; DateTime max = TradeHistory.Last().Time; if (min > max) { DateTime tmp = min; min = max; max = tmp; } int interval = HistogrammIntervalSec * 1000; int count = (int)((max - min).TotalMilliseconds / interval + 0.5); ResizeableArray <TickerTradeHistoryInfoItem> list = new ResizeableArray <TickerTradeHistoryInfoItem>(count + 1); double k = 1.0 / interval; for (int i = 0; i < count + 1; i++) { list[i] = new TickerTradeHistoryInfoItem() { Time = min.AddMilliseconds(i * interval) }; list[i].Low = double.MaxValue; list[i].OpenTime = list[i].Time.AddMilliseconds(interval); list[i].CloseTime = list[i].Time; } for (int i = 0; i < TradeHistory.Count; i++) { DateTime time = TradeHistory[i].Time; int index = (int)((time - min).TotalMilliseconds * k); TickerTradeHistoryInfoItem item = list[index]; TradeInfoItem trade = TradeHistory[i]; item.TradeCount++; item.TradeVolume += trade.Amount; if (TradeHistory[i].Type == TradeType.Sell) { item.SellCount--; item.SellVolume -= trade.Amount; } else { item.BuyCount++; item.BuyVolume += trade.Amount; } if (item.Low > trade.Rate) { item.Low = trade.Rate; } if (item.High < trade.Rate) { item.High = trade.Rate; } if (item.OpenTime >= trade.Time) { item.OpenTime = trade.Time; item.Open = trade.Rate; } if (item.CloseTime <= trade.Time) { item.CloseTime = trade.Time; item.Close = trade.Rate; } } TickerTradeHistoryInfoItem last = null; for (int i = 0; i < count + 1; i++) { if (list[i].TradeCount == 0) { if (last != null) { list[i].Open = list[i].Close = list[i].High = list[i].Low = last.Close; } else { list[i].Open = list[i].Close = list[i].High = list[i].Low = 0; } } else { last = list[i]; } } for (int i = 0; i < list.Count; i++) { CalculateTrendInfo(list, i); } return(list); }
public static double[] ToDouble(ResizeableArray <TimeBaseValue> list) { return(list.Select((i) => i.Value).ToArray()); }