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

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:
    Hello I have multiTimeframe strategy (DataSeries 1 and 2). Stategy will run on Dataseries #1 ( Timeframe H1) but I need check how 2 SMA will cross on


    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.


    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.


    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.
        Hello, I want to create an indicator that show data in a chart but calculate in other charttime different to the time of the chart where is showed. For example:
        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);
          [B]AddDataSeries(BarsPeriodType.Minute, 5); // <<<<<<<<<<<<<<<<<<<<<<<<<<<[/B]
                  }
                  [...]


          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];
          [B]                double input0    = BarsArray[1][0]; // <<<<<<<<<<<<<<<<<<<<<<<<[/B]
                          //double input1    = Input[1];
          [B]                double input1    = BarsArray[1][1]; // <<<<<<<<<<<<<<<<<<<<<<<<[/B]
                          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.
            Hi Ninjatrader support team, I encountered a problem when using EMA Indicator on a secound dataseries. The plotting gets stopped after a while: emaStop.png
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              Hi Chelsea,

              First off sorry for the delay and thanks for your replies; I've been trying to get it when I've been able to take the time for it but without much succeed.

              In fact I've ended up giving up trying to plot it and I'm just trying to calculate correctly the values, still without much succeed. Apparently with the EMA and some other SMA example I've found, work, but for some reason I can't get it when trying to do it with a RSI, Stochastics, etc.

              What I am doing is changing the input series with BarsArray[1] (being BarsArray[1] the secondary series I add in the very indicator), calculate the values only when BarsInProgress == 1, and assume that values are to be consulted by the strategy in "steps" of 5 minutes (for example if the higher TF is 5 minutes, I consult values in the indicator Series in times such as 10:00, 10:05, 10:10, etc.).

              However, the values I am getting do not match with the values I get by applying the same indicators (RSI, Stochs, etc.) directly (in the chart) to the secondary series of data.

              Could you please provide some example of this (an indicator applied to a secondary dataseries to be used into a strategy) which doesn't use the Close[] but also the High[], Low[], etc?

              Thanks in advance.
              Last edited by manugarc; 07-05-2019, 04:50 AM.

              Comment


                #8
                Hello manugarc,

                Are you finding the example I provided you is not what you are wanting to achieve?

                I'm not aware of any examples that add a secondary series and use the data from that series except for the example I have provided you.
                Chelsea B.NinjaTrader Customer Service

                Comment


                  #9
                  Hi Chelsea,

                  I know; I tried to find some others and probably this is the most similar to what I needed to do. I guess NT8 is great for doing some things but still not ready for others. I ended up doing it other way. Thanks for your time though.

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by GwFutures1988, Today, 02:48 PM
                  1 response
                  5 views
                  0 likes
                  Last Post NinjaTrader_Clayton  
                  Started by ScottWalsh, 04-16-2024, 04:29 PM
                  6 responses
                  30 views
                  0 likes
                  Last Post ScottWalsh  
                  Started by frankthearm, Today, 09:08 AM
                  10 responses
                  36 views
                  0 likes
                  Last Post frankthearm  
                  Started by mmenigma, Today, 02:22 PM
                  1 response
                  3 views
                  0 likes
                  Last Post NinjaTrader_Jesse  
                  Started by NRITV, Today, 01:15 PM
                  2 responses
                  9 views
                  0 likes
                  Last Post NRITV
                  by NRITV
                   
                  Working...
                  X