Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Reverse Issue with Order Handleing

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

    Reverse Issue with Order Handleing

    I have a strategy when a trade goes against me a specific amount, I stop out and enter a reverse trade. So, a long trade that stops out will turn into a Short trade. I've coded it and it works when I try on the ES and MES separately. However, my strategy uses the ES and MES together based on account size and amount of risk. So, if is determined I need 2.3 contracts, it enters with 2 ES and 3 MES contracts. The initial part of the trade works perfectly, however when the stop is hit, the ES reversal fires off fine, the MES gets ignored. I've also tried this with market orders instead of limit orders, and while the MES does enter the trade, the Profit and Stop loss orders are not placed.

    Profit target and stop loss are set by the set commands and the ES and MES each have their own signals. I believe this is an order handling issue, but I have no idea how to resolve it. I'm hoping to find help here before going down the unmanaged order rabbit hole.


    Code:
    if (stopTriggered == true && secondChanceComplete == false && EnableSecondChance == true )
                            {
                                Print(String.Format("Second Chance Activated | {0} | Mini: {1} | Micro: {2}", scQuantityFull,scMiniQuantityInt, scMicroQuantityInt ));
    
                                //execute second chance trade
    
                                    SetProfitTarget( "SCEntry", CalculationMode.Price, scTargetPrice);
                                    SetStopLoss(  "SCEntry", CalculationMode.Price, scStopPrice, false);
    
    
                                if (MicroInstrument == true)
                                {
                                    SetProfitTarget( "SCMicroEntry", CalculationMode.Price, scTargetPrice);
                                    SetStopLoss( "SCMicroEntry", CalculationMode.Price, scStopPrice, false);
                                }
    
    
    
    
                                if (direction == "SHORT")
                                {                
                                        if (MiniQuantityInt > 0)
                                        {
                                            EnterLongLimit(miniTradeDataSeries, liveUntilCancelled, scMiniQuantityInt, scEntryPrice, "SCEntry" );
    
                                        }
    
    
                                    if (MicroInstrument == true)
                                    {                    
                                        if (MicroQuantityInt > 0)
                                        {
                                            EnterLongLimit(microTradeDataSeries, liveUntilCancelled, scMicroQuantityInt, scEntryPrice, "SCMicroEntry" );  
                                        }
                                    }
    
    
                                }
                                else if (direction == "LONG")
                                {                                                   
                                        if (MiniQuantityInt > 0)
                                        {
                                            EnterShortLimit( miniTradeDataSeries, liveUntilCancelled, scMiniQuantityInt, scEntryPrice, "SCEntry");
                                        }
    
    
                                    if (MicroInstrument == true )
                                    {
                                        if (MicroQuantityInt > 0)
                                        {
                                            EnterShortLimit( microTradeDataSeries, liveUntilCancelled, scMicroQuantityInt, scEntryPrice, "SCMicroEntry");
    
                                        }
    
                                    }
    
                                }          
    
    
                                 secondChanceComplete = true; //ensures second chance is only run once per day
                            }​

    stopTriggered is set to true during OnOrderUpdate when stop order is filled

    Code:
    if (order.Name == "Stop loss")
                        stopOrder = order;
    
    
     if (stopOrder != null && stopOrder.IsBacktestOrder && State == State.Realtime)
          stopOrder = GetRealtimeOrder(stopOrder);
    
     if (stopOrder != null && stopOrder == order)
     {
    
        if (order.OrderState == OrderState.Filled)
           stopTriggered     = true;
      }​​

    #2
    Hello maltin,

    Thanks for your post.

    I see in the code you shared that you are using the same signal names for your Long entry orders and Short entry orders. You will need to assign different signal names for Long and Short entry orders and pair sets of SetStopLoss()/SetProfitTarget() to keep them separated since you are using the Price parameter for the stop/target orders.

    For example, instead of assigning "SCEntry to both the Long and Short Entry orders, you should use something like "SCEntryLong" for Long Entry orders and "SCEntryShort" for Short Entry orders. These would need pairs of SetStopLoss()/SetProfitTarget() with the appropriate fromEntrySignal name of "SCEntryLong" and "SCEntryShort".

    Further, SetStopLoss()/SetProfitTarget() are being set after the entry orders. This should be changed so that SetStopLoss()/SetProfitTarget() are set before the entry orders are placed since Set method prep NinjaTrader to submit protective orders upon the execution of entry orders.

    For example:

    if (condition)
    {

    SetProfitTarget( "SCEntryLong", CalculationMode.Price, scTargetPrice);
    SetStopLoss( "SCEntryLong", CalculationMode.Price, scStopPrice, false);​​
    EnterLongLimit(miniTradeDataSeries, liveUntilCancelled, scMiniQuantityInt, scEntryPrice, "SCEntryLong" );
    }


    From the SetStopLoss() help guide:
    • Stop loss orders are submitted in real-time on incoming executions from entry orders
    • Since they are submitted upon receiving an execution, the Set method should be called prior to submitting the associated entry order to ensure an initial level is set.
    ​From the SetProfitTarget() help guide:
    • Profit target orders are submitted in real-time on incoming executions from entry orders
    • Since they are submitted upon receiving an execution, the Set method should be called prior to submitting the associated entry order to ensure an initial level is set.​

    SetStopLoss(): https://ninjatrader.com/support/help...ub=setstoploss
    SetProfitTarget(): https://ninjatrader.com/support/help...b=setprofittar get​
    <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
      Hi Brandon, Thank you for the response! I've updated the signals for long and short, and placed the setprofittarget and setstoploss directly above each entry order, as you indicated. I'm still getting one of my reverse entry ignored by the Internal Order handling rules. I've read through that part of the manual, but I can't pin down why my the second order is being ignored.

      The logic to enter these orders is triggered by this logic:
      if (stopOrder != null && stopOrder.OrderState == OrderState.Filled)
      {
      // enter the reverse orders for MES and ES here
      }

      The MES order fires, but the ES one is now ignored.

      I've noticed that all stop orders created by SetStopOrder() have a "Stop loss" signal. Do you think this might have something to do with my issue?

      Or could it be related to the fact that I'm reversing on the MES and ES at the same price level?
      Last edited by maltin; 05-16-2023, 08:31 PM.

      Comment


        #4
        Hello maltin,

        Thanks for your note.

        Without seeing the code for the strategy I am not certain what the exact cause of the error message could be.

        Are you using Exit methods and Set methods in your logic?

        Please share a reduced copy of the strategy that demonstrates the error and I will be happy to see if anything specific stands out in your code that might cause this error.

        Note that a reduced copy refers to a copy of the script that contains the minimum amount of code needed to reproduce the issue. All other code is commented out or removed.

        To create a copy of your script to modify, open a New > NinjaScript Editor, select your script, right-click in the Editor, select 'Save as', name the script, and click OK.​
        <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


          #5
          Hi Brandon,

          I solved the issue! I used Exit methods to get better control of exit signals to insure the stops on both sides were filled before entering the new trades. Thank you for the hint around using exit orders!

          Cheers!
          Last edited by maltin; 05-18-2023, 12:17 AM.

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by NullPointStrategies, Yesterday, 05:17 AM
          0 responses
          72 views
          0 likes
          Last Post NullPointStrategies  
          Started by argusthome, 03-08-2026, 10:06 AM
          0 responses
          143 views
          0 likes
          Last Post argusthome  
          Started by NabilKhattabi, 03-06-2026, 11:18 AM
          0 responses
          76 views
          0 likes
          Last Post NabilKhattabi  
          Started by Deep42, 03-06-2026, 12:28 AM
          0 responses
          47 views
          0 likes
          Last Post Deep42
          by Deep42
           
          Started by TheRealMorford, 03-05-2026, 06:15 PM
          0 responses
          51 views
          0 likes
          Last Post TheRealMorford  
          Working...
          X