/// <summary> /// 添加新行情。 /// </summary> /// <param name="bCalcImmediately" default=false>是否立即进行指标计算</param> /// <returns> /// true: 成功 /// false: 失败,原因指标对象可能未初始化 /// </returns> public override bool AppendMD(MarketDetph md, bool bCalcImmediately) { // 预处理深度行情,并缓存到未算深度行情队列中。 base.AppendMD(md, bCalcImmediately); // 请在这里填写实现代码。 ma.AppendMD(md, bCalcImmediately); macd.AppendMD(md, bCalcImmediately); string[] timesplit = md.updateTime.Split(':'); if (Int32.Parse(timesplit[2]) < 1) { InputOrder req = new InputOrder(); req.InstrumentID = md.instrumentId; req.Qty = 1; req.Price = md.latestPrice; req.Hedge = '0'; req.IsMarket = false; if (Int32.Parse(timesplit[1]) > 30) { req.Direction = '0'; req.OpenClose = '0'; } else { req.Direction = '1'; req.OpenClose = '1'; } ReqOrderInsert(req); } // 是否需要立即进行指标计算。 if (bCalcImmediately) { return(CalculateMDIndicator()); } else { return(true); } }
/// <summary> /// 添加新行情。 /// </summary> /// <param name="bCalcImmediately" default=false>是否立即进行指标计算</param> /// <returns> /// true: 成功 /// false: 失败,原因指标对象可能未初始化 /// </returns> public override bool AppendMD(MarketDetph md, bool bCalcImmediately) { // 预处理深度行情,并缓存到未算深度行情队列中。 base.AppendMD(md, bCalcImmediately); // 请在这里填写实现代码。 // 是否需要立即进行指标计算。 if (bCalcImmediately) { return(Calculate()); } else { return(true); } }
//将xml字符串转换成行情MarketDetph结构 public void Transfer(string strXml, ref MarketDetph md) { XmlDocument doc = new XmlDocument(); doc.LoadXml(strXml); //取得节点名为Q的XmlNode集合 XmlNodeList xxList = doc.GetElementsByTagName("Q"); foreach (XmlNode xxNode in xxList) { XmlNodeList childList = xxNode.ChildNodes; //取得InstrumentID下的子节点集合 foreach (XmlNode xxxxNode in xxList) { // 合约ID md.instrumentId = xxxxNode.Attributes["InstrumentID"].Value; // 交易所ID md.exchangeId = xxxxNode.Attributes["ExchangeID"].Value; // 交易日 md.tradingday = xxxxNode.Attributes["TradingDay"].Value; // 更新时间 md.updateTime = xxxxNode.Attributes["UpdateTime"].Value; // 更新毫秒 md.updateMS = Int32.Parse(xxxxNode.Attributes["UpdateMillisec"].Value); // 最新价 string szTemp = xxxxNode.Attributes["LastPrice"].Value; if (szTemp.Length == 0) { md.latestPrice = CommonDef.dInvalidValue; } else { md.latestPrice = double.Parse(xxxxNode.Attributes["LastPrice"].Value); } // 开盘价 szTemp = xxxxNode.Attributes["OpenPrice"].Value; if (szTemp.Length == 0) { md.open = CommonDef.dInvalidValue; } else { md.open = double.Parse(xxxxNode.Attributes["OpenPrice"].Value); } // 最高价 szTemp = xxxxNode.Attributes["HighestPrice"].Value; if (szTemp.Length == 0) { md.high = CommonDef.dInvalidValue; } else { md.high = double.Parse(xxxxNode.Attributes["HighestPrice"].Value); } // 最低价 szTemp = xxxxNode.Attributes["LowestPrice"].Value; if (szTemp.Length == 0) { md.low = CommonDef.dInvalidValue; } else { md.low = double.Parse(xxxxNode.Attributes["LowestPrice"].Value); } // 现量 md.latestVol = Int32.Parse(xxxxNode.Attributes["Volume"].Value); // 持仓量 szTemp = xxxxNode.Attributes["LowestPrice"].Value; if (szTemp.Length == 0) { md.low = CommonDef.iInvalidValue; } else { md.openInt = (int)double.Parse(xxxxNode.Attributes["OpenInterest"].Value); } // 收盘价 szTemp = xxxxNode.Attributes["ClosePrice"].Value; if (szTemp.Length == 0) { md.todayClosePrice = CommonDef.dInvalidValue; } else { md.todayClosePrice = double.Parse(xxxxNode.Attributes["ClosePrice"].Value); } // 成交金额 szTemp = xxxxNode.Attributes["Turnover"].Value; if (szTemp.Length == 0) { md.turnover = CommonDef.dInvalidValue; } else { md.turnover = double.Parse(xxxxNode.Attributes["Turnover"].Value); } // 均价 szTemp = xxxxNode.Attributes["AveragePrice"].Value; if (szTemp.Length == 0) { md.avgLatestPrice = CommonDef.dInvalidValue; } else { md.avgLatestPrice = double.Parse(xxxxNode.Attributes["AveragePrice"].Value); } // 昨结算价 szTemp = xxxxNode.Attributes["PreSettlementPrice"].Value; if (szTemp.Length == 0) { md.preSettlementPrice = CommonDef.dInvalidValue; } else { md.preSettlementPrice = double.Parse(xxxxNode.Attributes["PreSettlementPrice"].Value); } // 昨收价 szTemp = xxxxNode.Attributes["PreClosePrice"].Value; if (szTemp.Length == 0) { md.yesterdayClosePrice = CommonDef.dInvalidValue; } else { md.yesterdayClosePrice = double.Parse(xxxxNode.Attributes["PreClosePrice"].Value); } // 昨持仓量 md.yesterdayOpenInt = (int)double.Parse(xxxxNode.Attributes["PreOpenInterest"].Value); // 结算价 szTemp = xxxxNode.Attributes["SettlementPrice"].Value; if (szTemp.Length == 0) { md.settlementPrice = CommonDef.dInvalidValue; } else { md.settlementPrice = double.Parse(xxxxNode.Attributes["SettlementPrice"].Value); } // 涨停价 szTemp = xxxxNode.Attributes["UpperLimitPrice"].Value; if (szTemp.Length == 0) { md.upperLimitPrice = CommonDef.dInvalidValue; } else { md.upperLimitPrice = double.Parse(xxxxNode.Attributes["UpperLimitPrice"].Value); } // 跌停价 szTemp = xxxxNode.Attributes["LowerLimitPrice"].Value; if (szTemp.Length == 0) { md.lowerLimitPrice = CommonDef.dInvalidValue; } else { md.lowerLimitPrice = double.Parse(xxxxNode.Attributes["LowerLimitPrice"].Value); } // 买一价 szTemp = xxxxNode.Attributes["BidPrice1"].Value; if (szTemp.Length == 0) { md.bidPrice1 = CommonDef.dInvalidValue; } else { md.bidPrice1 = double.Parse(xxxxNode.Attributes["BidPrice1"].Value); } // 卖一量 md.bidVol1 = Int32.Parse(xxxxNode.Attributes["BidVolume1"].Value); // 卖一价 szTemp = xxxxNode.Attributes["AskPrice1"].Value; if (szTemp.Length == 0) { md.askPrice1 = CommonDef.dInvalidValue; } else { md.askPrice1 = double.Parse(xxxxNode.Attributes["AskPrice1"].Value); } // 卖一量 md.askVol1 = Int32.Parse(xxxxNode.Attributes["AskVolume1"].Value); // 买二价 //md.bidPrice2 = double.Parse(xxxxNode.Attributes["BidPrice2"].Value); // 买二量 //md.bidVol2 = Int32.Parse(xxxxNode.Attributes["BidVolume2"].Value); // 卖二价 //md.askPrice2 = double.Parse(xxxxNode.Attributes["AskPrice2"].Value); // 卖二量 //md.askVol2 = Int32.Parse(xxxxNode.Attributes["AskVolume2"].Value); // 买三价 //md.bidPrice3 = double.Parse(xxxxNode.Attributes["BidPrice3"].Value); // 买三量 //md.bidVol3 = Int32.Parse(xxxxNode.Attributes["BidVolume3"].Value); // 卖三价 //md.askPrice3 = double.Parse(xxxxNode.Attributes["AskPrice3"].Value); // 卖三量 //md.askVol3 = Int32.Parse(xxxxNode.Attributes["AskVolume3"].Value); // 买四价 //md.bidPrice4 = double.Parse(xxxxNode.Attributes["BidPrice4"].Value); // 买四量 //md.bidVol4 = Int32.Parse(xxxxNode.Attributes["BidVolume4"].Value); // 卖四价 //md.askPrice4 = double.Parse(xxxxNode.Attributes["AskPrice4"].Value); // 卖四量 //md.askVol4 = Int32.Parse(xxxxNode.Attributes["AskVolume4"].Value); // 买五价 //md.bidPrice5 = double.Parse(xxxxNode.Attributes["BidPrice5"].Value); // 买五量 //md.bidVol5 = Int32.Parse(xxxxNode.Attributes["BidVolume5"].Value); // 卖五价 //md.askPrice5 = double.Parse(xxxxNode.Attributes["AskPrice5"].Value); // 卖五量 //md.askVol5 = Int32.Parse(xxxxNode.Attributes["AskVolume5"].Value); } } }
virtual public bool AppendMD(MarketDetph md, bool bCalcImmediately) { return true; }
virtual public bool OnMarketDepth(MarketDetph md) { DebugOut("Call CStrategy Function AppendMD from MainForm Successfully"); return(true); }
virtual public bool OnMarketDepth(MarketDetph md) { return(true); }
/* 参数格式 * 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); } } } } }
virtual public bool AppendMD(MarketDetph md, bool bCalcImmediately) { DebugOut("Call CStrategy Function AppendMD from MainForm Successfully"); return(true); }