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

Object Reference Not Set

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

    Object Reference Not Set

    Problem: Object Reference Error

    Objective: Store 5Min OHLC prices and corresponding 60Min prices in a 2D array of 24 X8 (Past 24- 5 mins bars). Update these prices on bar update.Save the timestamps in another array of 24 X 1.

    Code:

    Initialization
    --------------------------------------------------------------
    else if (State == State.Configure)
    {
    AddDataSeries("EURUSD", Data.BarsPeriodType.Minute, 1, Data.MarketDataType.Last);
    AddDataSeries("EURUSD", Data.BarsPeriodType.Minute, 5, Data.MarketDataType.Last);
    AddDataSeries("EURUSD", Data.BarsPeriodType.Minute, 60, Data.MarketDataType.Last);
    AddDataSeries("EURUSD", Data.BarsPeriodType.Minute, 240, Data.MarketDataType.Last);
    AddDataSeries("EURUSD", Data.BarsPeriodType.Minute, 480, Data.MarketDataType.Last);
    AddDataSeries("EURUSD", Data.BarsPeriodType.Minute, 720, Data.MarketDataType.Last);
    AddDataSeries("EURUSD", Data.BarsPeriodType.Day, 1, Data.MarketDataType.Last);
    AddDataSeries("EURUSD", Data.BarsPeriodType.Week, 1, Data.MarketDataType.Last);
    SetProfitTarget("", CalculationMode.Price, 0);
    SetStopLoss("", CalculationMode.Price, 0, false);

    }
    else if (State == State.DataLoaded)
    {
    i=0;
    DateTime[] dt=new DateTime[BarsRequiredToTrade]; <-----------------------------initialize (I have tried to initialize on state.defaults too without success)
    double[,] prices5=new double[BarsRequiredToTrade,4];

    }

    Use of code:
    ----------------------------------------------------------------------------------


    protected override void OnBarUpdate()
    {
    if (BarsInProgress == 2)
    {

    if (CurrentBars[0] <1|| CurrentBars[1] <1 || CurrentBars[2] <2 || CurrentBars[3] <2 || CurrentBars[4] <2 || CurrentBars[5] <2 || CurrentBars[6] <1 || CurrentBars[7] <1|| CurrentBars[8] <1)
    return;



    if(i<BarsRequiredToTrade)
    {

    dt[i]=Times[2][0];<---------error
    prices5[i,0]=Opens[2][0] ;
    }

    What am i doing wrong? Your help would be greatly appreciated.

    THank You

    #2
    Hi MyFirstMillion, thanks for your note.

    Your data series are fine, so it must be coming from this:
    dt[i]

    Whats the index of 'i' when you get the error? Also consider using a List<T> object, this way you can call MyList.Add(object) and the array can expand dynamically:

    Represents a strongly typed list of objects that can be accessed by index. Provides methods to search, sort, and manipulate lists.


    I look forward to hearing of your results.

    Chris L.NinjaTrader Customer Service

    Comment


      #3
      Originally posted by NinjaTrader_ChrisL View Post
      Hi MyFirstMillion, thanks for your note.

      Your data series are fine, so it must be coming from this:
      dt[i]

      Whats the index of 'i' when you get the error? Also consider using a List<T> object, this way you can call MyList.Add(object) and the array can expand dynamically:

      Represents a strongly typed list of objects that can be accessed by index. Provides methods to search, sort, and manipulate lists.


      I look forward to hearing of your results.
      Both dt and i are null.

      Comment


        #4
        Hello MyFirstMillion,

        You are creating a DateTime array "dt" within the scope of OnStateChange if (State == State.DataLoaded). As you are creating a new array within this scope, it would not be accessible from OnBarUpdate. I suspect that you are creating a DateTime array "dt" as a private variable for the class as well as for the scope of OnStateChange if (State == State.DataLoaded).

        Please consider the following:

        Code:
        public class MyCustomStrategy6 : Strategy
        {
            private DateTime[] dt;
            protected override void OnStateChange()
            {
                if (State == State.SetDefaults)
                {
                    Description                                    = @"Enter the description for your new custom Strategy here.";
                    Name                                        = "MyCustomStrategy6";
                    IsInstantiatedOnEachOptimizationIteration    = true;
                }
                else if (State == State.Configure)
                {
                    AddDataSeries("EURUSD", Data.BarsPeriodType.Minute, 1, Data.MarketDataType.Last);
                    AddDataSeries("EURUSD", Data.BarsPeriodType.Minute, 5, Data.MarketDataType.Last);
                }
                else if (State == State.DataLoaded)
                {
                    dt=new DateTime[3];
                }
            }
        
            protected override void OnBarUpdate()
            {
                if (BarsInProgress == 2)
                {
                    dt[BarsInProgress] = Times[2][0];
                }
            }
        }
        We look forward to assisting.
        .
        JimNinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by aussugardefender, Today, 01:07 AM
        0 responses
        3 views
        0 likes
        Last Post aussugardefender  
        Started by pvincent, 06-23-2022, 12:53 PM
        14 responses
        238 views
        0 likes
        Last Post Nyman
        by Nyman
         
        Started by TraderG23, 12-08-2023, 07:56 AM
        9 responses
        384 views
        1 like
        Last Post Gavini
        by Gavini
         
        Started by oviejo, Today, 12:28 AM
        0 responses
        4 views
        0 likes
        Last Post oviejo
        by oviejo
         
        Started by pechtri, 06-22-2023, 02:31 AM
        10 responses
        125 views
        0 likes
        Last Post Leeroy_Jenkins  
        Working...
        X