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

Newbie Code Help --- ATR Multiple for Stop

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

    #16
    Originally posted by NinjaTrader_Josh View Post
    You need to call SetTrailStop before you place your order. After that you should not call it again. It will trail on its own tick by tick. From your code it looks like you are trying to use your own trailing logic and in that case you should not be using SetTrailStop. You should be using ExitLongStop() and just modify that order constantly.
    The SetTrailStop call is before the entry conditions. Currrently, the call does keep running after entry, but previously it did not and it still didn't work (when SetTrailStop was in the calculation logic section of the code that is programmed to stop running once position is no longer flat).

    The only thing I'm trying to do is to have the value for "price" calculated right at entry based on a recent ATR. Upon an order being filled, I want a trailing stop order to be submitted to the market and monitored by the broker (and function like any other trailing stop order you would place manually with your broker).

    I want a trailing stop order to be placed with a static price calculated and set right at entry...not a stop order that is constantly updated by code.

    Comment


      #17
      If you do not want it updating based on your code you should only call SetTrailStop once. In terms of if your order will be held at your brokerage this depends. Please see this article: http://www.ninjatrader-support2.com/...ead.php?t=5349
      Josh P.NinjaTrader Customer Service

      Comment


        #18
        Originally posted by NinjaTrader_Josh View Post
        If you do not want it updating based on your code you should only call SetTrailStop once. In terms of if your order will be held at your brokerage this depends. Please see this article: http://www.ninjatrader-support2.com/...ead.php?t=5349
        I understand but in my case it really doesn't matter. The value for TrailStop will not change once a position is entered, so even if it updates the trail stop order after entry, it would be with an identical value. Since that is apparently not the problem, I'll probably put the SetTrailStop call back in the Calc Logic section so that it will stop running upon entry. It will now only be running SetTrailStop call when the strategy is flat.

        But that is not the problem: when an order gets filled, a trailing stop order is not being generated. All I get is the "Entered internal SetStopTarget() method" statement in the output window. Regardless of where the actual order is residing (whether it is simulated on my machine, held on my broker's servers, or held at the exchange) NT should still show a working trailstop order in the orders tab, shouldn't it? When I use SetStopLoss or SetProfitTarget in Initialize, I get live working orders that show up on the Orders tab. Nothing shows up over there. Again, trailing stop orders are not even getting placed. Regardless of if they are updated or not, or where they reside, the orders simply aren't being placed.

        Comment


          #19
          Do I need a SetStopLoss in the Intialize() section as well as in the OnBarUpdate section? Right now there is nothing in Initialize. Not sure why that would matter, but just a thought...

          Comment


            #20
            That does not matter. Please try with an as-simple-as-possible strategy.

            Code:
            if (Position.MarketPosition == MarketPosition.Flat)
            {
                 SetTrailStop(...);
                 EnterLong();
            }
            and see what happens.
            Josh P.NinjaTrader Customer Service

            Comment


              #21
              Originally posted by NinjaTrader_Josh View Post
              That does not matter. Please try with an as-simple-as-possible strategy.

              Code:
              if (Position.MarketPosition == MarketPosition.Flat)
              {
                   SetTrailStop(...);
                   EnterLong();
              }
              and see what happens.
              Taking it down to that level helped my troubleshooting a lot. Thanks

              It is working now. I think it was a combination of problems. For one, I had the calc mode set to price, not ticks. My ATR is calculating ticks away from entry to set a stop, not a price level to set the stop.

              Another thing is that I had the SetTrailStop's outside of the brackets for the entry conditions. I now have them directly above each entry condition, both within the same bracket (like you've shown above). Before it was like this...

              Code:
              SetTrailStop("Buy",...)
              SetTrailStop("Sell",...)
               
              if(...)
              {
              EnterLong("Buy");
              }
               
              if(...)
              {
              EnterShort("Sell");
              }
              Now I have:

              Code:
              if(...)
              {
              SetTrailStop("Buy",...)
              EnterLong("Buy");
              }
               
              if(...)
              {
              SetTrailStop("Sell",...)
              EnterShort("Sell");
              }
              Would that have been all it was the whole time, just the placement of SetTrailStop has to be in the same "if" statement as the Enter commands?

              Comment


                #22
                It shouldn't have mattered, but doesn't hurt to leave it as you have it. Most likely the calculation mode differences were what was causing issues. Perhaps it ended up trying to submit at an invalid price.
                Josh P.NinjaTrader Customer Service

                Comment


                  #23
                  Originally posted by NinjaTrader_Josh View Post
                  It shouldn't have mattered, but doesn't hurt to leave it as you have it. Most likely the calculation mode differences were what was causing issues. Perhaps it ended up trying to submit at an invalid price.
                  In the next day or so, I'll run a test with it the old way to see if it gives me problems again. Just curious. Thanks again for your help and quick responses.

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by judysamnt7, 03-13-2023, 09:11 AM
                  4 responses
                  59 views
                  0 likes
                  Last Post DynamicTest  
                  Started by ScottWalsh, Today, 06:52 PM
                  4 responses
                  36 views
                  0 likes
                  Last Post ScottWalsh  
                  Started by olisav57, Today, 07:39 PM
                  0 responses
                  7 views
                  0 likes
                  Last Post olisav57  
                  Started by trilliantrader, Today, 03:01 PM
                  2 responses
                  21 views
                  0 likes
                  Last Post helpwanted  
                  Started by cre8able, Today, 07:24 PM
                  0 responses
                  10 views
                  0 likes
                  Last Post cre8able  
                  Working...
                  X