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, 09: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;
      [COLOR=#0000FF][B]private Series<double> secondarySeries;[/B][/COLOR]
      
      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.*/
      [COLOR=#0000FF][B]secondarySeries = new Series<double>(SMA(BarsArray[1], 50));[/B][/COLOR]
      
            // 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
      {
      [COLOR=#0000FF] private Series<double> _hODSeries;[/COLOR]
      
      
              protected override void OnStateChange()
              {
      
              }        
              else if (State == State.Configure)
              {            
      
      [COLOR=#0000FF]              hODSeries = new Series<double>(CurrentDayOHL(BarsArray[0]));[/COLOR]        
              }        
              else if (State == State.DataLoaded)
              {            
      
              }          
      
              protected override void OnBarUpdate()
      
              {
      [COLOR=#0000FF]               _hODSeries[0] = CurrentDayOHL(BarsArray[0]).CurrentHigh[0];[/COLOR]
      
              }
      }
      Last edited by Epistemophilic; 06-18-2020, 12:20 PM.

      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 Brevo, Today, 01:45 AM
        0 responses
        4 views
        0 likes
        Last Post Brevo
        by Brevo
         
        Started by aussugardefender, Today, 01:07 AM
        0 responses
        3 views
        0 likes
        Last Post aussugardefender  
        Started by pvincent, 06-23-2022, 12:53 PM
        14 responses
        241 views
        0 likes
        Last Post Nyman
        by Nyman
         
        Started by TraderG23, 12-08-2023, 07:56 AM
        9 responses
        384 views
        1 like
        Last Post Gavini
        by Gavini
         
        Started by oviejo, Today, 12:28 AM
        0 responses
        6 views
        0 likes
        Last Post oviejo
        by oviejo
         
        Working...
        X