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

Exit based on indicator

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

    Exit based on indicator

    I'm trying to make a strategy and have the exit target be conditional when price hits the SMA

    The target needs to be adjusted on every bar update since the SMA is adjusting every bar although this code seems to set target at the SMA at the time of the Entry and maintain that target price even though the SMA moves. How can I have my target change to the new SMA on every bar update? I considered using just close > SMA...exit but that will only execute on bar close and I need the target set so if price hits the SMA during bar formation, I exit the position. Thanks.
    Code:
    if (Close[0]>Open[0])
    					
                {
                    EnterLong(1, "LongEntry");
                }
    		//Exit Condition set 1
    				if (GetCurrentAsk() == SMA(14)[0])
                {
                    ExitLong("LongExit", "LongEntry");
                }

    #2
    Have you considered just placing a target exit limit order at the SMA price level for every bar while you're in the long position?
    BertrandNinjaTrader Customer Service

    Comment


      #3
      I'm not sure how to go about doing that. Do you mind giving me a sample for LongExits?

      Comment


        #4
        Yes, I would look into something along the lines of this example below.
        Attached Files
        BertrandNinjaTrader Customer Service

        Comment


          #5
          I've implemented the code but the SMA exit orders are never executed when I have a StopLoss or another ExitLong routine in here. If I eliminate the stoploss in the initialize section or eliminate Condition 5 & 6, the SMA exit works fine. Please advise if you see something wrong.
          Code:
          	if (Close[0]>Open[0]
          					&& Position.MarketPosition == MarketPosition.Flat)
                      {
                          EnterLong(DefaultQuantity, "LongEntry");
                      }
          
                      // Condition set 2
                      if (Close[0]<Open[0]
          					&& Position.MarketPosition == MarketPosition.Flat)
                      {
                          EnterShort(DefaultQuantity, "ShortEntry");
                      }
          			
                      // Condition set 3
                      if (Position.MarketPosition == MarketPosition.Long)
                      {
                          ExitLongStopLimit(SMA(14)[0], SMA(14)[0], "LongTarget", "LongEntry");
                      }
          
                      // Condition set 4
                      if (Position.MarketPosition == MarketPosition.Short)
                      {
                          ExitShortStopLimit(SMA(14)[0], SMA(14)[0], "ShortTarget", "ShortEntry");
                      }
          
                     //  Condition set 5
                      if ((Close[0] < MIN(Low, 5)[1]) && (Position.MarketPosition == MarketPosition.Long))
                      {
                         ExitLong("LongStop", "LongEntry");
                      }
          
                   //    Condition set 6
                      if ((Close[0] > MAX(High, 5)[1])&& (Position.MarketPosition == MarketPosition.Short))
                     {
                          ExitShort("ShortStop", "ShortEntry");
                      }
          Last edited by Tdschulz; 01-19-2012, 12:05 PM.

          Comment


            #6
            Hi Tdschulz,

            To check what is happening here, first assign TraceOrders = true; in your Initialize() method.

            Then view output using Tools > Output Window.

            You will likely see messages related to Internal Order Handling rules. Part of the managed system will ignore orders if they would compete with already working orders. You can read more about internal order handling rules here in the last section.
            Ryan M.NinjaTrader Customer Service

            Comment


              #7
              exit limit conflict with stop loss

              I'm with the same problem. I found in internal order handling rules:

              "Methods that generate orders to exit a position will be ignored if:

              • A position is open and an order submitted by a set method (SetStopLoss() for example) is active."


              So, Can't we have two simple exits working togheter, one if stop loss is reached and, if not, another with a cross SMA?

              When I use market orders to exit, no problem, but I'd like to use Limit orders to exit. Is there a way to solve this problem?

              Thanks,

              Daniel

              Comment


                #8
                Then you would need to move away from Set() methods and cancel the limit exit order working first and once the cancellation has been confirmed, issue you the new exit order desired. Another approach is using our unmanaged order submission mode, that would not use any of the order handling rules.
                BertrandNinjaTrader Customer Service

                Comment


                  #9
                  Thanks Bertrand, I'll study the alternatives.

                  Comment


                    #10
                    Originally posted by NinjaTrader_Bertrand View Post
                    Yes, I would look into something along the lines of this example below.
                    Bertrand is there a SMAExit.zip for NinjaTrader8 This will not load into Ninja8

                    Thanks

                    Comment


                      #11
                      Originally posted by NinjaTrader_Bertrand View Post
                      Yes, I would look into something along the lines of this example below.
                      Bertrand is there a SMAExit.zip for NinjaTrader8 This will not load into Ninja8

                      Thanks

                      Comment


                        #12
                        Hello DTSSTS,

                        Thanks for your notes.

                        I am not aware of any SMAExit reference sample compatible for NinjaTrader 8. Note that no scripts built for NinjaTrader 7 will import into NinjaTrader 8 due to code-breaking changes.

                        The SMAExit script would need to be converted to be compatible with NinjaTrader 8.

                        That said, I have created a simple Strategy Builder strategy in NinjaTraer 8 demonstrating the same concept for you to view. See the attached example script.
                        Attached Files
                        Brandon H.NinjaTrader Customer Service

                        Comment


                          #13
                          Thanks appreciated.

                          Comment

                          Latest Posts

                          Collapse

                          Topics Statistics Last Post
                          Started by affilife, 02-17-2024, 09:53 AM
                          4 responses
                          116 views
                          0 likes
                          Last Post rjm1903
                          by rjm1903
                           
                          Started by AdamDJ8, Today, 08:29 PM
                          0 responses
                          4 views
                          0 likes
                          Last Post AdamDJ8
                          by AdamDJ8
                           
                          Started by AdamDJ8, 05-07-2024, 09:18 PM
                          3 responses
                          41 views
                          0 likes
                          Last Post AdamDJ8
                          by AdamDJ8
                           
                          Started by Jorge Mendieta, Today, 07:35 PM
                          1 response
                          9 views
                          0 likes
                          Last Post Jorge Mendieta  
                          Started by rjm1903, Today, 04:14 PM
                          0 responses
                          11 views
                          0 likes
                          Last Post rjm1903
                          by rjm1903
                           
                          Working...
                          X