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

Position filled, Stop not set.

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

    Position filled, Stop not set.

    Ive just be debugging a strategy in visual Studio and found a null error due to the fact that I have a position but the stop order has not been set?

    Shouldn't OnExection place the stop order as soon as an order is filled?

    #2
    Hello GKonheiser,

    This will depend on how you are placing your Stop order. If you are placing your stop order using something like SetStopLoss() inside of Initialize() then yes this will happen after OnExecution but not inside of it.

    You may see our example on Monitoring stop and profit orders for a better understanding of this.


    Let me know if you have any questions.
    JCNinjaTrader Customer Service

    Comment


      #3
      This is what I have in OnExecution, so if the stop order is not placed when the my order is filled when is it placed?


      protected override void OnExecution(IExecution execution)
      {
      if(this.indPivAverage.ExtrnTrend[0] == 1) // OnExecution long trades
      {
      if (pEntry != null && pEntry == execution.Order)
      {
      if ( execution.Order.OrderState == OrderState.Filled
      || execution.Order.OrderState == OrderState.PartFilled
      || (execution.Order.OrderState == OrderState.Cancelled && execution.Order.Filled > 0)
      )
      {
      // Stop order
      pStop = ExitLongStop(0, true, execution.Order.Filled, lPiv - stop * TickSize, "Stop of Long", "pEntry");

      // Resets the entryOrder object to null after the order has been filled
      if (execution.Order.OrderState != OrderState.PartFilled)
      {
      longPrice = pEntry.AvgFillPrice;
      pEntry = null;
      }
      }
      }

      Comment


        #4
        Hello GKonheiser,

        Placing your Stop order this way should have it be placed inside of OnExeuction.

        If your stop order is not placed you may want to enable TraceOrders = true inside of Initialize() and see if there are any message about your order not being placed.



        Let me know if you get any messages about your order not being placed.
        JCNinjaTrader Customer Service

        Comment


          #5
          ok so there's a problem some where, will investigate.

          Comment


            #6
            Hi JC

            I enabled trace and have the following output. It like the open position just disapears, can you shed any light on it, as the log all of a sudden goes to "This was an exit order but no position exists to exit'

            29/11/2011 11:32:00 Entered internal PlaceOrder() method at 29/11/2011 11:32:00: BarsInProgress=0 Action=SellShort OrderType=Limit Quantity=1 LimitPrice=2238 StopPrice=0 SignalName='pEntry' FromEntrySignal=''
            29/11/2011 11:32:00 Ignore order amendment: Action=SellShort OrderType=Limit Quantity=1 LimitPrice=2238 StopPrice=0 SignalName=pEntry' FromEntrySignal='' Reason='Order already has this stop price/limit price/quantity'
            29/11/2011 11:32:00
            29/11/2011 11:32:00 Entered internal PlaceOrder() method at 29/11/2011 11:32:00: BarsInProgress=0 Action=BuyToCover OrderType=Stop Quantity=1 LimitPrice=0 StopPrice=2246 SignalName='Stop of Short' FromEntrySignal='pEntry'
            29/11/2011 14:05:00 Entered internal PlaceOrder() method at 29/11/2011 14:05:00: BarsInProgress=0 Action=Sell OrderType=Stop Quantity=1 LimitPrice=0 StopPrice=2241 SignalName='Stop of Short' FromEntrySignal='pEntry'
            29/11/2011 14:05:00 Ignored PlaceOrder() method: Action=Sell OrderType=Stop Quantity=1 LimitPrice=0 StopPrice=2241 SignalName='Stop of Short' FromEntrySignal='pEntry' Reason='This was an exit order but no position exists to exit'
            **NT** Error on calling 'OnBarUpdate' method for strategy 'PivShortCode/847923e787b94f2698b39b9774bb2a9b': Object reference not set to an instance of an object.
            29/11/2011 14:27:00 CancelAllOrders: BarsInProgress=0
            28/11/2011 22:00:00 CancelAllOrders: BarsInProgress=1
            29/11/2011 14:27:00 CancelAllOrders: BarsInProgress=2

            Comment


              #7
              Hello GKonheiser,

              The output results seem to show an issue inside of your OnBarUpdate() method most likely.

              For starters the "pEntry" signal should be a Long order according to your OnExecution() method but it is being put in as a SellShort order as shown in in Blue below.

              29/11/2011 11:32:00 Entered internal PlaceOrder() method at 29/11/2011 11:32:00: BarsInProgress=0 Action=SellShort OrderType=Limit Quantity=1 LimitPrice=2238 StopPrice=0 SignalName='pEntry' FromEntrySignal=''

              The next item is that your Stop price is being set to the same price as the an entry order (Red above) that could result in an order rejection.

              Lastly, I see that you are getting an error inside of OnBarUpdate():
              **NT** Error on calling 'OnBarUpdate' method for strategy 'PivShortCode/847923e787b94f2698b39b9774bb2a9b': Object reference not set to an instance of an object.

              I would suggest finding the object that you are referencing and start checking its values with the Print() statement as it looks to be a logic error.
              JCNinjaTrader Customer Service

              Comment


                #8
                Hi JC,

                pEntry can be both a long or a short order, ie it will check in the morning to see if it is a buy day or a sell day.

                Secondly the stop price is adjusted once the order is in the money by a certain amount.

                Lastly the null error in onbar update is because the the stop order is all of a sudden null when it should have a order in it, this is causing the problem,

                "This was an exit order but no position exists to exit" its as if the position has disappeared ?

                Comment


                  #9
                  Hello GKonheiser,

                  From the code snippets that you have sent you are only placing "ExitLongStop" which is only going to be working if you are in a Long Position which is causing this.

                  You may either want to add some logic in your strategy to check to see what position you are in and use the appropriate Exit() method like ExitShortStop(), or use different entry order names for long or short orders.
                  JCNinjaTrader Customer Service

                  Comment


                    #10
                    I found the error. I copied a load of code and missed one of the ExitLongStop commands.

                    Thanks for your help

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by PaulMohn, Yesterday, 02:06 AM
                    7 responses
                    20 views
                    0 likes
                    Last Post PaulMohn  
                    Started by f.saeidi, Today, 08:01 AM
                    0 responses
                    3 views
                    0 likes
                    Last Post f.saeidi  
                    Started by ezrollin, 02-26-2022, 11:14 PM
                    8 responses
                    210 views
                    0 likes
                    Last Post NinjaTrader_ChelseaB  
                    Started by Mindset, Yesterday, 10:38 PM
                    1 response
                    15 views
                    0 likes
                    Last Post NinjaTrader_BrandonH  
                    Started by SentientDavid, Today, 01:34 AM
                    2 responses
                    27 views
                    0 likes
                    Last Post bltdavid  
                    Working...
                    X