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

Variable number of Add() calls a problem?

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

    Variable number of Add() calls a problem?

    I have an indicator that allows the user to show an Advance-Decline line, just Advances, or just Declines. In Initialize() I Add() just what the user requires -- doing either 1 Add() or 2 Add() calls. The problem is that doing just 1 Add() call causes problems, mainly hanging the chart display (just blank background showing). It may also hang NT itself. I have discovered that if in the single cases I Add() the other data series even though I have no need for it the problems go away -- all works fine.

    I have noticed one strange thing -- NT seems to persist Instruments[], but not Inputs[]. At the very beginning they are both length 1 on entry to Initialize(), and length 3 on exit (after 2 Add() calls). On subsequent calls to Initialize() Instruments[] is length 3 (containing the things I Add()ed before), while Inputs[] is length 1. If I only do one Add() then on exit Instruments[] is still length 3, while Inputs[] is only length 2. I am guessing that this mismatch is what causes the problem.

    In the error case, using Visual Studio shows that after Initialize() I never get called again -- OnStartup() never gets called in the error case.

    Supporting my hypothesis that the problem is Add()ing fewer series, if I make the Advances and Declines cases do two Add()s -- adding an extra unneeded data series -- then all works just fine. Plotted values are correct. No hangs.

    Does anyone know what is going on, or what I should do? Until I hear a better idea I'll just have to add an unwanted extra data series -- at least that works. Is this a NinjaTrader bug? It sure sounds like it to me.

    One thought I have -- since I am going to do the correct number of Add()s, what if on entry to Initialize() I truncated Instruments[] to be the same length as Inputs[]? Sounds like worth a try. How do I truncate Instruments[]?

    --EV
    Last edited by ETFVoyageur; 07-04-2015, 03:54 AM.

    #2
    OK -- A modest amount of testing appears to verify my hypothesis. I added code to truncate Instruments[] to the same length as Inputs[] before doing my Add()s in Initialize(). After all, why shouldn't Instruments[] be the same each time Initialize() gets called? Initialize() is always going to do the Add()s it needs to, so persisting Instruments[] is at best useless, and at worst causes serious problems (as I have demonstrated).

    I am pretty sure that my solution is highly UNsupported code! But it is the best way I see to address what looks to me like a NinjaTrader bug. I welcome anyone's comments -- why I am wrong, what a better way (preferably supported) to address the problem might be, or any improvements in my code. I would be especially interested if anyone knows of any problems my code might cause.

    This fix is needed only for indicators that may be doing a different number of Add() calls on different passes through Initialize(). As I explained originally, this indicator does so because the user can use the Indicators dialog to choose actions that require different numbers of inputs. If you need this, put the following code in Initialize() prior to doing your Add() for inputs:
    Code:
                // Instruments[] may have cloned data.  When Instruments[] is longer than we need it causes problems.
                // Adding fewer data series to it can cause NinjaTrader chart hangs and NinjaTrader hangs.
                // Truncating Instruments[] to the same length as Inputs[] (which does not have cloned data) solves the problem
                int len= Inputs.Length;
                if (len < Instruments.Length) {
                    Instrument[] inst = new NinjaTrader.Cbi.Instrument[len];
                    for (int i=0; i<len; ++i) {
                        inst[i] = Instruments[i];
                    }
                    Instruments = inst;
                }
    Last edited by ETFVoyageur; 07-04-2015, 11:12 AM.

    Comment


      #3
      Hello ETFVoyageur,

      Thank you for your post and for providing a possible solution to what appears an issue with Add() and Initialize().

      Would you mind providing a script that demonstrates what you are conveying in the first post?

      Comment


        #4
        Patrick,

        Just so you know, I am not ignoring you. I need to write a test case for you, because my actual indicator (a) is fixed and (b) would be more hindrance than help for you.

        In the meantime, the problem is:
        • We are talking about data inputs, not Lines or Plots. As far as I know they work fine.
        • On the very first call to Initialize(), Instruments[] and Inputs[] are consistent. Each has just what was added before the indicator got called -- in my case that is the primary data series.
        • Initialize() Add()s whatever other inputs it wants, say ^NHIN and ^NLON
        • All is fine for this pass
        • The user reconfigures the indicator, changing something in the Indicators dialog such that only ^NHIN is now needed
        • On exiting the dialog, NT goes through its cloning of the indicator
        • On this call to Initialize Inputs[] is reinitialized -- it looks just as it did the very first time. Instruments[], however, is carried forward in the cloning. It contains 3 items this time -- the primary data series, ^NHIN, and ^NLON. In other words it is not consistent with Inputs[] this time, and it is not in the same state it was on the first call to Initialize().
        • Initialize() adds fewer inputs than before -- in this case just ^NHIN. That is what causes the problem. If Initialize() adds at least as many inputs as it added before there is no problem.
        • It appears that, if on returning from Initialize() Instruments[] and Inputs[] are inconsistent then NinjaTrader goes off the deep end. The indicator never gets called again.


        -- EV

        Comment


          #5
          confirmation of problem

          Hi,
          I can confirm this issue. Using a string[] input for a list of stocks, if it is changed after the first load, a blank chart is the result.
          saltminer

          Comment


            #6
            saltminer,

            Do you perhaps have a script that reproduces the same behavior?

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by GLFX005, Today, 03:23 AM
            0 responses
            1 view
            0 likes
            Last Post GLFX005
            by GLFX005
             
            Started by XXtrader, Yesterday, 11:30 PM
            2 responses
            11 views
            0 likes
            Last Post XXtrader  
            Started by Waxavi, Today, 02:10 AM
            0 responses
            6 views
            0 likes
            Last Post Waxavi
            by Waxavi
             
            Started by TradeForge, Today, 02:09 AM
            0 responses
            14 views
            0 likes
            Last Post TradeForge  
            Started by Waxavi, Today, 02:00 AM
            0 responses
            3 views
            0 likes
            Last Post Waxavi
            by Waxavi
             
            Working...
            X