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      = MarkHigh.Price * 0.999;
                tp.ZhiSun = price - (MarkHigh.Price - MarkLow.Price) / 2;
            }
            if (ot == OpenType.KaiKong)
            {
                tp.Y      = MarkLow.Price * 1.001;
                tp.ZhiSun = price + (MarkHigh.Price - MarkLow.Price) / 2;
            }
            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)
                {
                    OpenWeiTuos.Remove(weituobianhao);
                    decimal realDealQty = (decimal)dealQty + (decimal)t.EnterPoint.PartDealQty - (decimal)t.EnterPoint.DealQty;
                    t.EnterPoint.DealQty = (double)((decimal)dealQty + (decimal)t.EnterPoint.PartDealQty);
                    if (currentTick.IsReal)
                    {
                        t.Status = OpenStatus.Opened;
                        RaiseMessage(new PolicyMessageEventArgs(string.Format("{0}-策略已接受开仓通知,成交数量:{1}", this.policyName, dealQty)));
                    }
                }
                else if (status == OpenStatus.Open)
                {
                    OpenWeiTuos.Add(weituobianhao, weituobianhao);
                    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)
                    {
                        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 (NeedCheDanWeiTuos.ContainsKey(weituobianhao))
                    {
                        NeedCheDanWeiTuos.Remove(weituobianhao);
                    }
                    RaiseMessage(new PolicyMessageEventArgs(string.Format("{0}-策略已接受入场撤单通知", this.policyName)));
                }
                else if (status == OpenStatus.CloseCanceled)
                {
                    if (NeedCheDanWeiTuos.ContainsKey(weituobianhao))
                    {
                        NeedCheDanWeiTuos.Remove(weituobianhao);
                    }
                    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 (tickdata.Time.TimeOfDay >= new TimeSpan(15, 00, 00))
                {
                    foreach (var item in tps)
                    {
                        PTradePoints tp = item.Value;
                        if (!tp.Finished)
                        {
                            if (tp.EnterPoint.OpenType == OpenType.KaiDuo)
                            {
                                Leave(tickdata.Bid, parameter.qty, tp.TradeGuid);
                            }
                            if (tp.EnterPoint.OpenType == OpenType.KaiKong)
                            {
                                Leave(tickdata.Ask, parameter.qty, tp.TradeGuid);
                            }
                        }
                    }
                }
                if (currentDay != tickdata.Time.Date)
                {
                    currentDay = tickdata.Time.Date;
                    this.Reset();
                }
                currentTick = tickdata;
                liveDataProcessor.ReceiveTick(tickdata);

                if (this.SecInfo.isLive(tickdata.Time.TimeOfDay))
                {
                    if (IsSimulateFinished || (!IsSimulateFinished && !tickdata.IsReal))
                    {
                        if (parameter.DebugModel)
                        {
                            if (CanOpen)
                            {
                                if (tickdata.Bid >= MarkHigh.Price * 0.999 && KaiKong)
                                {
                                    KaiKong = false;
                                    Open(tickdata.Bid, parameter.qty, OpenType.KaiKong);
                                }
                                if (tickdata.Ask <= MarkLow.Price * 1.001 && KaiDuo)
                                {
                                    KaiDuo = false;
                                    Open(tickdata.Ask, parameter.qty, OpenType.KaiDuo);
                                }
                            }
                            foreach (var item in tps)
                            {
                                PTradePoints tp = item.Value;
                                if (!tp.Finished)
                                {
                                    if (tp.EnterPoint.OpenType == OpenType.KaiDuo)
                                    {
                                        if (tickdata.Bid >= tp.Y)
                                        {
                                            KaiDuo = true;
                                            Leave(tickdata.Bid, parameter.qty, tp.TradeGuid);
                                        }
                                    }
                                    if (tp.EnterPoint.OpenType == OpenType.KaiKong)
                                    {
                                        if (tickdata.Ask <= tp.Y)
                                        {
                                            KaiKong = true;
                                            Leave(tickdata.Ask, parameter.qty, tp.TradeGuid);
                                        }
                                    }
                                }
                            }
                        }
                        #region 止盈、止损
                        foreach (var item in tps)
                        {
                            PTradePoints tp = item.Value;
                            if (!tp.Finished)
                            {
                                double qty = tp.EnterPoint.DealQty - (tp.OutPoint != null ? tp.OutPoint.OpenQty : 0);
                                if (qty > 0)
                                {
                                    if (tp.EnterPoint.OpenType == OpenType.KaiDuo)
                                    {
                                        if (tickdata.Bid <= tp.ZhiSun)
                                        {
                                            if (tp.OutPoint != null)
                                            {
                                                for (int i = 0; i < tp.OutPoint.OpenPointWeiTuo.Count; i++)
                                                {
                                                    if (tp.OutPoint.OpenPointWeiTuo[i].OpenQty > tp.OutPoint.OpenPointWeiTuo[i].DealQty)
                                                    {
                                                        string wtbh = tp.OutPoint.OpenPointWeiTuo[i].Weituobianhao;
                                                        NeedCheDanWeiTuos.Add(wtbh, wtbh);
                                                    }
                                                }
                                            }
                                            KaiDuo = true;
                                            Leave(tickdata.Bid, qty, tp.TradeGuid);
                                        }
                                    }
                                    else if (tp.EnterPoint.OpenType == OpenType.KaiKong)
                                    {
                                        if (tickdata.Ask >= tp.ZhiSun)
                                        {
                                            if (tp.OutPoint != null)
                                            {
                                                for (int i = 0; i < tp.OutPoint.OpenPointWeiTuo.Count; i++)
                                                {
                                                    if (tp.OutPoint.OpenPointWeiTuo[i].OpenQty > tp.OutPoint.OpenPointWeiTuo[i].DealQty)
                                                    {
                                                        string wtbh = tp.OutPoint.OpenPointWeiTuo[i].Weituobianhao;
                                                        NeedCheDanWeiTuos.Add(wtbh, wtbh);
                                                    }
                                                }
                                            }
                                            KaiKong = true;
                                            Leave(tickdata.Ask, qty, tp.TradeGuid);
                                        }
                                    }
                                }
                            }
                        }
                        #endregion
                    }
                }
                LiveDataUpdate(tickdata);
            }
        }