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

Simple indicator on second symbol

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

    Simple indicator on second symbol

    I am trying to make a very simple indicator that returns the volume on the second symbol on a chart.

    I understand that I can use the "Vol" indicator on a chart and change the input series to the second symbol.

    Does anybody know how to make this very simple indicator?

    Thanks

    #2
    Hello Baseheadz,

    You can see a sample by clicking Tools > Edit NinajScript > SampleMultiInstrument.

    Volume for additional series is accessed with Volumes[]

    Ryan M.NinjaTrader Customer Service

    Comment


      #3
      Copied the code from the Vol indicator and I pasted it into a new indicator script. I added a 1 second bar and I changed another line "Value.Set(Volume[1][0]);"

      I'm getting errors when I compile the script. Any help would be greatly appreciated!



      // Copyright (C) 2006, NinjaTrader LLC <www.ninjatrader.com>.
      // NinjaTrader reserves the right to modify or overwrite this NinjaScript component with each release.
      //

      #region Using declarations
      using System;
      using System.Diagnostics;
      using System.Drawing;
      using System.Drawing.Drawing2D;
      using System.ComponentModel;
      using System.Xml.Serialization;
      using NinjaTrader.Data;
      using NinjaTrader.Gui.Chart;
      #endregion

      // This namespace holds all indicators and is required. Do not change it.
      namespace NinjaTrader.Indicator
      {
      /// <summary>
      /// Volume is simply the number of shares (or contracts) traded during a specified time frame (e.g. hour, day, week, month, etc).
      /// </summary>

      protected override void Initialize()
      {
      // Add a 1 second Bars object - BarsInProgress index = 1
      Add(PeriodType.second, 1);

      }
      [Description("Volume is simply the number of shares (or contracts) traded during a specified time frame (e.g. hour, day, week, month, etc).")]
      public class VolDATA2 : Indicator
      {
      /// <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(new Plot(new Pen(Color.Blue, 2), PlotStyle.Bar, "Volume"));
      Add(new Line(Color.DarkGray, 0, "Zero line"));

      }

      /// <summary>
      /// Called on each bar update event (incoming tick)
      /// </summary>
      protected override void OnBarUpdate()
      {
      Value.Set(Volume[1][0]);
      }
      }
      }

      #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 VolDATA2[] cacheVolDATA2 = null;

      private static VolDATA2 checkVolDATA2 = new VolDATA2();

      /// <summary>
      /// Volume is simply the number of shares (or contracts) traded during a specified time frame (e.g. hour, day, week, month, etc).
      /// </summary>
      /// <returns></returns>
      public VolDATA2 VolDATA2()
      {
      return VolDATA2(Input);
      }

      /// <summary>
      /// Volume is simply the number of shares (or contracts) traded during a specified time frame (e.g. hour, day, week, month, etc).
      /// </summary>
      /// <returns></returns>
      public VolDATA2 VolDATA2(Data.IDataSeries input)
      {
      if (cacheVolDATA2 != null)
      for (int idx = 0; idx < cacheVolDATA2.Length; idx++)
      if (cacheVolDATA2[idx].EqualsInput(input))
      return cacheVolDATA2[idx];

      lock (checkVolDATA2)
      {
      if (cacheVolDATA2 != null)
      for (int idx = 0; idx < cacheVolDATA2.Length; idx++)
      if (cacheVolDATA2[idx].EqualsInput(input))
      return cacheVolDATA2[idx];

      VolDATA2 indicator = new VolDATA2();
      indicator.BarsRequired = BarsRequired;
      indicator.CalculateOnBarClose = CalculateOnBarClose;
      #if NT7
      indicator.ForceMaximumBarsLookBack256 = ForceMaximumBarsLookBack256;
      indicator.MaximumBarsLookBack = MaximumBarsLookBack;
      #endif
      indicator.Input = input;
      Indicators.Add(indicator);
      indicator.SetUp();

      VolDATA2[] tmp = new VolDATA2[cacheVolDATA2 == null ? 1 : cacheVolDATA2.Length + 1];
      if (cacheVolDATA2 != null)
      cacheVolDATA2.CopyTo(tmp, 0);
      tmp[tmp.Length - 1] = indicator;
      cacheVolDATA2 = 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>
      /// Volume is simply the number of shares (or contracts) traded during a specified time frame (e.g. hour, day, week, month, etc).
      /// </summary>
      /// <returns></returns>
      [Gui.Design.WizardCondition("Indicator")]
      public Indicator.VolDATA2 VolDATA2()
      {
      return _indicator.VolDATA2(Input);
      }

      /// <summary>
      /// Volume is simply the number of shares (or contracts) traded during a specified time frame (e.g. hour, day, week, month, etc).
      /// </summary>
      /// <returns></returns>
      public Indicator.VolDATA2 VolDATA2(Data.IDataSeries input)
      {
      return _indicator.VolDATA2(input);
      }
      }
      }

      // This namespace holds all strategies and is required. Do not change it.
      namespace NinjaTrader.Strategy
      {
      public partial class Strategy : StrategyBase
      {
      /// <summary>
      /// Volume is simply the number of shares (or contracts) traded during a specified time frame (e.g. hour, day, week, month, etc).
      /// </summary>
      /// <returns></returns>
      [Gui.Design.WizardCondition("Indicator")]
      public Indicator.VolDATA2 VolDATA2()
      {
      return _indicator.VolDATA2(Input);
      }

      /// <summary>
      /// Volume is simply the number of shares (or contracts) traded during a specified time frame (e.g. hour, day, week, month, etc).
      /// </summary>
      /// <returns></returns>
      public Indicator.VolDATA2 VolDATA2(Data.IDataSeries input)
      {
      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.VolDATA2(input);
      }
      }
      }
      #endregion

      Comment


        #4
        You're close. Should be Volumes[][]

        Value.Set(Volumes[1][0]);
        Ryan M.NinjaTrader Customer Service

        Comment


          #5
          I changed that to "volumes."

          I still get an error on line 23: "protected override void Initialize()"

          The error is "Expected Class, delegate, enum, interface, or struct"

          Comment


            #6
            I took a second look at your code snippet and you have two Initialize() methods. That won't work, so I would delete what you're using currently. You also aren't capitalizing Second in the Add() method.

            Attached is basic 1 second volume indicator you can work from. To get familiar with basic indicator structure in NinjaScript, I suggest going through the tutorials here:
            Attached Files
            Ryan M.NinjaTrader Customer Service

            Comment


              #7
              Thanks for this. I am using the indicator and I also went through all of the tutorials on using "add" and multiple bar objects.

              I changed the add line to:
              Add("NQ 06-11",PeriodType.Second, 1);

              I created a new 1 second ES 06-11 chart and I inserted the OneSecondVolume indicator on the chart
              There is nothing plotting on the panel below when I do this.

              Does the indicator need to be working in real time to plot?

              Comment


                #8
                Baseheadz, which datafeed are you using here? Would it support historical tick data?
                BertrandNinjaTrader Customer Service

                Comment


                  #9
                  Zen-Fire. Yes it supports historical. If I open a new chart, I can load the NQ 06-11 on 1 second bars and it loads fine.

                  Comment


                    #10
                    Thanks, can you share the modified indicator you attempt using then? I tried the one from Ryan with your changed line on ES 1 sec and see no issue - what session templates are you working on?
                    BertrandNinjaTrader Customer Service

                    Comment


                      #11
                      Here is the code:



                      // This namespace holds all indicators and is required. Do not change it.
                      namespace NinjaTrader.Indicator
                      {
                      /// <summary>
                      /// Enter the description of your new custom indicator here
                      /// </summary>
                      [Description("Enter the description of your new custom indicator here")]
                      public class OneSecondVolume : Indicator
                      {
                      #region Variables
                      #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(new Plot(Color.FromKnownColor(KnownColor.Orange), PlotStyle.Line, "Plot0"));
                      Overlay = false;
                      Add("NQ 06-11",PeriodType.Second, 1);
                      }

                      /// <summary>
                      /// Called on each bar update event (incoming tick)
                      /// </summary>
                      protected override void OnBarUpdate()
                      {
                      if (CurrentBars[1] < 0) return;


                      Plot0.Set(Volumes[1][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 Plot0
                      {
                      get { return Values[0]; }
                      }
                      #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 OneSecondVolume[] cacheOneSecondVolume = null;

                      private static OneSecondVolume checkOneSecondVolume = new OneSecondVolume();

                      /// <summary>
                      /// Enter the description of your new custom indicator here
                      /// </summary>
                      /// <returns></returns>
                      public OneSecondVolume OneSecondVolume()
                      {
                      return OneSecondVolume(Input);
                      }

                      /// <summary>
                      /// Enter the description of your new custom indicator here
                      /// </summary>
                      /// <returns></returns>
                      public OneSecondVolume OneSecondVolume(Data.IDataSeries input)
                      {
                      if (cacheOneSecondVolume != null)
                      for (int idx = 0; idx < cacheOneSecondVolume.Length; idx++)
                      if (cacheOneSecondVolume[idx].EqualsInput(input))
                      return cacheOneSecondVolume[idx];

                      lock (checkOneSecondVolume)
                      {
                      if (cacheOneSecondVolume != null)
                      for (int idx = 0; idx < cacheOneSecondVolume.Length; idx++)
                      if (cacheOneSecondVolume[idx].EqualsInput(input))
                      return cacheOneSecondVolume[idx];

                      OneSecondVolume indicator = new OneSecondVolume();
                      indicator.BarsRequired = BarsRequired;
                      indicator.CalculateOnBarClose = CalculateOnBarClose;
                      #if NT7
                      indicator.ForceMaximumBarsLookBack256 = ForceMaximumBarsLookBack256;
                      indicator.MaximumBarsLookBack = MaximumBarsLookBack;
                      #endif
                      indicator.Input = input;
                      Indicators.Add(indicator);
                      indicator.SetUp();

                      OneSecondVolume[] tmp = new OneSecondVolume[cacheOneSecondVolume == null ? 1 : cacheOneSecondVolume.Length + 1];
                      if (cacheOneSecondVolume != null)
                      cacheOneSecondVolume.CopyTo(tmp, 0);
                      tmp[tmp.Length - 1] = indicator;
                      cacheOneSecondVolume = 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>
                      /// Enter the description of your new custom indicator here
                      /// </summary>
                      /// <returns></returns>
                      [Gui.Design.WizardCondition("Indicator")]
                      public Indicator.OneSecondVolume OneSecondVolume()
                      {
                      return _indicator.OneSecondVolume(Input);
                      }

                      /// <summary>
                      /// Enter the description of your new custom indicator here
                      /// </summary>
                      /// <returns></returns>
                      public Indicator.OneSecondVolume OneSecondVolume(Data.IDataSeries input)
                      {
                      return _indicator.OneSecondVolume(input);
                      }
                      }
                      }

                      // This namespace holds all strategies and is required. Do not change it.
                      namespace NinjaTrader.Strategy
                      {
                      public partial class Strategy : StrategyBase
                      {
                      /// <summary>
                      /// Enter the description of your new custom indicator here
                      /// </summary>
                      /// <returns></returns>
                      [Gui.Design.WizardCondition("Indicator")]
                      public Indicator.OneSecondVolume OneSecondVolume()
                      {
                      return _indicator.OneSecondVolume(Input);
                      }

                      /// <summary>
                      /// Enter the description of your new custom indicator here
                      /// </summary>
                      /// <returns></returns>
                      public Indicator.OneSecondVolume OneSecondVolume(Data.IDataSeries input)
                      {
                      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.OneSecondVolume(input);
                      }
                      }
                      }
                      #endregion

                      Comment


                        #12
                        I am using 24/7 session templates

                        Comment


                          #13
                          That's exactly the same code I'm using here - how many days of data are you loading up? Would you see any errors in your Control Center log tab?
                          BertrandNinjaTrader Customer Service

                          Comment


                            #14
                            There is an error. I attached a file which shows a screen shot of the chart as well as the error from the log tab.
                            Attached Files

                            Comment


                              #15
                              Please try using this OnBarUpdate() instead below and reapply the indicator then to your chart - would it work then?

                              protected override void OnBarUpdate()
                              {
                              if (CurrentBars[0] < 0 || CurrentBars[1] < 0) return;


                              Plot0.Set(Volumes[1][0]);
                              }
                              BertrandNinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by helpwanted, Today, 03:06 AM
                              1 response
                              16 views
                              0 likes
                              Last Post sarafuenonly123  
                              Started by Brevo, Today, 01:45 AM
                              0 responses
                              11 views
                              0 likes
                              Last Post Brevo
                              by Brevo
                               
                              Started by aussugardefender, Today, 01:07 AM
                              0 responses
                              6 views
                              0 likes
                              Last Post aussugardefender  
                              Started by pvincent, 06-23-2022, 12:53 PM
                              14 responses
                              244 views
                              0 likes
                              Last Post Nyman
                              by Nyman
                               
                              Started by TraderG23, 12-08-2023, 07:56 AM
                              9 responses
                              387 views
                              1 like
                              Last Post Gavini
                              by Gavini
                               
                              Working...
                              X