Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Conditional Bars Object for Indicators

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

    Conditional Bars Object for Indicators

    Hi,

    is it possible and legal to add a bars object to an indicator based on a boolean condition (selectable in the property grid)?

    protected override void Initialize()
    {
    if (condition == true)
    Add(PeriodType.Minute, 5);
    }


    Regards
    Ralph

    #2
    Ralph,

    Unfortunately not. When you are in the UI the code has already run through the Initialize() method and added whatever plots would be needed.
    Josh P.NinjaTrader Customer Service

    Comment


      #3
      Actually it can be done, I've made a number of indicators where a secondary bar series is added only after a parameter is set.

      I know Initialize() is called before the parameter is set, but it's called again later and it works fine. If it didn't there would be no way to specify the symbol for a secondary bar series other than hard-coding it, which would be absurd.

      I don't know about adding plots, maybe that is different, but for bars objects it works.

      Code:
      protected override void Initialize()
      { 
           if (Symbol2 != "")
                Add(Symbol2, BarsPeriod.Id, BarsPeriod.Value);
      }

      Comment


        #4
        I had the impression too that this conditional approach could work. After a quick test I got an 'Index out of range' error message:

        Error on calculating indicator min/max value for indicator XXX. Please check the 'GetMinMaxValues' method

        Here is the sequence of what I did:
        - Apply the indicator to the chart, condition is false - OK
        - Modify the indicator by setting the condition to true - OK
        - Modify the indicator by setting the condition to false - error message above occures

        Regards
        Ralph

        Comment


          #5
          Yes, I'm seeing that too. It looks like you can Add() a data series, but you can't un-Add() it. In the indicator where I use it, I don't need to do that.

          Maybe you can just always add it, and ignore it in OnBarUpdate if your condition is false.

          Comment


            #6
            Thanks a lot for confirmation, kdoren.
            I just wrote a little test indicator containing that conditional add-statement only. This behaviour seems to occur consistantly.

            What I try to do is to exploit the volume tick data contained in the NT data base for an indicator. For example, I apply that indicator on a 5-minute chart and calculate a volume distribution based on single-tick volume data. The condition comes into play if I apply this indicator to a higher time frame. For daily charts (as an example), the tick volume would be an overkill, I would then try to use minute date instead.

            An alternative might be to insert the volume based calculations in separate indicators and instantiate one of these volume indicators as desired. That's not a bad approach to be able to use these volume indicators as standalone applications in additions. The "aftertaste" is, I have to duplicate big portions of the code, these volume indicators differ only regarding the usage of a different sub-dataseries.

            Regards
            Ralph

            Comment


              #7
              Try adding something like this in Initialize:

              PHP Code:
              barValue = Math.Max(1, base.BarsPeriod.Value);
              Add(hedgeInstrument, BarsPeriod.Id, barValue); 
              
              In variables add:

              private int barValue;

              Comment


                #8
                Josh,

                I wrote a little test indicator to describe the chain of executions. I attached the code as the .cs file instead of exporting it with NT7. Reason: after extracting the zip file, the indicators directory is empty (v7.15). Anyway, here is the issue.

                To describe what happens, I tagged every instance with a unique instance ID and put a print statement in every method to see what the order of execution is.

                At first I applied the Indicator 'Test' to a chart window:
                Code:
                (+) Constructor instanceID: 8
                Set_MyCondition instanceID: 8 myCondition: True
                Initialize instanceID: 8
                Set_MyCondition instanceID: 8 myCondition: True
                Get_MyCondition instanceID: 8 myCondition: True
                Get_MyCondition instanceID: 8 myCondition: True
                (#) OnBarUpdate instanceID: 8
                After construction, variable myCondition is set to true. Then Initialize() is called and a secondary data series is added based on myCondition.

                Then I re-opened the property grid and changed myCondition to false. Now I got an error message (index out of range) and the indicator aborts:
                ERROR: Error on calculating indicator min/max value for indicator 'Test'. Please check the 'GetMinMaxValues' method: Der Index war außerhalb des Arraybereichs.

                Summary: As a first step it seems to be possible to add a data series based on a condition because the chain of events sets the variables first and then calls Initialize(). Setting the variable back to false causes the internal logic to crash, which is a bug in my opinion.

                Regards
                Ralph
                Attached Files

                Comment


                  #9
                  Ralph,

                  As mentioned, this is not supported. You can't switch it to false and expect it to remove the bars it already added which is why you get the "Index out of range" message because you are trying to remove bars, but it still wants them.
                  Josh P.NinjaTrader Customer Service

                  Comment


                    #10
                    Josh, please be aware about the following: NT never modifies an indicator instance when you hit OK after a property grid modification. Instead (1) it droppes the old instance which is somewhen trashed by the garbage collector and (2) creates a new and fresh indicator instance (which does not contain a secondary data series in this case). But the fact that there was an indicator instance with a secondary data series and now there is a new instance with a secondary data series some how confuses the internal NT logic.

                    Regards
                    Ralph

                    Comment


                      #11
                      Ralph,

                      Thank you for your post. That is correct, you will get new instances.
                      Josh P.NinjaTrader Customer Service

                      Comment


                        #12
                        Josh, how does spread charting work with NT7? This approach deals with different data series too. Is it possible to modify the spread-instruments later on, or have they to be fixed once defined. Do you have code examples for spread charting? Maybe I could get there some inspirations regarding what should work and is supported by you guys.

                        Thanks
                        Ralph

                        Comment


                          #13
                          Ralph,

                          Spread charting is not supported. You would have to custom program your own indicator to do this. For an idea you can try searching the forums for another user's indicator called Ratio. Changing secondary instruments after defined is not supported by us.
                          Josh P.NinjaTrader Customer Service

                          Comment


                            #14
                            Ralph, I've made some multi-instrument indicators which are posted in the sharing section, such as "Spread"

                            http://www.ninjatrader.com/support/f...d=1&linkid=289

                            They always Add() a secondary bars series, it's not conditional, so there's never an issue with removing it. The Symbol for the bar series is a parameter, and it can be changed without a problem. It seems that it only triggers a problem if an indicator Add()s a bar series, then at a later time it doesn't Add() the Bar Series, it looks like NT can't handle removing it. I think the solution is just to always add every bar series you might need, then ignore any that you wind up not needing.

                            Comment


                              #15
                              Originally posted by kdoren View Post
                              ...The Symbol for the bar series is a parameter, and it can be changed without a problem...
                              That is a viable option, I'll try to implement that.

                              BTW, interesting indicator. Seems to be a challenging task to implement a spread indicator. I have to syncronize different data series of the same indicator in relation to the session break. This way you really learn how different data series are handled.

                              It is a great job, to provide such functionality for us users. Thanks a lot NT!

                              Regards
                              Ralph

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                              0 responses
                              601 views
                              0 likes
                              Last Post Geovanny Suaza  
                              Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                              0 responses
                              347 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by Mindset, 02-09-2026, 11:44 AM
                              0 responses
                              103 views
                              0 likes
                              Last Post Mindset
                              by Mindset
                               
                              Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                              0 responses
                              559 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by RFrosty, 01-28-2026, 06:49 PM
                              0 responses
                              558 views
                              1 like
                              Last Post RFrosty
                              by RFrosty
                               
                              Working...
                              X