using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Text.RegularExpressions; using System.Xml; using TradeIdeas.XML; namespace TradeIdeas.TIProGUI { public class SymbolTranslation { private static List _appConfig; public enum ExchangeList { Indices, NYSE, AMEX, CAV, OTC, PINK, CAT, NASD, ARCA, ALL, NULL }; public struct SymbolTranslationArgs { public string Pattern; public string Replacement; } public static SymbolTranslationArgs NewRule; public static List[] Rules; public static List SymbolGroupNames = new List(); public static List AltMethodValues = new List(); public static List CutOffValues = new List(); private static int _numberSymbolGroups=0; public static string TranslateSymbol(string GroupName, string Symbol, string Exchange, bool alternateMethod) { string replacementSymbol = Symbol; string prefixedSymbol = Exchange + ":" + Symbol; // if not using alternate method then the symbol should be lower case. if (!alternateMethod) { replacementSymbol = replacementSymbol.ToLower(); prefixedSymbol = prefixedSymbol.ToLower(); } int index = SymbolGroupNames.IndexOf(GroupName); if (index == -1) return replacementSymbol; //return if no groups defined if (Rules[index].Count > 0) { foreach (SymbolTranslationArgs rule in Rules[index]) { try { if (Regex.IsMatch(prefixedSymbol, rule.Pattern, RegexOptions.IgnoreCase)) { replacementSymbol = Regex.Replace(prefixedSymbol, rule.Pattern, rule.Replacement, RegexOptions.IgnoreCase); // At this point we can exit loop so that we apply only one rule change for a symbol //if (!alternateMethod) // replacementSymbol = replacementSymbol.ToLower(); break; } } catch (Exception e) { string debugView = e.StackTrace; } } } //Note: if there are no rules we return the original symbol return replacementSymbol; } /// /// Load the rules from the appConfig files (in particular Common.xml). /// Remember that the rules need to handle the case where the exchange data /// for a symbol is unavailable. /// /// public static void LoadRules(List appConfig) { _appConfig = appConfig.Node("SYMBOL_TRANSLATION"); List _symbolGroupNames = new List(); //local list _numberSymbolGroups = 0; // Read the number of symbol translation groups foreach (XmlElement node in _appConfig.Node("SYMBOL_GROUPS").Enum()) { _symbolGroupNames.Add(node.Property("NAME", null)); // If new names do not exist then use the old NAME property if (String.IsNullOrEmpty(node.Property("TEXT", null))) { SymbolGroupNames.Add(node.Property("NAME", null)); } else { SymbolGroupNames.Add(node.Property("TEXT", null)); } AltMethodValues.Add(node.Property("ALT_METHOD_CHECKED", -1)); CutOffValues.Add(node.Property("CUTOFF_CHECKED", -1)); _numberSymbolGroups++; } Rules = new List[_numberSymbolGroups]; int index = 0; int index1 = 0; foreach (string name in _symbolGroupNames) { Rules[index1] = new List(); // Initialize list index1++; foreach (XmlElement node in _appConfig.Node(name).Enum()) { NewRule.Pattern = ""; NewRule.Replacement = ""; string pattern = node.Property("PATTERN", null); string replacement = node.Property("REPLACEMENT", null); if ((null == pattern) || (null == replacement)) // Problem in XML file. Skip the offending entry. continue; NewRule.Pattern = pattern; NewRule.Replacement = replacement; Rules[index].Add(NewRule); } index++; } } } }