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

Accessing a specific Value of a Series<T> with an Indicator Method

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

    Accessing a specific Value of a Series<T> with an Indicator Method

    How do I access a specific Value of an indicator after initializing a Series<T> with an indicator method?

    For example, how do I access the Value of CurrentHigh[0] with the _hODSeries below?

    Code:
    public class SomeStrat : Strategy
    {
            private Series<double> _hODSeries;
            private CurrentDayOHL _hodIndi;
    
    
            protected override void OnStateChange()
            {
    
    
    
            }
            else if (State == State.DataLoaded)
            {
                _hODSeries = new Series<double>(CurrentDayOHL(BarsArray[0]));
                _hodIndi = CurrentDayOHL(BarsArray[0]);
    
            }
    
            protected override void OnBarUpdate()
            {
                bool asdf = _hodIndi.CurrentHigh[0] == Highs[0][0];
    
            }
    }

    #2
    Hello Epistemophilic,

    Thank you for your post.

    It doesn't look like you're correctly initializing your Series<T> variable and you're trying to assign it an indicator object, which won't work because this variable is expecting to have double values assigned to it.

    Here's a link to our help guide that gives an example of correctly initializing a Series<T>:

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

    You could certainly save the value of _hodIndi.CurrentHigh[0] to _hodSeries, but you couldn't tell it to contain everything the CurrentDayOHL does. So this would work:

    Code:
     public class SomeStrat : Strategy 
    {         
    
        private Series<double> _hODSeries;         
        private CurrentDayOHL _hodIndi;           
    
        protected override void OnStateChange()         
        {            
        }         
        else if (State == State.Configure)         
        {             
            _hODSeries = new Series<double>(this);         
        }         
        else if (State == State.DataLoaded)         
        {             
            _hodIndi = CurrentDayOHL(BarsArray[0]);          
        }          
    
        protected override void OnBarUpdate()         
        {             
            // I can then assign the CurrentHigh[0] to my series and then I could reference that for each bar using my series T.               
            // In your example, that's a redundant value anyway as you can reference that value for a given bar using _hodIndi.CurrentHigh[0]             
            _hODSeries[0] = _hodIndi.CurrentHigh[0];              
            bool asdf = _hodIndi.CurrentHigh[0] == Highs[0][0];          
        } 
    }
    Please let us know if we may be of further assistance to you.
    Last edited by NinjaTrader_Kate; 06-18-2020, 08:54 AM. Reason: Code formatting was messed up
    Kate W.NinjaTrader Customer Service

    Comment


      #3
      Originally posted by NinjaTrader_Kate View Post
      Hello Epistemophilic,

      Thank you for your post.

      It doesn't look like you're correctly initializing your Series<T> variable and you're trying to assign it an indicator object, which won't work because this variable is expecting to have double values assigned to it.

      Here's a link to our help guide that gives an example of correctly initializing a Series<T>:

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

      You could certainly save the value of _hodIndi.CurrentHigh[0] to _hodSeries, but you couldn't tell it to contain everything the CurrentDayOHL does. So this would work:

      Code:
       public class SomeStrat : Strategy
      {
      
      private Series<double> _hODSeries;
      private CurrentDayOHL _hodIndi;
      
      protected override void OnStateChange()
      {
      }
      else if (State == State.Configure)
      {
      _hODSeries = new Series<double>(this);
      }
      else if (State == State.DataLoaded)
      {
      _hodIndi = CurrentDayOHL(BarsArray[0]);
      }
      
      protected override void OnBarUpdate()
      {
      // I can then assign the CurrentHigh[0] to my series and then I could reference that for each bar using my series T.
      // In your example, that's a redundant value anyway as you can reference that value for a given bar using _hodIndi.CurrentHigh[0]
      _hODSeries[0] = _hodIndi.CurrentHigh[0];
      bool asdf = _hodIndi.CurrentHigh[0] == Highs[0][0];
      }
      }
      Please let us know if we may be of further assistance to you.
      Thank you for the reply. I am a little confused wrt to your answer. You stated, "It doesn't look like you're correctly initializing your Series<T> variable and you're trying to assign it an indicator object, which won't work because this variable is expecting to have double values assigned to it."

      I was using the example from the the documentation found at the following link:

      https://ninjatrader.com/support/help...nstruments.htm
      Initializing a Series<T> with an Indicator Method

      "Passing in an indicator method as an argument when instantiating a Series<T> object provides an alternative to the process outlined above. Because indicator methods already contain Series objects synced to the bars on a chart, they can be used to inform the constructor of Series<T> of how many index slots to create."

      Below is the example from the NinjaTrader 8 documentation.

      Code:
      // Declare two Series objects
      private Series<double> primarySeries;
      private Series<double> secondarySeries;
      
      protected override void OnStateChange()
      {
          if (State == State.Configure)
        {
            // Adds a secondary bar object to the strategy.
            AddDataSeries(BarsPeriodType.Minute, 5);
        }
        else if (State == State.DataLoaded)
        {
            // Syncs a Series object to the primary bar object
            primarySeries = new Series<double>(this);
      
            /* Syncs another Series object to the secondary bar object.
             We use an arbitrary indicator overloaded with an ISeries<double> input to achieve the sync.
             The indicator can be any indicator. The Series<double> will be synced to whatever the
             BarsArray[] is provided.*/
      secondarySeries = new Series<double>(SMA(BarsArray[1], 50));
      
            // Stop-loss orders are placed 5 ticks below average entry price
            SetStopLoss(CalculationMode.Ticks, 5);
      
            // Profit target orders are placed 10 ticks above average entry price
            SetProfitTarget(CalculationMode.Ticks, 10);
        }
      }
      As you can see in the above code found in the NinjaTrader 8 documentation, Series<T> secondarySeries is being initialized with an Indicator Method. Can you please elaborate and explain how my example code differs from the above code? Does my code not work because there are several double values from the CurrentDayOHL?

      I do understand that the code in my first post is redundant. I was just trying to provide an example of what I wanted to do with the Series<T> variable.

      ADD & UPDATE: I think I get it. Initializing the Series<T> with an indicator simply provides to the Series<T> how many index slots to create and does not pass/assign the double from the indicator. So in the example from the documentation above, as the secondarySeries is being initialized with the SMA from BarsArray[1], the secondarySeries has the same number of index slots as the SMA indicator but does not have the double from the SMA. Therefore, after initializing the Series<T> with an indicator, it is necessary to later assign the value from the indicator in OnBarUpate or somewhere else.

      Is that correct? Like the example below:

      Code:
      public class SomeStrat : Strategy
      {
       private Series<double> _hODSeries;
      
      
              protected override void OnStateChange()
              {
      
              }        
              else if (State == State.Configure)
              {            
      
                    hODSeries = new Series<double>(CurrentDayOHL(BarsArray[0]));        
              }        
              else if (State == State.DataLoaded)
              {            
      
              }          
      
              protected override void OnBarUpdate()
      
              {
                     _hODSeries[0] = CurrentDayOHL(BarsArray[0]).CurrentHigh[0];
      
              }
      }
      Last edited by Epistemophilic; 06-18-2020, 11:20 AM.

      Comment


        #4
        Hello Epistemophilic,

        Thank you for your reply.

        You're pretty much correct there with your edit, I apologize for my delayed reply. Initializing the Series<T> with an indicator simply provides to the Series<T> how many index slots to create and does not pass/assign the double from the indicator, you do have to assign whichever plot you want's values to it after initialization. So if you wanted to assign _hODSeries the CurrentDayOHL.CurrentHigh value, you don't actually have to do it on every bar - you could do it like this if you wanted:

        Code:
                private Series<double> _hODSeries;
                protected override void OnStateChange()
                {
                    if (State == State.SetDefaults)
                    {
                        Description                                    = @"Enter the description for your new custom Strategy here.";
                        Name                                        = "TestIndi";
                        Calculate                                    = Calculate.OnBarClose;
                        EntriesPerDirection                            = 1;
                        EntryHandling                                = EntryHandling.AllEntries;
                        IsExitOnSessionCloseStrategy                = true;
                        ExitOnSessionCloseSeconds                    = 30;
                        IsFillLimitOnTouch                            = false;
                        MaximumBarsLookBack                            = MaximumBarsLookBack.TwoHundredFiftySix;
                        OrderFillResolution                            = OrderFillResolution.Standard;
                        Slippage                                    = 0;
                        StartBehavior                                = StartBehavior.WaitUntilFlat;
                        TimeInForce                                    = TimeInForce.Gtc;
                        TraceOrders                                    = false;
                        RealtimeErrorHandling                        = RealtimeErrorHandling.StopCancelClose;
                        StopTargetHandling                            = StopTargetHandling.PerEntryExecution;
                        BarsRequiredToTrade                            = 20;
                        // Disable this property for performance gains in Strategy Analyzer optimizations
                        // See the Help Guide for additional information
                        IsInstantiatedOnEachOptimizationIteration    = true;
                    }
                    else if (State == State.DataLoaded)
                    {
                        _hODSeries = new Series<double>(CurrentDayOHL(BarsArray[0]));
                        _hODSeries = CurrentDayOHL(BarsArray[0]).CurrentHigh;
                    }
                }
        
                protected override void OnBarUpdate()
                {
                    Print(_hODSeries[0]); // will print the current day's high for the current bar
                }
        Please let us know if we may be of further assistance to you.

        Kate W.NinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by traderhawk, Today, 05:53 AM
        1 response
        17 views
        0 likes
        Last Post NinjaTrader_Jesse  
        Started by ezrollin, Yesterday, 11:26 PM
        1 response
        40 views
        0 likes
        Last Post NinjaTrader_PaulH  
        Started by ezrollin, Yesterday, 09:10 PM
        1 response
        52 views
        0 likes
        Last Post NinjaTrader_PaulH  
        Started by Angus7, Today, 04:31 AM
        8 responses
        52 views
        0 likes
        Last Post Angus7
        by Angus7
         
        Started by aligator, Yesterday, 11:24 PM
        0 responses
        43 views
        0 likes
        Last Post aligator  
        Working...
        X