Пример #1
0
        /// <summary>
        /// Adds an option instrument to this builder.
        /// Option is added to chains for the currently set {@link #Product product} and/or
        /// {@link #Underlying underlying} to the {@link OptionSeries series} that corresponding
        /// to all other currently set attributes.This method is safe in the sense that is ignores
        /// illegal state of the builder.It only adds an option when all of the following conditions are met:
        /// <ul>
        ///  <li>{@link #CFI CFI code} is set and starts with either "OC" for call or "OP" for put.
        ///  <li>{@link #SetExpiration(int) expiration} is set and is not zero;
        ///  <li>{@link #Strike strike} is set and is not `Double.NaN` nor `Double.isInfinite()`;
        ///  <li>{@link #Product product} or {@link #Underlying underlying symbol} are set;
        /// </ul>
        /// All the attributes remain set as before after the call to this method, but
        /// {@link #Chains() chains} are updated correspondingly.
        /// </summary>
        /// <param name="option">option to add.</param>
        public void AddOption(InstrumentProfile option)
        {
            bool isCall = CFI.StartsWith("OC");

            if (!isCall && !CFI.StartsWith("OP"))
            {
                return;
            }
            if (series.Expiration == 0)
            {
                return;
            }
            if (double.IsNaN(Strike) || double.IsInfinity(Strike))
            {
                return;
            }
            if (Product.Length > 0)
            {
                GetOrCreateChain(Product).AddOption(series, isCall, Strike, option);
            }
            if (Underlying.Length > 0)
            {
                GetOrCreateChain(Underlying).AddOption(series, isCall, Strike, option);
            }
        }
Пример #2
0
        internal void AddOption(bool isCall, double strike, InstrumentProfile option)
        {
            SortedDictionary <double, InstrumentProfile> map = isCall ? Calls : Puts;

            if (Put(map, strike, option) == null)
            {
                Strikes = null;
            }
        }
Пример #3
0
        internal void AddOption(OptionSeries series, bool isCall, double strike, InstrumentProfile option)
        {
            OptionSeries os;

            if (!seriesMap.TryGetValue(series, out os))
            {
                os = new OptionSeries(series);
                seriesMap.Add(os, os);
            }
            os.AddOption(isCall, strike, option);
        }
Пример #4
0
        /// <summary>
        /// Java's Map like put method.
        /// Associates the specified value with the specified key in this map (optional operation).
        /// If the map previously contained a mapping for the key, the old value is replaced by the specified value.
        /// </summary>
        /// <param name="map">map</param>
        /// <param name="key">key with which the specified value is to be associated</param>
        /// <param name="value">key with which the specified value is to be associated</param>
        /// <returns>the previous value associated with key, or null if there was no mapping for key.
        /// A null return can also indicate that the map previously associated null with key</returns>
        private InstrumentProfile Put(SortedDictionary <double, InstrumentProfile> map, double key, InstrumentProfile value)
        {
            if (!map.ContainsKey(key))
            {
                map.Add(key, value);
                return(null);
            }
            InstrumentProfile oldValue = map[key];

            map[key] = value;
            return(oldValue);
        }
 /// <summary>
 /// Write profile.
 /// </summary>
 /// <param name="type"></param>
 /// <param name="ip"></param>
 /// <exception cref="System.InvalidOperationException">Can't format certain profile.</exception>
 /// <exception cref="System.IO.IOException">If an I/O error occurs.</exception>
 private void WriteProfile(string type, InstrumentProfile ip)
 {
     writer.WriteField(type);
     foreach (InstrumentProfileField field in enumFormats[type])
     {
         writer.WriteField(field.GetField(ip));
     }
     foreach (string field in customFormats[type])
     {
         writer.WriteField(ip.GetField(field));
     }
     writer.WriteRecord(null);
 }
 /// <summary>
 /// Writes profiles from list.
 /// </summary>
 /// <param name="profiles"></param>
 /// <param name="skipRemoved"></param>
 /// <exception cref="System.InvalidOperationException">Can't format certain profile.</exception>
 /// <exception cref="System.IO.IOException">If an I/O error occurs.</exception>
 private void WriteProfiles(IList <InstrumentProfile> profiles, bool skipRemoved)
 {
     for (int i = 0; i < profiles.Count; i++)
     {
         string type = types[i]; // atomically captured
         if (REMOVED_TYPE.Equals(type) && skipRemoved)
         {
             continue;
         }
         InstrumentProfile ip = profiles[i];
         WriteProfile(type, ip);
     }
 }
        /// <summary>
        /// Writes formats from list.
        /// </summary>
        /// <param name="profiles"></param>
        /// <param name="skipRemoved"></param>
        /// <exception cref="System.ArgumentException">If attempt to write record without fields was made.</exception>
        /// <exception cref="System.IO.IOException">If an I/O error occurs.</exception>
        /// <exception cref="System.InvalidOperationException">Can't format profile field.</exception>
        private void WriteFormats(IList <InstrumentProfile> profiles, bool skipRemoved)
        {
            HashSet <string> updated = new HashSet <string>();

            for (int i = 0; i < profiles.Count; i++)
            {
                string type = types[i]; // atomically captured
                if (REMOVED_TYPE.Equals(type) && skipRemoved)
                {
                    continue;
                }
                InstrumentProfile ip = profiles[i];
                HashSet <InstrumentProfileField> enumFormat;
                HashSet <string> customFormat;
                customFormats.TryGetValue(type, out customFormat);
                if (!enumFormats.TryGetValue(type, out enumFormat))
                {
                    updated.Add(type);
                    enumFormat = new HashSet <InstrumentProfileField>();
                    enumFormat.Add(InstrumentProfileField.SYMBOL);
                    // always write symbol (type is always written by a special code)
                    enumFormats[type]   = enumFormat;
                    customFormat        = new HashSet <string>();
                    customFormats[type] = customFormat;
                }
                if (!REMOVED_TYPE.Equals(type))
                {
                    // collect actual used fields for non-removed instrument profiles
                    foreach (InstrumentProfileField ipf in fields)
                    {
                        if (ipf != InstrumentProfileField.TYPE && ipf.GetField(ip).Length > 0)
                        {
                            if (enumFormat.Add(ipf))
                            {
                                updated.Add(type);
                            }
                        }
                    }
                    if (ip.AddNonEmptyCustomFieldNames(customFormat))
                    {
                        updated.Add(type);
                    }
                }
            }
            foreach (string type in updated)
            {
                WriteFormat(type);
            }
        }
Пример #8
0
        /// <summary>
        /// Sets value of this field (in numeric representation) to specified profile.
        /// </summary>
        /// <param name="ip">Profile tos set value.</param>
        /// <param name="value">Value ehich set to field.</param>
        /// <exception cref="System.ArgumentException">If this field has no numeric representation</exception>
        public void SetNumericField(InstrumentProfile ip, double value)
        {
            switch (Name)
            {
            case F_ICB: ip.SetICB((int)value); return;

            case F_SIC: ip.SetSIC((int)value); return;

            case F_MULTIPLIER: ip.SetMultiplier(value); return;

            case F_SPC: ip.SetSPC(value); return;

            case F_EXPIRATION: ip.SetExpiration((int)value); return;

            case F_LAST_TRADE: ip.SetLastTrade((int)value); return;

            case F_STRIKE: ip.SetStrike(value); return;
            }
            throw new ArgumentException("textual field " + this);
        }
Пример #9
0
        /// <summary>
        /// Returns value of this field for specified profile in numeric representation.
        /// </summary>
        /// <param name="ip">Profile from get field.</param>
        /// <returns>Value of this field for specified profile in numeric representation.</returns>
        /// <exception cref="System.ArgumentException">If this field has no numeric representation.</exception>
        public double GetNumericField(InstrumentProfile ip)
        {
            switch (Name)
            {
            case F_ICB: return(ip.GetICB());

            case F_SIC: return(ip.GetSIC());

            case F_MULTIPLIER: return(ip.GetMultiplier());

            case F_SPC: return(ip.GetSPC());

            case F_EXPIRATION: return(ip.GetExpiration());

            case F_LAST_TRADE: return(ip.GetLastTrade());

            case F_STRIKE: return(ip.GetStrike());
            }
            throw new ArgumentException("textual field " + this);
        }
Пример #10
0
 /// <summary>
 /// Make a instrument profile key for hashing inside this class.
 /// </summary>
 /// <param name="ip"></param>
 /// <returns></returns>
 private int GetInstrumentProfileKey(InstrumentProfile ip)
 {
     return((ip.GetType() + ip.GetSymbol()).GetHashCode());
 }
Пример #11
0
        /// <summary>
        /// Sets value of this field (in textual representation) to specified profile.
        /// </summary>
        /// <param name="ip">Profile to set field.</param>
        /// <param name="value">Value that set into field.</param>
        /// <exception cref="System.InvalidOperationException">If text uses wrong format or contains invalid values.</exception>
        public void SetField(InstrumentProfile ip, string value)
        {
            try
            {
                switch (Name)
                {
                case F_TYPE: ip.SetType(value); return;

                case F_SYMBOL: ip.SetSymbol(value); return;

                case F_DESCRIPTION: ip.SetDescription(value); return;

                case F_LOCAL_SYMBOL: ip.SetLocalSymbol(value); return;

                case F_LOCAL_DESCRIPTION: ip.SetLocalDescription(value); return;

                case F_COUNTRY: ip.SetCountry(value); return;

                case F_OPOL: ip.SetOPOL(value); return;

                case F_EXCHANGE_DATA: ip.SetExchangeData(value); return;

                case F_EXCHANGES: ip.SetExchanges(value); return;

                case F_CURRENCY: ip.SetCurrency(value); return;

                case F_BASE_CURRENCY: ip.SetBaseCurrency(value); return;

                case F_CFI: ip.SetCFI(value); return;

                case F_ISIN: ip.SetISIN(value); return;

                case F_SEDOL: ip.SetSEDOL(value); return;

                case F_CUSIP: ip.SetCUSIP(value); return;

                case F_ICB: ip.SetICB((int)ParseNumber(value)); return;

                case F_SIC: ip.SetSIC((int)ParseNumber(value)); return;

                case F_MULTIPLIER: ip.SetMultiplier(ParseNumber(value)); return;

                case F_PRODUCT: ip.SetProduct(value); return;

                case F_UNDERLYING: ip.SetUnderlying(value); return;

                case F_SPC: ip.SetSPC(ParseNumber(value)); return;

                case F_ADDITIONAL_UNDERLYINGS: ip.SetAdditionalUnderlyings(value); return;

                case F_MMY: ip.SetMMY(value); return;

                case F_EXPIRATION: ip.SetExpiration(ParseDate(value)); return;

                case F_LAST_TRADE: ip.SetLastTrade(ParseDate(value)); return;

                case F_STRIKE: ip.SetStrike(ParseNumber(value)); return;

                case F_OPTION_TYPE: ip.SetOptionType(value); return;

                case F_EXPIRATION_STYLE: ip.SetExpirationStyle(value); return;

                case F_SETTLEMENT_STYLE: ip.SetSettlementStyle(value); return;

                case F_PRICE_INCREMENTS: ip.SetPriceIncrements(value); return;

                case F_TRADING_HOURS: ip.SetTradingHours(value); return;

                default: throw new InvalidOperationException("cannot process field " + this);
                }
            }
            catch (Exception exc)
            {
                throw new InvalidOperationException(String.Format("Can't set field '{0}': {1}", this, exc));
            }
        }
Пример #12
0
        /// <summary>
        /// Return next instrument profile.
        /// </summary>
        /// <returns>Next instrument profile.</returns>
        /// <exception cref="System.IO.IOException"></exception>
        /// <exception cref="com.dxfeed.ipf.InstrumentProfileFormatException"></exception>
        public InstrumentProfile Next()
        {
            while (true)
            {
                int      line = reader.GetLineNumber();
                string[] record;
                try
                {
                    record = reader.ReadRecord();
                }
                catch (CSVFormatException csvException)
                {
                    throw new InstrumentProfileFormatException(csvException.Message);
                }
                catch (Exception exc)
                {
                    if (exc is IOException && exc.InnerException is SocketException)
                    {
                        throw exc.InnerException;
                    }

                    throw new IOException("Next failed: " + exc);
                }

                if (record == null) // EOF reached
                {
                    return(null);
                }
                if (record.Length == 0 || record.Length == 1 && String.IsNullOrEmpty(record[0])) // skip empty lines
                {
                    continue;
                }
                if (record[0].StartsWith(Constants.METADATA_PREFIX))
                {
                    switch (record[0])
                    {
                    case Constants.FLUSH_COMMAND:
                        RaiseOnFlush();
                        break;

                    case Constants.COMPLETE_COMMAND:
                        RaiseOnComplete();
                        break;
                    }
                    if (!record[0].EndsWith(Constants.METADATA_SUFFIX)) // skip comments
                    {
                        continue;
                    }
                    // detected valid metadata record - parse and remember new format
                    object[] newFormat = new object[record.Length];
                    newFormat[0] = InstrumentProfileField.TYPE;
                    for (int i = 1; i < record.Length; i++)
                    {
                        if ((newFormat[i] = InstrumentProfileField.Find(record[i])) == null)
                        {
                            newFormat[i] = record[i];
                        }
                    }
                    string key = record[0].Substring(
                        Constants.METADATA_PREFIX.Length,
                        record[0].Length - Constants.METADATA_SUFFIX.Length - Constants.METADATA_PREFIX.Length);
                    formats[key] = newFormat;
                    continue;
                }
                // detected instrument profile record - parse and remember new profile
                object[] format = formats[record[0]];
                if (format == null)
                {
                    throw new InstrumentProfileFormatException("undefined format " + record[0] + " (line " + line + ")");
                }
                if (record.Length != format.Length)
                {
                    throw new InstrumentProfileFormatException("wrong number of fields (line " + line + ")");
                }
                InstrumentProfile ip = new InstrumentProfile();
                for (int i = 0; i < format.Length; i++)
                {
                    try
                    {
                        if (format[i].GetType() == typeof(InstrumentProfileField))
                        {
                            InstrumentProfileField field = (InstrumentProfileField)format[i];
                            field.SetField(ip, record[i]);
                        }
                        else
                        {
                            ip.SetField((string)format[i], record[i]);
                        }
                    }
                    catch (Exception e)
                    {
                        throw new InstrumentProfileFormatException(e.Message + " (line " + line + ")");
                    }
                }
                return(ip);
            }
        }