public void RegistersIndicatorProperly()
        {
            var expected = 0;

            foreach (var type in _indicatorTestsTypes)
            {
                var indicatorTest = Activator.CreateInstance(type);
                if (indicatorTest is CommonIndicatorTests <IndicatorDataPoint> )
                {
                    var indicator = (indicatorTest as CommonIndicatorTests <IndicatorDataPoint>).Indicator;
                    Assert.DoesNotThrow(() => _algorithm.RegisterIndicator(_spy, indicator, Resolution.Minute, Field.Close));
                    expected++;
                }
                else if (indicatorTest is CommonIndicatorTests <IBaseDataBar> )
                {
                    var indicator = (indicatorTest as CommonIndicatorTests <IBaseDataBar>).Indicator;
                    Assert.DoesNotThrow(() => _algorithm.RegisterIndicator(_spy, indicator, Resolution.Minute));
                    expected++;
                }
                else if (indicatorTest is CommonIndicatorTests <TradeBar> )
                {
                    var indicator = (indicatorTest as CommonIndicatorTests <TradeBar>).Indicator;
                    Assert.DoesNotThrow(() => _algorithm.RegisterIndicator(_spy, indicator, Resolution.Minute));
                    expected++;
                }
                else
                {
                    throw new NotSupportedException($"RegistersIndicatorProperlyPython(): Unsupported indicator data type: {indicatorTest.GetType()}");
                }
                var actual = _algorithm.SubscriptionManager.Subscriptions
                             .Single(s => s.TickType == LeanData.GetCommonTickType(SecurityType.Equity))
                             .Consolidators.Count;
                Assert.AreEqual(expected, actual);
            }
        }
        public void RegisterPythonCustomIndicatorProperly()
        {
            const string code = @"
class GoodCustomIndicator :
    def __init__(self):
        self.IsReady = True
        self.Value = 0
    def Update(self, input):
        self.Value = input.Value
        return True
class BadCustomIndicator:
    def __init__(self):
        self.IsReady = True
        self.Value = 0
    def Updat(self, input):
        self.Value = input.Value
        return True";

            using (Py.GIL())
            {
                var module = PythonEngine.ModuleFromString(Guid.NewGuid().ToString(), code);

                var goodIndicator = module.GetAttr("GoodCustomIndicator").Invoke();
                Assert.DoesNotThrow(() => _algorithm.RegisterIndicator(_spy, goodIndicator, Resolution.Minute));

                var actual = _algorithm.SubscriptionManager.Subscriptions
                             .Single(s => s.TickType == LeanData.GetCommonTickType(SecurityType.Equity))
                             .Consolidators.Count;
                Assert.AreEqual(1, actual);

                var badIndicator = module.GetAttr("BadCustomIndicator").Invoke();
                Assert.Throws <NotImplementedException>(() => _algorithm.RegisterIndicator(_spy, badIndicator, Resolution.Minute));
            }
        }
示例#3
0
        public void TradeBarToTradeBar()
        {
            var algorithm = new QCAlgorithm();

            algorithm.SubscriptionManager.SetDataManager(new DataManagerStub(algorithm));
            var security     = algorithm.AddEquity("SPY");
            var consolidator = algorithm.ResolveConsolidator("SPY", Resolution.Minute);

            var inputType  = security.Subscriptions.Single(s => s.TickType == LeanData.GetCommonTickType(SecurityType.Equity)).Type;
            var outputType = consolidator.OutputType;

            Assert.AreEqual(inputType, outputType);
        }
示例#4
0
        /// <summary>
        /// Initialize a instance of LeanDataReader from a path to a zipped data file.
        /// It also supports declaring the zip entry CSV file for options and futures.
        /// </summary>
        /// <param name="filepath">Absolute or relative path to a zipped data file, optionally the zip entry file can be declared by using '#' as separator.</param>
        /// <example>
        /// var dataReader = LeanDataReader("../relative/path/to/file.zip")
        /// var dataReader = LeanDataReader("absolute/path/to/file.zip#zipEntry.csv")
        /// </example>
        public LeanDataReader(string filepath)
        {
            Symbol     symbol;
            DateTime   date;
            Resolution resolution;
            string     zipEntry = null;

            var isFutureOrOption = filepath.Contains("#");

            if (isFutureOrOption)
            {
                zipEntry = filepath.Split('#')[1];
                filepath = filepath.Split('#')[0];
            }

            var fileInfo = new FileInfo(filepath);

            if (!LeanData.TryParsePath(fileInfo.FullName, out symbol, out date, out resolution))
            {
                throw new ArgumentException($"File {filepath} cannot be parsed.");
            }

            if (isFutureOrOption)
            {
                symbol = LeanData.ReadSymbolFromZipEntry(symbol, resolution, zipEntry);
            }

            var marketHoursDataBase = MarketHoursDatabase.FromDataFolder();
            var dataTimeZone        = marketHoursDataBase.GetDataTimeZone(symbol.ID.Market, symbol, symbol.SecurityType);
            var exchangeTimeZone    = marketHoursDataBase.GetExchangeHours(symbol.ID.Market, symbol, symbol.SecurityType).TimeZone;

            var tickType = LeanData.GetCommonTickType(symbol.SecurityType);
            var fileName = Path.GetFileNameWithoutExtension(fileInfo.Name);

            if (fileName.Contains("_"))
            {
                tickType = (TickType)Enum.Parse(typeof(TickType), fileName.Split('_')[1], true);
            }

            var dataType = LeanData.GetDataType(resolution, tickType);
            var config   = new SubscriptionDataConfig(dataType, symbol, resolution,
                                                      dataTimeZone, exchangeTimeZone, tickType: tickType,
                                                      fillForward: false, extendedHours: true, isInternalFeed: true);

            _date     = date;
            _zipPath  = fileInfo.FullName;
            _zipentry = zipEntry;
            _config   = config;
        }
示例#5
0
        public void TickTypeQuoteToQuoteBar()
        {
            var algorithm = new QCAlgorithm();

            algorithm.SubscriptionManager.SetDataManager(new DataManagerStub(algorithm));
            var security     = algorithm.AddForex("EURUSD", Resolution.Tick);
            var consolidator = algorithm.ResolveConsolidator("EURUSD", Resolution.Minute);

            var tickType = security.Subscriptions.Single(s => s.TickType == LeanData.GetCommonTickType(SecurityType.Forex)).TickType;

            var outputType = consolidator.OutputType;

            Assert.AreEqual(TickType.Quote, tickType);
            Assert.AreEqual(typeof(QuoteBar), outputType);
        }
        public void OrdersAreSubmittedWhenRequiredForTargetsToExecute(
            Language language,
            double[] historicalPrices,
            decimal lastVolume,
            int expectedOrdersSubmitted,
            decimal expectedTotalQuantity)
        {
            var actualOrdersSubmitted = new List <SubmitOrderRequest>();

            var time            = new DateTime(2018, 8, 2, 16, 0, 0);
            var historyProvider = new Mock <IHistoryProvider>();

            historyProvider.Setup(m => m.GetHistory(It.IsAny <IEnumerable <HistoryRequest> >(), It.IsAny <DateTimeZone>()))
            .Returns(historicalPrices.Select((x, i) =>
                                             new Slice(time.AddMinutes(i),
                                                       new List <BaseData>
            {
                new TradeBar
                {
                    Time   = time.AddMinutes(i),
                    Symbol = Symbols.AAPL,
                    Open   = Convert.ToDecimal(x),
                    High   = Convert.ToDecimal(x),
                    Low    = Convert.ToDecimal(x),
                    Close  = Convert.ToDecimal(x),
                    Volume = 100m
                }
            })));

            var algorithm = new QCAlgorithm();

            algorithm.SubscriptionManager.SetDataManager(new DataManagerStub(algorithm));
            algorithm.SetPandasConverter();
            algorithm.SetHistoryProvider(historyProvider.Object);
            algorithm.SetDateTime(time.AddMinutes(5));

            var security = algorithm.AddEquity(Symbols.AAPL.Value);

            security.SetMarketPrice(new TradeBar {
                Value = 250, Volume = lastVolume
            });

            algorithm.SetFinishedWarmingUp();

            var orderProcessor = new Mock <IOrderProcessor>();

            orderProcessor.Setup(m => m.Process(It.IsAny <SubmitOrderRequest>()))
            .Returns((SubmitOrderRequest request) => new OrderTicket(algorithm.Transactions, request))
            .Callback((OrderRequest request) => actualOrdersSubmitted.Add((SubmitOrderRequest)request));
            orderProcessor.Setup(m => m.GetOpenOrders(It.IsAny <Func <Order, bool> >()))
            .Returns(new List <Order>());
            algorithm.Transactions.SetOrderProcessor(orderProcessor.Object);

            var model = GetExecutionModel(language);

            algorithm.SetExecution(model);

            var changes = SecurityChangesTests.CreateNonInternal(new[] { security }, Enumerable.Empty <Security>());

            model.OnSecuritiesChanged(algorithm, changes);

            algorithm.History(new List <Symbol> {
                security.Symbol
            }, historicalPrices.Length, Resolution.Minute)
            .PushThroughConsolidators(symbol => algorithm.Securities[symbol].Subscriptions.Single(s => s.TickType == LeanData.GetCommonTickType(SecurityType.Equity)).Consolidators.First());

            var targets = new IPortfolioTarget[] { new PortfolioTarget(security.Symbol, 10) };

            model.Execute(algorithm, targets);

            Assert.AreEqual(expectedOrdersSubmitted, actualOrdersSubmitted.Count);
            Assert.AreEqual(expectedTotalQuantity, actualOrdersSubmitted.Sum(x => x.Quantity));

            if (actualOrdersSubmitted.Count == 1)
            {
                var request = actualOrdersSubmitted[0];
                Assert.AreEqual(expectedTotalQuantity, request.Quantity);
                Assert.AreEqual(algorithm.UtcTime, request.Time);
            }
        }
示例#7
0
 /// <summary>
 /// The LeanDataReader constructor
 /// </summary>
 /// <param name="config">The <see cref="SubscriptionDataConfig"/></param>
 /// <param name="symbol">The <see cref="Symbol"/> that will be read</param>
 /// <param name="resolution">The <see cref="Resolution"/> that will be read</param>
 /// <param name="date">The <see cref="DateTime"/> that will be read</param>
 /// <param name="dataFolder">The root data folder</param>
 public LeanDataReader(SubscriptionDataConfig config, Symbol symbol, Resolution resolution, DateTime date, string dataFolder)
 {
     _date    = date;
     _zipPath = LeanData.GenerateZipFilePath(dataFolder, symbol, date, resolution, LeanData.GetCommonTickType(symbol.SecurityType));
     _config  = config;
 }