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

Assigning a series to a variable

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

    Assigning a series to a variable

    is it possible to assign a data series to a variable so I can use it elsewhere in the code in a more readable way?

    I have Add(PeriodType.Minute, 10);

    How can I use that elsewhere in the script without having to reference it by: BarArray[1]?

    to be more clear what i'd like to be able to do is 'rename it' so that I can say something like:
    if (SMA(20)[0] > SMA(TenMinSeries, 20)[0])
    rather than
    if (SMA(20)[0] > SMA(BarsArray[1], 20)[0])

    also,

    i'm trying to apply a BarsArray to a Rising function as follows but don't know how.

    Code:
    protected override void OnBarUpdate()
            {
    			if(CurrentBars[0] <= BarsRequired ||
    				CurrentBars[1] <= BarsRequired ||
    				CurrentBars[2] <= BarsRequired)
    				return;
    			
    			if(BarsInProgress == 0)
    			{
    				//check ten minute SMA status
    				if(Rising(SMA(25)))       //this obviously won't work, how to apply BarsArray[1]?
    				{
    					//do something
    				}
    			}
            }
    Last edited by ShruggedAtlas; 02-07-2012, 01:54 PM.

    #2
    Hi ShruggedAtlas,

    Add will just add the series to the script, but you access using BarsArray, Closes, Highs, etc.

    Yes, you can assign a variable to the value of a secondary series.
    double myVariable = Closes[1][0];

    SMA will look for a series input, which are available for added series as well. It's similar to the variable example, but note that you use only 1 bracket to specify it as a series rather than 2 brackets.

    if(Rising(SMA(Closes[1], 25)))
    {
    //do something
    }
    Ryan M.NinjaTrader Customer Service

    Comment


      #3
      Ok I see, I was thinking I had to type out BarsArray[x] every time. I see now how Closes and Highs etc relate to BarsArray series. Thanks thats a big help!

      Comment


        #4
        I defined variables but since they aren't series I can't use them in my Rising function. Is there a way to rename the series? I suppose I must create my own data series object?

        Code:
        protected override void OnBarUpdate()
                {
        			double HourlyClose = Closes[2][0];
        			double TenMinClose = Closes[1][0];
        			
        			if(CurrentBars[0] <= BarsRequired ||
        				CurrentBars[1] <= BarsRequired ||
        				CurrentBars[2] <= BarsRequired)
        				return;
        			
        			if(BarsInProgress == 0)
        			{
        				//check hourly SMA status
        				if(Rising(SMA(HourlyClose,25))) 
        					//this doesn't work either because HourlyClose is not a series :-(
        				{
        					//do something
        				}
        			}

        Comment


          #5
          I think I figured it out

          I think I figured it out. i added a DataSeries variable then created a new DataSeries object using that variable then I was able to use the name I wanted. Awesome!

          Code:
                 #region Variables
          		private DataSeries HourlyCloses;
                  #endregion
          
          
                  protected override void Initialize()
                  {
                      CalculateOnBarClose = false;
          			
          			Add(PeriodType.Minute, 10);
          			Add(PeriodType.Minute, 60);
          			
          			HourlyCloses = new DataSeries(this);
          			
                  }
          
          
                  protected override void OnBarUpdate()
                  {
          			double HourlyClose = Closes[2][0];
          			double TenMinClose = Closes[1][0];
          			
          			if(CurrentBars[0] <= BarsRequired ||
          				CurrentBars[1] <= BarsRequired ||
          				CurrentBars[2] <= BarsRequired)
          				return;
          			
          			if(BarsInProgress == 0)
          			{
          				//check hourly SMA status
          				if(Rising(SMA(HourlyCloses,25))) 
          					//this Works!!!
          				{
          					//do something
          				}
          			}
                  }

          Comment


            #6
            Right, in order to pass it in as input for an indicator, it needs to be a data series. You can convert it to one, but will have to follow the technique in this sample here to make sure the data series is synced to the secondary series.
            Note: In NinjaTrader 8 It is no longer needed to use an indicator to sync a secondary series. This can be done directly from the Series&lt;T&gt; (https://ninjatrader.com/support/helpGuides/nt8/NT%20HelpGuide%20English.html?seriest.htm) constructor. This post is left for historical purposes. Series objects are useful for
            Ryan M.NinjaTrader Customer Service

            Comment


              #7
              Ok I'll read that and make sure I'm syncing them up properly. I'll let you know if I have any trouble.

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by CortexZenUSA, Today, 12:53 AM
              0 responses
              1 view
              0 likes
              Last Post CortexZenUSA  
              Started by CortexZenUSA, Today, 12:46 AM
              0 responses
              1 view
              0 likes
              Last Post CortexZenUSA  
              Started by usazencortex, Today, 12:43 AM
              0 responses
              2 views
              0 likes
              Last Post usazencortex  
              Started by sidlercom80, 10-28-2023, 08:49 AM
              168 responses
              2,264 views
              0 likes
              Last Post sidlercom80  
              Started by Barry Milan, Yesterday, 10:35 PM
              3 responses
              11 views
              0 likes
              Last Post NinjaTrader_Manfred  
              Working...
              X