Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Importing 5 minute indicator series, in to another indicator (use on 1min)

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

    Importing 5 minute indicator series, in to another indicator (use on 1min)

    Hi

    I have built 2 indicators.

    The 1st, outputs an SMA, based on the 5minute dataseries
    The 2nd , imports the first as an object. and will be loaded on a 1 minute chart.

    The intention is to see the data from the 5 minute SMA, on the 1 minute timeframe.

    But the Problem is , that the data from the 5minute SMA is distorted when i load it on the 1 minute chart.
    > this distortion, also occurs when i access the object through the 2nd indicator that i have built.

    Here is the Main script,(that is loaded on a 1 minute)
    Code:
    public class MainSnippet : Indicator
    {
    
        private Indicator _SMASnippet101;   // import indicator as object
        private Series<double> SMA_5m_import; // store the output series to local series
    
        [Browsable(false)]
        [XmlIgnore]
        public Series<double> SMA_5min      
        {
            get { return Values[0]; }
        }
        protected override void OnStateChange()
        {
            if (State == State.SetDefaults)
            {
               //
            }
            else if (State == State.Configure)
            {
                AddDataSeries("EURUSD", Data.BarsPeriodType.Minute, 5);
            }
            else if (State == State.DataLoaded)
            {
                SMA_5m_import = new Series<double>(this); // 5m
    
                var SMA5min = SMASnippet101(20);
                SMA_5m_import = SMA5min.SMA_5m;   // the series from the object is imported to local series
                _SMASnippet101 = SMA5min;
    
            }
        }
    
        protected override void OnBarUpdate()
        {
            SMA_5min[0] = SMA_5m_import[0];  // imported series used in Main method, & stored in the Output signal
        }
    }​

    Here is the 1st indicator / object that i'm trying to import
    > its simply a SMA, using a 5 minute BarsArray
    Code:
            [Browsable(false)]
            [XmlIgnore]
            public Series<double> SMA_5m
            {
                get { return Values[0]; }
            }
    
    
            protected override void OnStateChange()
            {
                if (State == State.SetDefaults)
                {
                    Name = "SMASnippet101";
    
                    AddPlot(Brushes.Goldenrod, NinjaTrader.Custom.Resource.NinjaScriptIndicatorNameSMA);
    
                }
                else if (State == State.Configure)
                {
                    priorSum = 0;
                    sum = 0;
    
                    AddDataSeries("EURUSD", Data.BarsPeriodType.Minute, 5);  // Import 5 minute DataSeries
    
                }
            }
    
            protected override void OnBarUpdate()
            {
                if (BarsInProgress == 1)
                {
                    SMA_5m[0] = SMA_Method(Closes[1]);  // calculate SMA, using 5 minute Data Series
                }
            }​

    What i am hoping to see is that the signal from the 5minute data series stays flat for the 5 minute period.
    Like this..
    > but instead the signal from the 5min is distorted ( i get different values on every bar), when the Main indicator is loaded on a 1min
    Click image for larger version  Name:	image.png Views:	0 Size:	145.3 KB ID:	1310532
    Last edited by yertle; 07-14-2024, 03:47 AM.

    #2
    to further illustrate the issue.
    This indicator just plots the bar color of a range of timeframes

    This indicator is set to calculate OnbarClose , but yet the 4hr data output changes on each 5 minute bar.

    Click image for larger version

Name:	image.png
Views:	95
Size:	78.6 KB
ID:	1310593

    Comment


      #3
      Hello yertle,

      In your sub indicators code you shouldn't need to add the 5 minute series or using any BarsInProgress logic, you can call that directly from the parent and supply the 5 minute series to it. The Original SMA's code is a good way to create the indicator so it makes use of that input series by using Input in its logic in place of Close.

      The following uses the stock SMA and produces the result you are looking for.

      Code:
      protected override void OnStateChange()
      {
           if (State == State.SetDefaults)
           {
                AddPlot(Brushes.Red, "MyPlot");
           }
           if (State == State.Configure)
           {
      
                AddDataSeries(null, Data.BarsPeriodType.Minute, 5); // Import 5 minute DataSeries
           }
      }
      
      protected override void OnBarUpdate()
      {
           if(CurrentBars[0] < 1 || CurrentBars[1] < 1) return;
           Value[0] = SMA(Closes[1], 12)[0];
      }


      Click image for larger version

Name:	NinjaTrader_YRQ3d71SWE.png
Views:	97
Size:	26.7 KB
ID:	1310656

      Comment


        #4
        Thank you Jesse.

        But, what about when multiple data series are involved.

        I dont think the constructor of my custom object (/ sub indicator) accepts the multiple data series correctly.

        Is there something i need to do to get the constructor working for multiple dataseries?

        Code:
        else if (State == State.Configure)
        {
        AddDataSeries("EURUSD", Data.BarsPeriodType.Minute, 5);
        AddDataSeries("EURJPY", Data.BarsPeriodType.Minute, 5);
        }​
        
        protected override void OnBarUpdate()
        {
        if(CurrentBars[0] < 1 || CurrentBars[1] < 1) return;
        
        Value[0] = CustomSMA(Closes[1],Closes[2], 12)[0];
        }​

        Comment


          #5
          Hello yertle,

          If multiple series are involved you need to call the indicator again with the other series to get the other series value.

          Values[0][0] = SMA(Closes[1], 12)[0];
          Values[1][0]= SMA(Closes[2], 12)[0];

          Comment


            #6
            I am just using the SMA as an example.

            My actual sub indicator uses multiple instruments to calculate its signal.
            & its signal is not the primary data series (Values[0])
            So i need to import it in Dataloaded, and not the OnBarUpdate

            ie. Sub indicator
            public class CustomSignal : Indicator
            {
            protected override void OnBarUpdate()
            {​
            values[0] = closes[1][0] +closes[2][0];
            values[1] = values[0] / (closes[3][0] +closes[4][0]) ; // i want to import values 1 ( i cant do this directly in the onbarupdate, it needs to be done in the dataloaded)
            }
            }

            Primary Indicator
            else if (State == State.DataLoaded)
            {
            SMA_5m_import = new Series<double>(this); // 5m

            var Signal5min = CustomSignal(Closes[1],Closes[2], Closes[3],Closes[4]); // Question: since the sub indicator uses multiple data series for calculation, how do i handle the constructor?

            SMA_5m_import = Signal5min.Values1_Series; //Here, i am importing the Values[1] series
            _SMASnippet101 = Signal5min;

            }​
            Last edited by yertle; 07-15-2024, 09:33 AM.

            Comment


              #7
              Hello yertle,

              A indicator can only take 1 input from its host.

              You could add the secondary series into the sub indicator if you needed to use multiple series, the structure of that code would be the same as what I already provided. You would call it from the host like the following:

              protected override void OnBarUpdate()
              {
              if(CurrentBars[0] < 1 ) return;
              if(BarsInProgress != 0) return;
              Value[0] = ATestIndicator()[0];
              }

              This gives the same staircase looking result. The host also needs to add the same secondary series. Because of that it may also be beneficial to just program this signal directly into the primary indicator instead of using a secondary indicator for that purpose. ​

              Comment


                #8
                is accessing the value[1] series possible?
                - i thought this needed to be done in dataloaded

                protected override void OnBarUpdate()
                {
                if(CurrentBars[0] < 1 ) return;
                if(BarsInProgress != 0) return;
                Value[0] = ATestIndicator()[1][0]; //? ?
                }
                Last edited by yertle; 07-15-2024, 10:25 AM.

                Comment


                  #9
                  Hello yertle,

                  Yes you can access previous bars values but you need to wait at least that many bars before doing that. If you need to use 1 bars ago CurrentBar needs to be 1 or more so you can look back at a previous bar.

                  The code you provided has two bars ago parameters at the end which is not valid, you would use just [1] at the end of the indicators syntax to get 1 bars ago.

                  ATestIndicator()[1]

                  Comment


                    #10
                    What i mean is accessing the Values[1] dataseries. ie.. "BP2"
                    (not the prior bar)

                    sub indicator
                    [Browsable(false)]
                    [XmlIgnore]
                    public Series<double> BP
                    {
                    get { return Values[0]; }
                    }

                    [Browsable(false)]
                    [XmlIgnore]
                    public Series<double> BP2
                    {
                    get { return Values[1]; }
                    }​

                    So accessing it, would look something like this
                    host
                    protected override void OnBarUpdate()
                    {
                    if(CurrentBars[0] < 1 ) return;
                    if(BarsInProgress != 0) return;
                    Value[0] = ATestIndicator().BP2[0]; //?
                    }​
                    Last edited by yertle; 07-15-2024, 10:45 AM.

                    Comment


                      #11
                      Hello yertle

                      In your indicator you would add public properties for the plots to achieve that. In your code that would be the BP and BP2 plot properties. Those would be used with a bars ago just like you have in this code.

                      Comment


                        #12
                        i have had issues with trying to get the values[2][0] series like that.

                        Is this all that's required to set them as global?

                        sub indicator
                        [Browsable(false)]
                        [XmlIgnore]
                        public Series<double> BP
                        {
                        get { return Values[0]; }
                        }

                        [Browsable(false)]
                        [XmlIgnore]
                        public Series<double> BP2
                        {
                        get { return Values[1]; }
                        }​​

                        Comment


                          #13
                          Hello yertle,

                          Values[2] would assume you have a third plot, the two you have here are the first and second AddPlot's that you used.

                          Comment

                          Latest Posts

                          Collapse

                          Topics Statistics Last Post
                          Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                          0 responses
                          590 views
                          0 likes
                          Last Post Geovanny Suaza  
                          Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                          0 responses
                          342 views
                          1 like
                          Last Post Geovanny Suaza  
                          Started by Mindset, 02-09-2026, 11:44 AM
                          0 responses
                          103 views
                          0 likes
                          Last Post Mindset
                          by Mindset
                           
                          Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                          0 responses
                          555 views
                          1 like
                          Last Post Geovanny Suaza  
                          Started by RFrosty, 01-28-2026, 06:49 PM
                          0 responses
                          552 views
                          1 like
                          Last Post RFrosty
                          by RFrosty
                           
                          Working...
                          X