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

Simple Script, but stops triggering on Targets and Stops.

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

    Simple Script, but stops triggering on Targets and Stops.

    I've been using various examples in the docs and this forum (which are awesome by the way) to set up a script to manage automated entries, targets, and stops.
    • EnterLong() to get into orders.
    • OnOrderUpdate() to set the orders to my own variables for reference.
    • ExitLongLimit() and ExitLongStopMarket() for the target and stop loss on the OnExecutionUpdate() Method.

    This all works great, until after it runs through about 7 orders, then it stops triggering on the Target and Stops, and just closes at the end of the session.

    I'm attaching my script (it's very simple), hoping that someone can look at it and find what might be obviously missing as I'm well versed in C#, but very new on the NinjaScript side.

    I've chosen to use the "ExitLong" methods vs. the "Set" methods as I want to eventually add in some more complicated trailing stop logic with one order's profit target, altering the other's stop. However, none of that code is in there yet.

    I also created a short video showing what I see on my chart after I run the strategy script on it which will show what I mean by "stops triggering":


    https://www.loom.com/share/202bc989b...528ab54281e64b


    Thank you!
    Attached Files
    Last edited by ruben888; 02-22-2021, 08:32 PM.

    #2
    Hello ruben888,

    Thanks for your post and welcome to the NinjaTrader forums!

    When you run your strategy, do you see any error messages printed in the "Log" tab of the control center (related to your strategy)?
    Paul H.NinjaTrader Customer Service

    Comment


      #3
      No. I just see the outputs from the Print statements in the file. But that's interesting is it possible the script is "crashing" but not printing an error?

      Comment


        #4
        Hello ruben888,

        Thanks for your reply.

        No, if the script were to crash, there would be error messages.

        A diagnostic tool you can enable is "TraceOrders" which by default is set to false in State.SetDefaults. When enabled it will fill your ninja script output window with information about all orders: https://ninjatrader.com/support/help...raceorders.htm

        When enabled, TraceOrders shows that your profit and stop orders are being canceled. The reason they are being canceled is that they are automatically canceled if not filled on the bar they are submitted on. That is the default behavior in strategies that use the "managed approach" which you should review here: https://ninjatrader.com/support/help...d_approach.htm

        Looking at your video, (thank-you) you can see all your trades are filled on the same bar. So what happens is your strategy fills the entry order then places the stop and profit orders but if the price does not hit either in that bar, on the next bar these orders are then automatically canceled and your position remains until it is finally closed by the end of the session setting. Once the order has been close your strategy then again start placing new orders until such time as the trade is not completed in the same bar.

        To maintain the profit/stop orders, you would have to resubmit them on each bar, or use the overload of the method that provides a bool for IsLiveUntilCanceled. You can review them here:




        Paul H.NinjaTrader Customer Service

        Comment


          #5
          Thank you Paul, that makes a lot of sense. After posting this I was watching the strategy on a playback and saw the orders being cancelled and I didn't know why!

          I started with that method overload you mentioned (that includes the IsLiveUntilCanceled) as it's the one in the examples. However, I was getting an exception that was crashing the whole NinjaTrader application. I was able to attach the Visual Studio debugger to the .exe process and I saw that it was an "out of range" exception on the 1st parameter (barsInProgressIndex), but even just setting it to 0 (zero) wasn't working.

          I will try again with method and see if I can get it sorted out.

          Also, I appreciate the clarity on "TraceOrders" that will come in useful.

          Comment


            #6
            Hello ruben888,

            Thanks for your reply.

            I meant to also mention that a much simpler approach here is to use the SetProfitTarget and SetStopLoss methods as these will not be canceled. You can also dynamically adjust them in your code. So you can use the SetStopLoss as a trailing stop by providing the logic to trail by and resubmit the stop at the intended level. Likewise, you can adjust the SetProfitTarget as you wish.

            The only consideration here is to "reset" these methods back to some initial value for (before) the next entry order as the methods retain the last value set.
            References:


            Paul H.NinjaTrader Customer Service

            Comment


              #7
              I started with that SetProfitTarge/SetStopLoss approach, but for some reason that wasn't moving the stops when I was calling from OnExecutionUpdate()
              (even though I read that it was supposed to).

              However, I'm quite a bit further down the road now about understanding what's going on and able to debug now, etc... so I'll try that again too.

              If I go down the SetProfitTarget and SetStopLoss path (vs the "Exit" methods path) is there some general downside to that for the kind of approach for what I'm doing here?

              NOTE: I'll likely avoid the whole "unmanaged" path for another lifetime

              Comment


                #8
                ... you did mention "ONLY consideration here is to "reset" these methods back to some initial value" , just seeing if I'm locking myself into something I'm going to want to re-do l later if I try to get more advanced.

                Comment


                  #9
                  Hello ruben888,

                  Thanks for your reply.

                  You can code this however you wish and perhaps you will want to duplicate your effort and go both ways to help in finding which works best for you.


                  Paul H.NinjaTrader Customer Service

                  Comment


                    #10
                    I was able to able to use ExitLongStopMarket() with the overload you mentioned to allow order to NOT be cancelled after the bar close.

                    This worked!

                    I decided to stick with this approach as it seems to be more flexible if I want to expand the logic in the future.

                    Thanks for your awesome support.

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    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
                    4 views
                    0 likes
                    Last Post elirion
                    by elirion
                     
                    Started by gentlebenthebear, Today, 01:30 AM
                    0 responses
                    5 views
                    0 likes
                    Last Post gentlebenthebear  
                    Working...
                    X