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

Hull MA of Macd Historigram

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

    Hull MA of Macd Historigram

    I am new to NT (from Tradestation) and am in the midst of learning NinjaScript. For a 1st project I am altering a MACD to add a Hull MA to the Diff Historigram of Macd.

    I have copied Macd, Changed names, added a period for the HMA, and added a plot line to the Initialize() section of the indicator as well as the HMA logic to the OnBarUpdate() section. This is where I've run into a challenge.

    I called the plot "DiffAv" in the initialize section, but when I compile the code, I get the error message "The name 'DiffAv' does not exist in the current context (I got the same error message on the indicator SMAVolume tutorial). Despite a few hours puzzling this through and trying different approaches, I keep coming back to the same error. It likely is something simple I am just missing, but any help would be greatly appreciated. I have attached the indicator for reference.

    Guy
    Attached Files

    #2
    Welcome to NinjaTrader and the world of OO programming.

    The simple solution is to add the following code under the "Properties" region.

    Code:
    /// <summary>
    /// </summary>
    [Browsable(false)]
    [XmlIgnore()]
    public DataSeries DiffAv
    {
        get { return Values[3]; }
    }
    A simple explanation is as follows:

    - When you Add() a plot in Initialize(), you are adding an object that defines the visualization of the plot, NOT the actual data that is plotted.

    - By adding a plot via the Add() method, NT internally adds a dataseries object held in the Values[] collection that holds the actual data to be plotted.

    - A property is then created that (code you were missing above) that accesses the DataSeries object

    I also see another problem with your code:

    DiffAv.Set(HMA(Diff, HMAPeriod));

    Should be:

    DiffAv.Set(HMA(Diff, HMAPeriod)[0]);

    What you are doing is setting the actual indicator itself vs setting the value that the indicator holds at the current bar.
    RayNinjaTrader Customer Service

    Comment


      #3
      Thanks a ton Ray!

      This makes sense now and thanks also for the welcome. I added the code you suggested and and the indicator compiled. That said, I ran into another snag ... When I added it to a chart, NinjaTrader stopped working and crashed. I restarted it and tried again only to have the same result. If there is an error in the indicator code, it seems odd that it would crash the program. Any suggestions?

      GUY

      Comment


        #4
        The problem is:

        /// <summary>
        /// </summary>
        [Description("Number of bars for HMADiff")]
        [Category("Parameters")]
        public int HMAPeriod
        {
        get { return HMAPeriod; }
        set { HMAPeriod = Math.Max(1, value); }
        }

        The "setter" is recursive meaning, its an endless loop which eventually will cause NT to crash. For clarification -

        - NinjaScript indicators run compiled not interpreted, the same as the rest of the application source code
        - The advantage is that its runs as fast as possible and does not go through an interpreter
        - The downside is the application will inherit bugs such as endless loops
        - Normally we can trap endless loops at run time but .NET 2.0 no longer allows this
        - We have added for the next release an internal check that scans for recursive properties and warns the user of them

        To fix:

        Under variables change HMAperiod to hmaPeriod (note the case)
        In the setter, change to hmaPeriod = Math.Max() ....
        RayNinjaTrader Customer Service

        Comment


          #5
          Forgot...

          Also change the "getter" to

          return hmaPeriod;
          RayNinjaTrader Customer Service

          Comment


            #6
            I made the changes to the hmaPeriod and also found an old reference to HMADiff which I later changed to DiffAv. I got a new error when I changed the setter to hmaPeriod = Math.Max ();}, but if I leave it at Math.Max (value, 1);} the indicator now compiles and runs on the chart without crashing. However ... it does not display anything on the chart ... Where do you think I went wrong?

            Comment


              #7
              I figured it out. I had earlier removed the macd plot. When I added it back, everything worked. Thanks tons for the help!!! The learning curve feels a bit steep, but I appreciate your assistance over some of the speed bumps!

              GUY

              Comment


                #8
                NinjaScript is C#, a full blown programming language. The benefit is that you have a lot more flexibility to express your ideas than you do with a proprietary language. Not to mention, your effort is portable since C# is a real language that can be used outside of NinjaTrader.
                RayNinjaTrader Customer Service

                Comment


                  #9
                  Why Value[3] and not Value[0]? Shouldn't we be passing the most recent Value?

                  I'm trying to create my own indicators and it has been extremely frustrating with Ninja !! Something that would take me a couple minutes with Ensign is taking me hours.

                  edit: I figued it out. Thanks.
                  Last edited by AO76; 07-07-2007, 01:12 PM.

                  Comment


                    #10
                    In this context, Values[3] (not Value[3]) refers to a dataseries object in the Values collection that is referenced by a slot integer and not some value three bars ago. Each time you add a plot, you will want to make sure that it will have a corresponding entry in the "Properties" section with a get/return Values statement that references a unique slot integer. For example, if my custom indicator has four plots it should also have a separate get/return statements referencing Values[0], Values[1], Values[2], Values[3]. Of course, if you use the indicator wizard, this is done automatically for you. Open up any multi-plot standard indicator like KeltnerChannel and you'll see what I mean. HTH

                    Regards,

                    Whitmark

                    P.S. Nice to see an Ensign user working with NinjaTrader
                    whitmark
                    NinjaTrader Ecosystem Vendor - Whitmark Development

                    Comment


                      #11
                      Thanks Whitmark -- I just figured it out after staring at it for far too long !!!

                      Thanks.

                      Comment


                        #12
                        No problem . . . it can be tricky at first to know when those [ ]'s refer to collection indices (e.g., Values[0], Plots[0], Lines[0]) and dataseries n bars ago (e.g., Close[0], Typical[0], Volume[0]). You might find this thread useful going forward in your development endeavors.



                        Regards,

                        Whitmark
                        whitmark
                        NinjaTrader Ecosystem Vendor - Whitmark Development

                        Comment

                        Latest Posts

                        Collapse

                        Topics Statistics Last Post
                        Started by PaulMohn, Today, 03:49 AM
                        0 responses
                        6 views
                        0 likes
                        Last Post PaulMohn  
                        Started by inanazsocial, Today, 01:15 AM
                        1 response
                        9 views
                        0 likes
                        Last Post NinjaTrader_Jason  
                        Started by rocketman7, Today, 02:12 AM
                        0 responses
                        10 views
                        0 likes
                        Last Post rocketman7  
                        Started by dustydbayer, Today, 01:59 AM
                        0 responses
                        4 views
                        0 likes
                        Last Post dustydbayer  
                        Started by trilliantrader, 04-18-2024, 08:16 AM
                        5 responses
                        23 views
                        0 likes
                        Last Post trilliantrader  
                        Working...
                        X