Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

Handling reentry after stop trigger

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

    Handling reentry after stop trigger

    I have a strategy which generates "buy" and "sell" signals. Now if I set a stop loss for a long trade using setstoploss function and it gets triggered , I want to ignore all the subsequent buy signals and wait until a "sell" signal is generated. For this I need to know whenever a stop gets triggered so that I can set a flag. How do I do this ?

    #2
    Hello Sgeesala,

    Thank you for your post

    You can use the OnOrderUpdate() method to capture when the Stop Loss order it filled and then set a boolean variable to true.

    Example:
    Code:
    protected override void OnOrderUpdate(IOrder order)
    {
         if(order.OrderState == OrderState.Filled && order.Name == "Stop Loss")
               waitTilShort = true;
    }
    http://www.ninjatrader.com/support/h...rderupdate.htm
    Cal H.NinjaTrader Customer Service

    Comment


      #3
      Originally posted by NinjaTrader_Cal View Post
      Hello Sgeesala,

      Thank you for your post

      You can use the OnOrderUpdate() method to capture when the Stop Loss order it filled and then set a boolean variable to true.

      Example:
      Code:
      protected override void OnOrderUpdate(IOrder order)
      {
           if(order.OrderState == OrderState.Filled && order.Name == "Stop Loss")
                 waitTilShort = true;
      }
      http://www.ninjatrader.com/support/h...rderupdate.htm
      I am in kind of same situation. I want to enter only once in the same direction and dont want to reenter regardless what side order is filled either stop loss or profit target. when I place waittilshort=true then on compile time it says no such variable defined.

      Comment


        #4
        Hello trader333,

        Thank you for your post.

        The variable bool would need to be defined before it can be used. Look to the 'Variables' listed above the Initialize() method --> click the '+' button to expand --> then add:
        Code:
        private bool waitTilShort = false;

        Comment


          #5
          Hello sgeesala and trader333,

          Another way this can be done is by using if(Position.MarketPosition == MarketPosition.Flat). So you can place all your code to enter in the if condition, meaning you will have to be flat before another entry can be taken.

          Please visit the following link for information on Position.MarketPosition: http://www.ninjatrader.com/support/h...etposition.htm

          Comment


            #6
            Thanks Patrick.
            One last question, my strategy is both short and long how do i prevent it from reentering same direction until opposite trade has been entered. let me explain
            I am buying when rsi is above 70 and shorting when rsi is below 30. I entered long and once stoploss or profit targets are hit I dont want to reenter in the same direction until I take short trade.

            Comment


              #7
              Hello trader333,

              Thank you for your response.

              The following is a basic example using two bools to switch between going long and short:
              Code:
                      #region Variables
                      private bool goLong = true;
              		private bool goShort = true;
                      #endregion
              
                      /// <summary>
                      /// This method is used to configure the strategy and is called once before any strategy method is called.
                      /// </summary>
                      protected override void Initialize()
                      {
              			
                      }
              
                      /// <summary>
                      /// Called on each bar update event (incoming tick)
                      /// </summary>
                      protected override void OnBarUpdate()
                      {
              			if(RSI(14, 9)[0] > 70 && goLong)
              			{
              				EnterLong();
              				goLong = false;
              				goShort = true;
              			}
              			else if(RSI(14, 9)[0] < 30 && goShort)
              			{
              				EnterShort();
              				goLong = true;
              				goShort = false;
              			}
              		}

              Comment


                #8
                Can you show how to wait for the next signal regardless as to whether or not it's in the same direction or opposite?

                For example, if conditionlong entry is exited with either a stop-loss or profit target, wait until the next conditionlong or conditionshort.
                Last edited by ScorpioTravis; 01-13-2017, 12:20 PM.

                Comment


                  #9
                  Hello ScorpioTravis,

                  Thank you for your response.

                  Once the position is closed it should be waiting for the next signal in any case. If you only want to enter when flat then use the reference at my previous post: http://ninjatrader.com/support/forum...67&postcount=5

                  Please let me know if you have any questions.

                  Comment


                    #10
                    Originally posted by NinjaTrader_PatrickH View Post

                    Once the position is closed it should be waiting for the next signal in any case.
                    If it exits on a target or stop it will re-enter on the next bar when the conditionlong is still true.

                    However, I now see how I can modify the conditionlong so it's not true on subsequent bars after the first bar that it's true..

                    Thank you.

                    Comment


                      #11
                      Hello,

                      I'm having a similar issue, the strategy conditions for a long trade have been met with a profit target, and I do not want to reenter the market, though the "conditions" to go long still exist.

                      How do I "code" the strategy to avoid reentering the market.

                      In summary:

                      1) conditions are met for long trade
                      2) profit targets are fulfilled
                      3) original conditions still exist for long trade
                      4) NEED EXCEPTION CODE TO AVOID RE-ENTRY

                      Goal is to wait for next SHORT conditions.

                      Thanks for your help in advance!

                      MLT

                      Comment


                        #12
                        Hello mlthompson,

                        Thank you for your post.

                        Use a bool to switch between trading long and trading short. For example:
                        Code:
                            public class SwitchExample : Strategy
                            {
                        		private bool TradeSwitch; // true for long, false for short
                        		private bool FirstTrade = true; // utilize to avoid missing first signal
                                protected override void Initialize()
                                {
                                    
                                }
                                protected override void OnBarUpdate()
                                {
                        			if (FirstTrade) // place first trade
                        			{
                        				if (Close[0] > Open[0]) // basic entry condition
                        				{
                        					EnterLong();
                        					FirstTrade = false; // first trade was placed
                        					TradeSwitch = true; // went long
                        				}
                        				else
                        				{
                        					EnterShort();
                        					FirstTrade = false; // first trade was placed
                        					TradeSwitch = false; // went short
                        				}
                        			}
                        			
                        			// Enter short if prior position was long and we are now flat
                        			if (TradeSwitch
                        				&& Position.MarketPosition == MarketPosition.Flat
                        				&& Close[0] < Open[0])
                        			{
                        				EnterShort();
                        				TradeSwitch = false;
                        			}
                        			
                        			// Enter long is prior position was short and we are now flat
                        			else if (!TradeSwitch
                        				&& Position.MarketPosition == MarketPosition.Flat
                        				&& Close[0] > Open[0])
                        			{
                        				EnterLong();
                        				TradeSwitch = true;
                        			}
                                }
                            }
                        Please let me know if you have any questions.

                        Comment

                        Latest Posts

                        Collapse

                        Topics Statistics Last Post
                        Started by XXtrader, Yesterday, 11:30 PM
                        2 responses
                        11 views
                        0 likes
                        Last Post XXtrader  
                        Started by Waxavi, Today, 02:10 AM
                        0 responses
                        6 views
                        0 likes
                        Last Post Waxavi
                        by Waxavi
                         
                        Started by TradeForge, Today, 02:09 AM
                        0 responses
                        11 views
                        0 likes
                        Last Post TradeForge  
                        Started by Waxavi, Today, 02:00 AM
                        0 responses
                        2 views
                        0 likes
                        Last Post Waxavi
                        by Waxavi
                         
                        Started by elirion, Today, 01:36 AM
                        0 responses
                        7 views
                        0 likes
                        Last Post elirion
                        by elirion
                         
                        Working...
                        X