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

OnStateChange error

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

    OnStateChange error

    Hello,

    I am using a strategy with Renko bar type and a 3rd party indicator. When I add a trail stop and have it set to equal the price of the 3rd party indicator, I get the following error when enabling:

    Error on calling 'OnStateChange' method: Object reference not set to an instance of an object.
    When SetTrailStop is removed from the strategy, it compiles, enables, and runs fine.

    Any thoughts will be appreciated.
    Chris

    #2
    Hello Chris,

    The error indicates that a variable with a null value was called.

    What is the line of code causing the error?
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      This is it:

      else if (State == State.Configure)
      {
      SetTrailStop(@"BUY1", CalculationMode.Price, TSSuperTrend1.UpTrend[0], false);
      AddRenko("ES 03-18", 3, Data.MarketDataType.Last);
      SetTrailStop(@"SELL1", CalculationMode.Price, TSSuperTrend1.DownTrend[0], false);
      }
      else if (State == State.DataLoaded)
      I have moved them around with no luck.

      Chris

      Comment


        #4
        Hello Chris,

        You cannot call an indicator in State.Configure as the data has not yet loaded.

        Remove TSSuperTrend1.UpTrend[0].

        If you are planning to use a price from an indicator for the stop loss, you should be setting this in OnBarUpdate.
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Thanks Chelsea,

          So I moved SetTrailStop line to OnBarUpdate area, and it complies and enables fine. The issue now however, is that the trail stop doesnt seem to be working at all. Any ideas?

          Thanks,
          Chris

          Comment


            #6
            Hello Chris,

            No stop loss order is being placed at all?

            What is the signal name of the entry order?

            Is the SetTrailStop set before the entry is placed?
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              Chelsea,

              Not at all. My signal for entry long is "BUY1" and signal for short entry is "SELL1". They are placed after Entry order. I think the issue is that I am trying to set the trail stop to equal the uptrend/downtrend plot (green/red dash marks), but don't quite know how. Are you familiar with this and could possibly help me with this? I have attached a screen shot for better explanation of red/green dash marks.

              Thanks,
              Chris
              Attached Files

              Comment


                #8
                Hello Chris,

                Set the trail stop to a valid price before placing the order.

                Use prints to ensure that the price you are using for a long position is less than the bid or for a short position is greater than the ask.


                Below is a link to an example that uses SetStopLoss(), but this would work the same as SetTrailStop().
                Chelsea B.NinjaTrader Customer Service

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by trilliantrader, Today, 08:16 AM
                2 responses
                6 views
                0 likes
                Last Post trilliantrader  
                Started by samish18, Today, 08:31 AM
                1 response
                1 view
                0 likes
                Last Post NinjaTrader_Clayton  
                Started by Creamers, 09-08-2023, 10:26 AM
                6 responses
                157 views
                0 likes
                Last Post JonyGurt  
                Started by funk10101, Today, 08:14 AM
                1 response
                2 views
                0 likes
                Last Post NinjaTrader_Jesse  
                Started by bill2023, Yesterday, 08:51 AM
                3 responses
                22 views
                0 likes
                Last Post bltdavid  
                Working...
                X