• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Correct way to declare, use and plot indicators with different timeframes as input

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

    Correct way to declare, use and plot indicators with different timeframes as input

    Dear NT support team,

    I have not used NT8 for a while and these questions may be too simple, but for some reason I do not get to make it work although I am pretty sure I was able to do it years ago (but I can't find those codes).

    Basically what I need to do is to work with indicators (inside a strategy) which work in different timeframes in order to use their calculated values as well as to plot them in the chart.

    I think it's very similar to this issue:
    https://ninjatrader.com/support/foru...-sma-indicator

    However, I can't get it and I do not understand why.

    Following that basic example, and using a 1min chart in which I load the strategy, I am declaring the indicator this way:

    Code:
    private SMA sma5MIN;
    Expecting to be able to compare the 5 min SMA with another one which uses 1 min values as input.

    in the "State == State.Configure" code, I am adding:

    Code:
    AddDataSeries(BarsPeriodType.Minute, 5);
    sma5MIN = SMA(BarsArray[1], 14);
    sma5MIN.Panel = 4;
    AddChartIndicator(sma5MIN);
    I have tried using Closes[1], but I get the same result, which is a 1 min SMA indicator is added to the chart, but not he 5 min based SMA (or whatever indicator I am trying); it is always a 1 min based indicator.

    What am I missing?


    2) Asuming the solution to previous issue is probably simple (hopefully ), I'm seeing I have strategies in which I get the values of indicators by declaring them every time I need to do it, i.e.:

    Code:
    double someValue = SMA(...);
    But I am pretty sure I also used to do it by just declaring the indicator at he beginning and later on, just using that indicator object/instance, i.e. (following the example of the SMA):

    Code:
    double someValue = sma5MIN.Values[i];
    Is there any difference in doing it this way? Is it better/worse? (I assume this way is more efficient since it's using always the same instance of the indicator)

    Thanks in adavance.

    #2
    Hello manugarc,

    AddChartIndicator() must be called from State.DataLoaded.

    Below is a link to the help guide.
    https://ninjatrader.com/support/help...tindicator.htm

    Also from the help guide:
    "An indicator being added via AddChartIndicator() cannot use any additional data series hosted by the calling strategy, but can only use the strategy's primary data series. If you wish to use a different data series for the indicator's input, you can add the series in the indicator itself and explicitly reference it in the indicator code (please make sure though the hosting strategy has the same AddDataSeries() call included as well)"

    BarsArray cannot be used until State.DataLoaded.
    https://ninjatrader.com/support/help.../barsarray.htm

    This means if you want to use a secondary series in the indicator and add it to the chart, you must call AddDataSeries() in the code of the indicator (and leave the input series as the default).

    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Hi ChelseaB,

      Thank you for your answer; I find it interesting in order to make this work.

      However, I am trying it in a simple test indicator and I am not managing to make it work; it seems that the series array is not loaded for some reason.

      Scenario:

      - I am just testing the indicator code by adding it to a 1 min chart, not along with any strategy.

      - In order to use the 5 min dataseries, I've already added it to the chart (I guess it is needed, in the same way that if used into the strategy, it needs to add the dataseries prior to be used by the indicator, as you suggested).

      - In the indicator code, into the State.Configure section, I am adding the dataseries this way:

      Code:
      AddDataSeries(BarsPeriodType.Minute, 5);
      - But nothing is calculated since in the very first invocation of OnBarUpdate() method I am getting this error:

      Error on calling 'OnBarUpdate' method on bar 0: You are accessing an index with a value that is invalid since it is out-of-range...

      When accesing to BarsArray[1][0] or Closes[1][0] (I've tried both of them).

      So I think dataseries for 5 min is not being loaded in the indicator (it is loaded into the chart for sure, since I can see it on it) for some reason I do not manage to figure out.

      Am I still missing something?

      Comment


        #4
        Hello manugarc,

        Series that are added to a script with AddDataSeries() do not need to be added directly to a chart.

        The error is letting you know you have an index error.

        May I confirm that both CurrentBars[0] and CurrentBars[1] are both greater than the highest barsAgo index used in the code?

        Are you running the logic for both BarsInProgress 0 and BarsInProgress 1?

        Below is a link to a forum post about index errors.
        https://ninjatrader.com/support/foru...13#post1048513
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5

          Hi Chelsea,

          Thank you for the tip; actually deleting the code for BarsInProgress == 0 avoids the error (I was trying to fill every value in the 1 min timeframe, but I am going to focus in make it work before any other thing).

          So now I've tested it with an indicator which is not overlayed in the main chart in order to make it easier. I've done it now with the RSI, so it can be displayed in its own panel. However, while not raising any error, the indicador shows no values. I'm just copying the code I am using, highlighting the lines I've modified in order to force the indicator to use the desired TF instead of the main in the chart, as you suggested, to try to make more obvious any error I am commiting in the code and hopefully letting you see it and that way allow you to tell me what am I doing wrong. Thanks in advance.

          Scenario:

          - Same as before (1 min main chart, adding this indicator, the RSI trying to make it use the 5 min bars as input)

          Indicator code:


          Code:
                  [...]
                  else if (State == State.Configure)
                  {
                      constant1 = 2.0 / (1 + Smooth);
                      constant2 = (1 - (2.0 / (1 + Smooth)));
                      constant3 = (Period - 1);
          AddDataSeries(BarsPeriodType.Minute, 5); // <<<<<<<<<<<<<<<<<<<<<<<<<<<
                  }
                  [...]


          Code:
          protected override void OnBarUpdate()
                  {
          
                      if (BarsInProgress == 0)
                          {
          
                          //
          
                      }else if (BarsInProgress == 1){
          
                          if (CurrentBar == 0)
                          {
                              down[0]        = 0;
                              up[0]        = 0;
          
                              if (Period < 3)
                                  Avg[0] = 50;
          
                              return;
                          }
          
                          //double input0    = Input[0];
                          double input0    = BarsArray[1][0]; // <<<<<<<<<<<<<<<<<<<<<<<<
                          //double input1    = Input[1];
                          double input1    = BarsArray[1][1]; // <<<<<<<<<<<<<<<<<<<<<<<<
                          down[0]            = Math.Max(input1 - input0, 0);
                          up[0]            = Math.Max(input0 - input1, 0);
          
                          if (CurrentBar + 1 < Period)
                          {
                              if (CurrentBar + 1 == Period - 1)
                                  Avg[0] = 50;
                              return;
                          }
          
                          if ((CurrentBar + 1) == Period)
                          {
                              // First averages
                              avgDown[0]    = smaDown[0];
                              avgUp[0]    = smaUp[0];
                          }
                          else
                          {
                              // Rest of averages are smoothed
                              avgDown[0]    = (avgDown[1] * constant3 + down[0]) / Period;
                              avgUp[0]    = (avgUp[1] * constant3 + up[0]) / Period;
                          }
          
                          double avgDown0    = avgDown[0];
                          double value0    = avgDown0 == 0 ? 100 : 100 - 100 / (1 + avgUp[0] / avgDown0);
                          Default[0]        = value0;
                          Avg[0]            = constant1 * value0 + constant2 * Avg[1];
                      }
                  }

          Comment


            #6
            Hello manugarc,

            For a plot to show, the plot must have a value set for every primary bar.

            Are you ensuring that every primary bar has a plot value set?

            Below is a link to an example.
            https://ninjatrader.com/support/foru...25#post1058825
            Chelsea B.NinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by psuhjkajaytha, Today, 01:08 AM
            0 responses
            1 view
            0 likes
            Last Post psuhjkajaytha  
            Started by NinjaCustomer, Yesterday, 11:35 PM
            0 responses
            1 view
            0 likes
            Last Post NinjaCustomer  
            Started by sdauteuil, Yesterday, 11:29 PM
            0 responses
            1 view
            0 likes
            Last Post sdauteuil  
            Started by williamzz, Yesterday, 09:50 PM
            0 responses
            19 views
            0 likes
            Last Post williamzz  
            Started by Steve L, Yesterday, 05:01 PM
            0 responses
            6 views
            0 likes
            Last Post Steve L
            by Steve L
             
            Working...
            X