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

Getting error after position is closed

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

    Getting error after position is closed

    Hello

    I use the code hereafter, that opens a long position then closes this latter after the profit and loss reaches 100€.

    When the PnL of 100€ is reached, the (account) position is well closed but I have an error that I
    try to intercept with try/catch blocks in order for the strategy not to be disabled but it is still disabled after the error...

    The error is :
    Exception while trying to access Strategy Position : La référence d'objet n'est pas définie à une instance d'un objet.
    **NT** Error on calling 'OnBarUpdate' method for strategy 'IchimokuExpertStrategy/682e1335e2244371ba1fae15d0be724c': La référence d'objet n'est pas définie à une instance d'un objet.


    Thanks !

    Code:
    		double ask = 0;
    		double bid = 0;
    		double PnL = 0;
    		DateTime dt = new DateTime();
    		bool done = false;
            /// <summary>
            /// Called on each bar update event (incoming tick)
            /// </summary>
            protected override void OnBarUpdate()
            {
    			dt = DateTime.Now;	
    			
    			Print("**** Account Positions ****");
    			if (Account.Positions.Count>0)
    			{
    				for (int i=0;i<Account.Positions.Count;i++)
    				{
    					try
    					{
    						Print("Account Position " + i);
    						Print(">> " + dt + " : Instrument Name = " + Account.Positions[i].Instrument.FullName);
    						Print(">> " + dt + " : PnL = " + Account.Positions[i].GetProfitLoss(GetCurrentBid(), PerformanceUnit.Currency));
    						Print(">> " + dt + " : Qty = " + Account.Positions[i].Quantity);
    						double profit = Account.Positions[i].GetProfitLoss(GetCurrentBid(), PerformanceUnit.Currency);
    						if (profit>=100)
    						{
    							Account.Positions[i].Close();
    						}
    					}
    					catch(Exception ex)
    					{
    						Print("Exception while trying to access Account Position " + i + " : " + ex.Message);
    					}
    				}
    			}
    			
    			try
    			{
    				Print("**** Strategy Position ****");
    				PnL = Position.GetProfitLoss(GetCurrentBid(), PerformanceUnit.Currency);
    				Print(">> " + dt + " : PnL =" + PnL);
    				Print(">> " + dt + " : Qty =" + Position.Quantity);
    			}
    			catch(Exception ex)
    			{
    				[B][COLOR="lime"]Print("Exception while trying to access Strategy Position : " + ex.Message);[/COLOR][/B]
    			}
    						
    			if (this.Historical == true) return;
    			
    			double ask = this.GetCurrentAsk();
    			double bid = this.GetCurrentBid();
    			if (done == false)
    			{
    				Print("Will enter long position");
    				IOrder iorder = EnterLong(1000000);
    				done = true;
    			}			
    			
    			Print("");
            }
    Last edited by investdatasystems; 03-14-2018, 03:16 AM.

    #2
    I have just tried to put a try/catch block around all the code in the OnBarUpdate() overrided method but the Strategy is disabled also when the Index error is printed.

    Comment


      #3
      Adding this condition seems to resolve my issue :

      Code:
      if (this.Positions.GetLength(0)>0)
      I hope that will help

      Comment


        #4
        Hello

        Thank you for the post.

        To start, I would not suggest using try/catch statements in ninjascript. The reason is that if you bypass an exception with a try/catch, you could allow the script to still try to execute other code in a bad state, the script should instead be stopped so you can correct the exception.

        The error "Object reference not set to an instance of an object." means that an object you have used was null when you used it. As I cannot see the rest of the prints you are using I am not sure where this error would have come from during the execution of the script.

        For this type of problem, instead of using a try catch, I would suggest using some Prints to see either where the logical execution stops or to print out the objects you use to know which is null.

        In your current logic, you have prints already so you should have some output surrounding the exception. If you run the script without any try catch, it should be the last output before the script stopped which is helpful.

        If you can post the output leading up to the exception that would be helpful as we may be able to compare where the prints stopped to narrow what logic to look at. Otherwise, you should be able to use the output from your prints to locate the problematic line and we could further review why that was null knowing what object it was.

        I look forward to being of further assistance.
        JesseNinjaTrader Customer Service

        Comment


          #5
          Thank you for your reply.

          I have resolved the issue by adding the following code before any further attempt to read the Strategy Position :

          if (this.Positions.GetLength(0)>0){
          if (this.Position.Quantity>0){

          }
          }
          I'll also avoid using try/catch blocks then.

          Thanks again !!!
          Last edited by investdatasystems; 03-14-2018, 10:17 AM.

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by warreng86, 11-10-2020, 02:04 PM
          5 responses
          1,355 views
          0 likes
          Last Post NinjaTrader_Manfred  
          Started by Perr0Grande, Today, 08:16 PM
          0 responses
          3 views
          0 likes
          Last Post Perr0Grande  
          Started by elderan, Today, 08:03 PM
          0 responses
          5 views
          0 likes
          Last Post elderan
          by elderan
           
          Started by algospoke, Today, 06:40 PM
          0 responses
          10 views
          0 likes
          Last Post algospoke  
          Started by maybeimnotrader, Today, 05:46 PM
          0 responses
          12 views
          0 likes
          Last Post maybeimnotrader  
          Working...
          X