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

account.CreateOrder() returns error: object reference not set to instance

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

    account.CreateOrder() returns error: object reference not set to instance

    Ninjascript Output:
    MES 12/14/2021 1:48:50 PM bid<=enprice
    MES 12/14/2021 1:48:50 PM activeAcc is S1Dec6729606
    Indicator 'aaBarBO': Error on calling 'OnBarUpdate' method on bar 80: Object reference not set to an instance of an object.

    Code producing above output:
    Order createEntry_S(double enPrice, double bid, string orderName)
    {
    Print(sym + " " + Time[0].ToString() + " bid<=enprice");
    Print(sym + " " + Time[0].ToString() + " activeAcc is " + activeAcc.Name);
    return activeAcc.CreateOrder(
    Instrument,
    OrderAction.Sell,
    OrderType.Limit,
    OrderEntry.Manual,
    TimeInForce.Day,
    input_qty,
    enPrice,
    enPrice,
    "",
    orderName + DateTime.Now.ToString(),
    DateTime.MaxValue,
    null);
    }

    The code executes with no error on Sim101 account. But it cannot execute with the other account S1Dec6729606, which is a Topsteptrader-Ninjatrader account.

    Thank you for any help.

    #2
    Hello xxzbill,

    object reference not set to instance means an object being used was null, from the given details that would likely be the account variable.

    Please make sure that you checked that the account name you are using to find the account is correct.

    You can verify if the account is null like the following:

    Code:
    if(account == null) Print("Account is null");
    JesseNinjaTrader Customer Service

    Comment


      #3
      Thanks for the reply.
      I thought that was what I was doing with this line: Print(sym + " " + Time[0].ToString() + " activeAcc is " + activeAcc.Name);
      If it can print activeAcc.Name then activeAcc is not null, right? From the output it shows that the error happens right after checking activeAcc is not null.

      Comment


        #4
        added your code

        Order createEntry_S(double enPrice, double bid, string orderName)
        {
        Print(sym + " " + Time[0].ToString() + " bid<=enprice");
        Print(sym + " " + Time[0].ToString() + " activeAcc is " + activeAcc.Name);

        if (activeAcc == null) Print(sym + " " + Time[0].ToString() + " activeAcc is null");

        return activeAcc.CreateOrder(
        Instrument,
        OrderAction.Sell,
        OrderType.Limit,
        OrderEntry.Manual,
        TimeInForce.Day,
        input_qty,
        enPrice,
        enPrice,
        "",
        orderName + DateTime.Now.ToString(),
        DateTime.MaxValue,
        null);
        }

        The output is the same:

        MES 12/14/2021 6:20:12 PM bid>enprice
        MES 12/14/2021 6:20:12 PM activeAcc is S1Dec6729606
        Indicator 'aaBarBO': Error on calling 'OnBarUpdate' method on bar 4587: Object reference not set to an instance of an object.

        So activeAcc is not null.

        Also the Print right after returning from createEntry_S() is not executed:
        en1 = createEntry_S(enPrice, GetCurrentBid(), "en1_SHORT");
        Print(sym + " " + Time[0].ToString()+" activeAcc.Submit(new[] {en1});");

        So the error is from activeAcc.CreateOrder(). But activeAcc is not null, what else could be the problem?

        Also this issue never occurs on Sim101 account with exactly the same code.

        Comment


          #5
          Hello xxzbill,

          From the error something is still null, you would need to check the other variables that you have used here to make sure that none of them are null. You are returning an object from your method so that would also be an area to confirm the en1 variable is not null.



          JesseNinjaTrader Customer Service

          Comment


            #6
            As you suggested, I added a Print after accout.CreateOrder() to isolate the error:

            {
            Print(sym + " " + Time[0].ToString() + " bid>enprice");
            Print(sym + " " + Time[0].ToString() + " activeAcc is " + activeAcc.Name);// == null ? "null" : "not null");
            if (activeAcc == null) Print(sym + " " + Time[0].ToString() + " activeAcc is null");

            Order returnOrder= activeAcc.CreateOrder(
            Instrument,
            OrderAction.Sell,
            OrderType.StopMarket,
            OrderEntry.Manual,
            TimeInForce.Day,
            input_qty,
            enPrice,
            enPrice,
            "",
            orderName + DateTime.Now.ToString(),
            DateTime.MaxValue,
            null);

            if (returnOrder == null) Print("returnOrder==null");

            return returnOrder;
            }

            Here is the output

            MES 12/15/2021 12:06:30 PM bid>enprice
            MES 12/15/2021 12:06:30 PM activeAcc is S1Dec6729606
            Indicator 'aaBarBO': Error on calling 'OnBarUpdate' method on bar 2298: Object reference not set to an instance of an object.

            The Print("returnOrder==null") does not execute. So the issue is in account.CreateOrder(). Here I commented on the variables in account.CreateOrder():

            Order returnOrder= activeAcc.CreateOrder(
            Instrument, // this is native to the base code and global. it should never be null
            OrderAction.Sell, //constants
            OrderType.StopMarket,
            OrderEntry.Manual,
            TimeInForce.Day,
            input_qty, // this is an integer
            enPrice, // this is a double
            enPrice,
            "",
            orderName + DateTime.Now.ToString(), // orderName is string, DateTime.Now is from base C# and should never be null
            DateTime.MaxValue, // a constant
            null);

            Comment


              #7
              Hello xxzbill,

              The next step would be to print the variables you used in CreateOrder to see which part is not correct. If the CreateOrder is failing that would indicate that part of the variables used were not correct. You may need to use OrderEntry.Automated if this is being used in a strategy.

              Another way to test this would be to manually fill in the values like the help guide sample to verify that works.

              JesseNinjaTrader Customer Service

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by dward123, 01-02-2024, 09:59 PM
              4 responses
              174 views
              0 likes
              Last Post Lancer
              by Lancer
               
              Started by ETFVoyageur, Today, 04:00 PM
              2 responses
              19 views
              0 likes
              Last Post ETFVoyageur  
              Started by AaronKTradingForum, Today, 03:44 PM
              1 response
              8 views
              0 likes
              Last Post AaronKTradingForum  
              Started by Felix Reichert, 04-26-2024, 02:12 PM
              11 responses
              80 views
              0 likes
              Last Post Felix Reichert  
              Started by junkone, 04-28-2024, 02:19 PM
              7 responses
              83 views
              1 like
              Last Post junkone
              by junkone
               
              Working...
              X