예제 #1
0
        /// <summary>
        ///     Add a new instrument
        /// </summary>
        /// <param name="instrument"></param>
        /// <param name="saveChanges">Set to true if saving to db should be done.</param>
        /// <returns>True if the insertion or update succeeded. False if it did not.</returns>
        /// <exception cref="ArgumentException"></exception>
        public async Task <Instrument> AddInstrument(Instrument instrument, bool saveChanges = true)
        {
            //Check if an instrument with these unique constraints already exists
            var existingInstrument = Context.Instruments.SingleOrDefault(x =>
                                                                         x.ID == instrument.ID ||
                                                                         x.Symbol == instrument.Symbol &&
                                                                         x.DatasourceID == instrument.DatasourceID &&
                                                                         x.ExchangeID == instrument.ExchangeID &&
                                                                         x.Expiration == instrument.Expiration &&
                                                                         x.Type == instrument.Type);

            if (existingInstrument != null)
            {
                throw new ArgumentException("Unique constraint violation");
            }

            ValidateInstrument(instrument);

            instrument.Datasource = Context.GetAttachedEntity(instrument.Datasource);
            instrument.Exchange   = Context.GetAttachedEntity(instrument.Exchange);

            instrument.Tags = new List <Tag>()
            {
                new Tag()
                {
                    Name = GetDescriptionHelper.GetDescription(instrument.Type, "FUTURE")
                }
            };
            instrument.ExpirationRuleID = 1;

            AddSessionToInstrument(instrument);

            Context.Instruments.Add(instrument);
            if (saveChanges)
            {
                await Context.DbContext.SaveChangesAsync().ConfigureAwait(false);
            }

            logger.Info($"Instrument Manager: successfully added instrument {instrument}");

            return(instrument);
        }
예제 #2
0
        /// <summary>
        /// Add a new instrument
        /// </summary>
        /// <param name="instrument"></param>
        /// <param name="saveChanges">Set to true if saving to db should be done.</param>
        /// <returns>True if the insertion or update succeeded. False if it did not.</returns>
        /// <exception cref="ArgumentException"></exception>
        public async Task <Instrument> AddInstrument(Instrument instrument, bool saveChanges = true)
        {
            //Check if an instrument with these unique constraints already exists
            var existingInstrument = Context.Instruments.SingleOrDefault(x =>
                                                                         (x.ID == instrument.ID) ||
                                                                         (x.Symbol == instrument.Symbol &&
                                                                          x.DatasourceID == instrument.DatasourceID &&
                                                                          x.ExchangeID == instrument.ExchangeID &&
                                                                          x.Expiration == instrument.Expiration &&
                                                                          x.Type == instrument.Type));

            if (existingInstrument != null)
            {
                //throw new ArgumentException("Unique constraint violation");
            }

            ValidateInstrument(instrument);

            //All this stuff is detached, so we need to get the attached objects
            instrument.Datasource      = Context.GetAttachedEntity(instrument.Datasource);
            instrument.Exchange        = Context.GetAttachedEntity(instrument.Exchange);
            instrument.PrimaryExchange = Context.GetAttachedEntity(instrument.PrimaryExchange);
            instrument.Tags            = instrument.Tags != null
                ? new List <Tag>(instrument.Tags.Select(Context.GetAttachedEntity).ToList())
                : new List <Tag>();

            //If necessary, load sessions from teplate or exchange
            if (instrument.SessionsSource == SessionsSource.Exchange && instrument.Exchange != null)
            {
                instrument.Sessions = instrument.Exchange.Sessions.Select(x => x.ToInstrumentSession()).ToList();
            }
            else if (instrument.SessionsSource == SessionsSource.Exchange && instrument.Exchange == null)
            {
                instrument.SessionsSource = SessionsSource.Custom;
                instrument.Sessions       = new List <InstrumentSession>();
            }
            else if (instrument.SessionsSource == SessionsSource.Template)
            {
                instrument.Sessions = new List <InstrumentSession>();
                var template = Context.SessionTemplates.Include(x => x.Sessions).FirstOrDefault(x => x.ID == instrument.SessionTemplateID);
                if (template != null)
                {
                    foreach (TemplateSession s in template.Sessions)
                    {
                        instrument.Sessions.Add(s.ToInstrumentSession());
                    }
                }
            }

            //Continuous future requires a bit of a workaround
            ContinuousFuture tmpCf = null;

            if (instrument.IsContinuousFuture)
            {
                tmpCf = instrument.ContinuousFuture; //EF can't handle circular references, so we hack around it
                instrument.ContinuousFuture   = null;
                instrument.ContinuousFutureID = null;
            }

            Context.Instruments.Add(instrument);
            if (saveChanges)
            {
                await Context.SaveChangesAsync().ConfigureAwait(false);
            }

            if (tmpCf != null)
            {
                tmpCf.UnderlyingSymbol = Context.GetAttachedEntity(tmpCf.UnderlyingSymbol);

                instrument.ContinuousFuture              = tmpCf;
                instrument.ContinuousFuture.Instrument   = instrument;
                instrument.ContinuousFuture.InstrumentID = instrument.ID.Value;
                if (saveChanges)
                {
                    await Context.SaveChangesAsync().ConfigureAwait(false);
                }
            }

            _logger.Info($"Instrument Manager: successfully added instrument {instrument}");

            return(instrument);
        }