//参数解析 /* * <strategy> * <strategyName>strategyName1</strategyName> * <traderName>traderName1</traderName> * <parameter>para1</parameter> * <instrument>instrument1</instrument> * <sessionID>111101</sessionID> * <frontID>222201</frontID> * <orderRef>1</orderRef> * <StartTimer>1</StartTimer> * <TimerSpan>100</TimerSpan> * </strategy> * * 注意 para1 格式必须是 * <params> * <Param type="int" name="p1">1</Param> * <Param type="double" name="p2">1.0</Param> * </params> */ public void AnalyzeXmlParam(string strXml, ref StrategyInfo strategyInfo) { XmlDocument doc = new XmlDocument(); doc.LoadXml(strXml); RunSingleStrategy.StrategyInfo.StrategyName = doc.SelectSingleNode("strategy/strategyName").InnerText; RunSingleStrategy.StrategyInfo.TraderName = doc.SelectSingleNode("strategy/traderName").InnerText; RunSingleStrategy.StrategyInfo.Parameter = doc.SelectSingleNode("strategy/parameter").InnerXml; RunSingleStrategy.StrategyInfo.SessionID = doc.SelectSingleNode("strategy/sessionID").InnerText; RunSingleStrategy.StrategyInfo.FrontID = doc.SelectSingleNode("strategy/frontID").InnerText; RunSingleStrategy.StrategyInfo.OrderRef = doc.SelectSingleNode("strategy/orderRef").InnerText; RunSingleStrategy.StrategyInfo.StartTimer = doc.SelectSingleNode("strategy/startTimer").InnerText; RunSingleStrategy.StrategyInfo.TimerSpan = doc.SelectSingleNode("strategy/timerSpan").InnerText; try { RunSingleStrategy.StrategyInfo.SaveHistory = Int32.Parse(doc.SelectSingleNode("strategy/savehistory").InnerText) != 0; RunSingleStrategy.StrategyInfo.HistoryPath = doc.SelectSingleNode("strategy/historypath").InnerText; } catch (Exception e) { } CommonDef.StrategyInstenceTimerStatus = (Int32.Parse(doc.SelectSingleNode("strategy/startTimer").InnerText) == 1) ? true : false; CommonDef.StrategyInstenceRunAfterOfflineStatus = (Int32.Parse(doc.SelectSingleNode("strategy/RunAfterOffline").InnerText) == 1) ? true : false;//0为空转,1为运行 string szInsts = doc.SelectSingleNode("strategy/instrument").InnerText; if (szInsts[szInsts.Length - 1].Equals(',')) { szInsts = szInsts.Remove(szInsts.Length - 1); } string[] arrInst = szInsts.Split(','); foreach (string szInst in arrInst) { RunSingleStrategy.StrategyInfo.Instruments.Add(szInst); } }
/* 参数格式 * 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); } } } } }