示例#1
0
        public void OnEndOfTimeStepDoesNotThrowWhenSeedsSameUnderlyingForTwoSecurities()
        {
            var qcAlgorithm = new QCAlgorithm();

            qcAlgorithm.SubscriptionManager.SetDataManager(new DataManagerStub(qcAlgorithm));
            qcAlgorithm.SetLiveMode(true);
            var testHistoryProvider = new TestHistoryProvider();

            qcAlgorithm.HistoryProvider = testHistoryProvider;
            var option = qcAlgorithm.AddOption(testHistoryProvider.underlyingSymbol);

            var symbol = Symbol.CreateOption(testHistoryProvider.underlyingSymbol, Market.USA, OptionStyle.American,
                                             OptionRight.Call, 1, new DateTime(2015, 12, 24));
            var symbol2 = Symbol.CreateOption(testHistoryProvider.underlyingSymbol, Market.USA, OptionStyle.American,
                                              OptionRight.Put, 1, new DateTime(2015, 12, 24));

            var optionContract  = qcAlgorithm.AddOptionContract(symbol, Resolution.Daily);
            var optionContract2 = qcAlgorithm.AddOptionContract(symbol2, Resolution.Minute);

            qcAlgorithm.OnEndOfTimeStep();
            var data = qcAlgorithm.Securities[testHistoryProvider.underlyingSymbol].GetLastData();

            Assert.AreEqual(testHistoryProvider.LastResolutionRequest, Resolution.Minute);
            Assert.IsNotNull(data);
            Assert.AreEqual(data.Price, 2);
        }
示例#2
0
        public void GetLastKnownPriceOfIlliquidAsset_RealData()
        {
            var algorithm = new QCAlgorithm();

            algorithm.SubscriptionManager.SetDataManager(new DataManagerStub(algorithm));
            algorithm.HistoryProvider = new SubscriptionDataReaderHistoryProvider();
            var cacheProvider = new ZipDataCacheProvider(new DefaultDataProvider());

            algorithm.HistoryProvider.Initialize(new HistoryProviderInitializeParameters(
                                                     null,
                                                     null,
                                                     new DefaultDataProvider(),
                                                     cacheProvider,
                                                     new LocalDiskMapFileProvider(),
                                                     new LocalDiskFactorFileProvider(),
                                                     null,
                                                     false,
                                                     new DataPermissionManager()));

            algorithm.SetDateTime(new DateTime(2014, 6, 6, 15, 0, 0));

            //20140606_twx_minute_quote_american_call_230000_20150117.csv
            var optionSymbol = Symbol.CreateOption("TWX", Market.USA, OptionStyle.American, OptionRight.Call, 23, new DateTime(2015, 1, 17));
            var option       = algorithm.AddOptionContract(optionSymbol);

            var lastKnownPrice = algorithm.GetLastKnownPrice(option);

            Assert.IsNotNull(lastKnownPrice);

            // Data gap of more than 15 minutes
            Assert.Greater((algorithm.Time - lastKnownPrice.EndTime).TotalMinutes, 15);

            cacheProvider.DisposeSafely();
        }
示例#3
0
        [TestCase(31, -31, 0.0004)] // Long To Longer (31 + -31 = 0)
        public void CallOTM_MarginRequirement(int startingHoldings, int expectedOrderSize, decimal targetPercentage)
        {
            // Initialize algorithm
            var algorithm = new QCAlgorithm();

            algorithm.SetFinishedWarmingUp();
            algorithm.Transactions.SetOrderProcessor(new FakeOrderProcessor());

            algorithm.SetCash(1000000);
            algorithm.SubscriptionManager.SetDataManager(new DataManagerStub(algorithm));

            var optionSymbol = Symbols.CreateOptionSymbol("SPY", OptionRight.Call, 411m, DateTime.UtcNow);
            var option       = algorithm.AddOptionContract(optionSymbol);

            option.Holdings.SetHoldings(4.74m, startingHoldings);
            option.FeeModel = new ConstantFeeModel(0);
            option.SetLeverage(1);

            // Update option data
            UpdatePrice(option, 4.78m);

            // Update the underlying data
            UpdatePrice(option.Underlying, 395.51m);

            var model  = new OptionMarginModel();
            var result = model.GetMaximumOrderQuantityForTargetBuyingPower(algorithm.Portfolio, option, targetPercentage, 0);

            Assert.AreEqual(expectedOrderSize, result.Quantity);
        }
示例#4
0
        public void GetLastKnownPriceOfIlliquidAsset_TestData()
        {
            // Set the start date on Tuesday
            _algorithm.SetStartDate(2014, 6, 10);

            var optionSymbol = Symbol.CreateOption("TWX", Market.USA, OptionStyle.American, OptionRight.Call, 23, new DateTime(2015, 1, 17));
            var option       = _algorithm.AddOptionContract(optionSymbol);

            // The last known price is on Friday, so we missed data from Monday and no data during Weekend
            var barTime = new DateTime(2014, 6, 6, 15, 0, 0, 0);

            _testHistoryProvider.Slices = new[]
            {
                new Slice(barTime, new[] { new TradeBar(barTime, optionSymbol, 100, 100, 100, 100, 1) }, barTime)
            }.ToList();

            var lastKnownPrice = _algorithm.GetLastKnownPrice(option);

            Assert.IsNotNull(lastKnownPrice);
            Assert.AreEqual(barTime.AddMinutes(1), lastKnownPrice.EndTime);
        }
 /// <summary>
 /// Creates and adds a new single <see cref="Option"/> contract to the algorithm
 /// </summary>
 /// <param name="symbol">The option contract symbol</param>
 /// <param name="resolution">The <see cref="Resolution"/> of market data, Tick, Second, Minute, Hour, or Daily. Default is <see cref="Resolution.Minute"/></param>
 /// <param name="fillDataForward">If true, returns the last available data even if none in that timeslice. Default is <value>true</value></param>
 /// <param name="leverage">The requested leverage for this equity. Default is set by <see cref="SecurityInitializer"/></param>
 /// <returns>The new <see cref="Option"/> security</returns>
 public Option AddOptionContract(Symbol symbol, Resolution resolution = Resolution.Minute, bool fillDataForward = true, decimal leverage = 0m)
 {
     return(_baseAlgorithm.AddOptionContract(symbol, resolution, fillDataForward, leverage));
 }