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

Failing to understand why my stop order isnt getting triggered

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

    Failing to understand why my stop order isnt getting triggered

    Good afternoon,

    I am having trouble understanding why my stop isnt getting triggered.

    Here is my strategy code.
    1) I have a short position that was initiated using
    Code:
     EnterShort(trade_quantity,"entry")
    2) I then want to attach a stop loss using the following code
    Code:
            protected override void OnBarUpdate()
            {//ON BAR STARTS
    
                // Handling Existing Shorts
                if (Position.MarketPosition == MarketPosition.Short)
                {                
                    // Stop Loss Adding
                    if ( conditions for placing the stop loss)
                    {   ExitShortStopMarket(trade_quantity,fill_track,"stop","entry");
                        Print("stop placed");
                        Print(Times[0][0]);
                        Print(stopOrder.ToString());}​
    My Print statements are showing in the output window meaning that stopOrder is successfully created.

    stop placed
    2023-11-16 10:42:00 AM
    orderId='NT-00028-1175' account='Backtest' name='stop' orderState=Working instrument='QQQ' orderAction=BuyToCover orderType='Stop Market' limitPrice=0 stopPrice=385.05 quantity=64 tif=Gtc oco='' filled=0 averageFillPrice=0 onBehalfOf='' id=-1 time='2023-11-16 10:42:00' gtd='2099-12-01' statementDate='2024-04-27'

    Now notice the stopPrice for the order which is 385.05.
    The below is a screenshot from the strategy. I highlighted in yellow where the "entry" short position was initiated, and then I am highlighting the wick of the candle at 10:48 that goes way pass the 385.05 stop price of the order.

    So why is that stop order not triggered then?


    Click image for larger version

Name:	Screenshot 2024-04-27 101419.jpg
Views:	46
Size:	124.2 KB
ID:	1301245

    Here is also how I assign names for the orders. Including it for completing purposes.

    Code:
            protected override void OnOrderUpdate(Order order, double limitPrice, double stopPrice, int quantity, int filled, double averageFillPrice, OrderState orderState, DateTime time, ErrorCode error, string nativeError)
            {
                // Assign order name
                if (order.Name == "entry")
                {    entryOrder = order;}
                else if (order.Name == "stop")
                {    stopOrder = order;}
                
                // Reset stopOrder to null
                if (stopOrder == order && (order.OrderState == OrderState.Filled || stopOrder.OrderState == OrderState.Cancelled))
                {    stopOrder = null;}
           }​

    #2
    Hello ChatTourist,

    Thanks for your post.

    What version of NinjaTrader are you using? Please provide the entire version number. This can be found under Help -> About (Example: 8.?.?.?)

    To clarify, are you seeing this behavior occur when backtesting a strategy in the Strategy Analyzer window?

    Please create a test strategy that contains only the necessary logic used to reproduce the behavior and prints, test the script to make sure the behavior is reproducible, and then send us an exported copy of the strategy along with the exact steps and settings you are using to reproduce the behavior so we may try reproducing the behavior on our end and investigate further.

    Exporting: https://ninjatrader.com/support/help...tAsSourceFiles

    We look forward to further assisting.
    Brandon H.NinjaTrader Customer Service

    Comment


      #3
      Hello Brandon,

      My version is: 8.1.3.0 64-bit

      I am attaching a test script that has the one trade hard coded in.

      Please make sure to set the correct dates for Strat Analyzer: 2023-11-16

      Click image for larger version  Name:	Screenshot 2024-04-28 173911.jpg Views:	16 Size:	216.0 KB ID:	1301363
      Last edited by NinjaTrader_BrandonH; 05-01-2024, 07:26 AM.

      Comment


        #4
        NinjaTrader_BrandonH Hi Brandon, just wanted to check if there were any updates about the stop order?

        Comment


          #5
          Hello ChartTourist,

          Thanks for your notes.

          I am seeing that the Entry order is closing at the end of the session when the Exit On Session Close behavior triggers.

          To understand exactly why the Exit order is not being placed or filled, you should further debug your strategy by adding prints one line above the condition to place the Exit order that prints out all the values used in the condition to place the exit order along with the time of the bar. Add labels and comparison operators to the prints as well so you know what is being compared. One line above the Exit order method, print out the price you are submitting the order too also.

          See this Support article Developer Guide on Debugging using Print() and TraceOrders for more information about using prints to understand the behavior of a script:
          Last edited by NinjaTrader_BrandonH; 05-01-2024, 07:39 AM.
          Brandon H.NinjaTrader Customer Service

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by haas88, 03-21-2024, 02:22 AM
          18 responses
          208 views
          0 likes
          Last Post haas88
          by haas88
           
          Started by Board game geek, Today, 02:20 AM
          0 responses
          2 views
          0 likes
          Last Post Board game geek  
          Started by knighty6508, Today, 01:20 AM
          2 responses
          14 views
          0 likes
          Last Post knighty6508  
          Started by franatas, Today, 01:53 AM
          0 responses
          3 views
          0 likes
          Last Post franatas  
          Started by knighty6508, Today, 01:17 AM
          0 responses
          9 views
          0 likes
          Last Post knighty6508  
          Working...
          X