public override List <TradeInfo> DoBuy(Properties strategyParam, DateTime d, StrategyContext context) { //取得行情库 IndicatorRepository repository = (IndicatorRepository)context.Get <Object>("repository"); if (repository == null) { return(null); } //读取代码 List <String> codes = LoadCodes(strategyParam, context); if (codes == null || codes.Count <= 0) { return(null); } //取得策略参数 double p_mainforcelow = strategyParam.Get <double>("mainforcelow"); int p_monthbutpt = strategyParam.Get <int>("monthbutpt", 0); //double p_mainforceclimb = strategyParam.Get<double>("mainforceclimb"); double p_mainforceslope = strategyParam.Get <double>("mainforceslope"); int p_mainforcerough = strategyParam.Get <int>("mainforcerough"); int p_buypointdays = strategyParam.Get <int>("buypointdays"); int p_maxbuynum = strategyParam.Get <int>("maxbuynum"); GetInMode p_fundpergetin = GetInMode.Parse(strategyParam.Get <String>("getinMode")); GrailParameter p_grail = GrailParameter.Parse(strategyParam.Get <String>("grail")); double stampduty = context.Get <double>("stampduty"); double volumecommission = context.Get <double>("volumecommission"); List <TradeInfo> results = new List <TradeInfo>(); //遍历 foreach (String code in codes) { TimeSerialsDataSet ds = repository[code]; if (ds == null) { continue; } KLine klineDay = ds.DayKLine; if (klineDay == null) { continue; } KLineItem klineItemDay = klineDay[d]; if (klineItemDay == null) { continue; } TimeSeries <ITimeSeriesItem <List <double> > > fundDay = ds.DayFundTrend; if (fundDay == null) { continue; } ITimeSeriesItem <List <double> > fundItemDay = fundDay[d]; if (fundItemDay == null) { continue; } int index = fundDay.IndexOf(fundItemDay); if (index <= 0) { continue; } ITimeSeriesItem <List <double> > prevfundItemDay = fundDay[index - 1]; if (!p_grail.CanBuy(d, code)) //大盘禁止买入的跳过 { continue; } if (p_mainforcelow > 0)//判断主力线上穿p_mainforcelow { if (fundItemDay.Value[0] < p_mainforcelow) { continue; } if (prevfundItemDay.Value[0] > p_mainforcelow) { continue; } } if (p_mainforceslope > 0) //判断主力线上升速度超过p_mainforceslope { if (fundItemDay.Value[0] - prevfundItemDay.Value[0] < p_mainforceslope) { continue; } } TradeInfo tradeInfo = new TradeInfo() { Direction = TradeDirection.Buy, Code = code, Amount = (int)(p_fundpergetin.Value / klineItemDay.CLOSE), EntrustPrice = klineItemDay.CLOSE, EntrustDate = d, TradeDate = d, TradePrice = klineItemDay.CLOSE, Stamps = stampduty, Fee = volumecommission, TradeMethod = TradeInfo.TM_AUTO, Reason = (p_mainforcelow <= 0 ? "" : "[主力线低位" + p_mainforcelow.ToString("F2") + "]") + (p_mainforceslope <= 0 ? "" : "[主力线上升速度超过" + p_mainforceslope.ToString("F2") + "]") }; results.Add(tradeInfo); } return(results); }
public override TradeInfo DoSell(HoldRecord holdRecord, DateTime d, Properties strategyParam, StrategyContext context) { if (holdRecord == null) { return(null); } //取得行情库 IndicatorRepository repository = (IndicatorRepository)context.Get <Object>("repository"); if (repository == null) { return(null); } TimeSerialsDataSet ds = repository[holdRecord.code]; if (ds == null) { return(null); } KLine klineDay = ds.DayKLine; KLineItem klineItemDay = klineDay[d]; if (klineItemDay == null) { return(null); } //取得策略参数 double p_maxprofilt = strategyParam.Get <double>("maxprofilt"); int p_maxholddays = strategyParam.Get <int>("maxholddays"); double p_stoploss = strategyParam.Get <double>("stoploss"); int p_choosedays = strategyParam.Get <int>("choosedays"); double p_chooseprofilt = strategyParam.Get <double>("chooseprofilt"); GrailParameter p_grail = GrailParameter.Parse(strategyParam.Get <String>("grail")); double stampduty = context.Get <double>("stampduty"); double volumecommission = context.Get <double>("volumecommission"); //大盘要求必须卖 if (p_grail.MustSell(d, holdRecord.code)) { TradeInfo tradeInfo = new TradeInfo() { Direction = TradeDirection.Sell, Code = holdRecord.code, Amount = holdRecord.amount, EntrustPrice = klineItemDay.CLOSE, EntrustDate = d, TradeDate = d, TradePrice = klineItemDay.CLOSE, Stamps = stampduty, Fee = volumecommission, TradeMethod = TradeInfo.TM_AUTO, Reason = "大盘指数要求卖出" }; return(tradeInfo); } double profilt = (klineItemDay.CLOSE - holdRecord.buyPrice) / holdRecord.buyPrice; //判断是否到达最大收益 if (p_maxprofilt > 0) { if (profilt >= p_maxprofilt) { TradeInfo tradeInfo = new TradeInfo() { Direction = TradeDirection.Sell, Code = holdRecord.code, Amount = holdRecord.amount, EntrustPrice = klineItemDay.CLOSE, EntrustDate = d, TradeDate = d, TradePrice = klineItemDay.CLOSE, Stamps = stampduty, Fee = volumecommission, TradeMethod = TradeInfo.TM_AUTO, Reason = "盈利达到" + p_maxprofilt.ToString("F2") }; return(tradeInfo); } } //盈利超过个股预期 if (holdRecord.expect > 0) { if (profilt >= p_maxprofilt) { TradeInfo tradeInfo = new TradeInfo() { Direction = TradeDirection.Sell, Code = holdRecord.code, Amount = holdRecord.amount, EntrustPrice = klineItemDay.CLOSE, EntrustDate = d, TradeDate = d, TradePrice = klineItemDay.CLOSE, Stamps = stampduty, Fee = volumecommission, TradeMethod = TradeInfo.TM_AUTO, Reason = "盈利达到个股预期" + holdRecord.expect.ToString("F2") }; return(tradeInfo); } } //预期要求立即卖出 if (holdRecord.expect == -1) { TradeInfo tradeInfo = new TradeInfo() { Direction = TradeDirection.Sell, Code = holdRecord.code, Amount = holdRecord.amount, EntrustPrice = klineItemDay.CLOSE, EntrustDate = d, TradeDate = d, TradePrice = klineItemDay.CLOSE, Stamps = stampduty, Fee = volumecommission, TradeMethod = TradeInfo.TM_AUTO, Reason = "个股预期极低" }; return(tradeInfo); } //个股观望天数超过预期 int holdDays = CalendarUtils.WorkDayCount(holdRecord.buyDate, d); if (holdRecord.expect < 0) { if (holdDays >= holdRecord.expect * -1) { TradeInfo tradeInfo = new TradeInfo() { Direction = TradeDirection.Sell, Code = holdRecord.code, Amount = holdRecord.amount, EntrustPrice = klineItemDay.CLOSE, EntrustDate = d, TradeDate = d, TradePrice = klineItemDay.CLOSE, Stamps = stampduty, Fee = volumecommission, TradeMethod = TradeInfo.TM_AUTO, Reason = "个股预期观望超过" + (-1 * holdRecord.expect).ToString() + "天" }; return(tradeInfo); } } //达到止损线 if (p_stoploss > 0) { if (-1 * profilt > p_stoploss) { TradeInfo tradeInfo = new TradeInfo() { Direction = TradeDirection.Sell, Code = holdRecord.code, Amount = holdRecord.amount, EntrustPrice = klineItemDay.CLOSE, EntrustDate = d, TradeDate = d, TradePrice = klineItemDay.CLOSE, Stamps = stampduty, Fee = volumecommission, TradeMethod = TradeInfo.TM_AUTO, Reason = "到达止损线" + p_stoploss.ToString("F2") }; return(tradeInfo); } } //达到最大持仓天数 if (p_maxholddays > 0) { if (holdDays > p_maxholddays) { TradeInfo tradeInfo = new TradeInfo() { Direction = TradeDirection.Sell, Code = holdRecord.code, Amount = holdRecord.amount, EntrustPrice = klineItemDay.CLOSE, EntrustDate = d, TradeDate = d, TradePrice = klineItemDay.CLOSE, Stamps = stampduty, Fee = volumecommission, TradeMethod = TradeInfo.TM_AUTO, Reason = "到达最大持仓天数" + p_maxholddays.ToString() }; return(tradeInfo); } } return(null); }