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

Error related to order execution (I think)

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

    Error related to order execution (I think)

    Hi. I'm just getting started and haven't coded for twenty years. Getting the following Error when I try to run my strat in the Playback.


    Time Category Message
    5/28/18 4:55:37 PM Default Fill type 'Default fill type': Error on calling 'OnBar' method: System.NullReferenceException: Object reference not set to an instance of an object. at NinjaTrader.NinjaScript.StrategyBase.AddExecution( Position position, Execution execution, Order order) at NinjaTrader.NinjaScript.StrategyBase.FillOrder(Ord er order, Double fillPrice, Double slippage) at NinjaTrader.NinjaScript.DefaultFillType.OnBar() at NinjaTrader.NinjaScript.StrategyBase.ProcessBackte stOrders()

    I believe it is related to this line of code because when I comment it out everything runs fine (minus the actual trades):

    EnterLongLimit(0, false, 1, GetCurrentAsk(), signal);

    Here's the entire OnBarUpdate section if it's helpful.

    protected override void OnBarUpdate()
    {
    if (CurrentBar < BarsRequiredToTrade)
    return;
    // OnBarUpdate() will be called on incoming tick events on all Bars objects added to the strategy
    // We only want to process events on our primary Bars object (main instrument) (index = 0) which
    // is set when adding the strategy to a chart
    if (BarsInProgress != 0)
    return;
    if (CurrentBars[0] < 0)
    return;
    // Checks if the RSI is crossing below or above
    buySignal = CrossAbove(rsi, BuyCross, 1);
    if (buySignal)
    {
    Print("Buycross"+rsi[0]);
    // Draws a square 1 tick above the high of the bar identifying when a limit order is issued
    Draw.Square(this, "My Square" + CurrentBar, false, 0, High[0] + TickSize, Brushes.DodgerBlue);

    // Enter a long position via a limit order at the current ask price
    signal = ("Buy Limit"+ CurrentBar.ToString());
    Print(signal);
    EnterLongLimit(0, false, 1, GetCurrentAsk(), signal);
    }
    SetProfitTarget(signal, CalculationMode.Ticks,BuyProfitTarget);
    SetStopLoss(signal,CalculationMode.Ticks,BuyStopLo ss,true);
    }

    Please let me know if there's any other info I can provide. Thanks.

    #2
    Hello stewarco,

    Thank you for your note.

    Without the full code we're unable to test on our end.

    If you'd like to upload the full code I can take a look and see if anything jumps out. Or if you'd prefer to email a copy, send to platformsupport[at]ninjatrader[dot]com with Attn: Alan P in the Subject line. Also within the email please include a link to this thread, and the files.

    I look forward to your reply.
    Alan P.NinjaTrader Customer Service

    Comment


      #3
      full code

      Hey Alan. Thanks for quick reply.
      I believe I have uploaded the code here. Please let me know if you don't see it.
      Attached Files

      Comment


        #4
        Hello stewarco,

        If you move the following from OnBarUpdate to State.Configure, do you still have an issue?

        Code:
        SetProfitTarget(signal, CalculationMode.Ticks,BuyProfitTarget);
        SetStopLoss(signal,CalculationMode.Ticks,BuyStopLoss,true);
        I look forward to your reply.
        Alan P.NinjaTrader Customer Service

        Comment


          #5
          same error

          Hi Alan. I tried the change you proposed, recompiled, and reloaded, and still got the same Error. Any other ideas?
          thanks,
          Colin

          Comment


            #6
            It does appear to be related to those two lines of code though... if I comment them out it appears to run fine (i.e. if I run the limitbuy without the stops/ profit lines it works)

            Comment


              #7
              Hello stewarco,

              You are passing “signal” to your SetProfitTarget/SetStopLoss, however this only gets set on a buySignal. Meanwhile on every OnBarUpdate you are calling SetProfitTarget and SetStopLoss, and since signal is still null, it would be expected to get an error. See red and green boxes in attached screen shot.

              Where you declare buySignal in your script, you could assign it a temp value as a means of getting around the issue. For example, private string signal=”SteepSki”; I’ve pointed this out in yellow in the attached screen shot.

              I suggest the following three samples which submit PT/SL’s.



              The OnOrderUpdate() and OnExecution() methods are reserved for experienced programmers. Instead of using Set() methods to submit stop-loss and profit target orders, you can submit and update them manually through the use of IOrder and IExecution objects in the OnOrderUpdate() and OnExecution() methods. The OnOrderUpdate()


              Please let us know if you need further assistance.
              Attached Files
              Alan P.NinjaTrader Customer Service

              Comment


                #8
                Thanks!

                Thanks very much Alan. I'll give it a shot and let you know if it works. I'm still learning, so want to spend the time to build the basics from scratch, but is there publicly available "shell" code where a lot of this stuff is already in place? I'm comfortable building the logic for my own strats but in terms of the code supporting order entry, stops, etc I'd imagine a lot of that is pretty standard and already exists somewhere?

                Thanks again for your help. As you can imagine, having access to timely support makes world of difference for a rusty programmer like me. I'm still in the "free demo" period but so far my experience has been very positive.

                Comment


                  #9
                  Hello stewarco,

                  The following links would be good places to look for existing strategies and indicators.

                  Following are links to all available NinjaScript reference samples within this forum section: Strategy Reference Samples (NinjaTrader 8) - also applicable to NT7 Using a time filter to limit trading hours (http://www.ninjatrader.com/support/forum/showthread.php?t=3226) Using multiple entry/exit signals simultaneously




                  Please let us know if you need further assistance.
                  Alan P.NinjaTrader Customer Service

                  Comment


                    #10
                    A few years later and this also helped me debug the exact same issue. Thanks.


                    Originally posted by stewarco View Post
                    It does appear to be related to those two lines of code though... if I comment them out it appears to run fine (i.e. if I run the limitbuy without the stops/ profit lines it works)

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by traderqz, Today, 12:06 AM
                    2 responses
                    3 views
                    0 likes
                    Last Post traderqz  
                    Started by RideMe, 04-07-2024, 04:54 PM
                    5 responses
                    28 views
                    0 likes
                    Last Post NinjaTrader_BrandonH  
                    Started by f.saeidi, Today, 08:13 AM
                    1 response
                    7 views
                    0 likes
                    Last Post NinjaTrader_ChelseaB  
                    Started by DavidHP, Today, 07:56 AM
                    1 response
                    6 views
                    0 likes
                    Last Post NinjaTrader_Erick  
                    Started by kujista, Today, 06:23 AM
                    3 responses
                    11 views
                    0 likes
                    Last Post kujista
                    by kujista
                     
                    Working...
                    X