Exemplo n.º 1
0
        public void Exit(LegExitType et1, LegExitType et2)
        {
            if (entry[0].CheckInflight)
            {
                return;
            }
            if (entry[1].CheckInflight)
            {
                return;
            }
            if (exit[0].CheckInflight)
            {
                return;
            }
            if (exit[0].CheckInflight)
            {
                return;
            }
            if (exitinprogress)
            {
                return;
            }
            exitinprogress = true;
            //FixInFlight();
            int tmp1 = lots1;
            int tmp2 = lots2;

            // this will correct for unfilled orders
            if (mleg1 == NinjaTrader.Cbi.MarketPosition.Long)
            {
                tmp1 -= (lots1 - positionX);
                tmp2 -= (lots2 + positionY);
            }

            if (mleg1 == NinjaTrader.Cbi.MarketPosition.Short)
            {
                tmp1 -= (lots1 + positionX);
                tmp2 -= (lots2 - positionY);
            }
            ExitLots(tmp1, tmp2, et1, et2);
        }
Exemplo n.º 2
0
        public void ExitLots(int _lots1, int _lots2, LegExitType et1, LegExitType et2)
        {
            if (mleg1 == NinjaTrader.Cbi.MarketPosition.Long)
            {
                _toexitX = _lots1;
                _toexitY = -_lots2;
            }
            if (mleg1 == NinjaTrader.Cbi.MarketPosition.Short)
            {
                _toexitX = -_lots1;
                _toexitY = _lots2;
            }
            entry[0].Cancel(!exitinprogress);
            entry[1].Cancel(!exitinprogress);

            exit[0].Cancel(true);
            exit[1].Cancel(true);

            double limitleg1 = 0;
            double limitleg2 = 0;

            OrderAction[] o      = { OrderAction.Sell, OrderAction.BuyToCover };
            string[]      o_name = { "Sell_X", "BuyToCover_Y" };

            if (mleg1 == MarketPosition.Long)
            {
                if (et1 == LegExitType.JoinBidOrAsk)
                {
                    if (_lots1 > 0)
                    {
                        limitleg1 = strategybase.GetCurrentAsk(0);
                    }
                    if (_lots1 < 0)
                    {
                        limitleg1 = strategybase.GetCurrentBid(0);
                    }
                }
                if (et2 == LegExitType.JoinBidOrAsk)
                {
                    if (_lots2 > 0)
                    {
                        limitleg2 = strategybase.GetCurrentBid(1);
                    }
                    if (_lots2 < 0)
                    {
                        limitleg2 = strategybase.GetCurrentAsk(1);
                    }
                }
            }

            if (mleg1 == MarketPosition.Short)
            {
                o      = new OrderAction[] { OrderAction.Buy, OrderAction.SellShort };
                o_name = new String[] { "BuyToCover_X", "Sell_Y" };

                if (et1 == LegExitType.JoinBidOrAsk)
                {
                    if (_lots1 > 0)
                    {
                        limitleg1 = strategybase.GetCurrentBid(0);
                    }
                    if (_lots1 < 0)
                    {
                        limitleg1 = strategybase.GetCurrentAsk(0);
                    }
                }
                if (et2 == LegExitType.JoinBidOrAsk)
                {
                    if (_lots2 > 0)
                    {
                        limitleg2 = strategybase.GetCurrentAsk(1);
                    }
                    if (_lots2 < 0)
                    {
                        limitleg2 = strategybase.GetCurrentBid(1);
                    }
                }
            }


            if (strategybase.TraceOrders)
            {
                string tmp = strategybase.Time[0].ToString();
                if (positionX != 0)
                {
                    tmp += " Exit:" + strategybase.Instruments[0].FullName + " " + o_name[0];
                }
                if (positionY != 0)
                {
                    tmp += "; " + strategybase.Instruments[1].FullName + " " + o_name[1];
                }
                if (positionX != 0 || positionY != 0)
                {
                    strategybase.Print(tmp);
                }
            }

            if (_lots1 > 0)
            {
                _exitedX      = 0;
                exit[0].Order = strategybase.SubmitOrderUnmanaged(0, o[0], (limitleg1 == 0) ? OrderType.Market : OrderType.Limit, _lots1, limitleg1, 0, "", o_name[0]);
            }
            else if (_lots1 < 0)
            {
                if (strategybase.TraceOrders)
                {
                    strategybase.Print("WARNING: Compensating overfill Leg1(" + (-1 * _lots1).ToString() + ")");
                }
                exit[0].Order = strategybase.SubmitOrderUnmanaged(0, o[1], (limitleg1 == 0) ? OrderType.Market : OrderType.Limit, -_lots1, limitleg1, 0, "", o_name[0]);
            }

            if (_lots2 > 0)
            {
                _exitedY      = 0;
                exit[1].Order = strategybase.SubmitOrderUnmanaged(1, o[1], (limitleg2 == 0) ? OrderType.Market : OrderType.Limit, _lots2, limitleg2, 0, "", o_name[1]);
            }
            else if (_lots2 < 0)
            {
                if (strategybase.TraceOrders)
                {
                    strategybase.Print("WARNING: Compensating overfill Leg2(" + (-1 * _lots2).ToString() + ")");
                }
                exit[1].Order = strategybase.SubmitOrderUnmanaged(1, o[0], (limitleg2 == 0) ? OrderType.Market : OrderType.Limit, -_lots2, limitleg2, 0, "", o_name[1]);
            }
        }
Exemplo n.º 3
0
        public void PartialExit(int units2exit, LegExitType et1, LegExitType et2)
        {
            if (entry[0].CheckInflight)
            {
                return;
            }
            if (entry[1].CheckInflight)
            {
                return;
            }
            if (exit[0].CheckInflight)
            {
                return;
            }
            if (exit[0].CheckInflight)
            {
                return;
            }
            if (exitinprogress)
            {
                return;
            }

            if (strategybase.TraceOrders)
            {
                strategybase.Print("PartialExit, " + units2exit.ToString() + " units2exit");
            }

            if (units2exit == 0)
            {
                return;
            }
            if (units2exit > units)
            {
                string tmp = "units2exit(" + units2exit.ToString() + ") can only exit " + units.ToString() + " units";
                string f   = "SingleSpread.PartialExit:";
                Zweistein.NinjaTraderLog.Process(f, tmp, LogLevel.Error, LogCategories.Strategy);
                if (strategybase.TraceOrders)
                {
                    strategybase.Print("ERROR " + f + tmp);
                }
                return;
            }


            exitinprogress = true;
            //FixInFlight();
            int tmp1 = lots1 * units2exit;
            int tmp2 = lots2 * units2exit;


            // this will correct for unfilled orders
            if (mleg1 == NinjaTrader.Cbi.MarketPosition.Long)
            {
                tmp1 -= (lots1 - positionX);
                tmp2 -= (lots2 + positionY);
            }

            if (mleg1 == NinjaTrader.Cbi.MarketPosition.Short)
            {
                tmp1 -= (lots1 + positionX);
                tmp2 -= (lots2 - positionY);
            }
            ExitLots(tmp1, tmp2, et1, et2);
            //    units -= units2exit;
            lots1 = tmp1;
            lots2 = tmp2;
        }