Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

How to reset boolflag when reversing a strategy with scale orders?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    How to reset boolflag when reversing a strategy with scale orders?

    Hi!
    My strategy scales in and out of positions with three scale orders according to the reference samples provided by NT in this forum. I exit with SetStopLoss() and SetProfitTarget() in OnExecution() or reverse the strategy. Since I want to be able to reverse my strategy with EnterLong()/EnterShort in OnBarUpdate() I don't want to use
    Code:
    if (position.MarketPosition== MarketPosition.Flat)
    in my entry conditions. Insted I want to use a boolflag "OkToEnter" to prevent the first and seccond scale order to enter one more time after they have hit their SL or PT until the last scale order is closed. How can I set my bool flag so that the strategy can reverse? I'v tried OnPositionUpdate() but then the strategy freezes.

    Code:
    #region Variables
           
                private IOrder enterLong1a_StrategyName         = null; 
                private IOrder enterLong1b_StrategyName         = null; 
                private IOrder enterLong1c_StrategyName         = null;
                private IOrder enterShort1a_StrategyName         = null; 
                private IOrder enterShort1b_StrategyName         = null;
                private IOrder enterShort1c_StrategyName         = null; 
            
                [B][COLOR=Red]private bool OkToEnter    = true;[/COLOR][/B] // This variable is used to determin wether entry orders can be sent or not
            
            #endregion
    
            
            protected override void Initialize()
            {
                // Entry handling
                EntriesPerDirection = 1;
                   EntryHandling         = EntryHandling.UniqueEntries;
                
                CalculateOnBarClose = true;
            }
    
            /// <summary>
            /// Called on each bar update event (incoming tick)
            /// </summary>
            protected override void OnBarUpdate()
            {
                
                //Boolflag    
                if ([B][COLOR=Red]OkToEnter[/COLOR][/B])
                {    
                    //Entry conditions long
                    if( )
                        {
                            // Send entry orders
                            enterLong1a_StrategyName = EnterLong(scaleOrderQuantity1,"Long_1a_StrategyName");
                            enterLong1b_StrategyName = EnterLong(scaleOrderQuantity2, "Long_1b_StrategyName");
                            enterLong1c_StrategyName = EnterLong(scaleOrderQuantity3, "Long_1c_StrategyName");
                        }                
                
                    //Entry conditions short
                    if( )
                        {        
                            // Send entry orders
                            enterShort1a_StrategyName = EnterShort(scaleOrderQuantity1,"Short_1a_StrategyName");
                            enterShort1b_StrategyName = EnterShort(scaleOrderQuantity2,"Short_1b_StrategyName");
                            enterShort1c_StrategyName = EnterShort(scaleOrderQuantity3,"Short_1c_StrategyName");
                        }
                }
            }
            
            
            protected override void OnExecution(IExecution execution)
            {
                    //Long_1a
                    if (enterLong1a_StrategyName != null && enterLong1a_StrategyName.Token == execution.Order.Token)
                    {
                        if (execution.Order.OrderState == OrderState.Filled || execution.Order.OrderState == OrderState.PartFilled || (execution.Order.OrderState == OrderState.Cancelled && execution.Order.Filled > 0))
                        {
                            SetStopLoss("Long_1a_StrategyName", CalculationMode.Ticks, stopLoss, false);
                            SetProfitTarget("Long_1a_StrategyName", CalculationMode.Ticks, target1);
                                                                    
                            // Resets the entryOrder object to null after the order has been partially filled
                            if (execution.Order.OrderState != OrderState.PartFilled)
                            {
                                enterLong1a_StrategyName    = null;
                            }
                        }
                    }
                    //Long_1b
                    if (enterLong1b_StrategyName != null && enterLong1b_StrategyName.Token == execution.Order.Token)
                    {
                        if (execution.Order.OrderState == OrderState.Filled || execution.Order.OrderState == OrderState.PartFilled || (execution.Order.OrderState == OrderState.Cancelled && execution.Order.Filled > 0))
                        {
                            SetStopLoss("Long_1b_StrategyName", CalculationMode.Ticks, stopLoss, false);
                            SetProfitTarget("Long_1b_StrategyName", CalculationMode.Ticks, target2);
                                            
                            // Resets the entryOrder object to null after the order has been partially filled
                            if (execution.Order.OrderState != OrderState.PartFilled)
                            {
                                enterLong1b_StrategyName    = null;
                            }
                        }
                    }
                    //Long_1c + reset bool flag
                    if (enterLong1c_StrategyName != null && enterLong1c_StrategyName.Token == execution.Order.Token)
                    {
                        if (execution.Order.OrderState == OrderState.Filled || execution.Order.OrderState == OrderState.PartFilled || (execution.Order.OrderState == OrderState.Cancelled && execution.Order.Filled > 0))
                        {
                            SetStopLoss("Long_1c_StrategyName", CalculationMode.Ticks, stopLoss, false);
                            SetProfitTarget("Long_1c_StrategyName", CalculationMode.Ticks, target3);
                                            
                            // Resets the entryOrder object to null after the order has been partially filled
                            if (execution.Order.OrderState != OrderState.PartFilled)
                            {
                                enterLong1c_StrategyName    = null;
                               [B][COLOR=Red] OkToEnter    = false;[/COLOR][/B]
                            }
                        }
                    }
                    //Short_1a
                    if (enterShort1a_StrategyName != null && enterShort1a_StrategyName.Token == execution.Order.Token)
                    {
                        if (execution.Order.OrderState == OrderState.Filled || execution.Order.OrderState == OrderState.PartFilled || (execution.Order.OrderState == OrderState.Cancelled && execution.Order.Filled > 0))
                        {
                            SetStopLoss("Short_1a_StrategyName", CalculationMode.Ticks, stopLoss, false);
                            SetProfitTarget("Short_1a_StrategyName", CalculationMode.Ticks, target1);
                                                                    
                            // Resets the entryOrder object to null after the order has been partially filled
                            if (execution.Order.OrderState != OrderState.PartFilled)
                            {
                                enterShort1a_StrategyName    = null;
                            }
                        }
                    }
                    //Short_1b
                    if (enterShort1b_StrategyName != null && enterShort1b_StrategyName.Token == execution.Order.Token)
                    {
                        if (execution.Order.OrderState == OrderState.Filled || execution.Order.OrderState == OrderState.PartFilled || (execution.Order.OrderState == OrderState.Cancelled && execution.Order.Filled > 0))
                        {
                            SetStopLoss("Short_1b_StrategyName", CalculationMode.Ticks, stopLoss, false);
                            SetProfitTarget("Short_1b_StrategyName", CalculationMode.Ticks, target2);
                                                                    
                            // Resets the entryOrder object to null after the order has been partially filled
                            if (execution.Order.OrderState != OrderState.PartFilled)
                            {
                                enterShort1b_StrategyName    = null;
                            }
                        }
                    }
                    //Short_1c + reset bool flag
                    if (enterShort1c_StrategyName != null && enterShort1c_StrategyName.Token == execution.Order.Token)
                    {
                        if (execution.Order.OrderState == OrderState.Filled || execution.Order.OrderState == OrderState.PartFilled || (execution.Order.OrderState == OrderState.Cancelled && execution.Order.Filled > 0))
                        {
                            SetStopLoss("Short_1c_StrategyName", CalculationMode.Ticks, stopLoss, false);
                            SetProfitTarget("Short_1c_StrategyName", CalculationMode.Ticks, target3);
                                                                    
                            // Resets the entryOrder object to null after the order has been partially filled
                            if (execution.Order.OrderState != OrderState.PartFilled)
                            {
                                enterShort1c_StrategyName    = null;
                                [B][COLOR=Red]OkToEnter    = false;[/COLOR][/B]
                            }
                        }
                    }
            }
            
            protected override void OnPositionUpdate(IPosition position)
            {
                if (position.MarketPosition == MarketPosition.Flat)
                    {
                        // Set boolflag to allow entry setups
                        [COLOR=Red][B]OkToEnter    = true;[/B][/COLOR]
                    }
            }

    #2
    poseidon_sthlm, let's see if I follow you correctly - you use already unique signal names for the scaling aspects, so with EntryHandling set to unqiue and EntriesPerDirection set to 1, wouldn't this be the behavior you're looking for?

    Comment


      #3
      I don't want scaleorder1 and scaleorder2 to enter one more time when they have hit their profit targets before scaleorder 3 has closed. I want to achieve the same behaviour as if I would use
      Code:
      if (Position.MarketPosition == MarketPosition.Flat)
      in my entry conditions, besides I also want the strategy to be able to reverse. That's why I use the boolflag insted of
      Code:
       (Position.MarketPosition == MarketPosition.Flat)
      . My problem is that I obviously can't set the boolflag in OnPositionUpdate() as shown in the code below because the strategy then freezes. As you can see below I try to set the boolflag (OkToEnter = true) in OnPositionUpdate() and then reset the boolflag (OkToEnter = false) in OnExecution(). May the sequenze of these methods cause the freezing when reversing the strategy?
      Last edited by poseidon_sthlm; 05-07-2010, 09:41 AM.

      Comment


        #4
        poseidon_sthlm,

        You may benefit from just using two separate bools, one for your longs and one for your shorts. Then it will allow for reversing no problem. As you reverse, be sure to set the bool for the opposite direction to allow for trading, and the bool for the current direction to not allow for more trading. That would do the trick.
        Josh P.NinjaTrader Customer Service

        Comment


          #5
          Thanks Josh! The two separate bools did the trick.

          /Regards

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by Geovanny Suaza, 02-11-2026, 06:32 PM
          0 responses
          597 views
          0 likes
          Last Post Geovanny Suaza  
          Started by Geovanny Suaza, 02-11-2026, 05:51 PM
          0 responses
          343 views
          1 like
          Last Post Geovanny Suaza  
          Started by Mindset, 02-09-2026, 11:44 AM
          0 responses
          103 views
          0 likes
          Last Post Mindset
          by Mindset
           
          Started by Geovanny Suaza, 02-02-2026, 12:30 PM
          0 responses
          556 views
          1 like
          Last Post Geovanny Suaza  
          Started by RFrosty, 01-28-2026, 06:49 PM
          0 responses
          555 views
          1 like
          Last Post RFrosty
          by RFrosty
           
          Working...
          X