// ***************************************************************** // **** Properties **** // ***************************************************************** // // #endregion//Properties #region no Public Methods // ***************************************************************** // **** Public Methods **** // ***************************************************************** // // // // // // #endregion//Public Methods #region Private Methods // ***************************************************************** // **** Private Methods **** // ***************************************************************** // // private void buttonStart_Click(object sender, EventArgs e) { m_HedgeOptionsWriter = new HedgeOptionsWriter(m_Log); m_InstrumentExistInDatabase = new List <InstrumentName>(); m_InstrumentPendingWriting = new List <InstrumentName>(); m_BREInstrumentInfoItems = new Dictionary <InstrumentName, InstrumentInfoItem>(); // Generate query to get all instruments from the data base. InstrumentInfoQuery BREInstrumentInfoQuery = new InstrumentInfoQuery(); ProductTypes productTypes; if (Enum.TryParse <ProductTypes>(textBoxProductType.Text, out productTypes)) { m_Product = new Product(textBoxExchangeName.Text, textBoxProductName.Text, productTypes); BREInstrumentInfoQuery.InstrumentName = new InstrumentName(m_Product, string.Empty); BREInstrumentInfoQuery.IsRead = true; BREInstrumentInfoQuery.Status = QueryStatus.New; } else { m_Log.NewEntry(LogLevel.Error, "Product type parse failed"); return; } // Submit query and store existing instruments from the data base. m_DatabaseReaderWriter.SubmitSync(BREInstrumentInfoQuery); List <InstrumentInfoItem> BREInstrumentInfoItems = BREInstrumentInfoQuery.Results; foreach (InstrumentInfoItem BREInstrumentInfoItem in BREInstrumentInfoItems) { if (m_BREInstrumentInfoItem == null) { m_BREInstrumentInfoItem = BREInstrumentInfoItem; // Need one of this only. } string instrumentNameString = BREInstrumentInfoItem.InstrumentNameTT; InstrumentName instrumentName; if (instrumentNameString != null && InstrumentName.TryDeserialize(instrumentNameString, out instrumentName)) { m_InstrumentExistInDatabase.Add(instrumentName); m_BREInstrumentInfoItems.Add(instrumentName, BREInstrumentInfoItem); } } // Launch market instruments request. m_Market.RequestProducts(new List <Product>() { m_Product }); m_Market.RequestInstruments(m_Product); }
// // private void buttonWrite_Click(object sender, EventArgs e) { List <InstrumentInfoQuery> BREInstrumentInfoQuerys = new List <InstrumentInfoQuery>(); foreach (InstrumentName instrumentName in m_InstrumentPendingWriting) { if (m_BREInstrumentInfoItems.ContainsKey(instrumentName)) { InstrumentInfoItem BREInstrumentInfoItem = m_BREInstrumentInfoItems[instrumentName]; InstrumentInfoQuery BREInstrumentInfoQuery = new InstrumentInfoQuery(); BREInstrumentInfoQuery.InstrumentName = instrumentName; BREInstrumentInfoQuery.IsRead = false; BREInstrumentInfoQuery.Status = QueryStatus.New; BREInstrumentInfoQuery.Results.Add(BREInstrumentInfoItem); if (!m_InstrumentExistInDatabase.Contains(instrumentName)) { BREInstrumentInfoQuerys.Add(BREInstrumentInfoQuery); } } else { m_Log.NewEntry(LogLevel.Error, "No instrument found for instrument {0}.", instrumentName); return; } } foreach (InstrumentInfoQuery BREInstrumentInfoQuery in BREInstrumentInfoQuerys) { m_DatabaseReaderWriter.SubmitAsync(BREInstrumentInfoQuery); } m_UIDispatcher.Invoke( new Action(() => { this.buttonWrite.Enabled = false; }) ); }
// ***************************************************************** // **** Private Methods **** // ***************************************************************** // // // // /// <summary> /// This function will generate resulting instruments for the quote instrument. /// Also, it will generate the ratios and hedge instruments in that resulting instrument. /// </summary> /// <param name="instrumentInfoItem"></param> /// <returns></returns> public bool TryGenerateHedgeOptions(InstrumentName quoteInstrument, InstrumentInfoItem instrumentInfoItem, HedgeOptions hedgeOptions) { bool isSuccess = false; string hedgeOptionString = instrumentInfoItem.HedgeOptions; // Consider the target example: // 1x{1xGE_U4.-1xGE_Z4}-1x{1xGE_Z4.-1xGE_H5}=1x{1xGE_U4.-2xGE_Z4.1xGE_H5}|-1x{1xGE_U4.-1xGE_Z4}+1x{1xGE_M4.-1xGE_U4}=1x{1xGE_M4.-2xGE_U4.1xGE_Z4} // |2x{1xGE_U4.-1xGE_Z4}-1x{1xGE_U4.-1xGE_H5}=1x{1xGE_U4.-2xGE_Z4.1xGE_H5}|-2x{1xGE_U4.-1xGE_Z4}+1x{1xGE_M4.-1xGE_Z4}=1x{1xGE_M4.-2xGE_U4.1xGE_Z4} // |-3x{1xGE_U4.-1xGE_Z4}+1x{1xGE_M4.-1xGE_H5}=1x{1xGE_M4.-3xGE_U4.3xGE_Z4.-1xGE_H5} // First step: Parse different hedges and resulting instruments. char seperator = '|'; string[] hedgeOptionUnits = hedgeOptionString.Split(new char[] { seperator }, StringSplitOptions.RemoveEmptyEntries); if (hedgeOptionUnits.Length > 0) { string exchangeName = quoteInstrument.Product.Exchange; List <string> matchedResults = new List <string>(); foreach (string hedgeOptionUnit in hedgeOptionUnits) { // Second step: Parse hedge instruments and resulting instruments. // Target: 1x{1xGE_U4.-1xGE_Z4}-1x{1xGE_Z4.-1xGE_H5}=1x{1xGE_U4.-2xGE_Z4.1xGE_H5} seperator = '='; string[] hedgeAndResultingInstruments = hedgeOptionUnit.Split(new char[] { seperator }, StringSplitOptions.RemoveEmptyEntries); if (hedgeAndResultingInstruments.Length == 2) { HedgeOption hedgeOption = null; bool isQuoteInstrument = false; // Quote instrument always the first. // Third step: Parse hedge instruments further by their weights. // Target: 1x{1xGE_U4.-1xGE_Z4}-1x{1xGE_Z4.-1xGE_H5} string hedgeInstrumentsPart = hedgeAndResultingInstruments[0]; string pattern = @"-?\d+x{[^}]+}"; matchedResults.Clear(); MatchCollection matchCollection = Regex.Matches(hedgeInstrumentsPart, pattern); foreach (Match match in matchCollection) { string matchedValue = match.Value; matchedResults.Add(matchedValue); } // Fourth step: Parse weight and instrument name for each hedge instrument. // Target: 1x{1xGE_U4.-1xGE_Z4} foreach (string matchedInstrument in matchedResults) { int weight = 0; // 1. Weight: string firstPart = matchedInstrument.Substring(0, matchedInstrument.IndexOf('x')); if (int.TryParse(firstPart, out weight) && weight != 0) { // 2. Name: pattern = @"\{(?<hedgeName>\S+)\}"; Match match = Regex.Match(matchedInstrument, pattern); string name = match.Groups["hedgeName"].Value; // Fifth step: Parse instrument name out. //// There may be alternative way to do it. The data base contains the instrument name TT and we may use that. // Target: 1xGE_U4.-1xGE_Z4 InstrumentName instrumentComposed = quoteInstrument; if (QTMath.TryComposeInstrumentName(name, quoteInstrument, m_Log, out instrumentComposed)) { if (!isQuoteInstrument) { hedgeOption = new HedgeOption(); hedgeOption.QuoteInstrument = instrumentComposed; hedgeOption.QuoteWeight = weight; hedgeOption.TryAddInstrumentAndWeight(instrumentComposed, weight); isQuoteInstrument = true; } else { if (instrumentComposed != quoteInstrument) { hedgeOption.TryAddInstrumentAndWeight(instrumentComposed, weight); } else { m_Log.NewEntry(LogLevel.Error, "instrument composed same as quote instrument {0}.", instrumentComposed); return(isSuccess); } } } else { m_Log.NewEntry(LogLevel.Error, "Failed to compose instrument name for string {0}.", name); return(isSuccess); } } else { m_Log.NewEntry(LogLevel.Error, "instrument weight parse failed for {0}.", matchedInstrument); return(isSuccess); } } // If the hedge option exists. if (hedgeOption != null) { // Sixth step: Continue to read the resulting instrument if hedge option is valid. // Target: 1x{1xGE_U4.-2xGE_Z4.1xGE_H5} string resultingInstrumentPart = hedgeAndResultingInstruments[1]; // 1.Weight: string weightString = resultingInstrumentPart.Substring(0, resultingInstrumentPart.IndexOf('x')); int resultingWeight = 0; if (int.TryParse(weightString, out resultingWeight) && resultingWeight != 0) { // 2.Name: pattern = @"\{(?<resultingName>\S+)\}"; Match match = Regex.Match(resultingInstrumentPart, pattern); string resultingInstrumentName = match.Groups["resultingName"].Value; ResultingInstrument resultingInstrument = new ResultingInstrument(); resultingInstrument.ResultingInstrumentNameDataBase = resultingInstrumentName; hedgeOption.ResultingInstrument = resultingInstrument; hedgeOption.ResultingWeight = resultingWeight; // 3. Compose resulting instrument name: InstrumentName instrumentComposed = quoteInstrument; if (QTMath.TryComposeInstrumentName(resultingInstrumentName, quoteInstrument, m_Log, out instrumentComposed)) { resultingInstrument.ResultingInstrumentName = instrumentComposed; resultingInstrument.ResultingInstrumentNameTT = instrumentComposed.FullName; if (!hedgeOptions.TryAddHedgeOption(hedgeOption)) { m_Log.NewEntry(LogLevel.Error, "Failed to add hedge option for instrument {0}.", quoteInstrument); return(isSuccess); } } else { m_Log.NewEntry(LogLevel.Error, "Failed to compose instrument name for string {0}.", resultingInstrumentName); return(isSuccess); } } } } else { m_Log.NewEntry(LogLevel.Error, "hedge and resulting instruments parse failed for string {0}.", hedgeOptionUnit); return(isSuccess); } } isSuccess = true; } else { m_Log.NewEntry(LogLevel.Error, "hedge options parse failed for string {0}.", hedgeOptionString); return(isSuccess); } return(isSuccess); }