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

CancelOrder() on non-existent IOrders

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

    CancelOrder() on non-existent IOrders

    Hello,

    Does calling CancelOrder() on a non-existent ("=null") IOrder cause an error to occur within the strategy or does ninjascript choose to ignore the CancelOrder() request?

    #2
    Hello Elynt,

    Thank you for your inquiry.

    A run-time error will occur if the order is null: "object reference not set to an instance of an object".

    It is always suggested to run a null check before attempting to access objects that can be null.

    Please take a look at this post in our Tips section for more information about checking for null references: http://ninjatrader.com/support/forum...44&postcount=1

    Please, let us know if we may be of further assistance.
    Zachary G.NinjaTrader Customer Service

    Comment


      #3
      Makes perfect sense. Thank you for the link!

      Comment


        #4
        Thought I'd post a similar problem I'm having in this thread instead of starting a new one.

        I'm having difficulty canceling pending entry orders once one entry order is filled. On entry conditions, two different IOrder objects are assigned two different order types at two different prices, one higher and one lower than the current price. Both IOrder objects are sent to the market successfully (one is "accepted" the other "working"). When one order is filled, the other order ought to cancel, but it does not until the close of the bar (at which time the Managed Approach cancels the pending order). The log shows no signs of attempting to close the pending order prior to the close of the bar.

        Here is my code:

        Code:
        protected override void Initialize()
                {
                     CalculateOnBarClose = false;
                     EntriesPerDirection = 1;
            	     EntryHandling = EntryHandling.UniqueEntries;
        			
                }
        protected override void OnBarUpdate()
                {
        			
                           if (Position.MarketPosition != MarketPosition.Flat && nullcheck == false)
        			{
        				nullcheck = true;
        
        				if (entryOrderUpHigh != null) 
        				{
        					if (entryOrderUpHigh.OrderState == OrderState.Filled)
        					{
        						if (entryOrderUpLow != null)
        						{
        							CancelOrder(entryOrderUpLow);
        							entryOrderUpLow = null;
        						}
        					}
        				}
        				if (entryOrderUpLow != null) 
        				{
        					if (entryOrderUpLow.OrderState == OrderState.Filled)
        					{
        						if (entryOrderUpHigh != null)
        						{
        							CancelOrder(entryOrderUpHigh);
        							entryOrderUpHigh = null;
        						}
        					}
        				}
        			}
        [INDENT][INDENT]if (entry conditions)
        {
        entryOrderUpHigh = stop order
        entryOrderUpLow = limit order
        }[/INDENT][/INDENT][INDENT]if (FirstTickOfBar)
        {
        nullcheck = false;
        }[/INDENT]
        }
        Thanks for the help!

        Comment


          #5
          Hello Elynt,

          I would suggest utilizing the OnExecution() method to cancel your other pending order once one of them fills rather than doing that in OnBarUpdate(). OnBarUpdate() is only called at the close of a bar (if CalculateOnBarClose is set to true) or on every new tick (if CalculateOnBarClose is set to false).

          OnExecution() is called upon the execution, or fill, of an order. More information about the OnExecution() method can be found in the help guide at this link: http://ninjatrader.com/support/helpG...nexecution.htm

          If you would like to cancel an order upon another order filling, OnExecution() is what you would want to utilize.

          Please, let us know if we may be of further assistance.
          Zachary G.NinjaTrader Customer Service

          Comment

          Latest Posts

          Collapse

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