Пример #1
0
 public static MarketSnap GetOrCreateMarketSnap(string marketSnapCode, DateTime snapDate, SymmetryEntities dc, ThrowBehavior behavior = ThrowBehavior.Throw)
 {
     try
     {
         var schedule = dc.MarketSnapSchedules.Single(s => s.MarketSnapCode.Equals(marketSnapCode));
         var snap = dc.MarketSnaps.SingleOrDefault(s => s.MarketSnapSchedule.MarketSnapCode.Equals(marketSnapCode, StringComparison.InvariantCultureIgnoreCase) &&
             s.ValueDate == snapDate);
         if (snap == null)
         {
             snap = new MarketSnap
             {
                 ValueDate = snapDate,
                 MarketSnapSchedule = schedule
             };
             dc.MarketSnaps.Add(snap);
         }
         return snap;
     }
     catch (Exception ex)
     {
         Exceptions.Rethrow("GetOrCreateMarketSnap", behavior, ex);
     }
     return null;
 }
    private bool saveResponses(IList<ExtractClass> items_, SymmetryEntities dc_, MarketSnap snap_)
    {
      if (items_ == null || !items_.Any())
        return false;

      var symmetryCodeToPrices = new SortedDictionary<string, decimal?[]>();

      int objectLength = Enum.GetValues(typeof(RIC_SOURCE)).Length;

      foreach (var response in items_.Where(x=>x.BidPrice.HasValue && x.AskPrice.HasValue))
      {
        try
        {
          var fi = FIHelpers.GetFIByAnyIdentifier(response.RIC, dc_, ThrowBehavior.Throw);

          // extract bid / ask price from line

          var scaledValue = Convert.ToDecimal(response.BidPrice.Value + response.AskPrice.Value)/2/100;

          // save this particular source of a price

          var ricSource = RICHelper.GetSourceFromRIC(response.RIC);
          SaveQuote(fi.FIID, QuoteValueType.Price, getQuoteSource(ricSource, dc_), scaledValue, dc_, snap_);

          // get array to store

          decimal?[] values;

          if (!symmetryCodeToPrices.ContainsKey(fi.SymmetryCode))
          {
            values = new decimal?[objectLength];
            symmetryCodeToPrices[fi.SymmetryCode] = values;
          }
          else
          {
            values = symmetryCodeToPrices[fi.SymmetryCode];
          }

          // if the bidaskdate isn't the snapDate (i.e. =stale price), then don't want to include it in the average
          if (response.BidAskDate.HasValue && response.BidAskDate.Value.Date < snap_.ValueDate)
          {
            SLog.log.InfoFormat(
              "Not going to include quote with RIC=[{0}] as snapDate is {1} which is stale relative to {2}",
              response.RIC,
              response.BidAskDate.Value,
              snap_.ValueDate);

            continue;
          }

          // store in array to take average later

          RIC_SOURCE src = RICHelper.GetRICsourceFromRIC(response.RIC);
          values[(int) src] = scaledValue;
        }
        catch (Exception ex_)
        {
          SLog.log.Error(
            string.Format("Error extracting {0} pricing values from Datascope response row ({1})", sName, response.RIC),
            ex_);
        }
      }

      foreach (var code in symmetryCodeToPrices.Keys)
      {
        var nonZeros = symmetryCodeToPrices[code].Where(x => x.HasValue);

        if (!nonZeros.Any()) continue;

        decimal averagedValue = nonZeros.Select(x => x.Value).Average();

        var fi = FIHelpers.GetFIBySymmetryCode(code, dc_);

        // save quote to 'SYM' for this market snap
        SaveQuote(
          fiid_: fi.FIID,
          quoteType_: QuoteValueType.Price,
          quoteSource_: getQuoteSource(_symmetryQuoteSource, dc_),
          value_: averagedValue,
          dc_: dc_,
          snap_: snap_);
      }

      return true;
    }
    protected override void processResponsesFromDataScope(SymmetryEntities dc_, MarketSnap snap_, string marketSnapCode_)
    {
      OutputToCSV(m_resultsFromDataScope);

      var list = new List<ExtractClass>();

      foreach(var response in m_resultsFromDataScope)
      {
        foreach(var row in response.Rows)
        {
          try
          {
            var ric = GetValueFromObjectArray<string>((int)FieldsToReturn.RIC, row, ThrowBehavior.Throw);

            // extract bid / ask price from line

            var bid = GetValueFromObjectArray<double>((int)FieldsToReturn.BidPrice, row, ThrowBehavior.Throw);
            var ask = GetValueFromObjectArray<double>((int)FieldsToReturn.AskPrice, row, ThrowBehavior.Throw);

            list.Add(new ExtractClass()
            {
              RIC = ric,
              BidPrice = bid,
              AskPrice = ask
            });
          }
          catch (Exception ex_)
          {
            SLog.log.Error(string.Format("Error extracting {0} pricing values from Datascope response row ({1})", sName, ServiceHelpers.ToCsvLine(row)), ex_);
          }
        }
      }

      saveResponses(list, dc_, snap_);
    }
    protected override void processResponsesFromDataScope(SymmetryEntities dc_, MarketSnap snap_, string marketSnapCode_)
    {
      OutputToCSV(m_resultsFromDataScope);

      var list = new List<ExtractClass>();

      foreach (var response in m_resultsFromDataScope)
      {
        foreach (var row in response.Rows)
        {
          try
          {
            // extract values
            var ric = GetValueFromObjectArray<string>((int)FieldsToReturn.RIC, row, ThrowBehavior.Throw);
            var fi = m_ric_to_FIs[ric];
            var fix = GetValueFromObjectArray<double>((int)FieldsToReturn.UniversalClosePrice, row,ThrowBehavior.Throw);
            var closeDate = GetValueFromObjectArray<DateTime>((int)FieldsToReturn.UniversalClosePriceDate, row, ThrowBehavior.Throw);

            list.Add(new ExtractClass()
            {
              RIC = ric,
              Value = fix,
              CloseDate = closeDate
            });
          }
          catch (Exception ex_)
          {
            SLog.log.Error(string.Format("Error extracting {0} pricing values from Datascope response row ({1})", sName, ServiceHelpers.ToCsvLine(row)), ex_);
          }
        }
      }
      saveValues(list, dc_, snap_.MarketSnapSchedule.MarketSnapCode);
    }
Пример #5
0
 public abstract void SaveResults(SymmetryEntities dc_, MarketSnap snapToSaveTo_, string marketSnapCode_);
			public EDItem(FI databaseObject_, SymmetryEntities dc_, MarketSnap snap_, QuoteSource source_)
			{
				Underlying = databaseObject_;
				Snap = snap_;


				var px = QuoteHelpers.GetQuoteValue(snap_.MarketSnapID, source_.QuoteSourceID, databaseObject_.FIID, QuoteValueType.Price, dc_, ThrowBehavior.DontThrow);

				if (px.HasValue)
				{
					ClosePrice = Convert.ToDouble(px.Value);

					SLog.log.DebugFormat("{0} px = {1}", databaseObject_.SymmetryCode, ClosePrice);

					var opt1Strike = ClosePrice - (ClosePrice % 0.125);
					OptionBelowPrice = new EDOptionItem(databaseObject_, opt1Strike, ClosePrice, Underlying, snap_.ValueDate);

					SLog.log.DebugFormat("{0} belowStrikeToQueryFor = {1}", databaseObject_.SymmetryCode, opt1Strike);

					var opt2Strike = opt1Strike + 0.125;
					OptionAbovePrice = new EDOptionItem(databaseObject_, opt2Strike, ClosePrice, Underlying, snap_.ValueDate);

					SLog.log.DebugFormat("{0} aboveStrikeToQueryFor = {1}", databaseObject_.SymmetryCode, opt2Strike);
				}
				else
				{
					SLog.log.ErrorFormat("No price saved for {0} for marketSnapID = {1}", databaseObject_.SymmetryCode,
						snap_.MarketSnapID);
				}
			}
		protected override void processResponsesFromDataScope(SymmetryEntities dc_, MarketSnap saveToThisSnap_, string marketSnapCode_)
		{
			OutputToCSV(m_resultsFromDataScope);

			foreach (var response in m_resultsFromDataScope)
			{
				foreach (var row in response.Rows)
				{
					try
					{
						var ric = GetValueFromObjectArray<string>((int)FieldsToReturn.RIC, row, ThrowBehavior.Throw);
						var optionItem = m_ricToOptionItem[ric];

						double price = 0.0025; // this is the default value as the closest to zero that 

						try
						{
							var date = GetValueFromObjectArray<DateTime>((int)FieldsToReturn.UniversalClosePriceDate, row, ThrowBehavior.Throw);

							if (date.Date != m_markDate)
							{
								SLog.log.Error(string.Format("Too late for markDate.  Process was run for MarkDate={0} but datascope values are for {1}", m_markDate.ToString("dd-MMM-yyyy"), date.ToString("dd-MMM-yyyy")));
							}
							else
							{
								var rowPrice = GetValueFromObjectArray<double>((int)FieldsToReturn.UniversalClosePrice, row, ThrowBehavior.Throw);
								price = rowPrice;
							}

						}
						catch
						{
							SLog.log.Debug(string.Format("{0}-no price returned-will default to 0.0025", ric));
						}

						SLog.log.DebugFormat("{0} price = {1}", optionItem.ReutersTicker, price);

						optionItem.Price = price;
					}
					catch (Exception ex_)
					{
						SLog.log.Error(string.Format("Error extracting {0} pricing values from Datascope response row ({1})", sName, ServiceHelpers.ToCsvLine(row)), ex_);
					}
				}
			}

			foreach (var item in m_items)
			{
				var convexity = item.Convexity;

				if (convexity.HasValue)
				{
					var saveValue = Convert.ToDecimal(convexity.Value / 10000d);

					SaveQuote(item.Underlying.FIID, QuoteValueType.ImpliedVol, getQuoteSource(_symmetryQuoteSource, dc_), saveValue, dc_, item.Snap);

					SaveQuoteAsMLPMLP(item.Underlying.FIID, QuoteValueType.ImpliedVol, saveValue, dc_, item.Snap.ValueDate);
				}
			}
		}
Пример #8
0
        public static bool SaveQuote(MarketSnap snap, int sourceID, int fiid, QuoteValueType valueType, decimal value, DateTime? lastUpdated, SymmetryEntities dc, ThrowBehavior behavior = ThrowBehavior.Throw, bool overwrite = true)
        {
            bool saved = false;
            //FIChangeTracking.DisableChangeTracking(ref dc);
            try
            {
                var existingQuote = dc.Quotes.SingleOrDefault(q => q.MarketSnapID == snap.MarketSnapID && q.QuoteSourceID == sourceID && q.FIID == fiid);
                if (existingQuote == null)
                {
                    existingQuote = new Quote
                    {
                        LastUpdated = lastUpdated ?? DateHelpers.GetNow(),                         
                        FIID = fiid,
                        QuoteSourceID = sourceID,
                        MarketSnap = snap,
                        MarketSnapID = snap.MarketSnapID
                    };
                    dc.Quotes.Add(existingQuote);
                    dc.SaveChanges();
                }

                var existingQuoteValue = dc.QuoteValues.SingleOrDefault(q => q.QuoteID == existingQuote.QuoteID && q.QuoteValueType == valueType);
                if (existingQuoteValue != null && overwrite)
                {
                    if (existingQuoteValue.Value != value)
                    {
                        existingQuoteValue.Value = value;
                        existingQuote.LastUpdated = lastUpdated ?? DateHelpers.GetNow();
                        //FIChangeTracking.SetModified(existingQuoteValue, dc);
                        saved = true;
                    }
                }
                else
                {
                    dc.QuoteValues.Add(new QuoteValue
                    {
                        QuoteValueType = valueType,
                        Value = value,
                        QuoteID = existingQuote.QuoteID
                    });
                    saved = true;
                }
                dc.SaveChanges();
            }
            catch (Exception ex)
            {
                Exceptions.Rethrow("SaveQuote", behavior, ex);
            }
            finally
            {
                //FIChangeTracking.EnableChangeTracking(ref dc);
            }
            return saved;
        }
    private bool saveResponses(IList<ExtractClass> items_, SymmetryEntities dc_, MarketSnap snap_)
    {
      if (items_ == null || !items_.Any())
        return false;

      // want to save everything as 'SYM'
      QuoteSource symQS = getQuoteSource(_symmetryQuoteSource, dc_);


      foreach (var v in items_.Where(x => !string.IsNullOrEmpty(x.Identifier)))
      {
        try
        {
          var fi = FIHelpers.GetFIBySymmetryCode(v.Identifier, dc_, ThrowBehavior.DontThrow);

          if (fi == null && !string.IsNullOrEmpty(v.ISIN))
            fi = FIHelpers.GetFIByIdentifier(v.ISIN, IdentifierType.ISIN, dc_, ThrowBehavior.DontThrow);

          if (fi == null && v.Identifier.Contains("=")) // could be a RIC that was sent in
          {
            fi = FIHelpers.GetFIByAnyIdentifier(v.Identifier.Split('=')[0], dc_, ThrowBehavior.Throw);
          }

          if (fi == null)
          {
            SLog.log.ErrorFormat(
              "Could not find fi for the given pricing line.  Will not be able to save any prices. [File Identifer={0}]",
              fi.SymmetryCode);
            continue;
          }

          // maintain RIC code
          if(!string.IsNullOrEmpty(v.RIC))
          {
            var currentRICId = FIHelpers.GetFIIdentifier(v.Identifier, IdentifierType.RIC, dc_, ThrowBehavior.DontThrow);

            if (string.IsNullOrEmpty(currentRICId) || String.Compare(currentRICId, v.RIC, StringComparison.OrdinalIgnoreCase) != 0)
              FIBondHelpers.SetOrUpdateIdentifier(fi, IdentifierType.RIC, v.RIC, dc_);
          }

          var ricSource = RICHelper.GetSourceFromRIC(v.RIC);

          // save price and yield (from setup)
          {
            var qs = getQuoteSource(ricSource, dc_);

            // sometimes we only get yield values in both price/yield fields, so we have to go 
            // on the magnitude of the value to determine what it is that has been sent.
            // if we get a yield through only, then we need to calculate the price by a call to carbon

            decimal? yield = null;
            decimal? price = null;

            // look at the price first
            {
              if (v.BidPrice.HasValue && v.AskPrice.HasValue)
              {
                var mid = Convert.ToDecimal(v.BidPrice.Value + v.AskPrice.Value) / 2M;

                // if the value is less than 10, then assume it's a yield
                if (Math.Abs(mid) < 10M)
                {
                  yield = mid * 0.01M;
                }
                else
                {
                  price = mid;
                }
              }
            }

            {
              if (v.BidYield.HasValue)
              {
                var mid = v.AskYield.HasValue
                  ? Convert.ToDecimal(v.BidYield.Value + v.AskYield.Value)/2M
                  : Convert.ToDecimal(v.BidYield.Value);

                // if the value is less than 10, then assume it's a yield
                if (Math.Abs(mid) < 10M)
                {
                  yield = mid * 0.01M;
                }
                else
                {
                  price = mid;
                }
              }
            }

            // if we only have a yield, then calc the price
            if (yield.HasValue && !price.HasValue)
            {
              try
              {
                var result = BondSpreadRetriever.GetSpreads(
                bond_: fi.FIBond,
                priceType_: QuoteValueType.Yield,
                priceValue_: Convert.ToDouble(yield.Value),
                curveType_: SwapCurveType.USD3mLibor,
                asOf_: snap_.ValueDate.Date.AddHours(23).AddMinutes(59),
                client_: CarbonDataSource.GetPersistedInstance());

                if (result != null && result.Price.HasValue)
                  price = Convert.ToDecimal(result.Price.Value * 100d);
              }
              catch (Exception ex_)
              {
                SLog.log.Error(string.Format("Error calculating price for bond with identifier = {0}", v.Identifier), ex_);
              }
            }

            // have we got anything to save?

            if (yield.HasValue)
            {
              // save value for provider (e.g. TWEB / ICAP)
              if(qs!=null) SaveQuote(fi.FIID, QuoteValueType.Yield, qs, yield.Value, dc_, snap_);
              // save value for 'us' (e.g. SYM)
              SaveQuote(fi.FIID, QuoteValueType.Yield, symQS, yield.Value, dc_, snap_);
            }

            if (price.HasValue)
            {
              // save value for provider (e.g. TWEB / ICAP)
              if(qs!=null) SaveQuote(fi.FIID, QuoteValueType.Price, qs, price.Value, dc_, snap_);
              // save value for 'us' (e.g. SYM)
              SaveQuote(fi.FIID, QuoteValueType.Price, symQS, price.Value, dc_, snap_);
            }

          }
        }
        catch (Exception ex_)
        {
          SLog.log.Error(string.Format("Error extracting values for row with identifier = {0}", v.Identifier), ex_);
        }
      }

      return true;
    }
    protected override void processResponsesFromDataScope(SymmetryEntities dc_, MarketSnap snapToSaveTo_, string marketSnapCode_)
    {
      // output to CSV locally 
      OutputToCSV(m_resultsFromDataScope);

      var list = new List<ExtractClass>();

      var extractSetups = new[]
      {
          new extractSetup
          {
            QT=QuoteValueType.Price,
            AskField=FieldsToReturn.AskPrice,
            BidField=FieldsToReturn.BidPrice,
            Scalar=1
          },
          new extractSetup
          {
            QT=QuoteValueType.Yield,
            AskField=FieldsToReturn.AskYield,
            BidField=FieldsToReturn.BidYield,
            Scalar=1
          }
      };
      foreach(var response in m_resultsFromDataScope)
      {
        foreach (var row in response.Rows)
        {
          try
          {
            var isin = GetValueFromObjectArray<string>((int)FieldsToReturn.OriginalIdentifier, row, ThrowBehavior.Throw);
            var ric = GetValueFromObjectArray<string>((int)FieldsToReturn.RIC, row, ThrowBehavior.Throw);

            var status = GetValueFromObjectArray<string>((int) FieldsToReturn.AssetStatus, row, ThrowBehavior.DontThrow);
            
            var saveItem = new ExtractClass()
            {
              Identifier = isin,
              RIC = ric,
              AssetStatus=status
            };

            // save price and yield (from setup)
            foreach (var setupItem in extractSetups)
            {
              var bidprice = GetValueFromObjectArray<double>((int)setupItem.BidField, row, ThrowBehavior.Throw);
              var askprice = GetValueFromObjectArray<double>((int)setupItem.AskField, row, ThrowBehavior.Throw);

              if (setupItem.QT == QuoteValueType.Price)
              {
                saveItem.AskPrice = askprice;
                saveItem.BidPrice = bidprice;
              }
              else
              {
                saveItem.AskYield = askprice;
                saveItem.BidYield = bidprice;
              }

              list.Add(saveItem);
            }
          }
          catch (Exception ex_)
          {
            SLog.log.Error(string.Format("Error extracting {0} pricing values from Datascope response row ({1})", sName, ServiceHelpers.ToCsvLine(row)), ex_);
          }
        }
      }
      saveResponses(list, dc_, snapToSaveTo_);
    }