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

Volume Coding Indicator

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

    #16
    Originally posted by ginx10k View Post
    Okay, this doesn't make sense, sometimes it works, other times I keep getting this error.

    Free online storage and sharing with Screencast.com. 2 GB of storage and 2 GB of bandwidth per month for free. We won't compress, alter or take ownership of your content.


    Here's the FULL Code.
    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>
        /// Enter the description of your new custom indicator here
        /// </summary>
        [Description("Enter the description of your new custom indicator here")]
        public class CustomVolume : Indicator
        {
            #region Variables
            // Wizard generated variables
                private int myInput0 = 1; // Default setting for MyInput0
                private string InstrumentName = " ";
                private string contract = " ";
                
            // User defined variables (add any user defined variables below)
                private bool useFutures = true; //Ask User if they want to use Futures Volume for FOREX
            #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 ()
            {
                //Adding Volume per instruments
                InstrumentName = (Instrument.FullName.ToString());
                if(InstrumentName == "$USDJPY") {contract = "6J 06-14";}
                
                
                Add (new Plot (new Pen (Color.Red, 3), PlotStyle.Line, "VolumePlot"));
                Add ( "6J 06-14", PeriodType.Minute, 5);
                
                Overlay = false;
            }
    
            protected override void OnBarUpdate ()
            {
                if (CurrentBar < BarsRequired)
                    return;
                
                if (Volumes [0] [0] > Volumes [1][0])
                    VolumePlot.Set(Volumes [1][0]);
                Print ("The current bar's volume price is greater");
            }
    
            #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 VolumePlot 
            {
                get { return Values [0]; }
            }
    
            [Description("")]
            [GridCategory("Parameters")]
            public int MyInput0
            {
                get { return myInput0; }
                set { myInput0 = Math.Max(1, value); }
            }
            
            [Description("Use Futures Volume For FOREX")]
            [GridCategory("Parameters")]
            public bool UseFuturesVolume
            {
                get { return useFutures; }
                set { useFutures = 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 CustomVolume[] cacheCustomVolume = null;
    
            private static CustomVolume checkCustomVolume = new CustomVolume();
    
            /// <summary>
            /// Enter the description of your new custom indicator here
            /// </summary>
            /// <returns></returns>
            public CustomVolume CustomVolume(int myInput0, bool useFuturesVolume)
            {
                return CustomVolume(Input, myInput0, useFuturesVolume);
            }
    
            /// <summary>
            /// Enter the description of your new custom indicator here
            /// </summary>
            /// <returns></returns>
            public CustomVolume CustomVolume(Data.IDataSeries input, int myInput0, bool useFuturesVolume)
            {
                if (cacheCustomVolume != null)
                    for (int idx = 0; idx < cacheCustomVolume.Length; idx++)
                        if (cacheCustomVolume[idx].MyInput0 == myInput0 && cacheCustomVolume[idx].UseFuturesVolume == useFuturesVolume && cacheCustomVolume[idx].EqualsInput(input))
                            return cacheCustomVolume[idx];
    
                lock (checkCustomVolume)
                {
                    checkCustomVolume.MyInput0 = myInput0;
                    myInput0 = checkCustomVolume.MyInput0;
                    checkCustomVolume.UseFuturesVolume = useFuturesVolume;
                    useFuturesVolume = checkCustomVolume.UseFuturesVolume;
    
                    if (cacheCustomVolume != null)
                        for (int idx = 0; idx < cacheCustomVolume.Length; idx++)
                            if (cacheCustomVolume[idx].MyInput0 == myInput0 && cacheCustomVolume[idx].UseFuturesVolume == useFuturesVolume && cacheCustomVolume[idx].EqualsInput(input))
                                return cacheCustomVolume[idx];
    
                    CustomVolume indicator = new CustomVolume();
                    indicator.BarsRequired = BarsRequired;
                    indicator.CalculateOnBarClose = CalculateOnBarClose;
    #if NT7
                    indicator.ForceMaximumBarsLookBack256 = ForceMaximumBarsLookBack256;
                    indicator.MaximumBarsLookBack = MaximumBarsLookBack;
    #endif
                    indicator.Input = input;
                    indicator.MyInput0 = myInput0;
                    indicator.UseFuturesVolume = useFuturesVolume;
                    Indicators.Add(indicator);
                    indicator.SetUp();
    
                    CustomVolume[] tmp = new CustomVolume[cacheCustomVolume == null ? 1 : cacheCustomVolume.Length + 1];
                    if (cacheCustomVolume != null)
                        cacheCustomVolume.CopyTo(tmp, 0);
                    tmp[tmp.Length - 1] = indicator;
                    cacheCustomVolume = 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.CustomVolume CustomVolume(int myInput0, bool useFuturesVolume)
            {
                return _indicator.CustomVolume(Input, myInput0, useFuturesVolume);
            }
    
            /// <summary>
            /// Enter the description of your new custom indicator here
            /// </summary>
            /// <returns></returns>
            public Indicator.CustomVolume CustomVolume(Data.IDataSeries input, int myInput0, bool useFuturesVolume)
            {
                return _indicator.CustomVolume(input, myInput0, useFuturesVolume);
            }
        }
    }
    
    // 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.CustomVolume CustomVolume(int myInput0, bool useFuturesVolume)
            {
                return _indicator.CustomVolume(Input, myInput0, useFuturesVolume);
            }
    
            /// <summary>
            /// Enter the description of your new custom indicator here
            /// </summary>
            /// <returns></returns>
            public Indicator.CustomVolume CustomVolume(Data.IDataSeries input, int myInput0, bool useFuturesVolume)
            {
                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.CustomVolume(input, myInput0, useFuturesVolume);
            }
        }
    }
    #endregion
    As you can see I'm trying to to get the indicator to know which Chart it's on so it knows which FUTURES contract to use. I typed Conditional statements in the void Initialize() area, I don't know if that causes problems.

    Can someone please help fix this error.
    Contract "6J 06-14" comes into existence on a specific date. Before that date, as the contact does not exist, your second price DataSeries will not be created, so any reference to it will be out of the bounds of any array that references it.

    You will have to make some kind of arrangement to make sure that you are referencing existing contracts. One way might be to reverse your logic, and run on the 6J contract, calling $USDJPY, as the latter is an invariant string. Other ways would be more complex, as you would have to determine the date, so that you can use the correct contract.

    Comment


      #17
      Thanks a lot buddy. We'll Do.

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by judysamnt7, 03-13-2023, 09:11 AM
      4 responses
      59 views
      0 likes
      Last Post DynamicTest  
      Started by ScottWalsh, Today, 06:52 PM
      4 responses
      36 views
      0 likes
      Last Post ScottWalsh  
      Started by olisav57, Today, 07:39 PM
      0 responses
      7 views
      0 likes
      Last Post olisav57  
      Started by trilliantrader, Today, 03:01 PM
      2 responses
      21 views
      0 likes
      Last Post helpwanted  
      Started by cre8able, Today, 07:24 PM
      0 responses
      10 views
      0 likes
      Last Post cre8able  
      Working...
      X