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

multi time frame array

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

    multi time frame array

    I'm working with a multi time frame ind. It appears to work sometimes, but other times it gives me bad values. I think the problem is:

    if (BarsInProgress == 0)
    {
    myvariable[0];
    }
    if (BarsInProgress == 1)
    {
    myvariable
    1[0];
    }​
    I think it would work If I used
    myvariable
    [0][0];
    myvariable
    [1][0];
    but I don't know how to work with this. Every time I try to declare myvariable, I get the error:
    "cannot apply indexing with [] to an expression of type 'double'."
    Where can I learn how to use myvariable
    [0][0];​ instead of

    if (BarsInProgress == 0)
    {
    myvariable[0];
    }
    Thank you.


    #2
    What does myvariable represent?

    Comment


      #3
      To create a series variable, at the top instead of double myvariable; use Series<double> myvariable;

      In SetDefaults when State == State.DataLoaded, initialize it with myvariable = new Series<double>(this);

      Bear in mind there is no inbuilt "collection of series variables" that would do myvariable[0][0].

      You could do something like List<Series<double>> myvariable = new List<Series<double>>(); - then you would need to initialize it like for (int i = 0; i < BarsArray.Count; i++) myvariable.Add(new Series<double>(BarsArray[i]));

      The net effect of that would be that myvariable can be indexed into once to get the series for that BarsInProgress and twice to get to the bar within that series.

      Perhaps we are misunderstanding what you are trying to do.
      Last edited by QuantKey_Bruce; 04-19-2023, 04:36 AM.
      Bruce DeVault
      QuantKey Trading Vendor Services
      NinjaTrader Ecosystem Vendor - QuantKey

      Comment


        #4
        Thanks for that, Bruce. sorry for being vague. It's a trailing stop someone made for me. I've expanded it to work with three time frames. A strange thing happens with the indicator and I'm looking for guidance on where the problem is. Sometimes on a chart the stop looks good with very minor errors. The next day I'll put the same exact indicator on the same chart and it looks bad. The stop lines are in the wrong places. I've never seen anything like this before. My only guess is the problem is with barsinprogress == 0, 1, 2, which is what I'm using to separate the data series. My understanding is anyvariable[x][x] can work with multi time frames without using barsinprogress. If this is true, I want to use it, but I don't know how. If anyone knows of an indicator or a tutorial explaining how to do this I'd love to hear it. In the mean time I will experiment what you wrote above.

        Sorry if I'm still unclear, but I'm trying.
        Thank you.

        Comment


          #5
          I've been working on this indicator and it works perfectly on some charts. It does everything I want it to do. Then I'll put in on another chart and I get:

          "Indicator 'xxxx': Error on calling 'OnBarUpdate' method on bar 733: You are accessing an index with a value that is invalid since it is out-of-range. I.E. accessing a series [barsAgo] with a value of 5 when there are only 4 bars on the chart."

          This would make sense if the indicator didn't work. But it's perfect on certain charts. This baffles me.

          What I'm asking for is direction to where I should focus my attention. I know this is a common error, so where do most of the fixes come from? I'm using a multi time frame while-loop, barsinprogress == 0, 1, 2 to make a trailing stop. I'm using "while (i < CurrentBar - 1)". Is this wrong? How am I accessing an index with in invalid value with this? Is there a better way to write this to stop the error?

          Any help is greatly appreciated.
          Thank you.


          Comment


            #6
            If you have a series that is based on a certain BarsArray e.g. the one on the chart, but you try to index into it in OnBarUpdate, you need to check that you're not indexing past how many bars there are on that timeframe.

            For instance, if I do AddDataSeries(BarsPeriodType.Minute, 1); to add a new series that effectively becomes BarsArray[1] and is updated in OnBarUpdate when BarsInProgress == 1, and then in State.DataLoaded I create a series based on this like MySeries = new Series<double>(BarsArray[1]);, MySeries will always have as many indexable slots as BarsArray[1] does.

            So, in OnBarUpdate, if I do something like: double x; if (CurrentBar >= 20) x = MySeries[20]; it looks superficially like I'm checking to make sure MySeries has at least 20 bars in it, but CurrentBar could be CurrentBars[0] if BarsInProgress is 0.

            What I should be doing instead is: if (CurrentBars[1] >= 20) x = MySeries[20]; because MySeries is about BarsArray[1], so in the guard code I need to check CurrentBars[1] not CurrentBar.
            Bruce DeVault
            QuantKey Trading Vendor Services
            NinjaTrader Ecosystem Vendor - QuantKey

            Comment


              #7
              I am sure Bruce is helping you fix some causes (over my head mostly) Next time this happens, just as a test, Go to dataseries and add a few days to the days to load and see if that then allows your indicator to load

              Comment


                #8
                Thank you very much, Bruce, for the helpful insight. Thus far this indicator works on every chart except one. Some settings:

                State.SetDefaults
                BarsRequiredToPlot = 20;
                State.Configure
                AddDataSeries(BarsPeriodType.Tick, 1500);
                AddDataSeries(BarsPeriodType.Tick, 4500);​
                State.DataLoaded
                new Series<double>(BarsArray[1]);
                new Series<double>(BarsArray[2]);​
                OnBarUpdate()
                if (CurrentBars[0] <= BarsRequiredToPlot && CurrentBars[1] <= BarsRequiredToPlot && CurrentBars[2] <= BarsRequiredToPlot)
                return;​
                On all the while-loops:
                while (i < CurrentBars[0] && i < CurrentBars[1] && i < CurrentBars[2])

                It doesn't work on my 500, 1500, 4500 tick mtf chart. One time when my data feed was not running it loaded. But when the data feed started I got "... You are accessing an index with a value that is invalid since it is out-of-range...." The problem bars are always over 2500. In data series I added up to 27 days but got the same error. So I'm stumped.

                If you can see anything that could be the problem area please let me know.

                Thank you very much.

                Comment


                  #9
                  Try removing the indicator and adding it back to the chart. BarsRequiredToPlot is set (typically) in State.SetDefaults but if you changed the code AFTER you added it to the chart, it's already set. It makes me a little uncomfortable comparing CurrentBars[x] <= BarsRequiredToPlot because you don't have visibility of what that is on this instance. If you know you're going to be indexing back 23 bars or whatever you should probably define a constant and compare it with that in case BarsRequiredToPlot is set incorrectly on that instance of the indicator. You also could try setting BarsRequiredToPlot to 50 or whatever in State.Configure to override what is on the chart, though I would still recommend you define a code constant for how many bars you require if that is knowable. Also, check to make sure you're not accidentally doing something like Close[-1] because that is also out of range, and if you're using any variables for the bars back index you might accidentally have that happening.

                  To be safe, you could encapsulate the code blocks that use Closes[2][x] and the like with this type of guard code:
                  Code:
                  if (x >= 0 && CurrentBars[2] >= x)
                  {
                      var x = Closes[2][x];
                      // do whatever
                  }
                  Or, you can try to catch it all up front with an immediate if (CurrentBars[x] < y) return; at the top of the method but then it is on you to handle all the values you have of x and get the right values for y, and also to make sure you're not later doing Closes[2][-1] or another out of range value.
                  Last edited by QuantKey_Bruce; 05-01-2023, 05:17 AM.
                  Bruce DeVault
                  QuantKey Trading Vendor Services
                  NinjaTrader Ecosystem Vendor - QuantKey

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by lightsun47, Today, 03:51 PM
                  0 responses
                  5 views
                  0 likes
                  Last Post lightsun47  
                  Started by 00nevest, Today, 02:27 PM
                  1 response
                  8 views
                  0 likes
                  Last Post 00nevest  
                  Started by futtrader, 04-21-2024, 01:50 AM
                  4 responses
                  45 views
                  0 likes
                  Last Post futtrader  
                  Started by Option Whisperer, Today, 09:55 AM
                  1 response
                  14 views
                  0 likes
                  Last Post bltdavid  
                  Started by port119, Today, 02:43 PM
                  0 responses
                  9 views
                  0 likes
                  Last Post port119
                  by port119
                   
                  Working...
                  X