private void init() { props = insp.Utility.Bean.Properties.Load(FileUtils.GetDirectory() + "PARAM_FILE", Encoding.UTF8); //注册转换器 ConvertUtils.RegisteConvertor <String, TradeInfo>(ConvertUtils.strToObject <TradeInfo>); ConvertUtils.RegisteConvertor <TradeInfo, String>(ConvertUtils.objectToStr); ConvertUtils.RegisteConvertor <String, TradeDirection>(ConvertUtils.strtoenum <TradeDirection>); ConvertUtils.RegisteConvertor <TradeDirection, String>(ConvertUtils.enumtostr <TradeDirection>); ConvertUtils.RegisteConvertor <String, TradeIntent>(ConvertUtils.strtoenum <TradeIntent>); ConvertUtils.RegisteConvertor <TradeIntent, String>(ConvertUtils.enumtostr <TradeIntent>); }
/// <summary> /// 卖出操作 /// </summary> /// <returns></returns> public bool doSell() { showBeginMessage("选择卖出股票..."); try { //取得持仓 doGetHolds(); if (holdRecords.Count <= 0) { return(true); } //创建策略上下文 createStrategyContext(); //取得策略 String strategyname = props.Get <String>("strategy"); KeyValuePair <IStrategyMeta, insp.Utility.Bean.Properties> kv = context.GetStrateMetaAndParam(strategyname); if (kv.Key == null || kv.Value == null) { throw new Exception("缺少有效策略"); } StrategyInstance instance = (StrategyInstance)kv.Key.CreateInstance("1", kv.Value, ""); //取得卖出算法类 String sellername = kv.Value.Get <String>("seller"); Seller seller = (Seller)instance.Seller; if (instance.Seller == null) { throw new Exception("缺少有效的卖出策略:" + sellername); } //对持仓逐个判断是否要卖出 sellEntrust.Clear(); String tradeFilename = FileUtils.GetDirectory("records") + "trades.csv"; foreach (HoldRecord hold in holdRecords) { insp.Utility.Bean.Properties sellParams = seller.PDC.CreateProperties(hold.parameters); TradeInfo tradeInfo = seller.DoSell(hold, this.tradeDate, sellParams, context); if (tradeInfo == null) { continue; } File.AppendAllLines(tradeFilename, new string[] { tradeInfo.ToString() }); sellEntrust.Add(tradeInfo); } showResultMessage(""); return(true); }catch (Exception e) { showResultMessage("卖出操作异常", -1, e.Message); return(false); } }