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

Cancelling order

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

    Cancelling order

    Using the following strategy, the CancelOrder() method generate this error: "**NT** Error on calling 'OnBarUpdate' method for strategy 'Strategy02/80f3562106154d488f4581631b589818': Object reference not set to an instance of an object."

    Is there something that I am missing?

    Thanks,
    Sebastian


    Code:
    
    #region Using declarations
    using System;
    using System.ComponentModel;
    using System.Diagnostics;
    using System.Drawing;
    using System.Drawing.Drawing2D;
    using System.Xml.Serialization;
    using NinjaTrader.Cbi;
    using NinjaTrader.Data;
    using NinjaTrader.Indicator;
    using NinjaTrader.Strategy;
    #endregion
    
    
    namespace NinjaTrader.Strategy
    {
        
        
        [Description("Strategy02")]
        public class Strategy02 : Strategy
        {
            #region Variables
            private int        fast    = 10;
            private int        slow    = 25;
            
            private IOrder myEntryLongOrder = null;
            private int barNumberOfLongOrder = 0;
            
            
            #endregion
    
            
            protected override void Initialize()
            {
                SMA(Fast).Plots[0].Pen.Color = Color.Orange;
                SMA(Slow).Plots[0].Pen.Color = Color.Green;
    
                Add(SMA(Fast));
                Add(SMA(Slow));
    
                CalculateOnBarClose    = true;
            }
    
            
            protected override void OnBarUpdate()
            {
                if (CrossAbove(SMA(Fast), SMA(Slow), 1) && myEntryLongOrder == null) {
                        myEntryLongOrder = EnterLongLimit(0, true, 1, Low[0], "Long Entry");
                        barNumberOfLongOrder = CurrentBar;
                    }
                
                if (CurrentBar > barNumberOfLongOrder + 5){
                    CancelOrder(myEntryLongOrder);
                }
                
            }
            
            
            protected override void OnOrderUpdate(IOrder order)
            {
                if (myEntryLongOrder != null && myEntryLongOrder == order){
                    Print(order.ToString());
                    
                    if (order.OrderState == OrderState.Cancelled){
                        myEntryLongOrder = null;
                    }
                }
            }
            
            
            
    
            #region Properties
            /// <summary>
            /// </summary>
            [Description("Period for fast MA")]
            [GridCategory("Parameters")]
            public int Fast
            {
                get { return fast; }
                set { fast = Math.Max(1, value); }
            }
    
            /// <summary>
            /// </summary>
            [Description("Period for slow MA")]
            [GridCategory("Parameters")]
            public int Slow
            {
                get { return slow; }
                set { slow = Math.Max(1, value); }
            }
            #endregion
        }
    }

    #2
    Hello Sebastian,

    Thank you for writing in.

    Please check if myEntryLongOrder is not null before trying to cancel it.

    Please take a look at this link on our support forum on 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

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by Shansen, 08-30-2019, 10:18 PM
    24 responses
    939 views
    0 likes
    Last Post spwizard  
    Started by Max238, Today, 01:28 AM
    0 responses
    7 views
    0 likes
    Last Post Max238
    by Max238
     
    Started by rocketman7, Today, 01:00 AM
    0 responses
    4 views
    0 likes
    Last Post rocketman7  
    Started by wzgy0920, 04-20-2024, 06:09 PM
    2 responses
    28 views
    0 likes
    Last Post wzgy0920  
    Started by wzgy0920, 02-22-2024, 01:11 AM
    5 responses
    33 views
    0 likes
    Last Post wzgy0920  
    Working...
    X