private void Leave(double closePrice, double qty, Guid tradeGuid) { if (tps.ContainsKey(tradeGuid)) { PTradePoints tp = tps[tradeGuid]; if (tp.OutPoint == null) { OpenPoint op = new OpenPoint(); op.SecInfo = currentTick.SecInfo; //op.FirstTradePriceType = TradeSendOrderPriceType.ShiJiaWeiTuo_SH_SZ_WuDangJiChenShenChe; op.FirstTradePriceType = parameter.CloseOrderPriceType; op.CancelLimitTime = parameter.CloseOrderWaitSecond; op.ReTradePriceType = parameter.ReCloseOrderPriceType; op.ReEnterPecentage = 1; op.Openop = false; //op.Remark = string.Format("MA2:{0},Close{1}", args.Bar.MA.MA2, args.Bar.Close); tp.OutPoint = op; openPoints.Add(op); tp.HowToClose = CloseType.Normal; tp.Status = OpenStatus.Closed; } tp.OutPoint.OpenTime = currentTick.Time; tp.OutPoint.OpenQty += qty; if (tp.EnterPoint.OpenType == OpenType.KaiDuo) { tp.OutPoint.OpenType = OpenType.PingDuo; tp.OutPoint.OpenPrice = closePrice; } else { tp.OutPoint.OpenType = OpenType.PingKong; tp.OutPoint.OpenPrice = closePrice; } PolicyResultEventArgs arg = new PolicyResultEventArgs(); arg.Tickdata = currentTick; arg.PolicyName1 = this.policyName; arg.SecInfo = this.SecInfo; arg.IsReal = tp.IsReal; arg.PairePoint = tp; arg.OpenRmks = tp.EnterPoint.Remark; RaiseResult(arg); } }
private void Open(double price, double qty, OpenType ot) { PolicyResultEventArgs arg = new PolicyResultEventArgs(); arg.PolicyName1 = this.policyName; arg.SecInfo = this.SecInfo; arg.IsReal = currentTick.IsReal; OpenPoint op = new OpenPoint(); op.SecInfo = currentTick.SecInfo; op.OpenTime = currentTick.Time; op.OpenPrice = price; op.OpenType = ot; op.OpenQty = qty; op.DealQty = 0; op.Openop = true; op.FirstTradePriceType = parameter.EnterOrderPriceType; op.CancelLimitTime = parameter.EnterOrderWaitSecond; op.ReEnterPecentage = parameter.ReEnterPercent; //op.CancelLimitTime = 60; //op.ReEnterPecentage = 0.05; openPoints.Add(op); PTradePoints tp = new PTradePoints(op, 0); if (ot == OpenType.KaiDuo) { tp.Y = (1 + parameter.ZhiYingBeiShu) * price; tp.ZhiSun = (1 - parameter.ZhiSunBiLi) * price; } if (ot == OpenType.KaiKong) { tp.Y = (1 - parameter.ZhiYingBeiShu) * price; tp.ZhiSun = (1 + parameter.ZhiSunBiLi) * price; } tp.Fee = parameter.Fee; /////////////add////////////////////// tp.IsReal = arg.IsReal; this.tps.TryAdd(tp.TradeGuid, tp); arg.PairePoint = tp; arg.PolicyObject = this; arg.Tickdata = currentTick; RaiseResult(arg); }
public override void ManualClose(Guid tradeGuid) { if (tps.ContainsKey(tradeGuid)) { PTradePoints tp = tps[tradeGuid]; if (currentTick.IsReal ? !tp.Finished : !tp.Closed) { double qty = tp.EnterPoint.DealQty - (tp.OutPoint != null ? tp.OutPoint.OpenQty : 0); if (qty > 0) { if (tp.EnterPoint.OpenType == OpenType.KaiDuo) { Leave(currentTick.Bid, qty, tradeGuid); } else { Leave(currentTick.Ask, qty, tradeGuid); } } } } }
public override void Notify(Guid tradeGuid, OpenStatus status, double dealQty = 0, double dealPrice = 0, string weituobianhao = "", string pendWeituobianhao = "") { if (tps.ContainsKey(tradeGuid)) { PTradePoints t = tps[tradeGuid]; if (status == OpenStatus.Opened) { decimal realDealQty = (decimal)dealQty + (decimal)t.EnterPoint.PartDealQty - (decimal)t.EnterPoint.DealQty; t.EnterPoint.DealQty = (double)((decimal)dealQty + (decimal)t.EnterPoint.PartDealQty); if (t.EnterPoint.OpenType == OpenType.KaiDuo) { DuoHands += (Decimal)t.EnterPoint.DealQty; FrozenDuoHands -= (Decimal)t.EnterPoint.DealQty; } else { KongHands += (Decimal)t.EnterPoint.DealQty; FrozenKongHands -= (Decimal)t.EnterPoint.DealQty; } if (currentTick.IsReal) { t.Status = OpenStatus.Opened; RaiseMessage(new PolicyMessageEventArgs(string.Format("{0}-策略已接受开仓通知,成交数量:{1}", this.policyName, dealQty))); } } else if (status == OpenStatus.Open) { RaiseMessage(new PolicyMessageEventArgs(string.Format("{0}-策略已接受开仓下单通知", this.policyName))); } else if (status == OpenStatus.Close) { OpenPointWeiTuo opwt = new OpenPointWeiTuo(); opwt.Weituobianhao = weituobianhao; opwt.OpenQty = dealQty; t.OutPoint.OpenPointWeiTuo.Add(opwt); if (currentTick.IsReal) { RaiseMessage(new PolicyMessageEventArgs(string.Format("{0}-策略已接受平仓下单通知", this.policyName))); } } else if (status == OpenStatus.Closed) { double RealTotalDeal = 0; bool find = false; for (int j = 0; j < t.OutPoint.OpenPointWeiTuo.Count; j++) { if (t.OutPoint.OpenPointWeiTuo[j].Weituobianhao == weituobianhao) { OpenPointWeiTuo opwt = t.OutPoint.OpenPointWeiTuo[j]; decimal realDealQty = (decimal)dealQty + (decimal)opwt.PartDealQty - (decimal)opwt.DealQty; t.OutPoint.OpenPointWeiTuo[j].DealQty = dealQty + opwt.PartDealQty; find = true; } RealTotalDeal += t.OutPoint.OpenPointWeiTuo[j].DealQty; } if (t.EnterPoint.DealQty == RealTotalDeal) { if (t.EnterPoint.OpenType == OpenType.KaiDuo) { DuoHands -= (decimal)RealTotalDeal; } else { KongHands -= (decimal)RealTotalDeal; } t.Finished = true; PTradePoints tptemp; tps.TryRemove(tradeGuid, out tptemp); } if (currentTick.IsReal) { RaiseMessage(new PolicyMessageEventArgs(string.Format("{0}-策略已接受平仓通知-{1}-有单子:{2}", this.policyName, weituobianhao, find.ToString()))); } } else if (status == OpenStatus.OpenPending) { t.EnterPoint.PartDealQty = t.EnterPoint.DealQty; RaiseMessage(new PolicyMessageEventArgs(string.Format("{0}-策略已接收入场追单通知", this.policyName))); } else if (status == OpenStatus.ClosePending) { OpenPointWeiTuo opwt = new OpenPointWeiTuo(); opwt.Weituobianhao = pendWeituobianhao; opwt.OpenQty = dealQty; t.OutPoint.OpenPointWeiTuo.Add(opwt); RaiseMessage(new PolicyMessageEventArgs(string.Format("{0}-策略已接收出场追单通知", this.policyName))); } else if (status == OpenStatus.Failed) { RaiseMessage(new PolicyMessageEventArgs(string.Format("{0}-策略已接受失败通知", this.policyName))); } else if (status == OpenStatus.PartOpend) { if (dealQty > (t.EnterPoint.DealQty - t.EnterPoint.PartDealQty)) { decimal realDealQty = (decimal)dealQty + (decimal)t.EnterPoint.PartDealQty - (decimal)t.EnterPoint.DealQty; t.EnterPoint.DealQty = dealQty + t.EnterPoint.PartDealQty; RaiseMessage(new PolicyMessageEventArgs(string.Format("{0}-策略已接受部分入场成交通知,成交数量:{1}", this.policyName, dealQty))); } } else if (status == OpenStatus.PartClosed) { for (int j = 0; j < t.OutPoint.OpenPointWeiTuo.Count; j++) { if (t.OutPoint.OpenPointWeiTuo[j].Weituobianhao == weituobianhao) { OpenPointWeiTuo opwt = t.OutPoint.OpenPointWeiTuo[j]; if (dealQty > (opwt.DealQty - opwt.PartDealQty)) { RaiseMessage(new PolicyMessageEventArgs(string.Format("{0}-策略已接受部分出场成交通知,成交数量:{1}", this.policyName, dealQty))); decimal realDealQty = (decimal)dealQty + (decimal)opwt.PartDealQty - (decimal)opwt.DealQty; t.OutPoint.OpenPointWeiTuo[j].DealQty = dealQty + opwt.PartDealQty; } break; } } } else if (status == OpenStatus.OpenCanceled) { if (t.EnterPoint.DealQty == 0) { t.Finished = true; } RaiseMessage(new PolicyMessageEventArgs(string.Format("{0}-策略已接受入场撤单通知", this.policyName))); } else if (status == OpenStatus.CloseCanceled) { RaiseMessage(new PolicyMessageEventArgs(string.Format("{0}-策略已接受出场撤单通知", this.policyName))); } } }
protected override void dataReceiver_Data_Arrival(object sender, StockData.TickData tickdata) { if (tickdata.Code == string.Empty) { IsSimulateFinished = true; HistoryFinished(); LiveDataUpdate(tickdata); } else { if (currentDay != tickdata.Time.Date) { while (LiveBars["close"].Count > 120) { foreach (var item in LiveBars) { LiveBars[item.Key].RemoveAt(0); } } currentDay = tickdata.Time.Date; StopTime = tickdata.Time.Date.AddHours(14).AddMinutes(59); this.Reset(); } if (tickdata.Time > StopTime) { foreach (var item in tps) { PTradePoints tp = item.Value; if (!tp.Finished) { if (tp.EnterPoint.OpenType == OpenType.KaiDuo) { double qty = tp.EnterPoint.DealQty - (tp.OutPoint != null ? tp.OutPoint.OpenQty : 0); if (qty > 0) { Leave(currentTick.Bid, qty, tp.TradeGuid); } } else { double qty = tp.EnterPoint.DealQty - (tp.OutPoint != null ? tp.OutPoint.OpenQty : 0); if (qty > 0) { Leave(currentTick.Ask, qty, tp.TradeGuid); } } } } } currentTick = tickdata; liveDataProcessor.ReceiveTick(tickdata); if (this.SecInfo.isLive(tickdata.Time.TimeOfDay)) { if (IsSimulateFinished || (!IsSimulateFinished && !tickdata.IsReal)) { } } LiveDataUpdate(tickdata); } }
void liveDataProcessor_OnLiveBarArrival(object sender, LiveBarArrivalEventArgs args) { int inteval = ((LiveBars)sender).Inteval; #region 策略bar逻辑 if (inteval == parameter.BarInteval) { LiveBars["close"].Add(args.Bar.Close); LiveBars["high"].Add(args.Bar.High); LiveBars["low"].Add(args.Bar.Low); if (LiveBars["close"].Count > parameter.BarCount) { MarkDp mdp = new MarkDp(); List <Signal> listsig = mdp.GetDpData(LiveBars); bool NewPoint = false; for (int i = 0; i < listsig.Count; i++) { if (listsig[i].Info == "low" && !ListLowSig.ContainsKey(listsig[i].Index)) { ListLowSig.Add(listsig[i].Index, listsig[i].Price); NewPoint = true; } if (listsig[i].Info == "high" && !ListHighSig.ContainsKey(listsig[i].Index)) { ListHighSig.Add(listsig[i].Index, listsig[i].Price); NewPoint = true; } } if (NewPoint) { slope = CalSlope(ListSig.Keys.ToList(), ListSig.Values.ToList()); AvgDistance = 0; foreach (var item in ListSig) { AvgDistance += GetDistance(slope, item.Key, item.Value); } AvgDistance = AvgDistance / ListSig.Count; } double Cha = args.Bar.Close - ((slope * (LiveBars["close"].Count - 1 - avgX) / stdX) * stdY + avgY); if (Cha > 0) { if (KongHands > 0) { foreach (var item in tps) { PTradePoints tp = item.Value; if (!tp.Finished) { if (tp.EnterPoint.OpenType == OpenType.KaiKong) { double qty = tp.EnterPoint.DealQty - (tp.OutPoint != null ? tp.OutPoint.OpenQty : 0); if (qty > 0) { Leave(currentTick.Ask, qty, tp.TradeGuid); } } } } } if (slope < 0 && Math.Abs(Cha) > AvgDistance) { if (DuoHands + FrozenDuoHands == 0) { FrozenDuoHands += (Decimal)parameter.qty; Open(currentTick.Bid, parameter.qty, OpenType.KaiDuo); } } } else { if (DuoHands > 0) { foreach (var item in tps) { PTradePoints tp = item.Value; if (!tp.Finished) { if (tp.EnterPoint.OpenType == OpenType.KaiDuo) { double qty = tp.EnterPoint.DealQty - (tp.OutPoint != null ? tp.OutPoint.OpenQty : 0); if (qty > 0) { Leave(currentTick.Bid, qty, tp.TradeGuid); } } } } } if (slope > 0 && Math.Abs(Cha) > AvgDistance) { if (KongHands + FrozenKongHands == 0) { FrozenKongHands += (Decimal)parameter.qty; Open(currentTick.Ask, parameter.qty, OpenType.KaiKong); } } } } } #endregion }