Ejemplo n.º 1
0
        public void ReturnsCorrectBrokerageSymbol()
        {
            var mapper = new OandaSymbolMapper();

            var symbol          = Symbol.Create("EURUSD", SecurityType.Forex, Market.Oanda);
            var brokerageSymbol = mapper.GetBrokerageSymbol(symbol);

            Assert.AreEqual("EUR_USD", brokerageSymbol);

            symbol          = Symbol.Create("DE30EUR", SecurityType.Cfd, Market.Oanda);
            brokerageSymbol = mapper.GetBrokerageSymbol(symbol);
            Assert.AreEqual("DE30_EUR", brokerageSymbol);
        }
Ejemplo n.º 2
0
        public void ThrowsOnNullOrEmptySymbol()
        {
            var mapper = new OandaSymbolMapper();

            Assert.Throws <ArgumentException>(() => mapper.GetLeanSymbol(null, SecurityType.Forex, Market.Oanda));

            Assert.Throws <ArgumentException>(() => mapper.GetLeanSymbol("", SecurityType.Forex, Market.Oanda));

            var symbol = Symbol.Empty;

            Assert.Throws <ArgumentException>(() => mapper.GetBrokerageSymbol(symbol));

            symbol = null;
            Assert.Throws <ArgumentException>(() => mapper.GetBrokerageSymbol(symbol));

            symbol = Symbol.Create("", SecurityType.Forex, Market.Oanda);
            Assert.Throws <ArgumentException>(() => mapper.GetBrokerageSymbol(symbol));
        }
Ejemplo n.º 3
0
        public void ThrowsOnInvalidSecurityType()
        {
            var mapper = new OandaSymbolMapper();

            Assert.Throws <ArgumentException>(() => mapper.GetLeanSymbol("AAPL", SecurityType.Equity, Market.Oanda));

            var symbol = Symbol.Create("AAPL", SecurityType.Equity, Market.Oanda);

            Assert.Throws <ArgumentException>(() => mapper.GetBrokerageSymbol(symbol));
        }
Ejemplo n.º 4
0
        public void ThrowsOnUnknownSymbol()
        {
            var mapper = new OandaSymbolMapper();

            Assert.Throws <ArgumentException>(() => mapper.GetLeanSymbol("ABC_USD", SecurityType.Forex, Market.Oanda));

            var symbol = Symbol.Create("ABCUSD", SecurityType.Forex, Market.Oanda);

            Assert.Throws <ArgumentException>(() => mapper.GetBrokerageSymbol(symbol));
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Get historical data enumerable for a single symbol, type and resolution given this start and end time (in UTC).
        /// </summary>
        /// <param name="symbol">Symbol for the data we're looking for.</param>
        /// <param name="resolution">Resolution of the data request</param>
        /// <param name="startUtc">Start time of the data in UTC</param>
        /// <param name="endUtc">End time of the data in UTC</param>
        /// <returns>Enumerable of base data for this symbol</returns>
        public IEnumerable <BaseData> Get(Symbol symbol, Resolution resolution, DateTime startUtc, DateTime endUtc)
        {
            if (!_symbolMapper.IsKnownLeanSymbol(symbol))
            {
                throw new ArgumentException("Invalid symbol requested: " + symbol.Value);
            }

            if (resolution == Resolution.Tick)
            {
                throw new NotSupportedException("Resolution not available: " + resolution);
            }

            if (symbol.ID.SecurityType != SecurityType.Forex && symbol.ID.SecurityType != SecurityType.Cfd)
            {
                throw new NotSupportedException("SecurityType not available: " + symbol.ID.SecurityType);
            }

            if (endUtc < startUtc)
            {
                throw new ArgumentException("The end date must be greater or equal than the start date.");
            }

            var barsTotalInPeriod = new List <Candle>();
            var barsToSave        = new List <Candle>();

            // set the starting date/time
            DateTime date          = startUtc;
            DateTime startDateTime = date;

            // loop until last date
            while (startDateTime <= endUtc.AddDays(1))
            {
                string start = startDateTime.ToString("yyyy-MM-ddTHH:mm:ssZ");

                // request blocks of 5-second bars with a starting date/time
                var oandaSymbol = _symbolMapper.GetBrokerageSymbol(symbol);
                var bars        = DownloadBars(oandaSymbol, start, BarsPerRequest);
                if (bars.Count == 0)
                {
                    break;
                }

                var groupedBars = GroupBarsByDate(bars);

                if (groupedBars.Count > 1)
                {
                    // we received more than one day, so we save the completed days and continue
                    while (groupedBars.Count > 1)
                    {
                        var currentDate = groupedBars.Keys.First();
                        if (currentDate > endUtc)
                        {
                            break;
                        }

                        barsToSave.AddRange(groupedBars[currentDate]);

                        barsTotalInPeriod.AddRange(barsToSave);

                        barsToSave.Clear();

                        // remove the completed date
                        groupedBars.Remove(currentDate);
                    }

                    // update the current date
                    date = groupedBars.Keys.First();

                    if (date <= endUtc)
                    {
                        barsToSave.AddRange(groupedBars[date]);
                    }
                }
                else
                {
                    var currentDate = groupedBars.Keys.First();
                    if (currentDate > endUtc)
                    {
                        break;
                    }

                    // update the current date
                    date = currentDate;

                    barsToSave.AddRange(groupedBars[date]);
                }

                // calculate the next request datetime (next 5-sec bar time)
                startDateTime = GetDateTimeFromString(bars[bars.Count - 1].time).AddSeconds(5);
            }

            if (barsToSave.Count > 0)
            {
                barsTotalInPeriod.AddRange(barsToSave);
            }

            switch (resolution)
            {
            case Resolution.Second:
            case Resolution.Minute:
            case Resolution.Hour:
            case Resolution.Daily:
                foreach (var bar in AggregateBars(symbol, barsTotalInPeriod, resolution.ToTimeSpan()))
                {
                    yield return(bar);
                }
                break;
            }
        }