Example #1
0
        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);
            }
        }
Example #2
0
        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);
        }
Example #3
0
 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);
                 }
             }
         }
     }
 }
Example #4
0
        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)));
                }
            }
        }
Example #5
0
        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)
                {
                    currentDay = tickdata.Time.Date;
                    this.Reset();
                }
                currentTick = tickdata;
                liveDataProcessor.ReceiveTick(tickdata);

                if (this.SecInfo.isLive(tickdata.Time.TimeOfDay))
                {
                    if (IsSimulateFinished || (!IsSimulateFinished && !tickdata.IsReal))
                    {
                        if (MarkHigh != 0 && MarkLow != 0)
                        {
                            if (tickdata.Last > MarkHigh && MarkHigh > MarkLow)
                            {
                                if (DuoHands + FrozenDuoHands == 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(tickdata.Ask, qty, tp.TradeGuid);
                                                    }
                                                }
                                            }
                                        }
                                    }
                                    FrozenDuoHands += (Decimal)parameter.qty;
                                    Open(tickdata.Bid, parameter.qty, OpenType.KaiDuo);
                                }
                            }
                            if (tickdata.Last < MarkLow && MarkHigh > MarkLow)
                            {
                                if (KongHands + FrozenKongHands == 0)
                                {
                                    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(tickdata.Bid, qty, tp.TradeGuid);
                                                    }
                                                }
                                            }
                                        }
                                    }
                                    FrozenKongHands += (Decimal)parameter.qty;
                                    Open(tickdata.Ask, parameter.qty, OpenType.KaiKong);
                                }
                            }
                        }
                        #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.StartZhiYing)
                                    {
                                        if (tp.EnterPoint.OpenType == OpenType.KaiDuo)
                                        {
                                            if (tickdata.Last >= tp.Y)
                                            {
                                                tp.StartZhiYing = true;
                                            }
                                        }
                                        else if (tp.EnterPoint.OpenType == OpenType.KaiKong)
                                        {
                                            if (tickdata.Last <= tp.Y)
                                            {
                                                tp.StartZhiYing = true;
                                            }
                                        }
                                    }
                                    else
                                    {
                                        if (tp.EnterPoint.OpenType == OpenType.KaiDuo)
                                        {
                                            if (tickdata.Last > tp.Y)
                                            {
                                                tp.Y = tickdata.Last;
                                            }
                                            if ((tp.Y - tickdata.Last) / (tp.Y - tp.EnterPoint.OpenPrice) > parameter.HuiCheBiLi)
                                            {
                                                Leave(tickdata.Ask, qty, tp.TradeGuid);
                                            }
                                        }
                                        else
                                        {
                                            if (tickdata.Last < tp.Y)
                                            {
                                                tp.Y = tickdata.Last;
                                            }
                                            if ((tickdata.Last - tp.Y) / (tp.EnterPoint.OpenPrice - tp.Y) > parameter.HuiCheBiLi)
                                            {
                                                Leave(tickdata.Bid, qty, tp.TradeGuid);
                                            }
                                        }
                                    }
                                    if (tp.EnterPoint.OpenType == OpenType.KaiDuo)
                                    {
                                        if (tickdata.Last <= tp.ZhiSun)
                                        {
                                            Leave(tickdata.Ask, qty, tp.TradeGuid);
                                        }
                                    }
                                    else if (tp.EnterPoint.OpenType == OpenType.KaiKong)
                                    {
                                        if (tickdata.Last >= tp.ZhiSun)
                                        {
                                            Leave(tickdata.Bid, qty, tp.TradeGuid);
                                        }
                                    }
                                }
                            }
                        }
                        #endregion
                    }
                }
                LiveDataUpdate(tickdata);
            }
        }