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

Multi Instrument

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

    Multi Instrument

    I found this in documentation; very cool.

    Code:
    // Passing in the second Bars object held in a multi-instrument and timeframe strategy
    // The default value used for the SMA calculation is the close price
    double value = SMA(BarsArray[1], 20)[0];
    Print("The current SMA value is " + value.ToString());
    But my problem is that I want SMA of HIGH not CLOSE.

    If I do following, it will apply to primary instrument but I want SMA of HIGH of secondary instrument.

    Code:
    double value = SMA(High, 20)[0]
    How do I do that? Thanks in advance.

    #2
    Hello,

    Please go here:


    In that link you will see Highs[1][0];. Something like that is what you want to do.

    Edit: Added "s" for "Highs"...
    DenNinjaTrader Customer Service

    Comment


      #3
      Originally posted by NinjaTrader_Ben View Post
      Hello,

      Please go here:


      In that link you will see Highs[1][0];. Something like that is what you want to do.

      Edit: Added "s" for "Highs"...
      Awesome... You guys rock!

      Comment


        #4
        Originally posted by NinjaTrader_Ben View Post
        Hello,

        Please go here:


        In that link you will see Highs[1][0];. Something like that is what you want to do.

        Edit: Added "s" for "Highs"...
        I add this indicator to 1m ES chart but that does not load anything. Any idea what is wrong here?

        Code:
        #region Using declarations
        using System;
        using System.ComponentModel;
        using System.Diagnostics;
        using System.Drawing;
        using System.Drawing.Drawing2D;
        using System.Xml.Serialization;
        using NinjaTrader.Cbi;
        using NinjaTrader.Data;
        using NinjaTrader.Gui.Chart;
        #endregion
        
        // This namespace holds all indicators and is required. Do not change it.
        namespace NinjaTrader.Indicator
        {
            /// <summary>
            /// Tick is imported as secondary instrument
            /// </summary>
            [Description("Tick is imported as secondary instrument")]
            public class MyTickSlope : Indicator
            {
                #region Variables
                // Wizard generated variables
        		private int period = 20;
        		private int smooth = 7;
                // User defined variables (add any user defined variables below)
        		private string secondInstrument = "^TICK";
        		private IDataSeries highMA = null;
        		private IDataSeries lowMA = null;
        		private IDataSeries smoothMA = null;
        		private DataSeries hlMA = null;
                #endregion
        
                /// <summary>
                /// This method is used to configure the indicator and is called once before any bar data is loaded.
                /// </summary>
                protected override void Initialize()
                {
        			Add(secondInstrument, PeriodType.Minute, 1);
                    Add(new Plot(Color.FromKnownColor(KnownColor.Green), PlotStyle.Line, "TickUp"));
                    Add(new Plot(Color.FromKnownColor(KnownColor.Red), PlotStyle.Line, "TickDown"));
                    Overlay				= false;
        			hlMA = new DataSeries(this);
                }
        
                /// <summary>
                /// Called on each bar update event (incoming tick)
                /// </summary>
                protected override void OnBarUpdate()
                {
        			TickUp.Set(Highs[1][0]);
        			TickDown.Set(Lows[1][0]);
        			/*
        			highMA = SMA(Highs[1], Period);
        			lowMA = SMA(Lows[1], Period);
        			hlMA.Set((highMA[0] + lowMA[0]) / 2);
        			smoothMA = SMA(hlMA, Smooth);
        			if (smoothMA[0] > smoothMA[2]) TickUp.Set(smoothMA[0]);
        			if (smoothMA[0] < smoothMA[2]) TickDown.Set(smoothMA[0]);
        			*/
                }
        
                #region Properties
                [Browsable(false)]	// this line prevents the data series from being displayed in the indicator properties dialog, do not remove
                [XmlIgnore()]		// this line ensures that the indicator can be saved/recovered as part of a chart template, do not remove
                public DataSeries TickUp
                {
                    get { return Values[0]; }
                }
        
                [Browsable(false)]	// this line prevents the data series from being displayed in the indicator properties dialog, do not remove
                [XmlIgnore()]		// this line ensures that the indicator can be saved/recovered as part of a chart template, do not remove
                public DataSeries TickDown
                {
                    get { return Values[1]; }
                }
        
        		[Description("Period for MA")]
        		[GridCategory("Parameters")]
                [Gui.Design.DisplayName("01 Period")]
        		public int Period
        		{
        			get { return period; }
        			set { period = Math.Max(1, value); }
        		}
        
        		[Description("Smooth for MA")]
        		[GridCategory("Parameters")]
                [Gui.Design.DisplayName("02 Smooth")]
        		public int Smooth
        		{
        			get { return smooth; }
        			set { smooth = Math.Max(1, value); }
        		}
        
        		#endregion
            }
        }
        
        #region NinjaScript generated code. Neither change nor remove.
        // This namespace holds all indicators and is required. Do not change it.
        namespace NinjaTrader.Indicator
        {
            public partial class Indicator : IndicatorBase
            {
                private MyTickSlope[] cacheMyTickSlope = null;
        
                private static MyTickSlope checkMyTickSlope = new MyTickSlope();
        
                /// <summary>
                /// Tick is imported as secondary instrument
                /// </summary>
                /// <returns></returns>
                public MyTickSlope MyTickSlope(int period, int smooth)
                {
                    return MyTickSlope(Input, period, smooth);
                }
        
                /// <summary>
                /// Tick is imported as secondary instrument
                /// </summary>
                /// <returns></returns>
                public MyTickSlope MyTickSlope(Data.IDataSeries input, int period, int smooth)
                {
                    if (cacheMyTickSlope != null)
                        for (int idx = 0; idx < cacheMyTickSlope.Length; idx++)
                            if (cacheMyTickSlope[idx].Period == period && cacheMyTickSlope[idx].Smooth == smooth && cacheMyTickSlope[idx].EqualsInput(input))
                                return cacheMyTickSlope[idx];
        
                    lock (checkMyTickSlope)
                    {
                        checkMyTickSlope.Period = period;
                        period = checkMyTickSlope.Period;
                        checkMyTickSlope.Smooth = smooth;
                        smooth = checkMyTickSlope.Smooth;
        
                        if (cacheMyTickSlope != null)
                            for (int idx = 0; idx < cacheMyTickSlope.Length; idx++)
                                if (cacheMyTickSlope[idx].Period == period && cacheMyTickSlope[idx].Smooth == smooth && cacheMyTickSlope[idx].EqualsInput(input))
                                    return cacheMyTickSlope[idx];
        
                        MyTickSlope indicator = new MyTickSlope();
                        indicator.BarsRequired = BarsRequired;
                        indicator.CalculateOnBarClose = CalculateOnBarClose;
        #if NT7
                        indicator.ForceMaximumBarsLookBack256 = ForceMaximumBarsLookBack256;
                        indicator.MaximumBarsLookBack = MaximumBarsLookBack;
        #endif
                        indicator.Input = input;
                        indicator.Period = period;
                        indicator.Smooth = smooth;
                        Indicators.Add(indicator);
                        indicator.SetUp();
        
                        MyTickSlope[] tmp = new MyTickSlope[cacheMyTickSlope == null ? 1 : cacheMyTickSlope.Length + 1];
                        if (cacheMyTickSlope != null)
                            cacheMyTickSlope.CopyTo(tmp, 0);
                        tmp[tmp.Length - 1] = indicator;
                        cacheMyTickSlope = tmp;
                        return indicator;
                    }
                }
            }
        }
        
        // This namespace holds all market analyzer column definitions and is required. Do not change it.
        namespace NinjaTrader.MarketAnalyzer
        {
            public partial class Column : ColumnBase
            {
                /// <summary>
                /// Tick is imported as secondary instrument
                /// </summary>
                /// <returns></returns>
                [Gui.Design.WizardCondition("Indicator")]
                public Indicator.MyTickSlope MyTickSlope(int period, int smooth)
                {
                    return _indicator.MyTickSlope(Input, period, smooth);
                }
        
                /// <summary>
                /// Tick is imported as secondary instrument
                /// </summary>
                /// <returns></returns>
                public Indicator.MyTickSlope MyTickSlope(Data.IDataSeries input, int period, int smooth)
                {
                    return _indicator.MyTickSlope(input, period, smooth);
                }
            }
        }
        
        // This namespace holds all strategies and is required. Do not change it.
        namespace NinjaTrader.Strategy
        {
            public partial class Strategy : StrategyBase
            {
                /// <summary>
                /// Tick is imported as secondary instrument
                /// </summary>
                /// <returns></returns>
                [Gui.Design.WizardCondition("Indicator")]
                public Indicator.MyTickSlope MyTickSlope(int period, int smooth)
                {
                    return _indicator.MyTickSlope(Input, period, smooth);
                }
        
                /// <summary>
                /// Tick is imported as secondary instrument
                /// </summary>
                /// <returns></returns>
                public Indicator.MyTickSlope MyTickSlope(Data.IDataSeries input, int period, int smooth)
                {
                    if (InInitialize && input == null)
                        throw new ArgumentException("You only can access an indicator with the default input/bar series from within the 'Initialize()' method");
        
                    return _indicator.MyTickSlope(input, period, smooth);
                }
            }
        }
        #endregion

        Comment


          #5
          Any errors in the log tab as you apply it? You likely would need to add a CurrentBars check for both streams... if (CurrentBars[0] < 0 || CurrentBars[1] < 0) return;
          BertrandNinjaTrader Customer Service

          Comment


            #6
            Originally posted by NinjaTrader_Bertrand View Post
            Any errors in the log tab as you apply it? You likely would need to add a CurrentBars check for both streams... if (CurrentBars[0] < 0 || CurrentBars[1] < 0) return;
            It worked as expected. Thanks :-)

            Now when I change ES chart to 2m it blows up. But it works if I change the code to following

            Code:
            if (CurrentBars[0] < 0 || CurrentBars[1] < 1) return;
            Is there a smart way to make it work for 5m chart as well - without hard coding?

            Comment


              #7
              Hello TraderSU,

              There shouldn't be much of a difference between the code running on a 2 m chart or a 5 m chart. You could make the following change to stop all proccesing for the first Period bars.


              if (CurrentBars[0] < Period || CurrentBars[1] < Period) return;
              Ryan M.NinjaTrader Customer Service

              Comment


                #8
                When I draw something from inside this indicator... It goes to main chart :-( How can I draw something on this panel?

                Comment


                  #9
                  TraderSU,

                  You can set DrawOnPricePanel = false in the initialize() method.

                  Ryan M.NinjaTrader Customer Service

                  Comment


                    #10
                    Originally posted by NinjaTrader_RyanM View Post
                    TraderSU,

                    You can set DrawOnPricePanel = false in the initialize() method.

                    http://www.ninjatrader-support.com/H...ceOnPanel.html
                    Cool :-) Can I mix and match? Like draw few things on Price panel and others on Indicator panel?

                    Comment


                      #11
                      Unfortunately mixing and matching is not supported.
                      Josh P.NinjaTrader Customer Service

                      Comment


                        #12
                        One more please. How can I make the indicator panel with "Log" scale programatically?

                        Comment


                          #13
                          Unfortunately we don't support programmatic access to this property. Log scales are not available in version 6.5 They were added to our upcoming version 7 and can be set by right clicking in the vertical axis > Properties > Changing type to Logarithmic.
                          Ryan M.NinjaTrader Customer Service

                          Comment

                          Latest Posts

                          Collapse

                          Topics Statistics Last Post
                          Started by ETFVoyageur, Today, 02:15 AM
                          0 responses
                          5 views
                          0 likes
                          Last Post ETFVoyageur  
                          Started by Board game geek, Today, 01:34 AM
                          0 responses
                          5 views
                          0 likes
                          Last Post Board game geek  
                          Started by morrnel, 05-12-2024, 06:07 PM
                          3 responses
                          39 views
                          0 likes
                          Last Post wzgy0920  
                          Started by FishTrade, Yesterday, 11:11 PM
                          0 responses
                          7 views
                          0 likes
                          Last Post FishTrade  
                          Started by Austiner87, Yesterday, 03:42 PM
                          1 response
                          22 views
                          0 likes
                          Last Post NinjaTrader_Manfred  
                          Working...
                          X