Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Unmanaged approach order handling

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

    Unmanaged approach order handling

    Hello, I'm trying to solve an issue.

    When using Unmanaged approach, and submitting multiple entry orders via the SubmitOrderUnmanaged() method, and there are more than one open positions at a time, the exit orders submitted for those open positions are filled in a chronological order instead of their respective entry order's exit order.

    for example:

    Entry orders: STD_1_Entry_1, STD_2_Entry_1.
    Exit orders: STD_1_T1, STD_1_L1, STD_2_T1, STD_2_L1.
    Fill price for exit limit orders: STD_1_T1 = 100.0, STD_2_T1 = 99.0.

    STD_1_Entry_1 will be filled one bar before STD_2_Entry_1.

    but the open position relating to STD_1_Entry_1 will be filled by STD_2_T1, since its lower.

    Chart Image:
    Click image for larger version  Name:	Screenshot 2023-04-07 234352.png Views:	0 Size:	35.8 KB ID:	1245172

    Strategy analyzer Trades:
    Click image for larger version  Name:	Screenshot 2023-04-07 234607.png Views:	0 Size:	16.9 KB ID:	1245174
    As you can see, the exit order filled the first open position available.

    the question is:
    How can I connect entry orders to their corresponding exit orders, and essentially have the open position of that entry order only fill exit orders directly related to it ( unless its time in force exits the open position).

    This is the code for entry orders, it is submitted via OnBarUpdate(), the calculation is OnBarClose:
    Code:
    Order entryOrder = SubmitOrderUnmanaged(0, OrderAction.Buy, OrderType.Market, quantity, 0, 0, "", signalName);
    The exit orders are submitted via the OnExecutionUpdate method once the entry order is filled.

    I have created a custom class to keep orders as a reference for different uses (exit order advancing, canceling, etc.), and In this example I am changing orders to reduce profit target value each bar to avoid big loss on trades, but keeping them in sync inside my order managing class.

    This is the currently used code to enter those exit orders, they are connected by an OCO following the order managing class Id system i put in place

    Code:
    var stopLossOrder = SubmitOrderUnmanaged(0, OrderAction.Sell, OrderType.StopMarket, 1, 0, entryOrderStopPrice,orderManager.GenerateUniqueExitOrderSignalName(orderDetails.Pattern, "", orderQuantity), orderManager.GenerateUniqueExitOrderSignalName(orderDetails.Pattern, "L", orderQuantity));
    
    var profitTargetOrder = SubmitOrderUnmanaged(0, OrderAction.Sell, OrderType.Limit, 1, entryOrderProfitTarget, 0, orderManager.GenerateUniqueExitOrderSignalName(orderDetails.Pattern, "", orderQuantity), orderManager.GenerateUniqueExitOrderSignalName(orderDetails.Pattern, "T", orderQuantity));​


    This is the code for changing orders
    Code:
    // profitTargetOrder is assigned the right order  object via the order managing class.
    ChangeOrder(profitTargetOrder, profitTargetOrder.Quantity, profitTargetPrice, 0);
    Thanks for the continues help!
    Aviram Y.
    Last edited by Aviram Y; 04-07-2023, 03:38 PM.
    Aviram Y
    NinjaTrader Ecosystem Vendor - Aviram Y

    #2
    Hello Aviram Y,

    Thanks for your post.

    We are looking into the behavior you are reporting and will get back with you as soon as we have investigated this.

    Thanks in advance; I look forward to assisting further.
    Last edited by NinjaTrader_BrandonH; 04-10-2023, 11:19 AM.
    <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

    Comment


      #3
      Hello Aviram Y,

      Thanks for your patience.

      The NinjaTrader platform uses FIFO (First In First Out) for order management with the exchange when possible. You don't necessarily submit exit orders to exit a specific entry, you submit an exit order to reduce the total position size (e.g. EnterLong(1) twice, then you would have a 2 Long position, call ExitLong(1), now you have a 1 long position and the average entry price is adjusted).

      See the help guide documentation below for more information.

      FIFO: https://ninjatrader.com/support/help...hlightsub=FIFO

      Please let me know if I may assist further.
      <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

      Comment


        #4
        Hello BrandonH,

        Thanks for the reply.

        As far as I understand, FIFO will not allow for specific open positions to be connected with specific exit orders

        Because of the way my strategy was built, I have different scenarios delegated to Addon classes, and they each check for eligibility for trading on certain conditions in the market and enter a position whenever they are eligible, and thus they are named different things ( for example the STD prefix is one of them ).

        In the case of entering multiple contracts at the same time, the orders work as intended, meaning because the position was entered at the same time the position value for each contract will be filled either on the same tick, or by a small slippage delay, FIFO works great and each position is filled by its place in the queue.

        But in the case of entering multiple Long positions at different times, where each position has different stop loss and profit target exit orders intended for it, and both positions are open simultaneously, the first position that was opened will take the first exit order available.

        Is there no other way to handle these orders the way i intend? it just seems counter intuitive to do so..


        1. What are the alternatives in this case?

        2. What do you recommend doing?

        3. What about making different strategies for each scenario and having for example 3 different strategies run simultaneously? will the order logic be separated then? meaning i could have multiple open positions with different exit orders, one for each strategy but they are handled as separate cases?
        But even then I couldn't have multiple of the same open position type on each strategy at the same time since FIFO.


        4. Is there a way to create a feature request for this type of behavior?

        Thank you for you patience, Looking forward to hearing from you!

        Aviram Y.
        Aviram Y
        NinjaTrader Ecosystem Vendor - Aviram Y

        Comment


          #5
          Hello Aviram Y,

          Thanks for your notes.

          The NinjaTrader platform was designed to utilize FIFO functionality and there are no workarounds available for having your first position opened take a specific exit order. The first position opened will take the first exit order that is reached.

          I have added your vote to an existing feature request for support for pairing Entries and Exits with NS (Don't use FIFO). This request is being tracked under the number SFT-2928.

          As with all feature requests, interest is tracked before implementation is considered, so we cannot offer an ETA or promise of fulfillment. If implemented, it will be noted in the Release Notes page of the Help Guide.

          Release Notes — https://ninjatrader.com/support/help...ease_notes.htm

          Please let us know if we may be of further assistance to you.​
          <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

          Comment


            #6
            Hello BrandonH,

            Thanks for the reply.​

            Then what about questions 2 and 3? I would rather split the strategies than miss on trading opportunities, so this is a critical step in my strategy building.



            Thank you for your patience, Looking forward to hearing from you!

            Aviram Y.​
            Last edited by Aviram Y; 04-11-2023, 09:47 AM.
            Aviram Y
            NinjaTrader Ecosystem Vendor - Aviram Y

            Comment


              #7
              Hello Aviram Y,

              Thanks for your notes.

              A NinjaScript strategy is only able to see the orders submitted by that specific instance of the strategy so you could consider splitting the strategy into multiple strategies as you have described.

              For example, you could have 3 separate strategies running simultaneously as you noted in post # 4.

              Please let me know if I may assist further.
              <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

              Comment


                #8
                Hello BrandonH,

                Thanks for the reply.

                If this is the case, what if I would like to create a strategy that instantiates other strategies and essentially running multiple strategies at once through that strategy?
                That way avoiding clutter inside my accounts tab in the control center, since the strategy i built now consists of multiple scenarios.

                Is this possible?

                Will it separate the instantiated strategy logic from one another?(all the strategies will have their own logic, but will all be managed through the OnBarUpdate of the main strategy).

                Code:
                private YourStrategyClass1 strategy1;
                private YourStrategyClass2 strategy2;
                
                protected override void OnStateChange()
                {
                    if (State == State.SetDefaults)
                    {
                    }
                    else if (State == State.Configure)
                    {
                        strategy1 = new YourStrategyClass1();
                        strategy2 = new YourStrategyClass2();
                
                        // Initialize and configure each strategy
                        strategy1.SetInput(SOME_INPUT);
                        strategy2.SetInput(SOME_INPUT);
                
                    }
                }
                ​
                
                protected override void OnBarUpdate()
                {
                    // Implement your risk management and capital allocation logic here
                    strategy1.checkForTradingEligibility() // this will enter a position if true, this trade will be managed by strategy1, which will hold its own order manager instance inside it.
                    strategy2.checkForTradingEligibility()
                }
                ​

                Thanks again for the patience and answers, very appreciated!

                Aviram Y.
                Aviram Y
                NinjaTrader Ecosystem Vendor - Aviram Y

                Comment


                  #9
                  Hello Aviram Y,

                  Thanks for your notes.

                  There a no supported way to run multiple strategies from a single strategy instance. Each strategy is created manually by adding it to a chart or to the Strategies tab of the Control Center.

                  You could turn logic on or off in a single strategy based on some user input or enum but you have to manually enable that specific strategy.

                  Strategies have safeguards enabled which is why running them is intentionally a manual process because it trades on your behalf.

                  If you want a solution that you do not have to enable, you would need to use an indicator and addon framework.

                  You may consider trying to make a custom C# class for what you wanted to do in the example you provided. You would still need to use logic to turn on or off the variables you used but you could try adding custom C# logic in individual classes if you have advanced C# knowledge.
                  <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

                  Comment


                    #10
                    Is it me or does it seem weird that you specify an exit according to a specific order object via Signal Name - but the exit doesn't necessarily exit that particular order as FIFO overrules it?
                    Have I got that correct?

                    Can you turn off FIFO?
                    Last edited by Mindset; 04-25-2023, 12:45 AM.

                    Comment


                      #11
                      Hello Mindset,

                      Thanks for your notes.

                      No, FIFO cannot be turned off on the NinjaTrader platform. NinjaTrader will attempt to use FIFO when possible, but there are a few scenarios where this would not be possible.

                      See this help guide page for more information about how NinjaTrader utilizes FIFO: https://ninjatrader.com/support/help...hlightsub=fifo
                      <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by NullPointStrategies, Yesterday, 05:17 AM
                      0 responses
                      53 views
                      0 likes
                      Last Post NullPointStrategies  
                      Started by argusthome, 03-08-2026, 10:06 AM
                      0 responses
                      130 views
                      0 likes
                      Last Post argusthome  
                      Started by NabilKhattabi, 03-06-2026, 11:18 AM
                      0 responses
                      70 views
                      0 likes
                      Last Post NabilKhattabi  
                      Started by Deep42, 03-06-2026, 12:28 AM
                      0 responses
                      44 views
                      0 likes
                      Last Post Deep42
                      by Deep42
                       
                      Started by TheRealMorford, 03-05-2026, 06:15 PM
                      0 responses
                      49 views
                      0 likes
                      Last Post TheRealMorford  
                      Working...
                      X