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

Null object reference

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

    Null object reference

    Hello,

    I am trying to move my methods to a global class so I can reuse them.

    I have structured it as per an example by NT support.

    I've run into 2 problems:
    1. If I declare the method (CheckMACD) as static, the compiler complains that MACD needs to be called from an object reference.
    2. So I declare it as a regular public int. The problem is now that on calling MACD, a runtime error occurs: System.NullReferenceException: 'Object reference not set to an instance of an object.'

    Note that CurrentBar for some reason has a value of -1. I'm thinking that is the problem as I'm trying to access index of [0]?

    However if I code and wait until CurrentBar > 2, the code never runs as CurrentBar is always -1.

    The method is called like this:
    Code:
    test = cmsGlobals.CheckMACD(Close, MACDFastLength, MACDSlowLength, MACDLength, MACDThreshold);
    How to fix?

    Many Thanks, Caesar.

    The code is as follows (Note that there is no issue in calling CalculateDelta):

    Code:
    namespace NinjaTrader.NinjaScript.Indicators
    {
        public partial class cmsGlobals : Indicator
        {
            // This double can be accessed from within an indicator with MySharedMethods.CalculateDelta()
            // or can be accessed from any script using NinjaTrader.NinjaScript.Indicators.MySharedMethods.CalculateDelta()
    
            public static double CalculateDelta(double firstPrice, double secondPrice)
            {
                return Math.Abs(firstPrice - secondPrice);
            }
    
            public int CheckMACD(ISeries<double> Price, int FastLength, int SlowLength, int MACDLength, double MACDThreshold)
            {
                int retval = 0;
                double MyMACD = 0;
                double MACDAvg = 0;
                double MACDDiff0 = 0;
                double MACDDiff1 = 0;
                int myCurrentBar = 0;
    
                myCurrentBar = CurrentBar;
                MACDDiff0 = MACD(Price, FastLength, SlowLength, MACDLength).Diff[0];
                MACDDiff1 = MACD(Price, FastLength, SlowLength, MACDLength).Diff[1];
    
                if (Math.Abs(MACDDiff1) < Math.Abs(MACDDiff0))
                {
                    if (MACDDiff0 > MACDThreshold)
                        retval = 1;
                    else
                    if (MACDDiff0 < -(1 * MACDThreshold))
                        retval = -1;
                    else
                        retval = 0;
                }
    
                return retval;
            }
    Last edited by Skechers; 09-24-2022, 12:23 PM.

    #2
    Hello Skechers,

    I have an example of a partial class you may find helpful.
    Explanation: I wrote a base class Indicator class that I'm using to inherit all my other indicators from. So this baseclass is defined as: namespace NinjaTrader.NinjaScript.Indicators.AssistedTrades { public class ATBaseIndicator: Indicator { ... } } And any other indicator is defined as: namespace NinjaTrader.NinjaScr


    I tested printing the CurrentBar and MACD values and these printed without issue.

    Attached is a screenshot showing the test.
    Attached Files
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Hello Chelsea,

      The example works, but it is being called from it's own class. I'm calling the PrintPartialCloseMethod from another class.

      In my case I get the attached error.​

      Many Thanks, Caesar.
      Attached Files

      Comment


        #4
        Hello Skechers,

        Use a partial class of Indicator (and not your custom class) and the method can be called from all indicators.

        Below is a link to a video demonstrating.
        Chelsea B.NinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by kaywai, Today, 06:26 AM
        1 response
        6 views
        0 likes
        Last Post kaywai
        by kaywai
         
        Started by ct, 05-07-2023, 12:31 PM
        6 responses
        205 views
        0 likes
        Last Post wisconsinpat  
        Started by kevinenergy, 02-17-2023, 12:42 PM
        118 responses
        2,780 views
        1 like
        Last Post kevinenergy  
        Started by briansaul, Today, 05:31 AM
        0 responses
        10 views
        0 likes
        Last Post briansaul  
        Started by traderqz, Yesterday, 12:06 AM
        11 responses
        28 views
        0 likes
        Last Post NinjaTrader_Gaby  
        Working...
        X