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

Best Practices Question - Multiple Indicators across Multiple Timeframes

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

    Best Practices Question - Multiple Indicators across Multiple Timeframes

    This is a Best Practices question - I'm an experienced programmer, and have no problem rolling up my sleeves and writing C# code. I'm fairly new to NinjaScript, but I've gone through the support and tutorial docs like a good programmer should. I have a fairly complex strategy that I want to implement and backtest, and I think I know how to do in NS - but my current code is *ugly*. Scary ugly. Not only is ugly unpleasant, it tends to lead to un-maintainable, buggy, fragile code. So:

    My strategy uses several indicators - as many as ten - each applied to multiple time frames of the same instrument. The indicators can be fairly complex; for example, some have offsets or displacements applied, some are custom indicators with three or four values in their parameter list. I make multiple references to these indicators throughout my strategy, so I wind up writing code that looks like this:

    if (MyStochastic(15,20,3,3).KValue[0] < MyStochastic(15,20,3,3).DValue[0] &&
    CrossAbove(Close[1][0], MyTripleEMA(BarsArray[1], 20, 3))
    EnterLong( ...yada yada.... )

    Writing out the indicators by hand, when they're referenced multiple times, is both annoying, ugly, and prone to fat-finger error.

    I had thought that I could just declare instances of the indicators in the Variables region and instantiate them in the Initialize() method. This also seemed appealing, because it would let me give more pleasant logical names to multiple indicators of the same type:

    private MyStochastic myStochLong;
    private MyStochastic myStochShort;

    myStochLong = MyStochastic(15,20,3,3); // In Initialize()
    myStochLong.Displacement = 2;
    myStochShort = MyStochastic(10,5,2,2);
    myStochShort.Displacement = 1;

    if (CrossAbove(myStochLong.KLine, myStochShort.DLine, 1) etc etc ....


    And this approach seems to work ... as long as I'm just using one single time frame. Everything falls apart once I try to incorporate the use of BarsArray[] into this approach. I've tried to create arrays of indicators - my idea being an array of, say, TripleEMA's, one for each time frame - but NinjaScript seems to really, really dislike that (or maybe I'm just doing it wrong?)

    I've scanned several dozen of the sample strategy code files, looking for useful reference code, but I haven't stumbled across a solution yet. I have a sneaking suspicion that I need to create arrays of DataSeries objects, but I'd like to get some confirmation on that. Or am I barking up the wrong tree?

    #2
    Hello pbailey19,

    Thank you for your post.

    You can set multi-series indicators in the OnStartUp() method: http://www.ninjatrader.com/support/h.../onstartup.htm

    Or you can even set a null check in OnBarUpdate() before setting the indicator:
    Code:
     if (indy == null)
     indy = SMA(Highs[1], 5);
    Please let me know if you have any questions.

    Comment


      #3
      Thanks for the quick reply. It looks like OnStartUp() was the key nugget of information that I was missing.

      I take it there aren't any problems with using arrays of indicators, then, as long as they're "instantiated" in OnStartUp()? And, if you don't mind me asking ... would this be acceptable "best practices"? Or is there a better way to do what I'm trying to do?

      Comment


        #4
        Hello pbailey19,

        Thank you for your response.

        Setting the indicators to an Array will not improve performance of the code and you may run into sizing issues with the array itself. You can set your indicators in OnStartUp() as you were doing before in Initialize() (i.e. private MyStochastic = myStochLong).

        Even though you are seeing long conditions and indicators that are called multiple times with all their parameters listed, they are stored internally by NinjaTrader to improve performance. So using something like an array or even a double on OnBarUpdate() will not improve the performance of the script.

        Please let me know if I may be of further assistance.

        Comment


          #5
          How does one display the indicators that are used in OnStartUp()?

          i.e. I initialise a few indicators that uses BarArray[1] in OnStartUp() and I want to see it to make sure it is started properly in Panel 2

          Originally posted by NinjaTrader_PatrickH View Post
          Hello pbailey19,

          Thank you for your response.

          Setting the indicators to an Array will not improve performance of the code and you may run into sizing issues with the array itself. You can set your indicators in OnStartUp() as you were doing before in Initialize() (i.e. private MyStochastic = myStochLong).

          Even though you are seeing long conditions and indicators that are called multiple times with all their parameters listed, they are stored internally by NinjaTrader to improve performance. So using something like an array or even a double on OnBarUpdate() will not improve the performance of the script.

          Please let me know if I may be of further assistance.

          Comment


            #6
            Hello wwteo,

            Thank you for your post.

            For Strategies you would use the Add() method in Initialize(): http://www.ninjatrader.com/support/h...s/nt7/add2.htm

            For indicator you would need to add plots for the indicators you wish to plot from your indicator (or just add them to the chart manually). For information on Plots please visit the following link: http://www.ninjatrader.com/support/h...plot_class.htm

            Please let me know if I may be of further assistance.

            Comment


              #7
              I have tried the plot and the "StrategyPlot" solution. The problem is my indicators actually draw rectangles, triangles, changes colors and plots text there is also a separate volume window which needs to be disabled. Do I need to manually add those in or can I just call an undocumented method within the Indicator class to show the indicator in a particular panel?

              Comment


                #8
                Hello wwteo,

                Thank you for your response.

                All plots will always be on one panel. For drawing objects you can dictate whether they are drawn on the indicator's panel or the price panel with DrawOnPricePanel: http://www.ninjatrader.com/support/h...pricepanel.htm

                The indicator will be plotted on the price panel with Overlay = true, or not with Overlay = false: http://www.ninjatrader.com/support/h...t7/overlay.htm

                However, there is no method to have the plots on multiple panels. A way to work around this is to add all the necessary indicators with their settings to a chart > then right click in the chart > select Templates > Save As > give the template a name. You can now apply the template to any chart by right clicking in the chart and selecting Templates > Load.
                For information on Templates please visit the following link: http://www.ninjatrader.com/support/h..._templates.htm

                Please let me know if I may be of further assistance.

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by Rapine Heihei, Today, 08:19 PM
                1 response
                8 views
                0 likes
                Last Post NinjaTrader_Manfred  
                Started by Rapine Heihei, Today, 08:25 PM
                0 responses
                6 views
                0 likes
                Last Post Rapine Heihei  
                Started by f.saeidi, Today, 08:01 PM
                1 response
                9 views
                0 likes
                Last Post NinjaTrader_Manfred  
                Started by Rapine Heihei, Today, 07:51 PM
                0 responses
                8 views
                0 likes
                Last Post Rapine Heihei  
                Started by frslvr, 04-11-2024, 07:26 AM
                5 responses
                98 views
                1 like
                Last Post caryc123  
                Working...
                X