/// <summary> /// Parses MIB file to symbol list. /// </summary> /// <param name="file">File</param> /// <param name="stream">File stream</param> private void Parse(string file, TextReader stream) { if (stream == null) { throw new ArgumentNullException("stream"); } ParseParams pp = new ParseParams(); pp.File = file; string line; int row = 0; while ((line = stream.ReadLine()) != null) { if (!pp.StringSection && line.TrimStart().StartsWith("--", StringComparison.Ordinal)) { row++; continue; // commented line } ParseLine(pp, line, row); row++; } }
private void ParseLine(ParseParams pp, string line, int row) { line = line + "\n"; int count = line.Length; for (int i = 0; i < count; i++) { char current = line[i]; bool moveNext = Parse(pp, current, row, i); if (moveNext) { break; } } }
private void ParseLine(ParseParams pp, string line, int row) { line = line + "\n"; int count = line.Length; for (int i = 0; i < count; i++) { char current = line[i]; bool moveNext = Parse(pp, current, row, i); if (moveNext) { break; } } }
public DateTime ParseTimeInternal(string value, bool tryWithoutMSecs, int msecPrecision) { DateTime ret; DateTimeStyles parseExactStyle = DateTimeStyles.AllowLeadingWhite | DateTimeStyles.AllowTrailingWhite; if (lastSuccessfulParams != null) { if (DateTime.TryParseExact(value, lastSuccessfulParams.Format, lastSuccessfulParams.Culture, parseExactStyle, out ret)) { return(ret); } } CultureInfo[] cinfos = CultureInfo.GetCultures(CultureTypes.SpecificCultures); foreach (CultureInfo ci in cinfos) { string longTimePattern = ci.DateTimeFormat.LongTimePattern; if (tryWithoutMSecs) { if (DateTime.TryParseExact(value, longTimePattern, ci.DateTimeFormat, parseExactStyle, out ret)) { ParseParams pp = new ParseParams(); pp.Culture = ci; pp.Format = longTimePattern; pp.MillisecondsPresent = false; lastSuccessfulParams = pp; return(ret); } } string longTimePatternWithMillisecs = Regex.Replace(longTimePattern, "(:ss|:s)", "$1" + ci.NumberFormat.NumberDecimalSeparator + new string('f', msecPrecision)); if (DateTime.TryParseExact(value, longTimePatternWithMillisecs, ci.DateTimeFormat, parseExactStyle, out ret)) { ParseParams pp = new ParseParams(); pp.Culture = ci; pp.Format = longTimePatternWithMillisecs; pp.MillisecondsPresent = true; lastSuccessfulParams = pp; return(ret); } } throw new FormatException(string.Format("Cannot parse '{0}' as time", value)); }
private bool ParseLastSymbol(ParseParams pp, int row, int column) { if (pp.Temp.Length > 0) { Symbol s = new Symbol(pp.File, pp.Temp.ToString(), row, column); pp.Temp.Length = 0; if (s.ToString().StartsWith(Symbol.Comment.ToString())) { // ignore the rest symbols on this line because they are in comment. return(true); } _symbols.Add(s); } return(false); }
public override bool Parse() { var result = base.Parse(); if (result) { var parseParams = new ParseParams(Message); result = parseParams.Parse(); if (result) { Params = parseParams.Params; RemoveFirstCharactersOfMessage(parseParams.CharactersParsed); } } if (result) { var parseCounters = new ParseCounters(Message); result = parseCounters.Parse(); if (result) { ElapsedTime = parseCounters.ElapsedTime; Reads = parseCounters.Reads; Writes = parseCounters.Writes; Fetches = parseCounters.Fetches; Marks = parseCounters.Marks; RemoveFirstCharactersOfMessage(parseCounters.CharactersParsed); } } if (result && !string.IsNullOrWhiteSpace(Message)) { var parseTableCounts = new ParseTableCounts(Message); result = parseTableCounts.Parse(); if (result) { TableCounts = parseTableCounts.TableCounts; } } return(result); }
/// <summary> /// Parses MIB file to symbol list. /// </summary> /// <param name="file">File</param> /// <param name="stream">File stream</param> private void Parse(string file, TextReader stream) { if (stream == null) { throw new ArgumentNullException("stream"); } ParseParams pp = new ParseParams(); pp.File = file; string line; int row = 0; while ((line = stream.ReadLine()) != null) { if (!pp.StringSection && line.TrimStart().StartsWith("--", StringComparison.Ordinal)) { row++; continue; // commented line } ParseLine(pp, line, row); row++; } }
private bool Parse(ParseParams pp, char current, int row, int column) { switch (current) { case '\n': case '{': case '}': case '(': case ')': case '[': case ']': case ';': case ',': case '|': if (!pp.StringSection) { bool moveNext = ParseLastSymbol(pp, row, column); if (moveNext) { _symbols.Add(CreateSpecialSymbol(pp.File, '\n', row, column)); return true; } _symbols.Add(CreateSpecialSymbol(pp.File, current, row, column)); return false; } break; case '"': pp.StringSection = !pp.StringSection; break; case '\r': return false; default: if ((int)current == 0x1A) { // IMPORTANT: ignore invisible characters such as SUB. return false; } if (Char.IsWhiteSpace(current) && !pp.AssignSection && !pp.StringSection) { bool moveNext = ParseLastSymbol(pp, row, column); if (moveNext) { _symbols.Add(CreateSpecialSymbol(pp.File, '\n', row, column)); return true; } return false; } if (pp.AssignAhead) { pp.AssignAhead = false; ParseLastSymbol(pp, row, column); break; } if (pp.DotSection && current != '.') { ParseLastSymbol(pp, row, column); pp.DotSection = false; } if (current == '.' && !pp.StringSection) { if (!pp.DotSection) { ParseLastSymbol(pp, row, column); pp.DotSection = true; } } if (current == ':' && !pp.StringSection) { if (!pp.AssignSection) { ParseLastSymbol(pp, row, column); } pp.AssignSection = true; } if (current == '=' && !pp.StringSection) { pp.AssignSection = false; pp.AssignAhead = true; } break; } pp.Temp.Append(current); return false; }
private bool ParseLastSymbol(ParseParams pp, int row, int column) { if (pp.Temp.Length > 0) { Symbol s = new Symbol(pp.File, pp.Temp.ToString(), row, column); pp.Temp.Length = 0; if (s.ToString().StartsWith(Symbol.Comment.ToString())) { // ignore the rest symbols on this line because they are in comment. return true; } _symbols.Add(s); } return false; }
private bool Parse(ParseParams pp, char current, int row, int column) { switch (current) { case '\n': case '{': case '}': case '(': case ')': case '[': case ']': case ';': case ',': case '|': if (!pp.StringSection) { bool moveNext = ParseLastSymbol(pp, row, column); if (moveNext) { _symbols.Add(CreateSpecialSymbol(pp.File, '\n', row, column)); return(true); } _symbols.Add(CreateSpecialSymbol(pp.File, current, row, column)); return(false); } break; case '"': pp.StringSection = !pp.StringSection; break; case '\r': return(false); default: if ((int)current == 0x1A) { // IMPORTANT: ignore invisible characters such as SUB. return(false); } if (Char.IsWhiteSpace(current) && !pp.AssignSection && !pp.StringSection) { bool moveNext = ParseLastSymbol(pp, row, column); if (moveNext) { _symbols.Add(CreateSpecialSymbol(pp.File, '\n', row, column)); return(true); } return(false); } if (pp.AssignAhead) { pp.AssignAhead = false; ParseLastSymbol(pp, row, column); break; } if (pp.DotSection && current != '.') { ParseLastSymbol(pp, row, column); pp.DotSection = false; } if (current == '.' && !pp.StringSection) { if (!pp.DotSection) { ParseLastSymbol(pp, row, column); pp.DotSection = true; } } if (current == ':' && !pp.StringSection) { if (!pp.AssignSection) { ParseLastSymbol(pp, row, column); } pp.AssignSection = true; } if (current == '=' && !pp.StringSection) { pp.AssignSection = false; pp.AssignAhead = true; } break; } pp.Temp.Append(current); return(false); }
/* 参数格式 * const string arg = * "<strategy><strategyName>MATrend</strategyName>" + * "<traderName>0003</traderName>" + * "<parameter>" + * "<strategy id=\'MATrend\' name=\'均线穿越\' version=\'1.0\'>" + * "<description>这是关于MATrend策略的描述</description>" + * "<params>" + * "<param type=\'int\' name=\'p1\' direction=\'INOUT\' appearmode=\'文本框\' readonly=\'False\' title=\'策略参数一\' default=\'1\'></param>" + * "<param type=\'double\' name=\'p2\' direction=\'INOUT\' appearmode=\'文本框\' readonly=\'False\' title=\'策略参数二\' default=\'1.5\'></param>" + * "</params>" + * "</strategy>" + * "</parameter>" + * "<instrument>IF1404</instrument>" + * "<sessionID>10000</sessionID>" + * "<frontID>20000</frontID>" + * "<orderRef>1</orderRef>" + * "<startTimer>1</startTimer>" + * "<timerSpan>100</timerSpan>" + * "</strategy>"; */ static void Main(string[] args) { /*参数判断*/ if (args.Length <= 0) { Console.WriteLine("没有传入参数"); Console.ReadLine(); return; } if (args.Length > 0) { Console.WriteLine("传入的参数为:\n{0}", args[0]); } string hostName = System.Reflection.Assembly.GetExecutingAssembly().Location; hostName = hostName.Substring(0, hostName.LastIndexOf('\\')) + "\\config.ini"; CommonDef.IP = IniReadString(hostName, "IP", "Redis_IP"); CommonDef.Port = Convert.ToInt32(IniReadString(hostName, "Port", "Redis_Port")); StrategyInfo strategyInfo = StrategyInfo.getInstence(); XmlOpr.AnalyzeXmlParam(args[0], ref strategyInfo); Redis_Publish = new RedisClient(CommonDef.IP, CommonDef.Port); Redis_Subscribe = new RedisClient(CommonDef.IP, CommonDef.Port); string szDllName = RunSingleStrategy.StrategyInfo.StrategyName + ".dll"; //动态加载策略程序集DLL Assembly ass = Assembly.LoadFrom(szDllName); Type type = ass.GetType(CommonDef.FoctoryType); //创建工厂实例 Object oFactory = System.Activator.CreateInstance(type); string logPath = hostName.Substring(0, hostName.LastIndexOf('\\')) + "\\" + RunSingleStrategy.StrategyInfo.StrategyName + "_log.txt"; Log.PathFile = logPath; Log.SaveLog = true; //获取函数 MethodInfo mi = type.GetMethod(CommonDef.FoctoryMethod); //创建策略实例 Strategy = (CStrategy)mi.Invoke(oFactory, ParseParams.ParseStrategyParams(StrategyInfo.Parameter)); //定义接口对象 IOrderServiceEx orderService = new IOrderServiceEx(Redis_Publish); //用接口实例初始化策略 Strategy.ResetOrderService(orderService); Strategy.Initialize(RunSingleStrategy.StrategyInfo.Instruments.ToArray()); Strategy.InitIndicators(RunSingleStrategy.StrategyInfo.Instruments.ToArray()); //创建定时器 Timer_HeartBeat = new System.Timers.Timer(); Timer_HeartBeat.Elapsed += new System.Timers.ElapsedEventHandler(timer_Tick); Timer_HeartBeat.Interval = 5000; Timer_HeartBeat.AutoReset = true; //设置是执行一次(false)还是一直执行(true); Timer_HeartBeat.Enabled = true; //是否执行System.Timers.Timer.Elapsed事件; //创建策略定时器 if (Int32.Parse(RunSingleStrategy.StrategyInfo.StartTimer) == 1) { Timer_Strategy = new System.Timers.Timer(); Timer_Strategy.Elapsed += new System.Timers.ElapsedEventHandler(timer_Strategy_Tick); Timer_Strategy.Interval = Int32.Parse(RunSingleStrategy.StrategyInfo.TimerSpan) * 1000; Timer_Strategy.AutoReset = true; Timer_Strategy.Enabled = true; } //创建线程类 int _NewThreadId = GetCurrentThreadId(); SubPubThread spThread = new SubPubThread(_NewThreadId, Redis_Subscribe); Thread thread = new Thread(new ThreadStart(spThread.TreadProc)); thread.Name = "PubSub"; thread.Start(); //消息处理 tagMSG msg = new tagMSG(); while (GetMessage(ref msg, 0, 0, 0) > 0) { if (msg.message == CommonDef.WM_USER) { List <string> refEventList = new List <string>(); EventList.GetItem(ref refEventList); if (refEventList.Count == 0) { continue; } foreach (string strItem in refEventList) { string strStyle = XmlOpr.GetType(strItem); string strValue = null; if (strStyle.Equals("UR")) { //交易员是否离线 strValue = XmlOpr.GetVaule(strItem, "online"); if (strValue.Equals("on")) { Log.Out("交易员在线"); } else if (strValue.Equals("off")) { Log.Out("交易员离线"); } } else if (strStyle.Equals("SR")) { //策略启用停用 strValue = XmlOpr.GetVaule(strItem, "run"); if (strValue.Equals("run")) { Log.Out("策略启用"); } else if (strValue.Equals("stop")) { Log.Out("策略停用"); //退出子线程 Redis_Subscribe.UnSubscribe(CommonDef.subscribeChannels.ToArray()); System.Threading.Thread.Sleep(1000); //退出进程 break; } } else if (strStyle.Equals("SIR")) { //策略实例启用停用 if (strValue.Equals("run")) { Log.Out("策略实例启用"); CommonDef.StrategyInstenceStatus = true; } else if (strValue.Equals("stop")) { Log.Out("策略实例停用"); CommonDef.StrategyInstenceStatus = false; } } else if (strStyle.Equals("Q")) { //行情 MarketDetph md = new MarketDetph(); XmlOpr.Transfer(strItem, ref md); Strategy.AppendMD(md, true); } else if (strStyle.Equals("O")) { Log.Out("报单:" + strItem); //报单 OrderInfo order = new OrderInfo(); XmlOpr.Transfer(strItem, ref order); Strategy.OnOrder(order); } else if (strStyle.Equals("T")) { Log.Out("成交:" + strItem); //成交 TradeInfo trade = new TradeInfo(); XmlOpr.Transfer(strItem, ref trade); Strategy.OnTrade(trade); } else if (strStyle.Equals("OI")) { Log.Out("下单错误返回:" + strItem); //下单错误返回 OrderInfo orderError = new OrderInfo(); XmlOpr.Transfer(strItem, ref orderError); Strategy.OnOrderError(orderError); } else if (strStyle.Equals("OA")) { Log.Out("撤单错误返回:" + strItem); //撤单错误返回 ActionInput actionInput = new ActionInput(); XmlOpr.Transfer(strItem, ref actionInput); Strategy.OnOrderAction(actionInput); } } } } }