• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

Announcement

Collapse
No announcement yet.

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.

              https://ninjatrader.com/support/foru...ead.php?t=3222
              https://ninjatrader.com/support/foru...ead.php?t=5790
              https://ninjatrader.com/support/foru...ead.php?t=7499

              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.

                  https://ninjatrader.com/support/foru...ead.php?t=3220
                  https://ninjatrader.com/support/foru...ks.php?catid=8
                  https://ninjatrader.com/support/foru...ks.php?catid=7

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

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by GussJ, Today, 01:30 PM
                  1 response
                  5 views
                  0 likes
                  Last Post NinjaTrader_PaulH  
                  Started by GARZONJ, Today, 12:25 PM
                  3 responses
                  13 views
                  0 likes
                  Last Post NinjaTrader_PaulH  
                  Started by RandanAL, Today, 11:53 AM
                  1 response
                  13 views
                  0 likes
                  Last Post NinjaTrader_ChelseaB  
                  Started by yeshujbp, 07-21-2019, 02:40 PM
                  2 responses
                  35 views
                  0 likes
                  Last Post omololu
                  by omololu
                   
                  Started by seagraph, Yesterday, 03:27 PM
                  1 response
                  28 views
                  0 likes
                  Last Post NinjaTrader_Jesse  
                  Working...
                  X