public void ShouldThrowExceptionOnElementAtWhenIndexOutOfBounds() { var array = new ResizeableArray <int>(); Assert.Throws <ValidationException>(() => array.ElementAt(10)); Assert.Throws <ValidationException>(() => array.ElementAt(0)); array.Add(10); array.Add(11); Assert.Throws <ValidationException>(() => array.ElementAt(2)); }
public void ShouldReturnFlaseOnIsEmptyWhenLenghtIsGreaterThan0() { var array = new ResizeableArray <int>(); array.Add(10); array.Add(9); Assert.AreEqual(2, array.Length); Assert.False(array.IsEmpty); }
public void ShouldThrowExceptionOnSetWhenIndexOutOfBounds() { var array = new ResizeableArray <int>(); array.Add(1); array.Add(2); array.Add(3); Assert.AreEqual(3, array.Length); Assert.Throws <ValidationException>(() => array.Set(6, 15)); }
public void ShouldReturnCorrectElementOnElementAtWhenArrayIsNotEmpty() { var array = new ResizeableArray <int>(); array.Add(10); array.Add(0); Assert.AreEqual(10, array.ElementAt(0)); Assert.AreEqual(0, array.ElementAt(1)); Assert.AreEqual(2, array.Length); }
public void ShouldAddElementAtStartOfArrayOnPreprend() { var array = new ResizeableArray <int>(); array.Add(10); array.Add(15); array.Add(20); array.Prepend(25); Assert.AreEqual(25, array.ElementAt(0)); Assert.AreEqual(10, array.ElementAt(1)); Assert.AreEqual(15, array.ElementAt(2)); Assert.AreEqual(20, array.ElementAt(3)); }
public void ShouldAddElementAtCorrectIndexOnInsert() { var array = new ResizeableArray <int>(); array.Add(10); array.Add(15); array.Add(20); array.Insert(25, 1); Assert.AreEqual(10, array.ElementAt(0)); Assert.AreEqual(25, array.ElementAt(1)); Assert.AreEqual(15, array.ElementAt(2)); Assert.AreEqual(20, array.ElementAt(3)); }
public void ShouldAddElementAtTheEndOfArrayOnAdd() { var array = new ResizeableArray <int>(); array.Add(12); array.Add(16); array.Add(122); Assert.False(array.IsEmpty); Assert.AreEqual(3, array.Length); Assert.AreEqual(12, array.ElementAt(0)); Assert.AreEqual(16, array.ElementAt(1)); Assert.AreEqual(122, array.ElementAt(2)); }
public void ShouldReturnFirstIndexOnFindWhenElementExists() { var array = new ResizeableArray <int>(); array.Add(12); array.Add(16); array.Add(14); array.Add(16); array.Add(25); var index = array.Find(16); Assert.AreEqual(1, index); }
public void ShouldReturnMinusOneOnFindWhenElementDoesNotExist() { var array = new ResizeableArray <int>(); array.Add(12); array.Add(16); array.Add(14); array.Add(16); array.Add(25); var index = array.Find(200); Assert.AreEqual(-1, index); }
public void ShouldSetCorrectAtValueAtGivenIndexOnSet() { var array = new ResizeableArray <int>(); array.Add(1); array.Add(2); array.Add(3); array.Set(1, 15); Assert.AreEqual(3, array.Length); Assert.AreEqual(1, array.ElementAt(0)); Assert.AreEqual(15, array.ElementAt(1)); Assert.AreEqual(3, array.ElementAt(2)); }
public void ShouldRemoveElementAtCorrectIndexOnRemoveAt() { var array = new ResizeableArray <int>(); array.Add(1); array.Add(2); array.Add(3); Assert.AreEqual(3, array.Length); array.RemoveAt(1); Assert.AreEqual(2, array.Length); Assert.AreEqual(1, array.ElementAt(0)); Assert.AreEqual(3, array.ElementAt(1)); }
public void ShouldRemoveLastElementOnPop() { var array = new ResizeableArray <int>(); array.Add(1); array.Add(2); array.Add(3); Assert.AreEqual(3, array.Length); var element = array.Pop(); Assert.AreEqual(3, element); Assert.AreEqual(2, array.Length); Assert.AreEqual(1, array.ElementAt(0)); Assert.AreEqual(2, array.ElementAt(1)); }
public void ShouldDecreaseCapacityOnPop() { var array = new ResizeableArray <int>(); Assert.AreEqual(16, array.Capacity); for (int i = 0; i < 16; i++) { array.Add(i); } Assert.AreEqual(32, array.Capacity); Assert.AreEqual(16, array.Length); for (int i = 0; i < 8; i++) { array.Pop(); } Assert.AreEqual(16, array.Capacity); Assert.AreEqual(8, array.Length); for (int i = 0; i < 6; i++) { array.Pop(); } Assert.AreEqual(2, array.Length); Assert.AreEqual(16, array.Capacity); }
public void ShouldIncreseCapacityOnInsertWhenMaxCapacityIsReached() { var array = new ResizeableArray <int>(); Assert.AreEqual(16, array.Capacity); for (int i = 0; i < 15; i++) { array.Add(i); } Assert.AreEqual(16, array.Capacity); array.Insert(200, 4); Assert.AreEqual(32, array.Capacity); for (int i = 0; i < 16; i++) { if (i < 4) { Assert.AreEqual(i, array.ElementAt(i)); } if (i == 4) { Assert.AreEqual(200, array.ElementAt(i)); } if (i > 4) { Assert.AreEqual(i - 1, array.ElementAt(i)); } } }
public void ShouldDecreaseCapacityOnRemove() { var array = new ResizeableArray <int>(); Assert.AreEqual(16, array.Capacity); for (int i = 0; i < 16; i++) { array.Add(i); } Assert.AreEqual(32, array.Capacity); Assert.AreEqual(16, array.Length); for (int i = 0; i < 8; i++) { var elem = array.ElementAt(0); array.Remove(elem); } Assert.AreEqual(16, array.Capacity); Assert.AreEqual(8, array.Length); for (int i = 0; i < 6; i++) { var elem = array.ElementAt(0); array.Remove(elem); } Assert.AreEqual(2, array.Length); Assert.AreEqual(16, array.Capacity); }
void UpdateCandleStickData(ResizeableArray <CandleStickData> data) { for (int i = 0; i < Ticker.CandleStickData.Count; i++) { data.Add(Ticker.CandleStickData[i]); } Ticker.CandleStickData = data; }
public ResizeableArray <TradeInfoItem> GetTradeHistory() { ResizeableArray <TradeInfoItem> res = new ResizeableArray <TradeInfoItem>(TradeHistory.Count); foreach (var item in TradeHistory) { res.Add(item); } return(res); }
public override ResizeableArray <TradeInfoItem> GetTrades(Ticker ticker, DateTime starTime) { string address = string.Format("https://api.binance.com/api/v1/depth?symbol={0}&limit={1}", Uri.EscapeDataString(ticker.CurrencyPair), 1000); string text = ((Ticker)ticker).DownloadString(address); if (string.IsNullOrEmpty(text)) { return(null); } JArray trades = JsonConvert.DeserializeObject <JArray>(text); if (trades.Count == 0) { return(null); } ResizeableArray <TradeInfoItem> list = new ResizeableArray <TradeInfoItem>(1000); int index = 0; for (int i = 0; i < trades.Count; i++) { JObject obj = (JObject)trades[i]; DateTime time = new DateTime(obj.Value <Int64>("time")); int tradeId = obj.Value <int>("id"); if (time < starTime) { break; } TradeInfoItem item = new TradeInfoItem(null, ticker); bool isBuy = obj.Value <string>("type").Length == 3; item.AmountString = obj.Value <string>("qty"); item.Time = time; item.Type = isBuy ? TradeType.Buy : TradeType.Sell; item.RateString = obj.Value <string>("price"); item.Id = tradeId; double price = item.Rate; double amount = item.Amount; item.Total = price * amount; list.Add(item); index++; } if (ticker.HasTradeHistorySubscribers) { ticker.RaiseTradeHistoryChanged(new TradeHistoryChangedEventArgs() { NewItems = list }); } return(list); }
public void Add(LogType type, object owner, string name, string message, string description) { Messages.Add(new LogMessage() { Type = type, Owner = owner, Name = name, Text = message, Description = description, Time = DateTime.UtcNow }); Save(); RefreshVisual(); }
public static ResizeableArray <TickerCollection> GetItems(List <Exchange> exchanges) { ResizeableArray <TickerCollection> arbitrageList = new ResizeableArray <TickerCollection>(); List <List <Ticker> > markets = GetMarketsList(exchanges); if (markets.Count == 0) { return(arbitrageList); } //string[] marketItems = new string[] { "ETC", "LTC", "ADA", "XRP", "EOS", "NES", "ETH", "BTC", "STR", "XMR", "DASH", "ZEC", "NXT" }; var mFirst = markets[0]; for (int mi = 0; mi < mFirst.Count; mi++) { Ticker ticker = mFirst[mi]; //if(!marketItems.Contains(ticker.MarketCurrency)) // continue; //if(ticker.BaseCurrency != "BTC") // continue; TickerCollection info = new TickerCollection(); info.BaseCurrency = ticker.BaseCurrency; info.MarketCurrency = ticker.MarketCurrency; info.Add(ticker); for (int i = 1; i < markets.Count; i++) { Ticker tt = markets[i].FirstOrDefault((t) => t.BaseCurrency == ticker.BaseCurrency && t.MarketCurrency == ticker.MarketCurrency); if (tt == null) { continue; } info.Add(tt); } if (info.Count < 2) { continue; } info.UsdTicker = mFirst.FirstOrDefault((t) => t.MarketCurrency == info.BaseCurrency && t.BaseCurrency == "USDT"); arbitrageList.Add(info); } for (int ai = 0; ai < arbitrageList.Count; ai++) { TickerCollection coll = arbitrageList[ai]; for (int i = 0; i < coll.Count; i++) { coll.Tickers[i].UpdateMode = TickerUpdateMode.Arbitrage; } } return(arbitrageList); }
public void ShouldRemoveAllOccurencesOnRemove() { var array = new ResizeableArray <int>(); array.Add(12); array.Add(1); array.Add(2); array.Add(13); array.Add(1); Assert.AreEqual(5, array.Length); Assert.AreEqual(12, array.ElementAt(0)); Assert.AreEqual(1, array.ElementAt(1)); Assert.AreEqual(2, array.ElementAt(2)); Assert.AreEqual(13, array.ElementAt(3)); Assert.AreEqual(1, array.ElementAt(4)); array.Remove(1); Assert.AreEqual(3, array.Length); Assert.AreEqual(12, array.ElementAt(0)); Assert.AreEqual(2, array.ElementAt(1)); Assert.AreEqual(13, array.ElementAt(2)); }
public override bool UpdateTrades(Ticker ticker) { string address = string.Format("https://api.binance.com/api/v1/trades?symbol={0}&limit={1}", Uri.EscapeDataString(ticker.CurrencyPair), 1000); byte[] data = ((Ticker)ticker).DownloadBytes(address); if (data == null || data.Length == 0) { return(false); } ticker.TradeHistory.Clear(); ticker.TradeStatistic.Clear(); int index = 0, parseIndex = 0; List <string[]> items = JSonHelper.Default.DeserializeArrayOfObjects(data, ref parseIndex, TradeItemString); ResizeableArray <TradeInfoItem> newItems = new ResizeableArray <TradeInfoItem>(items.Count); for (int i = items.Count - 1; i >= 0; i--) { string[] item = items[i]; DateTime time = FromUnixTime(FastValueConverter.ConvertPositiveLong(item[3])); int tradeId = FastValueConverter.ConvertPositiveInteger(item[0]); TradeInfoItem t = new TradeInfoItem(null, ticker); bool isBuy = item[4][0] != 't'; t.AmountString = item[2]; t.Time = time; t.Type = isBuy ? TradeType.Buy : TradeType.Sell; t.RateString = item[1]; t.Id = tradeId; double price = t.Rate; double amount = t.Amount; t.Total = price * amount; ticker.TradeHistory.Add(t); newItems.Add(t); index++; } if (ticker.HasTradeHistorySubscribers) { ticker.RaiseTradeHistoryChanged(new TradeHistoryChangedEventArgs() { NewItems = newItems }); } return(true); }
public void ShouldIncreseCapacityOnAddWhenMaxCapacityIsReached() { var array = new ResizeableArray <int>(); Assert.AreEqual(16, array.Capacity); for (int i = 0; i < 20; i++) { array.Add(i); } for (int i = 0; i < 20; i++) { Assert.AreEqual(i, array.ElementAt(i)); } Assert.AreEqual(32, array.Capacity); }
protected override bool GetTradesCore(ResizeableArray <TradeInfoItem> list, Ticker ticker, DateTime startTime, DateTime endTime) { string address = string.Format("https://www.bitmex.com/api/v1/trade?symbol={0}&count=1000&startTime={1}&endTime={2}", ticker.Name, DateToString(startTime), DateToString(endTime)); string text = string.Empty; try { text = GetDownloadString(address); if (string.IsNullOrEmpty(text)) { return(false); } if (text[0] == '{') { JObject obj = JsonConvert.DeserializeObject <JObject>(text); LogManager.Default.Add(LogType.Error, this, Type.ToString(), "error in GetTradesCore", obj.Value <string>("message")); return(false); } JArray res = JsonConvert.DeserializeObject <JArray>(text); foreach (JObject obj in res.Children()) { TradeInfoItem item = new TradeInfoItem(); item.Ticker = ticker; item.RateString = obj.Value <string>("price"); item.AmountString = obj.Value <string>("size"); item.Type = obj.Value <string>("side")[0] == 'S' ? TradeType.Sell : TradeType.Buy; item.TimeString = obj.Value <string>("timestamp"); DateTime time = item.Time; if (list.Last() == null || list.Last().Time <= time) { list.Add(item); } else { break; } } } catch (Exception) { return(false); } return(true); }
public void Add(LogType type, object owner, string name, string message, string description) { lock (Messages) { Messages.Add(new LogMessage() { Type = type, Owner = owner, Name = name, Text = message, Description = description, Time = DateTime.UtcNow }); Debug.WriteLine(DateTime.UtcNow.ToLongTimeString() + ": " + message); } lock (this) { Save(); } RefreshVisual(); }
ResizeableArray <IInputDataWithTime> ISimulationDataProvider.LoadData() { if (string.IsNullOrEmpty(SimulationDataFile)) { return(null); } if (!File.Exists(SimulationDataFile)) { return(null); } ArbitrageHistoryHelper helper = ArbitrageHistoryHelper.FromFile(SimulationDataFile); ResizeableArray <IInputDataWithTime> res = new ResizeableArray <IInputDataWithTime>(helper.History.Count); foreach (ArbitrageStatisticsItem item in helper.History) { res.Add(item); } return(res); }
public void ShouldIncreseCapacityOnPrependWhenMaxCapacityIsReached() { var array = new ResizeableArray <int>(); Assert.AreEqual(16, array.Capacity); for (int i = 0; i < 15; i++) { array.Add(i); } Assert.AreEqual(16, array.Capacity); array.Prepend(200); Assert.AreEqual(32, array.Capacity); Assert.AreEqual(200, array.ElementAt(0)); for (int i = 1; i < 16; i++) { Assert.AreEqual(i - 1, array.ElementAt(i)); } }
ResizeableArray <CandleStickData> GetCandleSticks(int count) { ResizeableArray <CandleStickData> list = new ResizeableArray <CandleStickData>(); DateTime now = DateTime.UtcNow; DateTime start = now.AddMinutes(-count); Random r = new Random(); for (int i = 0; i < count; i++) { list.Add(new CandleStickData() { Time = start, Open = 4000 + 100 * r.NextDouble(), Close = 4000 + 100 * r.NextDouble(), High = 4200 + 100 * r.NextDouble(), Low = 3700 + 100 * r.NextDouble() }); start = start.AddMinutes(1); } return(list); }
protected override bool GetTradesCore(ResizeableArray <TradeInfoItem> list, Ticker ticker, DateTime start, DateTime end) { string address = string.Format("https://api.binance.com/api/v1/aggTrades?symbol={0}&limit={1}&startTime={2}&endTime={3}", Uri.EscapeDataString(ticker.CurrencyPair), 1000, ToUnixTimestampMs(start), ToUnixTimestampMs(end)); byte[] data = ((Ticker)ticker).DownloadBytes(address); if (data == null || data.Length == 0) { return(false); } int parseIndex = 0; List <string[]> items = JSonHelper.Default.DeserializeArrayOfObjects(data, ref parseIndex, AggTradeItemString); for (int i = 0; i < items.Count; i++) { string[] item = items[i]; DateTime time = FromUnixTimestampMs(FastValueConverter.ConvertPositiveLong(item[5])); //if(time > end) // break; int tradeId = FastValueConverter.ConvertPositiveInteger(item[0]); TradeInfoItem t = new TradeInfoItem(null, ticker); bool isBuy = item[6][0] != 't'; t.AmountString = item[2]; t.Time = time; t.Type = isBuy ? TradeType.Buy : TradeType.Sell; t.RateString = item[1]; t.Id = tradeId; double price = t.Rate; double amount = t.Amount; t.Total = price * amount; if (list.Last() == null || list.Last().Time < time) { list.Add(t); } } return(true); }
public override ResizeableArray<CandleStickData> GetCandleStickData(Ticker ticker, int candleStickPeriodMin, DateTime start, long periodInSeconds) { long startSec = (long)(start.Subtract(new DateTime(1970, 1, 1))).TotalSeconds; long end = startSec + periodInSeconds; string address = string.Format("https://bittrex.com/Api/v2.0/pub/market/GetTicks?marketName={0}&tickInterval={1}&_={2}", Uri.EscapeDataString(ticker.CurrencyPair), GetInvervalCommand(candleStickPeriodMin), GetNonce()); byte[] bytes = null; try { bytes = GetDownloadBytes(address); } catch(Exception) { return null; } if(bytes == null || bytes.Length == 0) return null; ResizeableArray<CandleStickData> list = new ResizeableArray<CandleStickData>(); int startIndex = 1; if(!JSonHelper.Default.SkipSymbol(bytes, ':', 3, ref startIndex)) return list; List<string[]> res = JSonHelper.Default.DeserializeArrayOfObjects(bytes, ref startIndex, new string[] { "O", "H", "L", "C", "V", "T", "BV" }); if(res == null) return list; for(int i = 0; i < res.Count; i++) { string[] item = res[i]; CandleStickData data = new CandleStickData(); data.Time = Convert.ToDateTime(item[5]); data.High = FastValueConverter.Convert(item[1]); data.Low = FastValueConverter.Convert(item[2]); data.Open = FastValueConverter.Convert(item[0]); data.Close = FastValueConverter.Convert(item[3]); data.Volume = FastValueConverter.Convert(item[6]); data.QuoteVolume = FastValueConverter.Convert(item[4]); data.WeightedAverage = 0; list.Add(data); } return list; }