Exemplo n.º 1
0
        public override void Notify(Guid tradeGuid, OpenStatus status, double dealQty = 0, double dealPrice = 0, string weituobianhao = "", string pendWeituobianhao = "")
        {
            lock (lock_tps)
            {
                for (int i = 0; i < tps.ToArray().Count(); i++)
                {
                    var t = tps[i];
                    if (t.TradeGuid == tradeGuid)
                    {
                        if (status == OpenStatus.Opened)
                        {
                            t.EnterPoint.OpenPrice = dealPrice;
                            decimal realDealQty = (decimal)dealQty + (decimal)t.EnterPoint.PartDealQty - (decimal)t.EnterPoint.DealQty;
                            holdHands           += realDealQty;
                            t.Status             = OpenStatus.Opened;
                            frozenBuyHands      -= realDealQty;
                            t.EnterPoint.DealQty = (double)((decimal)dealQty + (decimal)t.EnterPoint.PartDealQty);
                            RaiseMessage(new PolicyMessageEventArgs("策略已接受开仓通知"));
                            if (t.EnterPoint.SecInfo.Key == eosbtcsi.Key)
                            {
                                double huanbiqty = Math.Floor(dealPrice * (double)realDealQty * SecInfo.JingDu) / SecInfo.JingDu;
                                double eosqty    = Math.Floor((double)realDealQty * eosusdtsi.JingDu) / eosusdtsi.JingDu;
                                if (t.EnterPoint.OpenType == OpenType.Buy)
                                {
                                    if ((double)realDealQty > eosusdtsi.MinQty)
                                    {
                                        OpenArgs oa = new OpenArgs(
                                            eosusdt.Bid,
                                            eosqty,
                                            OpenType.Sell,
                                            eosusdtsi,
                                            eosusdt,
                                            t.TpRemark);
                                        OpenDelete od = new OpenDelete(OpenThread);
                                        od.BeginInvoke(oa, null, null);
                                        EosUsdtStatus = false;
                                    }
                                    if (huanbiqty >= SecInfo.MinQty)
                                    {
                                        OpenArgs oa2 = new OpenArgs(
                                            currentTick.Ask,
                                            huanbiqty,
                                            OpenType.Buy,
                                            SecInfo,
                                            currentTick,
                                            t.TpRemark);
                                        OpenDelete od2 = new OpenDelete(OpenThread);
                                        od2.BeginInvoke(oa2, null, null);
                                        BtcUsdtStatus = false;
                                    }
                                }
                                else
                                {
                                    if ((double)realDealQty > eosusdtsi.MinQty)
                                    {
                                        OpenArgs oa = new OpenArgs(
                                            eosusdt.Ask,
                                            eosqty,
                                            OpenType.Buy,
                                            eosusdtsi,
                                            eosusdt,
                                            t.TpRemark);
                                        OpenDelete od = new OpenDelete(OpenThread);
                                        od.BeginInvoke(oa, null, null);
                                        EosUsdtStatus = false;
                                    }
                                    if (huanbiqty >= SecInfo.MinQty)
                                    {
                                        OpenArgs oa2 = new OpenArgs(
                                            currentTick.Bid,
                                            huanbiqty,
                                            OpenType.Sell,
                                            SecInfo,
                                            currentTick,
                                            t.TpRemark);
                                        OpenDelete od2 = new OpenDelete(OpenThread);
                                        od2.BeginInvoke(oa2, null, null);
                                        BtcUsdtStatus = false;
                                    }
                                }

                                EosBtcStatus = true;
                            }
                            else if (t.EnterPoint.SecInfo.Key == eosusdtsi.Key)
                            {
                                EosUsdtStatus = true;
                            }
                            else if (t.EnterPoint.SecInfo.Key == SecInfo.Key)
                            {
                                BtcUsdtStatus = true;
                            }
                        }
                        else if (status == OpenStatus.Open)
                        {
                            RaiseMessage(new PolicyMessageEventArgs(string.Format("{0}-策略已接受开仓下单通知", this.policyName)));
                        }
                        else if (status == OpenStatus.Close)
                        {
                            RaiseMessage(new PolicyMessageEventArgs(string.Format("{0}-策略已接受平仓下单通知", this.policyName)));
                        }
                        else if (status == OpenStatus.Closed)
                        {
                            RaiseMessage(new PolicyMessageEventArgs(string.Format("{0}-策略已接受平仓通知", this.policyName)));
                        }
                        else if (status == OpenStatus.OpenPending)
                        {
                            t.EnterPoint.PartDealQty = t.EnterPoint.DealQty;
                            RaiseMessage(new PolicyMessageEventArgs(string.Format("{0}-策略HoldHands:{1}", this.policyName, holdHands)));
                        }
                        else if (status == OpenStatus.ClosePending)
                        {
                            RaiseMessage(new PolicyMessageEventArgs(string.Format("{0}-策略HoldHands:{1}", this.policyName, holdHands)));
                        }
                        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;
                                holdHands           += realDealQty;
                                frozenBuyHands      -= realDealQty;
                                t.EnterPoint.DealQty = dealQty + t.EnterPoint.PartDealQty;
                                if (t.EnterPoint.SecInfo.Key == eosbtcsi.Key)
                                {
                                    double huanbiqty = Math.Floor(dealPrice * (double)realDealQty * SecInfo.JingDu) / SecInfo.JingDu;
                                    double eosqty    = Math.Floor((double)realDealQty * eosusdtsi.JingDu) / eosusdtsi.JingDu;
                                    if (t.EnterPoint.OpenType == OpenType.Buy)
                                    {
                                        if ((double)realDealQty > eosusdtsi.MinQty)
                                        {
                                            OpenArgs oa = new OpenArgs(
                                                eosusdt.Bid,
                                                eosqty,
                                                OpenType.Sell,
                                                eosusdtsi,
                                                eosusdt,
                                                t.TpRemark);
                                            OpenDelete od = new OpenDelete(OpenThread);
                                            od.BeginInvoke(oa, null, null);
                                            EosUsdtStatus = false;
                                        }
                                        if (huanbiqty >= SecInfo.MinQty)
                                        {
                                            OpenArgs oa2 = new OpenArgs(
                                                currentTick.Ask,
                                                huanbiqty,
                                                OpenType.Buy,
                                                SecInfo,
                                                currentTick,
                                                t.TpRemark);
                                            OpenDelete od2 = new OpenDelete(OpenThread);
                                            od2.BeginInvoke(oa2, null, null);
                                            BtcUsdtStatus = false;
                                        }
                                    }
                                    else
                                    {
                                        if ((double)realDealQty > eosusdtsi.MinQty)
                                        {
                                            OpenArgs oa = new OpenArgs(
                                                eosusdt.Ask,
                                                eosqty,
                                                OpenType.Buy,
                                                eosusdtsi,
                                                eosusdt,
                                                t.TpRemark);
                                            OpenDelete od = new OpenDelete(OpenThread);
                                            od.BeginInvoke(oa, null, null);
                                            EosUsdtStatus = false;
                                        }
                                        if (huanbiqty >= SecInfo.MinQty)
                                        {
                                            OpenArgs oa2 = new OpenArgs(
                                                currentTick.Bid,
                                                huanbiqty,
                                                OpenType.Sell,
                                                SecInfo,
                                                currentTick,
                                                t.TpRemark);
                                            OpenDelete od2 = new OpenDelete(OpenThread);
                                            od2.BeginInvoke(oa2, null, null);
                                            BtcUsdtStatus = false;
                                        }
                                    }
                                    EosBtcStatus = true;
                                }
                                RaiseMessage(new PolicyMessageEventArgs(string.Format("{0}-策略已接受部分入场成交通知,成交数量:{1}", this.policyName, dealQty)));
                            }
                        }
                        else if (status == OpenStatus.PartClosed)
                        {
                            if (dealQty > (t.OutPoint.DealQty - t.OutPoint.PartDealQty))
                            {
                                RaiseMessage(new PolicyMessageEventArgs(string.Format("{0}-策略已接受部分出场成交通知,成交数量:{1}", this.policyName, dealQty)));
                                decimal realDealQty = (decimal)dealQty + (decimal)t.OutPoint.PartDealQty - (decimal)t.OutPoint.DealQty;
                                holdHands         -= realDealQty;
                                frozenSellHands   -= realDealQty;
                                t.OutPoint.DealQty = dealQty + t.OutPoint.PartDealQty;
                            }
                        }
                        else if (status == OpenStatus.OpenCanceled)
                        {
                            if (t.EnterPoint.SecInfo.Key == eosbtcsi.Key)
                            {
                                EosBtcStatus = true;
                            }
                            RaiseMessage(new PolicyMessageEventArgs(string.Format("{0}-策略已接受入场撤单通知", this.policyName)));
                        }
                        else if (status == OpenStatus.CloseCanceled)
                        {
                            frozenSellHands -= (decimal)dealQty;
                            RaiseMessage(new PolicyMessageEventArgs(string.Format("{0}-策略已接受出场撤单通知", this.policyName)));
                        }
                        else if (status == OpenStatus.OpenRest)
                        {
                            RaiseMessage(new PolicyMessageEventArgs(string.Format("{0}-策略已接收开仓剩余通知,数量:{1}", this.policyName, dealQty)));
                        }
                        else if (status == OpenStatus.CloseRest)
                        {
                            holdHands       -= (decimal)dealQty;
                            frozenSellHands -= (decimal)dealQty;
                            RaiseMessage(new PolicyMessageEventArgs(string.Format("{0}-策略已接收平仓剩余通知,数量:{1}", this.policyName, dealQty)));
                        }
                    }
                }
            }
        }
Exemplo n.º 2
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)
                {
                    loseArr.Add(loseCount);
                    currentDay = tickdata.Time.Date;
                    this.Reset();
                }
                currentTick = tickdata;
                SecondTick  = CurrentStockData.GetTick(SecondSi);
                liveDataProcessor.ReceiveTick(tickdata);

                if (this.SecInfo.isLive(tickdata.Time.TimeOfDay))
                {
                    if (IsSimulateFinished || (!IsSimulateFinished && !tickdata.IsReal))
                    {
                        if (CanOpen())
                        {
                            if (Math.Abs((tickdata.Time - SecondTick.Time).Seconds) <= 1)
                            {
                                double a = tickdata.Bid - SecondTick.Ask;
                                double b = (tickdata.Bid * parameter.Fee + SecondTick.Ask * parameter.SecondFee + tickdata.Last * parameter.Lirun);
                                double c = SecondTick.Bid - tickdata.Ask;
                                double d = (tickdata.Ask * parameter.Fee + SecondTick.Bid * parameter.SecondFee + tickdata.Last * parameter.Lirun);
                                List <CurrentBanalce> ACBS = CurrentBalances.getCurrentBalance(SecInfo.Market);
                                List <CurrentBanalce> BCBS = CurrentBalances.getCurrentBalance(parameter.SecondMarket);
                                foreach (CurrentBanalce aCBS in ACBS)
                                {
                                    if (aCBS.Code.ToUpper() == parameter.Code.ToUpper() || aCBS.Code.ToUpper() == parameter.Code.ToUpper() + "-SPOT")
                                    {
                                        if (aCBS.Ava > parameter.Qty * 2)
                                        {
                                            Code1AtoB = true;
                                        }
                                        else
                                        {
                                            Code1AtoB = false;
                                        }
                                    }
                                    if (aCBS.Code.ToUpper() == parameter.Code2.ToUpper() || aCBS.Code.ToUpper() == parameter.Code2.ToUpper() + "-SPOT")
                                    {
                                        if (aCBS.Ava > parameter.Qty * tickdata.Last * 2)
                                        {
                                            Code2BtoA = true;
                                        }
                                        else
                                        {
                                            Code2BtoA = false;
                                        }
                                    }
                                }
                                foreach (CurrentBanalce bCBS in BCBS)
                                {
                                    if (bCBS.Code.ToUpper() == parameter.Code.ToUpper() || bCBS.Code.ToUpper() == parameter.Code.ToUpper() + "-SPOT")
                                    {
                                        if (bCBS.Ava > parameter.Qty * 2)
                                        {
                                            Code1BtoA = true;
                                        }
                                        else
                                        {
                                            Code1BtoA = false;
                                        }
                                    }
                                    if (bCBS.Code.ToUpper() == parameter.Code2.ToUpper() || bCBS.Code.ToUpper() == parameter.Code2.ToUpper() + "-SPOT")
                                    {
                                        if (bCBS.Ava > parameter.Qty * tickdata.Last * 2)
                                        {
                                            Code2AtoB = true;
                                        }
                                        else
                                        {
                                            Code2AtoB = false;
                                        }
                                    }
                                }
                                if (a > b)
                                {
                                    if (Code1AtoB && Code2AtoB)
                                    {
                                        firstDeal  = false;
                                        secondDeal = false;
                                        string   tr = DateTime.Now.ToString("yyyyMMddHHmmss") + Rand6Int().ToString();
                                        OpenArgs oa = new OpenArgs(
                                            tickdata.Bid - parameter.HuaDian,
                                            parameter.qty,
                                            OpenType.Sell,
                                            SecInfo,
                                            tickdata,
                                            tr);
                                        OpenDelete od = new OpenDelete(OpenThread);
                                        od.BeginInvoke(oa, null, null);
                                        OpenArgs oa2 = new OpenArgs(
                                            SecondTick.Ask + parameter.HuaDian,
                                            parameter.qty,
                                            OpenType.Buy,
                                            SecondSi,
                                            SecondTick,
                                            tr);
                                        OpenDelete od2 = new OpenDelete(OpenThread);
                                        od2.BeginInvoke(oa2, null, null);
                                    }
                                    else
                                    {
                                        loseCount += 1;
                                    }
                                }
                                if (c > d)
                                {
                                    if (Code1BtoA && Code2BtoA)
                                    {
                                        firstDeal  = false;
                                        secondDeal = false;
                                        string   tr = DateTime.Now.ToString("yyyyMMddHHmmss") + Rand6Int().ToString();
                                        OpenArgs oa = new OpenArgs(
                                            tickdata.Ask + parameter.HuaDian,
                                            parameter.qty,
                                            OpenType.Buy,
                                            SecInfo,
                                            tickdata,
                                            tr);
                                        OpenDelete od = new OpenDelete(OpenThread);
                                        od.BeginInvoke(oa, null, null);
                                        OpenArgs oa2 = new OpenArgs(
                                            SecondTick.Bid - parameter.HuaDian,
                                            parameter.qty,
                                            OpenType.Sell,
                                            SecondSi,
                                            SecondTick,
                                            tr);
                                        OpenDelete od2 = new OpenDelete(OpenThread);
                                        od2.BeginInvoke(oa2, null, null);
                                    }
                                    else
                                    {
                                        loseCount += 1;
                                    }
                                }
                            }
                        }
                    }
                }
                LiveDataUpdate(tickdata);
            }
        }
Exemplo n.º 3
0
 public override void Notify(Guid tradeGuid, OpenStatus status, double dealQty = 0, double dealPrice = 0, string weituobianhao = "", string pendWeituobianhao = "")
 {
     if (tps.ContainsKey(tradeGuid))
     {
         TradePoints t = tps[tradeGuid];
         if (t.TradeGuid == tradeGuid)
         {
             if (status == OpenStatus.Opened)
             {
                 if (t.EnterPoint.SecInfo.Key == SecInfo.Key)
                 {
                     firstDeal = true;
                 }
                 if (t.EnterPoint.SecInfo.Key == SecondSi.Key)
                 {
                     secondDeal = true;
                 }
                 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.OpenPending)
             {
                 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.OpenRest)
             {
                 if (t.EnterPoint.SecInfo.Key == SecInfo.Key)
                 {
                     firstDeal = true;
                 }
                 if (t.EnterPoint.SecInfo.Key == SecondSi.Key)
                 {
                     secondDeal = true;
                 }
                 if (t.EnterPoint.DealQty == 0)
                 {
                     t.Finished = true;
                 }
                 RaiseMessage(new PolicyMessageEventArgs(string.Format("{0}-策略已接受入场撤单通知", this.policyName)));
             }
             else if (status == OpenStatus.OpenCanceled)
             {
                 if (t.EnterPoint.SecInfo.Key == SecInfo.Key)
                 {
                     double openPrice = 0;
                     if (t.EnterPoint.OpenType == OpenType.Buy)
                     {
                         openPrice = currentTick.Ask;
                     }
                     else
                     {
                         openPrice = currentTick.Bid;
                     }
                     OpenArgs oa = new OpenArgs(
                         openPrice,
                         parameter.qty,
                         t.EnterPoint.OpenType,
                         SecInfo,
                         currentTick,
                         t.TpRemark);
                     OpenDelete od = new OpenDelete(OpenThread);
                     od.BeginInvoke(oa, null, null);
                 }
                 if (t.EnterPoint.SecInfo.Key == SecondSi.Key)
                 {
                     double openPrice = 0;
                     if (t.EnterPoint.OpenType == OpenType.Buy)
                     {
                         openPrice = SecondTick.Ask;
                     }
                     else
                     {
                         openPrice = SecondTick.Bid;
                     }
                     OpenArgs oa = new OpenArgs(
                         openPrice,
                         parameter.qty,
                         t.EnterPoint.OpenType,
                         SecondSi,
                         SecondTick,
                         t.TpRemark);
                     OpenDelete od = new OpenDelete(OpenThread);
                     od.BeginInvoke(oa, null, null);
                 }
                 RaiseMessage(new PolicyMessageEventArgs(string.Format("{0}-策略已接受入场撤单通知", this.policyName)));
             }
             else if (status == OpenStatus.CloseCanceled)
             {
                 RaiseMessage(new PolicyMessageEventArgs(string.Format("{0}-策略已接受出场撤单通知", this.policyName)));
             }
         }
     }
 }
Exemplo n.º 4
0
        protected override void dataReceiver_Data_Arrival(object sender, StockData.TickData tickdata)
        {
            //Console.WriteLine("openSide:{0},signInHighPrice:{1},signInLowPrice:{2},tickdata.Last:{3},dealState:{4},openComplete:{5},closeComplete:{6}", openSide, signInHighPrice, signInLowPrice, tickdata.Last, dealState, openComplete, closeComplete);
            //Console.WriteLine("Ratio:{0}", Ratio);
            if (tickdata.Code == string.Empty)
            {
                IsSimulateFinished = true;
                HistoryFinished();
                LiveDataUpdate(tickdata);
            }
            else
            {
                if (currentDay != tickdata.Time.Date)
                {
                    currentDay = tickdata.Time.Date;
                    this.Reset();
                }
                if (tickdata.Last == 0)
                {
                    return;
                }
                liveDataProcessor.ReceiveTick(tickdata);
                currentTick = tickdata;
                if (parameter.StartTime <= tickdata.Time.TimeOfDay && parameter.EndTime >= tickdata.Time.TimeOfDay && init)
                {
                    if (IsSimulateFinished || (!IsSimulateFinished && !tickdata.IsReal))
                    {
                        if (dealState == true && openComplete && closeComplete)
                        {
                            dealState = false;
                        }

                        if (dealState == false)
                        {
                            if (openHands > 0)
                            {
                            }
                            if (tickdata.Last > signInHighPrice && openSide != 1)
                            {
                                if (openSide == 2)
                                {
                                    foreach (TradePoints tp in tps)
                                    {
                                        if (!tp.Finished)
                                        {
                                            if (!tickdata.IsReal)
                                            {
                                                tp.Finished = true;
                                                Leave(tp.EnterPoint.OpenQty, tickdata.Last, tp.TradeGuid);
                                            }
                                            else
                                            {
                                                //平空
                                                closeComplete = false;
                                                dealState     = true;
                                                CloseDelete od = new CloseDelete(CloseThread);
                                                od.BeginInvoke(parameter.qty, tickdata.Last, tp.TradeGuid, null, null);
                                            }
                                        }
                                    }
                                }

                                if (Ratio <= parameter.Ratio)
                                {
                                    if (!tickdata.IsReal)
                                    {
                                        openSide     = 1;
                                        openComplete = false;
                                        dealState    = true;
                                        openHands    = parameter.qty;
                                        OpenArgs oa = new OpenArgs(
                                            tickdata.Last,
                                            parameter.qty,
                                            OpenType.KaiDuo,
                                            SecInfo,
                                            tickdata,
                                            -1,
                                            "");
                                        OpenDelete od = new OpenDelete(OpenThread);
                                        od.BeginInvoke(oa, null, null);
                                    }
                                    else
                                    {
                                        openSide   = 1;
                                        holdHands += (decimal)parameter.Qty;
                                        Open(tickdata.Last, parameter.qty, OpenType.KaiDuo, SecInfo, tickdata, -1, "");
                                    }
                                }
                            }
                            else if (tickdata.Last < signInLowPrice && openSide != 2)
                            {
                                if (openSide == 1)
                                {
                                    foreach (TradePoints tp in tps)
                                    {
                                        if (!tp.Finished)
                                        {
                                            if (!tickdata.IsReal)
                                            {
                                                tp.Finished = true;
                                                Leave(tp.EnterPoint.OpenQty, tickdata.Last, tp.TradeGuid);
                                            }
                                            else
                                            {
                                                //平多
                                                closeComplete = false;
                                                dealState     = true;
                                                CloseDelete od = new CloseDelete(CloseThread);
                                                od.BeginInvoke(parameter.qty, tickdata.Last, tp.TradeGuid, null, null);
                                            }
                                        }
                                    }
                                }

                                if (Ratio <= parameter.Ratio)
                                {
                                    if (!tickdata.IsReal)
                                    {
                                        openSide     = 2;
                                        openComplete = false;
                                        dealState    = true;
                                        openHands    = parameter.qty;
                                        OpenArgs oa = new OpenArgs(
                                            tickdata.Last,
                                            parameter.qty,
                                            OpenType.KaiKong,
                                            SecInfo,
                                            tickdata,
                                            -1,
                                            "");
                                        OpenDelete od = new OpenDelete(OpenThread);
                                        od.BeginInvoke(oa, null, null);
                                    }
                                    else
                                    {
                                        openSide = 2;
                                        Open(tickdata.Last, parameter.qty, OpenType.KaiKong, SecInfo, tickdata, -1, "");
                                    }
                                }
                            }
                        }
                    }
                }
                LiveDataUpdate(tickdata);
                if (parameter.IsReal)
                {
                    try
                    {
                        UpdateChart();
                    }
                    catch { }
                }
            }
        }