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

Long Stop Order and Short Stop Order

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

    Long Stop Order and Short Stop Order

    Hello

    I'm trying to open 2 orders at the same time when a price is reached.

    I'm developing in Simulated Data Feed, with instrument FDAX 06.

    When I try to open these 2 orders simultaneously, then only the Long Stop Order is taken into account by NinjaTrader 7, but not the Short Stop Order.


    The code I use is the following :

    Code:
    string signalNameAchat = "12450" + ";" + "1" + ";" + "LONG";
    IOrder orderAchat = this.EnterLongStop((int)10, 12460, signalNameAchat);
    this.SetProfitTarget(signalNameAchat, CalculationMode.Price, 12470);
    this.SetStopLoss(signalNameAchat, CalculationMode.Price, 12450, false);
    
    string signalNameVente = "12450" + ";" + 1 + ";" + "SHORT";
    IOrder orderVente = this.EnterShortStop((int)10, 12300, signalNameVente);
    this.SetProfitTarget(signalNameVente, CalculationMode.Price, 12250);
    this.SetStopLoss(signalNameVente, CalculationMode.Price, 12350, false);

    Here is the code in the Initialize() function :

    Code:
           
     protected override void Initialize()
            {
                this.BarsRequired = 0;
                CalculateOnBarClose = false;
    			this.EntriesPerDirection = 8;
    			this.BarsRequired = 0;
    			this.RealtimeErrorHandling = RealtimeErrorHandling.TakeNoAction;
    			this.StrategySync = StrategySync.SubmitImmediately;
            }

    #2
    Hello investdatasystems,

    Thanks for your inquiry.

    It sounds like you are running into the Managed Approach's Internal Order Handling Rules that Reduce Unwanted Positions.
    Methods that generate orders to enter a position will be ignored if:
    • A position is open and an order submitted by an exit method (ExitLongLimit() for example) is active and the order is used to open a position in the opposite direction
    • A position is open and an order submitted by a set method (SetStopLoss() for example) is active and the order is used to open a position in the opposite direction
    • The strategy position is flat and an order submitted by an enter method (EnterLongLimit() for example) is active and the order is used to open a position in the opposite direction
    • The entry signal name is not unique
    These rules are publicly documented here: https://ninjatrader.com/support/help...d_approach.htm

    If you would like to create a strategy that places entry orders for opposite directions, I may suggest looking into the Unmanaged Approach. You can also bind these entry orders with OCO with the Unmanaged Approach so one entry will cancel the other.

    Unmanged Approach - https://ninjatrader.com/support/help...d_approach.htm

    I have also attached a template that can be referenced while developing an Unmanaged Approach strategy.

    Please let us know if we can be of further help.
    Attached Files
    JimNinjaTrader Customer Service

    Comment


      #3
      I will check the links you quote. Thank you for your reply and the code template
      Last edited by investdatasystems; 05-22-2018, 01:19 AM.

      Comment


        #4
        Does an empty string for OCO Id on 2 different orders link them ?

        Comment


          #5
          Hello investdatasystems,

          No, a blank string for OCO does not tie these orders together. A string must be specified for the pair. This can be tested with the template provided and changing the OCO ID's of the entry orders or exit orders. If the OCO string is blank, the orders will not cancel the other upon fill or cancellation.

          Please let us know if you have any additional questions.
          JimNinjaTrader Customer Service

          Comment


            #6
            Ok I see. I have just checked that and you are right, thanks.

            Why don't you use SetProfitTarget and SetStopLoss in your template ? Can't these functions be used in an unmanaged approach ?

            Comment


              #7
              Hello investdatasystems,

              No, the Set methods only exist in the Managed Approach and cannot be used in Unmanaged strategies. Protective orders are recommended to be placed in the OnExecution() method when your entry orders get filled. You may reference the previously linked Managed Approach and Unmanaged Approach documentation pages for a list of supported order methods for each approach.

              OnExecution() - https://ninjatrader.com/support/help...nexecution.htm

              You may also wish to reference the Managed Approach strategy for using OnOrderUpdate() and OnExecution() for submitting protective orders and the associated documentation. While this is a Managed strategy, it can offer some direction when using these methods.

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

              I'm happy to be of any further help.
              JimNinjaTrader Customer Service

              Comment


                #8
                Ok thanks for the clues.

                When using managed approach, if I use OpenLongStop and EnterShortStop at the same time, there is always one of the orders that is cancelled (Internal Handling).

                When using unmanaged approach, if I use 2 OrderSubmit (Sell Stop and Buy Stop) then the 2 orders are well accepted...

                How could I disable any Internal Handling for using the managed approach ? In that way, I could set a TP and a SL for each order and not be forced to let the bot run continuously.

                Thanks
                Last edited by investdatasystems; 05-23-2018, 09:09 AM.

                Comment


                  #9
                  Hello investdatasystems,

                  How could I disable any Internal Handling for using the managed approach ?
                  It would not be possible to disable these order handling rules in the Managed Approach. The Unmanaged Approach would need to be used to program a strategy that does not follow these rules.

                  In that way, I could set a TP and a SL for each order and not be forced to let the bot run continuously.
                  The strategy will still need to be enabled to have the protective orders submitted upon execution of an entry order, regardless of the approach used. The Set methods in the Managed Approach prep NinjaTrader to place these protective orders after the the entry execution is seen, and the same behavior can be defined by using the OnOrderUpdate() and OnExecution() methods to fire your protective orders as seen in the examples.

                  It would not make a difference if you are using the Managed Approach or Unmanaged Approach: The strategy needs to be enabled to submit these orders, and the NinjaScript options for canceling orders upon disabling the strategy have the same effect on Managed and Unmanaged strategies.

                  You may see that when testing the UnmanagedTemplate with "Immediately submit" and with the Order Handling options to cancel entry and exit orders upon disable turned off, the orders will remain active and the strategy can resume its position.

                  Demo - https://www.screencast.com/t/tXQni4om

                  More information on these options can be referenced in the pages below.

                  NinjaScript Options - https://ninjatrader.com/support/help...tegies_tab.htm

                  Syncing Account Positions - https://ninjatrader.com/support/help..._positions.htm

                  Please let us know if you have any questions.
                  JimNinjaTrader Customer Service

                  Comment


                    #10
                    Ok, so if I understand correctly, there is no way to open a position and to set a TP and SL and then close NinjaTrader, and wait for the TP or SL to be reached, and then have the order automatically closed (with NinjaTrader closed). Am I right ?

                    Thank you very much for your help.

                    Comment


                      #11
                      Hello investdatasystems,

                      The orders will have to be tied with OCO so the exchange can cancel the other order. The Set methods in the Managed Approach use OCO, and we can also use OCO with the Unmanaged Approach.

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

                      Comment


                        #12
                        Ok, I understand clearly now.

                        I will use the unmanaged approach and I will try to find out how to close the positions in the OnExecution overrided method (I don't want to use OCO for now).

                        Thanks again !

                        Comment

                        Latest Posts

                        Collapse

                        Topics Statistics Last Post
                        Started by DJ888, 04-16-2024, 06:09 PM
                        4 responses
                        12 views
                        0 likes
                        Last Post DJ888
                        by DJ888
                         
                        Started by terofs, Today, 04:18 PM
                        0 responses
                        11 views
                        0 likes
                        Last Post terofs
                        by terofs
                         
                        Started by nandhumca, Today, 03:41 PM
                        0 responses
                        7 views
                        0 likes
                        Last Post nandhumca  
                        Started by The_Sec, Today, 03:37 PM
                        0 responses
                        3 views
                        0 likes
                        Last Post The_Sec
                        by The_Sec
                         
                        Started by GwFutures1988, Today, 02:48 PM
                        1 response
                        9 views
                        0 likes
                        Last Post NinjaTrader_Clayton  
                        Working...
                        X