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

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​
    Brandon H.NinjaTrader Customer Service

    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.​
        Brandon H.NinjaTrader Customer Service

        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 fx.practic, 10-15-2013, 12:53 AM
          5 responses
          5,406 views
          0 likes
          Last Post Bidder
          by Bidder
           
          Started by Shai Samuel, 07-02-2022, 02:46 PM
          4 responses
          98 views
          0 likes
          Last Post Bidder
          by Bidder
           
          Started by DJ888, Yesterday, 10:57 PM
          0 responses
          8 views
          0 likes
          Last Post DJ888
          by DJ888
           
          Started by MacDad, 02-25-2024, 11:48 PM
          7 responses
          160 views
          0 likes
          Last Post loganjarosz123  
          Started by Belfortbucks, Yesterday, 09:29 PM
          0 responses
          9 views
          0 likes
          Last Post Belfortbucks  
          Working...
          X