• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.


No announcement yet.

Partner 728x90


Complicated exit strategy

  • Filter
  • Time
  • Show
Clear All
new posts

    Complicated exit strategy

    I'm working on it:
    Buy stop order (2 contracts)
    if it's triggered place stop loss
    Profit target 4 tick (1 contract)
    if profit target triggered move stop loss to BE + 1 tick.
    Then after every new completed bar move this stop 1 tick lower than previous bar.

    I use this sample https://ninjatrader.com/support/foru...orders?t=18890 cuz I need to cancel my buy stops.
    My question is how to do BE+ 1tick and move stop? Should it be OnExecutionUpdate or OnBarUpdate?


    Hello Leeroy_Jenkins,

    Thanks for your post.

    Moving the stop loss to breakeven + one would involve moving the stop loss to Position.AveragePrice + TickSize, or you can use Order.AverageFillPrice + TickSize.

    I have attached an example that can demonstrate. Please refer to the original SampleOnOrderUpdate strategy for how that strategy works as this example is based on it.

    SampleOnOrderUpdate - https://ninjatrader.com/support/help...and_onexec.htm

    We look forward to being of further assistance.
    Attached Files
    JimNinjaTrader Customer Service


      Thanks, just what I needed.

      Can you help me with another problem? I need to check every bar in array (ex. between bars with indeces 100-120). If any high of those bars < EMA (21) = false. If not = true. I tried Array.TrueForAll but I don't know how to use < here.

      And btw there is a problem with that sample for NT8 I posted above. It opens 1 position per day till market closes. I took sample from NT8 guide "Using CancelOrder() method to cancel orders" and it works great.
      Last edited by Leeroy_Jenkins; 08-19-2019, 05:44 AM.


        Hello Leeroy_Jenkins,

        If you would like to loop through specific bars, you could write you loop to go through your specified indexes and update a variable that tracks the highest value of these bars. For example:

        double trackedHigh = 0;
        for (int i = startindex; i < endindex; i++)
            if(Bars.GetHigh(i) > trackedHigh)
                trackedHigh = Bars.GetHigh(i);
        You could also consider using MAX if you wish to use barsAgo indexes.

        Bars.GetHigh - https://ninjatrader.com/support/help...us/gethigh.htm

        MAX - https://ninjatrader.com/support/help...aximum_max.htm

        We have moved our samples from the forum to the help guide, and we would suggest using the samples found in the help guide. Comparing the two, the logic is very much the same, but there is some functionality in the sample hosted on the forums where GetRealtimeOrder is not used to transition historical order objects to realtime order objects.

        Could you clarify if you see any issues with the Help Guide example linked below? If this example is working fine for you, I will see about having the old forum samples updated.


        I look forward to being of further assistance.
        JimNinjaTrader Customer Service


          I checked it closely and found that it stops after some trades. I didn't change code. I tried different instruments, tick charts and time-based. Log doesn't have any warnings/errors.
          Attached Files


            Hello Jim
            - with regard to the sample you posted could you confirm whether in row 140 and 143 there are two zeros on the tick multipliers that shouldn't be there?
            - does this sample work also with an Interactive Brokers data connection ? ( I have read on the help online a note telling something about this ... which I do not understand completely)
            - I have tried to launch it on market replay, as it is, but it did fire only 4 orders;
            - where are supposed to be inserted the entry conditions? at row 81?
            - is this sample only for long entry executions?
            Last edited by guidoisot; 08-20-2019, 08:04 AM.


              Yeah same problem with SampleOnOrderUpdateSplitEntryAndBE. It stops after few trades.


                from the help https://ninjatrader.com/support/help...and_onexec.htm
                it says
                ...when connected to market replay ... will result in order state events being fired prior to the order method return an Order object. ??

                But also this concept is not too easy for me to understand, except the fact that this onorderupdate is a method for "the more advanced user"


                  Hello Leeroy_Jenkins and quidoisot,

                  The strategy is not written to handle ExitOnSessionClose. When this happens, orders are cancelled but not set back to null, so the logic never re-enters. This example is only meant to demonstrate how the logic can be set up for a split entry that builds off of the SampleOnOrderUpdate concepts. It isn't intended to be a grab and go example. If you want to use it as such, it would be advised to observe the TraceOrders and take debugging steps to see where the strategy stops if you would like to build off of it.

                  As for SampleCancelOrder, this is the same idea, the example is just meant to demonstrate cancelling orders, there can certainly be situations where the logic ceases to process, because it does not handle all possible scenarios.

                  I have attached another version out of courtesy to show how this can be handled, but you will want to use TraceOrders and debug prints to find out why some certain actions are not occuring to get past those issues.

                  guidoisot Our Interactive Brokers adapter can have Execution updates and Order updates coming in a mixed order. Position updates will always come after Order updates, however. An example like the attached PsuedoOnExecutionPartialTest can be followed for IB. Another approach would be to design the strategy so it works off of OnOrderUpdate alone.

                  Please let me know if I can be of further assistance.
                  Attached Files
                  JimNinjaTrader Customer Service


                    Thank you @NinjaTrader_Jim


                      Hi @NinjaTrader_Jim

                      I try to implement your sample in my strategy but there are errors:

                      - Error on calling 'OnExecutionUpdate' method on bar 1805: Object reference not set to an instance of an object.
                      - A BuyToCover stop order placed at '11.06.2019 10:21:02' has been ignored since the stop price is less than or equal to the close price of the current bar. This is an invalid order and subsequent orders may also be ignored. Please fix your strategy.

                      It's only appeared on some bars. But there are ranges where are no errors.

                      I tried to use this logic from this thread: https://ninjatrader.com/support/foru...reater?t=62496
                      Math.Min(Close[0] - TickSize, yourStopPrice) // For sell stops Math.Max(Close[0] + TickSize, yourStopPrice) // For buy stops
                      But errors still exist.


                        Hello Leeroy_Jenkins,

                        The error means that an object in your strategy is null. This is likely an Order object, but could be another object. I suggest adding debugging prints so you can identify where the error is getting thrown so you can identify what object is null when you are referencing it, and then you will have a better idea on how you can fix your code so this object is not null.

                        Some questions to ask yourself while debugging:

                        What object is null and throwing the error?

                        How is it getting set to null?

                        How can I change my code so it is not null when I need to reference it?

                        Debugging steps will also need to be taken to observe what exact price level the order is being submitted to so you know what exact adjustments you need to make.

                        The Playback Connection can be helpful for repeating scenarios that happen with real-time data so you can run through them again with debugging prints added.

                        Debugging tips - https://ninjatrader.com/support/help...script_cod.htm

                        Playback Connection - https://ninjatrader.com/support/help...connection.htm

                        We look forward to being of further assistance.
                        JimNinjaTrader Customer Service


                        Latest Posts


                        Topics Statistics Last Post
                        Started by lunardiplomacy, Today, 07:57 AM
                        0 responses
                        Last Post lunardiplomacy  
                        Started by Tagliareni, Yesterday, 08:10 PM
                        1 response
                        Last Post NinjaTrader_PaulH  
                        Started by SDaCosta30, Today, 04:19 AM
                        1 response
                        Last Post NinjaTrader_PaulH  
                        Started by memonolog, Today, 02:39 AM
                        5 responses
                        Last Post memonolog  
                        Started by memonolog, Today, 07:07 AM
                        1 response
                        Last Post NinjaTrader_ChrisL