Beispiel #1
0
        /// <summary>
        /// Creates a user defined universe symbol
        /// </summary>
        /// <param name="securityType">The security</param>
        /// <param name="market">The market</param>
        /// <returns>A symbol for user defined universe of the specified security type and market</returns>
        public static Symbol CreateSymbol(SecurityType securityType, string market)
        {
            var ticker = string.Format("qc-universe-userdefined-{0}-{1}", market.ToLower(), securityType);
            SecurityIdentifier sid;

            switch (securityType)
            {
            case SecurityType.Base:
                sid = SecurityIdentifier.GenerateBase(ticker, market);
                break;

            case SecurityType.Equity:
                sid = SecurityIdentifier.GenerateEquity(SecurityIdentifier.DefaultDate, ticker, market);
                break;

            case SecurityType.Option:
                sid = SecurityIdentifier.GenerateOption(SecurityIdentifier.DefaultDate, ticker, market, 0, 0, 0);
                break;

            case SecurityType.Forex:
                sid = SecurityIdentifier.GenerateForex(ticker, market);
                break;

            case SecurityType.Cfd:
                sid = SecurityIdentifier.GenerateCfd(ticker, market);
                break;

            case SecurityType.Commodity:
            case SecurityType.Future:
            default:
                throw new NotImplementedException("The specified security type is not implemented yet: " + securityType);
            }

            return(new Symbol(sid, ticker));
        }
Beispiel #2
0
        /// <summary>
        /// Creates a user defined universe symbol
        /// </summary>
        /// <param name="market">The market</param>
        /// <param name="securityType">The underlying option security type</param>
        /// <returns>A symbol for user defined universe of the specified security type and market</returns>
        public static Symbol CreateSymbol(string market, SecurityType securityType)
        {
            var ticker     = $"qc-universe-optioncontract-{securityType.SecurityTypeToLower()}-{market.ToLowerInvariant()}";
            var underlying = Symbol.Create(ticker, securityType, market);
            var sid        = SecurityIdentifier.GenerateOption(SecurityIdentifier.DefaultDate, underlying.ID, market, 0, 0, 0);

            return(new Symbol(sid, ticker));
        }
        private Subscription CreateSubscription(Resolution resolution, string symbol = "AAPL", bool isInternalFeed = false,
                                                SecurityType type = SecurityType.Equity, TickType tickType         = TickType.Trade)
        {
            var      start = DateTime.UtcNow;
            var      end   = start.AddSeconds(10);
            Security security;
            Symbol   _symbol;

            if (type == SecurityType.Equity)
            {
                _symbol  = new Symbol(SecurityIdentifier.GenerateEquity(DateTime.Now, symbol, Market.USA), symbol);
                security = new Equity(
                    _symbol,
                    SecurityExchangeHours.AlwaysOpen(DateTimeZone.Utc),
                    new Cash(Currencies.USD, 0, 1),
                    SymbolProperties.GetDefault(Currencies.USD),
                    ErrorCurrencyConverter.Instance,
                    RegisteredSecurityDataTypesProvider.Null
                    );
            }
            else if (type == SecurityType.Option)
            {
                _symbol = new Symbol(SecurityIdentifier.GenerateOption(DateTime.Now,
                                                                       SecurityIdentifier.GenerateEquity(DateTime.Now, symbol, Market.USA),
                                                                       Market.USA, 0.0m, OptionRight.Call, OptionStyle.American), symbol);
                security = new Option(
                    _symbol,
                    SecurityExchangeHours.AlwaysOpen(DateTimeZone.Utc),
                    new Cash(Currencies.USD, 0, 1),
                    new OptionSymbolProperties(SymbolProperties.GetDefault(Currencies.USD)),
                    ErrorCurrencyConverter.Instance,
                    RegisteredSecurityDataTypesProvider.Null
                    );
            }
            else if (type == SecurityType.Future)
            {
                _symbol  = new Symbol(SecurityIdentifier.GenerateFuture(DateTime.Now, symbol, Market.USA), symbol);
                security = new Future(
                    _symbol,
                    SecurityExchangeHours.AlwaysOpen(DateTimeZone.Utc),
                    new Cash(Currencies.USD, 0, 1),
                    SymbolProperties.GetDefault(Currencies.USD),
                    ErrorCurrencyConverter.Instance,
                    RegisteredSecurityDataTypesProvider.Null
                    );
            }
            else
            {
                throw new Exception("SecurityType not implemented");
            }
            var config = new SubscriptionDataConfig(typeof(TradeBar), _symbol, resolution, DateTimeZone.Utc, DateTimeZone.Utc, true, false, isInternalFeed, false, tickType);
            var timeZoneOffsetProvider     = new TimeZoneOffsetProvider(DateTimeZone.Utc, start, end);
            var enumerator                 = new EnqueueableEnumerator <BaseData>();
            var subscriptionDataEnumerator = new SubscriptionDataEnumerator(config, security.Exchange.Hours, timeZoneOffsetProvider, enumerator);
            var subscriptionRequest        = new SubscriptionRequest(false, null, security, config, start, end);

            return(new Subscription(subscriptionRequest, subscriptionDataEnumerator, timeZoneOffsetProvider));
        }
Beispiel #4
0
 private TestCaseData[] GetSymbolCreateTestCaseData()
 {
     return(new []
     {
         new TestCaseData("SPY", SecurityType.Equity, Market.USA, new Symbol(SecurityIdentifier.GenerateEquity("SPY", Market.USA), "SPY")),
         new TestCaseData("EURUSD", SecurityType.Forex, Market.FXCM, new Symbol(SecurityIdentifier.GenerateForex("EURUSD", Market.FXCM), "EURUSD")),
         new TestCaseData("SPY", SecurityType.Option, Market.USA, new Symbol(SecurityIdentifier.GenerateOption(SecurityIdentifier.DefaultDate, Symbols.SPY.ID, Market.USA, 0, default(OptionRight), default(OptionStyle)), "?SPY"))
     });
 }
Beispiel #5
0
        /// <summary>
        /// Creates a user defined universe symbol
        /// </summary>
        /// <param name="securityType">The security</param>
        /// <param name="market">The market</param>
        /// <returns>A symbol for user defined universe of the specified security type and market</returns>
        public static Symbol CreateSymbol(SecurityType securityType, string market)
        {
            var ticker = $"qc-universe-userdefined-{market.ToLowerInvariant()}-{securityType}";
            SecurityIdentifier sid;

            switch (securityType)
            {
            case SecurityType.Base:
                sid = SecurityIdentifier.GenerateBase(null, ticker, market);
                break;

            case SecurityType.Equity:
                sid = SecurityIdentifier.GenerateEquity(SecurityIdentifier.DefaultDate, ticker, market);
                break;

            case SecurityType.Option:
                var underlying = SecurityIdentifier.GenerateEquity(SecurityIdentifier.DefaultDate, ticker, market);
                sid = SecurityIdentifier.GenerateOption(SecurityIdentifier.DefaultDate, underlying, market, 0, 0, 0);
                break;

            case SecurityType.FutureOption:
                var underlyingFuture = SecurityIdentifier.GenerateFuture(SecurityIdentifier.DefaultDate, ticker, market);
                sid = SecurityIdentifier.GenerateOption(SecurityIdentifier.DefaultDate, underlyingFuture, market, 0, 0, 0);
                break;

            case SecurityType.IndexOption:
                var underlyingIndex = SecurityIdentifier.GenerateIndex(ticker, market);
                sid = SecurityIdentifier.GenerateOption(SecurityIdentifier.DefaultDate, underlyingIndex, market, 0, 0, OptionStyle.European);
                break;

            case SecurityType.Forex:
                sid = SecurityIdentifier.GenerateForex(ticker, market);
                break;

            case SecurityType.Cfd:
                sid = SecurityIdentifier.GenerateCfd(ticker, market);
                break;

            case SecurityType.Index:
                sid = SecurityIdentifier.GenerateIndex(ticker, market);
                break;

            case SecurityType.Future:
                sid = SecurityIdentifier.GenerateFuture(SecurityIdentifier.DefaultDate, ticker, market);
                break;

            case SecurityType.Crypto:
                sid = SecurityIdentifier.GenerateCrypto(ticker, market);
                break;

            case SecurityType.Commodity:
            default:
                throw new NotImplementedException($"The specified security type is not implemented yet: {securityType}");
            }

            return(new Symbol(sid, ticker));
        }
Beispiel #6
0
        public void Option_SubscriptionDataConfigList_CanOnlyHave_RawDataNormalization()
        {
            var option = new Symbol(SecurityIdentifier.GenerateOption(SecurityIdentifier.DefaultDate, Symbols.SPY.ID, Market.USA, 0, default(OptionRight), default(OptionStyle)), "?SPY");

            var subscriptionDataConfigList = new SubscriptionDataConfigList(option);

            Assert.DoesNotThrow(() => { subscriptionDataConfigList.SetDataNormalizationMode(DataNormalizationMode.Raw); });

            Assert.Throws(typeof(ArgumentException), () => { subscriptionDataConfigList.SetDataNormalizationMode(DataNormalizationMode.Adjusted); });
            Assert.Throws(typeof(ArgumentException), () => { subscriptionDataConfigList.SetDataNormalizationMode(DataNormalizationMode.SplitAdjusted); });
            Assert.Throws(typeof(ArgumentException), () => { subscriptionDataConfigList.SetDataNormalizationMode(DataNormalizationMode.Adjusted); });
            Assert.Throws(typeof(ArgumentException), () => { subscriptionDataConfigList.SetDataNormalizationMode(DataNormalizationMode.TotalReturn); });
        }
Beispiel #7
0
        public void SymbolWithSidContainingUnderlyingCreatedWithoutNullUnderlying()
        {
            var future    = Symbol.CreateFuture("ES", Market.CME, new DateTime(2020, 6, 19));
            var optionSid = SecurityIdentifier.GenerateOption(
                future.ID.Date,
                future.ID,
                future.ID.Market,
                3500m,
                OptionRight.Call,
                OptionStyle.American);

            var option = new Symbol(optionSid, "ES");

            Assert.IsNotNull(option.Underlying);
            Assert.AreEqual(future, option.Underlying);
        }
Beispiel #8
0
        public void SecurityManagerCanCreate_ConcreteOptions_WithCorrectSubscriptions()
        {
            var underlying               = SecurityIdentifier.GenerateEquity(new DateTime(1998, 01, 02), "SPY", Market.USA);
            var optionIdentifier         = SecurityIdentifier.GenerateOption(new DateTime(2015, 09, 18), underlying, Market.USA, 195.50m, OptionRight.Put, OptionStyle.European);
            var optionSymbol             = new Symbol(optionIdentifier, "SPY", Symbol.Empty);
            var optionMarketHoursDbEntry = _marketHoursDatabase.GetEntry(Market.USA, "SPY", SecurityType.Equity, TimeZones.NewYork);
            var optionSymbolProperties   = _symbolPropertiesDatabase.GetSymbolProperties(optionSymbol.ID.Market, "SPY", optionSymbol.ID.SecurityType, CashBook.AccountCurrency);

            var subscriptionTypes = new List <Tuple <Type, TickType> >
            {
                new Tuple <Type, TickType>(typeof(TradeBar), TickType.Trade),
                new Tuple <Type, TickType>(typeof(QuoteBar), TickType.Quote),
                new Tuple <Type, TickType>(typeof(OpenInterest), TickType.OpenInterest)
            };

            var optionSubscriptions = SecurityManager.CreateSecurity(subscriptionTypes,
                                                                     _securityPortfolioManager,
                                                                     _subscriptionManager,
                                                                     optionMarketHoursDbEntry.ExchangeHours,
                                                                     optionMarketHoursDbEntry.DataTimeZone,
                                                                     optionSymbolProperties,
                                                                     _securityInitializer,
                                                                     optionSymbol,
                                                                     Resolution.Second,
                                                                     false,
                                                                     1.0m,
                                                                     false,
                                                                     false,
                                                                     false,
                                                                     false);

            Assert.IsFalse(optionSymbol.IsCanonical());

            Assert.AreEqual(optionSubscriptions.Subscriptions.Count(), 3);
            Assert.IsTrue(optionSubscriptions.Subscriptions.Any(x => x.TickType == TickType.OpenInterest && x.Type == typeof(OpenInterest)));
            Assert.IsTrue(optionSubscriptions.Subscriptions.Any(x => x.TickType == TickType.Quote && x.Type == typeof(QuoteBar)));
            Assert.IsTrue(optionSubscriptions.Subscriptions.Any(x => x.TickType == TickType.Trade && x.Type == typeof(TradeBar)));
        }
Beispiel #9
0
        public void CanCreate_ConcreteOptions_WithCorrectSubscriptions()
        {
            var underlying       = SecurityIdentifier.GenerateEquity(new DateTime(1998, 01, 02), "SPY", Market.USA);
            var optionIdentifier = SecurityIdentifier.GenerateOption(new DateTime(2015, 09, 18), underlying, Market.USA, 195.50m, OptionRight.Put, OptionStyle.European);
            var optionSymbol     = new Symbol(optionIdentifier, "SPY", Symbol.Empty);

            var subscriptionTypes = new List <Tuple <Type, TickType> >
            {
                new Tuple <Type, TickType>(typeof(TradeBar), TickType.Trade),
                new Tuple <Type, TickType>(typeof(QuoteBar), TickType.Quote),
                new Tuple <Type, TickType>(typeof(OpenInterest), TickType.OpenInterest)
            };

            var configs  = _subscriptionManager.SubscriptionDataConfigService.Add(optionSymbol, Resolution.Second, false, false, false, false, false, subscriptionTypes);
            var security = _securityService.CreateSecurity(optionSymbol, configs, 1.0m, false);

            Assert.IsFalse(optionSymbol.IsCanonical());

            Assert.AreEqual(security.Subscriptions.Count(), 3);
            Assert.IsTrue(security.Subscriptions.Any(x => x.TickType == TickType.OpenInterest && x.Type == typeof(OpenInterest)));
            Assert.IsTrue(security.Subscriptions.Any(x => x.TickType == TickType.Quote && x.Type == typeof(QuoteBar)));
            Assert.IsTrue(security.Subscriptions.Any(x => x.TickType == TickType.Trade && x.Type == typeof(TradeBar)));
        }
Beispiel #10
0
        public void OptionOrderDoesNotThrow()
        {
            var equityPoints = new SortedList <DateTime, double>
            {
                { new DateTime(2019, 1, 3, 5, 0, 5), 100000 },
                { new DateTime(2019, 1, 4, 5, 0, 5), 90000 },
            };

            var series    = new Series <DateTime, double>(equityPoints);
            var equity    = Symbol.Create("SPY", SecurityType.Equity, Market.USA);
            var optionSid = SecurityIdentifier.GenerateOption(
                equity.ID.Date,
                equity.ID,
                equity.ID.Market,
                200m,
                OptionRight.Call,
                OptionStyle.American);
            var option = new Symbol(optionSid, optionSid.Symbol);

            var entryOrder = Order.CreateOrder(new SubmitOrderRequest(
                                                   OrderType.Market,
                                                   SecurityType.Option,
                                                   option,
                                                   1,
                                                   0m,
                                                   0m,
                                                   new DateTime(2019, 1, 3, 5, 0, 5),
                                                   string.Empty
                                                   ));
            var exitOrder = Order.CreateOrder(new SubmitOrderRequest(
                                                  OrderType.Market,
                                                  SecurityType.Option,
                                                  option,
                                                  -1,
                                                  0m,
                                                  0m,
                                                  new DateTime(2019, 1, 4, 5, 0, 5),
                                                  string.Empty
                                                  ));

            entryOrder.LastFillTime = new DateTime(2019, 1, 3, 5, 0, 5);
            exitOrder.LastFillTime  = new DateTime(2019, 1, 4, 5, 0, 5);

            entryOrder.GetType().GetProperty("Id").SetValue(entryOrder, 1);
            entryOrder.GetType().GetProperty("Price").SetValue(entryOrder, 100000m);

            Order marketOnFillOrder = null;

            exitOrder.GetType().GetProperty("Id").SetValue(exitOrder, 2);
            exitOrder.GetType().GetProperty("Price").SetValue(exitOrder, 80000m);
            exitOrder.GetType().GetProperty("Status").SetValue(exitOrder, OrderStatus.Filled);

            var orders = new[] { entryOrder, marketOnFillOrder, exitOrder }.Where(x => x != null);

            var looper = PortfolioLooper.FromOrders(series, orders);

            Assert.DoesNotThrow(() =>
            {
                foreach (var pointInTimePortfolio in looper)
                {
                    Assert.AreEqual(option, pointInTimePortfolio.Order.Symbol);
                    Assert.AreEqual(option.Underlying, pointInTimePortfolio.Order.Symbol.Underlying);
                }
            });
        }
        /// <summary>
        /// Parses the specified input stream into an enumerable of data
        /// </summary>
        /// <param name="source">The source of the stream</param>
        /// <param name="stream">The input stream to be parsed</param>
        /// <returns>An enumerable of base data</returns>
        public IEnumerable <BaseData> Parse(string source, Stream stream)
        {
            var count         = 0L;
            var referenceDate = DateTime.ParseExact(new FileInfo(source).Directory.Name, DateFormat.EightCharacter, null);

            using (var reader = new StreamReader(stream))
            {
                // skip the header row
                reader.ReadLine();

                string line;
                while ((line = reader.ReadLine()) != null)
                {
                    count++;

                    if (count % LogInterval == 0)
                    {
                        Log.Trace("AlgoSeekOptionsParser.Parse({0}): Parsed {1,3}M lines.", source, count / LogInterval);
                    }

                    Tick tick;
                    try
                    {
                        // filter out bad lines as fast as possible
                        EventType eventType;
                        switch (line[13])
                        {
                        case 'T':
                            eventType = EventType.Trade;
                            break;

                        case 'F':
                            switch (line[15])
                            {
                            case 'B':
                                eventType = EventType.Bid;
                                break;

                            case 'O':
                                eventType = EventType.Ask;
                                break;

                            default:
                                continue;
                            }
                            break;

                        default:
                            continue;
                        }

                        // parse csv check column count
                        const int columns = 11;
                        var       csv     = line.ToCsv(columns);
                        if (csv.Count < columns)
                        {
                            continue;
                        }

                        // ignoring time zones completely -- this is all in the 'data-time-zone'
                        var timeString = csv[0];
                        var hours      = timeString.Substring(0, 2).ToInt32();
                        var minutes    = timeString.Substring(3, 2).ToInt32();
                        var seconds    = timeString.Substring(6, 2).ToInt32();
                        var millis     = timeString.Substring(9, 3).ToInt32();
                        var time       = referenceDate.Add(new TimeSpan(0, hours, minutes, seconds, millis));

                        // detail: PUT at 30.0000 on 2014-01-18
                        var underlying = csv[4];

                        //FOR WINDOWS TESTING
                        //if (underlying.Equals("AUX", StringComparison.OrdinalIgnoreCase)
                        //|| underlying.Equals("CON", StringComparison.OrdinalIgnoreCase)
                        //|| underlying.Equals("PRN", StringComparison.OrdinalIgnoreCase))
                        //{
                        //continue;
                        //}

                        var optionRight = csv[5][0] == 'P' ? OptionRight.Put : OptionRight.Call;
                        var expiry      = DateTime.ParseExact(csv[6], "yyyyMMdd", null);
                        var strike      = csv[7].ToDecimal() / 10000m;
                        var optionStyle = OptionStyle.American; // couldn't see this specified in the file, maybe need a reference file
                        var sid         = SecurityIdentifier.GenerateOption(expiry, underlying, Market.USA, strike, optionRight, optionStyle);
                        var symbol      = new Symbol(sid, underlying);

                        var price    = csv[9].ToDecimal() / 10000m;
                        var quantity = csv[8].ToInt32();

                        tick = new Tick
                        {
                            Symbol   = symbol,
                            Time     = time,
                            TickType = eventType.TickType,
                            Exchange = csv[10],
                            Value    = price
                        };
                        if (eventType.TickType == TickType.Quote)
                        {
                            if (eventType.IsAsk)
                            {
                                tick.AskPrice = price;
                                tick.AskSize  = quantity;
                            }
                            else
                            {
                                tick.BidPrice = price;
                                tick.BidSize  = quantity;
                            }
                        }
                        else
                        {
                            tick.Quantity = quantity;
                        }
                    }
                    catch (Exception err)
                    {
                        Log.Error(err);
                        continue;
                    }

                    yield return(tick);
                }
            }
        }