Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

hard coding a fixed data series into an indicator

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

    hard coding a fixed data series into an indicator

    Hello everybody,

    I am aware about "AddDataSeries(...)" but my question is another one:

    Example:
    let's say we have the indicator SMA and we wanna have all its calculation, drawing, plotting, ... done in a 1-second-series. The result should be --> independent what the chart time interval currently is, the indicator should calculate and draw always related to 1-second data series. If we change the chart time to 5min or 30min or 1hour the plot shouldn't change its values and plots.

    (1)
    Is there any simple way to achieve this without the need to use AddDataSeries(...) ?

    (2)
    If question 1 is answered with "no" then I would use AddDataSeries as one would do for multi-series use and in the following I need to go through the whole indicator code and modify existing variables from this to this:

    high[0] --change-to---> highs[1][0]
    low[0] --change-to---> lows[1][0]
    close[0] --change-to---> closes[1][0]
    time[0] --change-to---> times[1][0]
    etc.

    Simply said: I wanna modify an existing indicator to run all its code logic at a predefined secondary data series instead at the primary chart time chosen.
    Looking forward to your comments and tips.

    Thanks to everyone !
    Patricia
    Last edited by patricia70; 07-30-2021, 01:58 PM.

    #2
    Hi Patricia, thanks for writing in.

    AddDataSeries will need to be used to do this. When an indicator adds a plot with AddPlot, there is a plot index for every bar of the primary series, so you must save the values in BarsInProgress == 1 then set the plot in BarsInProgress == 0. e.g.

    Code:
    private savedValue;
    OnBarUpdate:
    
    if(BarsInProgress == 0)
    {
    Value[0] = savedValue;
    }
    
    if(BarsInProgress == 1)
    {
    savedValue = SecondaryTimeFrameCalculation();
    }
    See our guide on multi time frame scripts here:

    https://ninjatrader.com/support/help...nstruments.htm

    Kind regards,
    -ChrisL

    Comment


      #3
      Dear Chris,

      that sounds very promising, just what I aimed for, simple and effective! I will try that in the weekend and let you know. Thanks a bunch !!!

      Comment


        #4
        Hello again,

        I just tried to proceed on this matter but I am already stuck in the beginning. As soon as I add AddDataSeries() to a indicator it doesn't work any more as expected and plots are not visible in the chart. For trying to undertsand what's happening I used the onboard SMA indicator. So please in the following, lets focus on the SMA indicator for education purposes.

        - I open the SMA code and right-click onto the code window and click "Save as..." with name SMAtest. I attach the indicator SMAtest to my 5min chart and I see the SMA plot as expected because till here everything is in original state.

        - Now after line 51 in the State.Configure block I add
        Code:
        AddDataSeries(Data.BarsPeriodType.Minute, 1);
        - I compile so the change takes effect and reload my chart. The plot disappeard, the indicator does not work any more just because of the addition of the one-liner.

        The error message is:
        Indicator 'SMAtest': Error on calling 'OnBarUpdate' method on bar -1: 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.
        Ok, understood. I need to fix this ...

        - I add following condition in the beginning of the OnBarUpdate block and additionally a debugging print line
        Code:
        protected override void OnBarUpdate()
        {
        // ensure both series have at least one bar
        if (CurrentBars[0] < 1 || CurrentBars[1] < 1) return;
        
        Print("CurrentBars[0] = " + CurrentBars[0] + " CurrentBar = " + CurrentBar);
        
        if (BarsArray[0].BarsType.IsRemoveLastBarSupported)
        {
        if (CurrentBar == 0)
        Value[0] = Input[0];
        else
        {
        ...
        Now the indicator doesn't throw an error and it plots the SMA line but at a very weird price level which I think is wrong. I understand how the bars gets constructed in primary and secondary data series, however I do not understand yet why the indicator logic fails just because I added the secondary data series. How can I mitigate this and construct the indicator to behave as original state, although the AddDataSeries() command is there ?
        Last edited by patricia70; 08-05-2021, 08:53 AM.

        Comment


          #5
          Hi Patricia, thanks for your reply.

          I'm not sure if I have enough context from your code. I attached an example indicator that calculates an SMA on a 1 second series regardless of the primary series. Please see attached.

          Kind regards,
          -ChrisL
          Attached Files

          Comment


            #6
            Hello Chris,

            thank you for assisting and code example you attached. I just changed three lines in your example:

            line 37:
            from "IsOverlay = false" to "true" so the plot gets drawed into the main chart window

            line 50:
            from 1 second to 1 Minute.

            line 55:
            periods changed from 20 to 14

            Now I open a chart with 1min time interval and for a comparison side-by-side I also added the SMA indicator (periods=14). The gold line (original SMA indicator) and the red line (your example indicator) looks pretty matched but I think there's a displacement. Any clue where it comes from and how to fix? I'd expect your example to show exactly the same line as the original SMA indicator on this example.

            Solved: In your code example in the State.SetDefaults I added displacement=-1 and now the plots both are identical. Great!

            I need to dig further into your example code for trying to understand why you used -1 as lastValue. Thank you so far

            EDIT: In your example you created a completely new indicator and named it "TestMultiTimeFrameIndicator" and instantiated the SMA indicator. Would it be technically possible to use your logic within the SMA indicator (yes, I am aware we cannot modify the original indicator, but just as example)
            Last edited by patricia70; 08-05-2021, 10:19 AM.

            Comment


              #7
              Hi Patricia, thanks for your reply.

              The 1 minute SMA is running OnBarClose, so it's not going to display the most current value until the 1-minute bar has closed. The secondary series indicator is lagging by 1 because, when running the script OnBarClose, the primary data series is always calculated first, then the second one. This is explained further here:



              To get them in sync, you would need to enable Tick Replay and calculate the indicator OnEachTick or OnPriceChanged.

              I only used -1 as an initial value for lastValue. It's used to make sure it starts plotting only after lastValue has been set. It's not nessicary, I just like to do that out of habit. You may remove the lastValue == -1 line if you want to.

              Kind regards,
              -ChrisL

              Comment


                #8
                Thank you for the helpful explanation Chris. Could you say something about the question regarding "integrate your example in the used indicator, in the example SMA" ? My goal is to configure a

                user input "DataSeries to use ="

                with options

                "Chart interval"
                "Fixed time interval of x minutes"

                which I can define further in the code as variables.

                Comment


                  #9
                  Hi Patricia, thanks for your reply.

                  It is possible to modify the SMA in any way you would like if you right click the code>Save As. For an example of taking user input and using it in AddDataSeries, see the "Correlation" indicator. Note, this will not function if the indicator is being used in a Strategy Analyzer optimization. You can handle that special case in code by checking IsInStrategyAnalyzer.

                  Kind regards.

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                  0 responses
                  566 views
                  0 likes
                  Last Post Geovanny Suaza  
                  Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                  0 responses
                  330 views
                  1 like
                  Last Post Geovanny Suaza  
                  Started by Mindset, 02-09-2026, 11:44 AM
                  0 responses
                  101 views
                  0 likes
                  Last Post Mindset
                  by Mindset
                   
                  Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                  0 responses
                  547 views
                  1 like
                  Last Post Geovanny Suaza  
                  Started by RFrosty, 01-28-2026, 06:49 PM
                  0 responses
                  548 views
                  1 like
                  Last Post RFrosty
                  by RFrosty
                   
                  Working...
                  X