Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

OBV Strategy not working

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

    OBV Strategy not working

    Hello - Attached is a simple SMA strategy based on the OBV. Can you tell me why it doesn't backtest in the Strategy Analyzer?

    Thank you for any help.
    Attached Files

    #2
    Hello JohnS52,

    Thanks for your post.

    When running a backtest on your strategy in the Strategy Analyzer, I see the following error message appear in the Log tab of the Control Center.

    Strategy 'OBVsma': Error on calling 'OnBarUpdate' method on bar 43: Object reference not set to an instance of an object.

    Do you see an error message in the Log tab of the Control Center when backtesting the script on your end?

    You would need to add debugging prints in the script to determine exactly which line of code is throwing the error.

    Below is a link to a forum post that demonstrates how to use prints to understand behavior.
    https://ninjatrader.com/support/foru...121#post791121

    Also, I noted that you are calling the SetStopLoss method after your Entry order method. The SetStopLoss method should be called before the Entry order method since SetStopLoss preps NinjaTrader to place an order.

    For example:

    Code:
    if (condition)
    {
        SetStopLoss();
        EnterLong();
    }
    Let me know if I may assist further.
    <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

    Comment


      #3
      Hello Brandon,

      Thank you for your comments and links. I am backtesting 6/1 - 8/1 2022. The program gets stuck on 6/1 in OnBarUpdate just before the If Position.MarketPosition == MarketPosition.Flat line of code. The OBV1 values are way off vs the 70 day chart I am looking at and thus the SMAs are way off as well. The Backtester tries 32,000+ times to get through the code then quits. My tab usually references Bar 114 though once it referenced Bar 41.

      What does it mean "Object reference not set to an instance of an object" in this context.
      This infamous and dreaded error message happens when you get a NullReferenceException. This exception is thrown when you try to access a member—for instance, a method or a property—on a variable that currently holds a null reference.
      Do I have to instantiate OBV somehow? The Print command shows OBV()[0] with an initial value of 0 or -1. Can OBV be used with SMAs? Does OBV change in value depending on the lookback period of the Chart? I've noticed that 5 - 15 - 60 minute charts all have varying values for OBV. Which shouldn't matter for my purpose - I just want to get the relative value for the chart that the strategy is running on.

      Your thoughts? Is this just not possible with OBV?
      Last edited by JohnS52; 08-02-2022, 08:41 PM.

      Comment


        #4
        Hello JohnS52,

        Thanks for your note.

        To see how the OBV indicator could be used in a NinjaScript strategy, you could open a New > Strategy Builder window, create a condition that compares the OBV indicator to a value, and click the 'View code' button to see the generated syntax required for using that indicator in a strategy.

        See this help guide page for more information about making an indicator to value comparison in the Strategy Builder: https://ninjatrader.com/support/help...lueComparisons

        Have you added debugging prints to your script to determine exactly how it is processing values?

        Below is a link to a forum post that demonstrates how to use prints to understand behavior.
        https://ninjatrader.com/support/foru...121#post791121

        Have you taken steps to reduce the script to determine which exact line of code is causing the error?

        When working with any issue, it is important to take steps to isolate the issue so that the exact line causing the behavior can be found. This is a process of elimination and a process of debugging by adding prints.

        First, it's great to create a copy of the script so that the original work is kept safe. Below is a link to a video that demonstrates making a copy of the script. Once the copy is created, the copy can be modified in any way without losing the original work.

        https://www.youtube.com/watch?v=BA0W...utu.be&t=8m15s

        The next step is to start commenting out code. It is helpful to think about when the error is occurring to target areas, and comment code from everywhere else quickly.

        If at any point after removing some code, the error stops, add the last removed item back.

        If the error is occurring while the script is loading, the issue is likely somewhere in OnStateChange(). In this situation its best to remove any logic in OnBarUpdate, remove any custom methods and classes, and remove as may properties as possible, while still being able to reproduce the error.

        If the error is occurring when saving a template, the issue is likely with a private or public variable declaration. First, remove all logic leaving the variable declarations behind. (Empty State.Configure, State.Dataloaded, State.Historical, OnBarUpdate, OnMarketData, and remove any custom methods.) Then start removing the variables one by one and testing until the error stops. When the error stops add the last removed item back. Ensure that this item is private or property serialized or using the Browsable(false) attribute. (More about the Browsable attribute below)

        If the error only occurs while the strategy is trading, this is likely a run-time method. For this its best to use prints to understand the behavior.

        Let me know if I may assist further.
        <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

        Comment


          #5
          Hello Brandon,

          Yes, I used the Strategy Builder to create this strategy. Yes, I used several Print statements in the code to try and track down the error. OBV1, SMA1, SMA2 and SMA3 all have values so they should process. Your comments are all good generic advice. I am attaching my latest version with the added Print statements and a Counter variable.

          The OBV doesn't have a range of values like ADX or RSI so I wouldn't know what value to compare it to. It appears to start out as a zero value with the first bar and builds its value from there. That's why I thought SMAs would be a good choice: the SMAs can be compared to each other and the OBV.

          The program moves easily through State.Configure and State.DataLoaded. It gets stuck in OnBarUpdate just before the If Position.MarketPosition == MarketPosition.Flat line of code. It won't move past that. Can you tell me why? A simple SMA strategy works fine but when I add the OBV content it doesn't work. Is this because this is what the OBV does? The error message suggests that the OBV has a null value - is that correct? If so, how to rectify?

          There must be a simple answer.
          Attached Files

          Comment


            #6
            Hello JohnS52,

            Thanks for your note.

            You could use the Strategy Builder to set up a condition that compares a value to an SMA indicator that uses the OBV indicator as its input. Or, you could create a Print action that prints out the value of the SMA indicator using the OBV indicator as its input.

            Then, you could click the 'View code' button to see the generated syntax.

            See the attached strategy for an example of this. You could then compare the generated syntax to the code in your script to see where there might be differences.

            Let me know if I may assist further.
            Attached Files
            <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

            Comment


              #7
              Hmm, very strange. I completely redid the Strategy in the Strategy Builder using the exact same code (but nothing commented out) and this time it works, even with the Print statements added back in as well. Well, that's a workable solution though puzzling.

              Comment


                #8
                Originally posted by JohnS52 View Post
                Hmm, very strange. I completely redid the Strategy in the Strategy Builder using the exact same code (but nothing commented out) and this time it works, even with the Print statements added back in as well. Well, that's a workable solution though puzzling.
                Often if you are simply learning to build or write code in C# and using the builder or NT platform, if you come across items which work and you are not sure why, but make changes and when reverted no longer work, try opening a brand new code base file. Import only your code that works, or should, and start a new which will clear any system based messages that it thinks it is handling or has handled which may not even be is use any longer. Some events may cause errors that when compiling and creating new methods there are no reverse actions to right the system base stepping forward and it continues to collect these errors and prevent new updates to handle properly. SO very important, when you start writing, decide a point when you start to create a new file, and can be changes you made you like to keep to create a file that is (zip) another location to refer back to, and then keep writing new features as needed. If large changes happen in your code, start a fresh file, manually copy paste your code into a brand new file. This helps the compile process to start handling only the code you are asking it to run currently.

                long winded explanation but let know if you have questions.

                Comment


                  #9
                  Thank you, that makes a lot of sense.

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by NullPointStrategies, Yesterday, 05:17 AM
                  0 responses
                  56 views
                  0 likes
                  Last Post NullPointStrategies  
                  Started by argusthome, 03-08-2026, 10:06 AM
                  0 responses
                  133 views
                  0 likes
                  Last Post argusthome  
                  Started by NabilKhattabi, 03-06-2026, 11:18 AM
                  0 responses
                  73 views
                  0 likes
                  Last Post NabilKhattabi  
                  Started by Deep42, 03-06-2026, 12:28 AM
                  0 responses
                  45 views
                  0 likes
                  Last Post Deep42
                  by Deep42
                   
                  Started by TheRealMorford, 03-05-2026, 06:15 PM
                  0 responses
                  49 views
                  0 likes
                  Last Post TheRealMorford  
                  Working...
                  X