Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Multiple Instruments

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

    Multiple Instruments

    Hello all,

    I'm new to the forum... in fact I'm new to NinjaTrader having defected from MT4

    I like to code my own indicators, but NinjaScript is completely unknown to me.

    As a start point, I tried to achieve what I mistakenly thought would be a simple task: display the current RSI of another instrument in a text box in the corner of the screen (thinking that something existed along the lines of iRSI for those who use mql4).

    I started off by adding 2 variables:
    Code:
            #region Variables
                private int Period = 14;
                private int Smooth = 3;
            #endregion
    And then calculated the current RSI and assigned the value to a textbox:
    Code:
     protected override void OnBarUpdate()
            {
    			int otherRSI = (int) RSI(Close, Period,Smooth)[0];
    			DrawTextFixed("RSI", otherRSI.ToString(), TextPosition.TopRight);
    				
    		}
    Easy

    Then I tried to figure out how to get the RSI of another instrument... and that is where I have been going around in circles.
    I think it has something to do with Add() and DataSeries but so far the solution has eluded me.

    If anybody could give me some pointers it would be very much appreciated!

    Thanks in advance

    honestknave
    Last edited by honestknave; 12-09-2013, 08:11 PM.

    #2
    Well, I think I may have solved it.

    Code:
     protected override void Initialize()
            {
    		    Add("$EURJPY", PeriodType.Minute, 60);
    	           Overlay				= true;
            }

    Code:
    protected override void OnBarUpdate()
            {
    			if (CurrentBars[0] <= BarsRequired || CurrentBars[1] <= BarsRequired)
                	        return;
    			if (BarsInProgress == 1)
        		          {
            	            int otherRSI = (int) RSI(Closes[1], Period,Smooth)[0];
    			    DrawTextFixed("RSI", "\nCurrentRSI " + otherRSI.ToString(), TextPosition.TopRight,Color.Green, new Font("Arial", 15),Color.Transparent, Color.Transparent,0);
    			}
    		}

    Comment


      #3
      Hi Honestknave,

      Thank you for posting and welcome to the forums!

      You are correct that it has something to do with the Add();

      You will want to add in a secondary data series in the code to create another instrument that you then can reference for the RSI.

      Code:
      Add("MSFT", PeriodType.Day, 1);
      You can then use a variety of ways to access that data from that particular data series, such as BarsArray or Closes. See the links below for both-
      http://www.ninjatrader.com/support/h...tml?closes.htm
      http://www.ninjatrader.com/support/h...?barsarray.htm

      Let me know if I can be of further assistance.
      Cal H.NinjaTrader Customer Service

      Comment


        #4
        Thanks Cal - I think we must have cross-posted!

        I now have a different issue.

        I've added 4 secondary data series (eventually it will be 16), most of which are of a higher time frame than the primary (I say most because the primary data series in tick based).

        Code:
                protected override void Initialize()
                {
        		    Add("$USDJPY", PeriodType.Minute, 1); //1
        		    Add("$USDJPY", PeriodType.Minute, 5); //2
        		    Add("$USDJPY", PeriodType.Minute, 15); //3
        		    Add("$USDJPY", PeriodType.Minute, 60); //4
        			Overlay				= true;
        			CalculateOnBarClose = false;
                }
        The script stopped working, so I debugged it with the following code in OnBarUpdate:

        Code:
        			Print("1 = " + CurrentBars[1]);
        			Print("2 = " + CurrentBars[2]);
        			Print("3 = " + CurrentBars[3]);
        			Print("4 = " + CurrentBars[4]);
        Which gives me the follow output:
        Code:
        1 = 21
        2 = 3
        3 = 0
        4 = -1
        Error on calling 'OnBarUpdate' method for indicator 'aaRSI' on bar 21: You are accessing an index with a value that is invalid since its out of range. I.E. accessing a series [barsAgo] with a value of 5 when there are only 4 bars on the chart
        The error message I'm still figuring out, but I can see that there is a problem with the number of bars in the higher arrays.

        Any suggestions on what I can do to remedy this problem? Unfortunately, simply multiplying a lower time frame to create a pseudo higher time frame won't work for what I need from the RSI figures.

        Comment


          #5
          Honestknave,

          Did you extend the the CurrentBars check to incorporate the new data series that you have added?
          Cal H.NinjaTrader Customer Service

          Comment


            #6
            Thanks for the reply again

            I have this code, which is why the code stops... but I want to know why I don't have enough bars?

            Code:
            if (CurrentBars[0] <= BarsRequired || CurrentBars[1] <= BarsRequired || CurrentBars[2] <=
            BarsRequired || CurrentBars[3] <= BarsRequired || CurrentBars[4] <= BarsRequired)
            {
            Print("insufficient bars");
            return;
            }

            Comment


              #7
              Honestknave,

              How days to load do you have set for the chart? Right click on the chart -> Data Series -> Under Data, 'Days to Load'

              You would most likely need to increase this value.
              Cal H.NinjaTrader Customer Service

              Comment


                #8
                Thanks again, Cal.

                I had the days set to 3, presuming that this would be sufficient as the largest time frame was H1 and barsrequired = 20.

                I extended this out to 10 days and I had partial improvement.

                Then I switched data providers and since then everything seems to be behaving itself.

                So I'm not exactly sure what the problem was, but it seems good now!

                Thanks once again

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                0 responses
                579 views
                0 likes
                Last Post Geovanny Suaza  
                Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                0 responses
                334 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
                554 views
                1 like
                Last Post Geovanny Suaza  
                Started by RFrosty, 01-28-2026, 06:49 PM
                0 responses
                551 views
                1 like
                Last Post RFrosty
                by RFrosty
                 
                Working...
                X