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

Adding Indicator to Strategy requiring highs and lows

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

    Adding Indicator to Strategy requiring highs and lows

    I need to add an indicator to my strategy and usually that's a non-brainer. The one problem I'm running into is that my strategy loads several different symbols and needs to add several different indicators as such:

    Add("AAPL" ,PeriodType.Minute, period);
    Add("SPY" ,PeriodType.Minute, period);
    Add("AMZN" ,PeriodType.Minute, period);
    ...


    indicator1 = MyIndicator(Closes[1], 1, false, period);
    indicator2 = MyIndicator(Closes[2], 1, false, period);
    indicator3 = MyIndicator(Closes[3], 1, false, period);


    // pseude code

    However all added indicators are for some reason running against the zero series (which is the E-Mini) and not the ones that I set above. What am I doing wrong?

    hope that makes sense
    Last edited by molecool; 11-18-2012, 05:39 AM.

    #2
    Originally posted by molecool View Post
    I need to add an indicator to my strategy and usually that's a non-brainer. The one problem I'm running into is that my strategy loads several different symbols and needs to add several different indicators as such:

    Add("AAPL" ,PeriodType.Minute, period);
    Add("SPY" ,PeriodType.Minute, period);
    Add("AMZN" ,PeriodType.Minute, period);
    ...


    indicator1 = MyIndicator(Closes[1], 1, false, period);
    indicator2 = MyIndicator(Closes[2], 1, false, period);
    indicator3 = MyIndicator(Closes[3], 1, false, period);


    // pseude code

    However all added indicators are for some reason running against the zero series (which is the E-Mini) and not the ones that I set above. What am I doing wrong?

    hope that makes sense
    You need to use a BarsInProgress filter to handle/control the indicator updates. Unless you do so, your code only specifies the values to be used for calculation, not when updates are to occur.

    Comment


      #3
      Update

      Quiet here today - everyone taking the weekend off I guess. Anyway, I got it to work by passing in a String into my indicator, which then gets added as a second data series. Non-optimal IMO but it's working.

      I did however verify that passing a DataSeries into the indicator via the usual initialize call like this for example fails:

      myIndicator = MyIndicator(Closes[1], 1, true, 3);

      The indicator will get added and runs but for some reason it prints values of the primary data series (i.e. Closes[0] or Close) and not what was passed in. I used print statements to verify that.

      Not sure if that's a limitation or a bug. I looked at the NT protected code on the bottom to see what's happening and based on what I'm seeing it *should* work as the series is being passed in properly. So call me confused....

      Can you guys create a simple demo indicator that just plots the values of the series that's being passed in? Then add it via a faux strategy and see what you get. I would prefer to code this cleanly obviously - hate those work arounds.

      Comment


        #4
        @koganam:

        Nope, that's not the problem. I'm doing this in my workaround. If you call an initializer of your indicator with a secondary data series then it should be the indicator's first/primary data series. It should not even be aware of the chart's main series.

        See my latest comment below. But here:

        myIndicator = MyIndicator(Closes[1], 1, true, 3);

        If I do this from my strategy running on SPY as Closes[0] and AAPL is Closes[1] then MyIndicator should not be aware of SPY as its primary series is AAPL. However, despite me constructing the indicator as above it's printing SPY values.

        Now the only workaround I found was to do this:

        myIndicator = MyIndicator(1, true, 3, "AAPL");

        Now I indeed use BarsInProgress in order to make sure I'm plotting the right value, because in this case my indicator has two data series, SPY and AAPL.

        I hope I make sense here. Am I mistaken in thinking that it should work the way I propose above?

        Comment


          #5
          Originally posted by molecool View Post
          Can you guys create a simple demo indicator that just plots the values of the series that's being passed in? Then add it via a faux strategy and see what you get. I would prefer to code this cleanly obviously - hate those work arounds.
          It would be easier if you posted some code you thought was incorrect, and we could test it out to see exactly what you are talking about, and probably correct it if need be.

          Comment


            #6
            Hi Sledge - all you have to do is to add a second series to your strategy:

            Add("AAPL" ,PeriodType.Minute, 5); // assuming you're using a 5-min chart, you can do this dynamically

            //Then you create a 5-period SMA:

            SMA mySMA = SMA(Closes[1], 5);

            // plot it:

            Add(mySMA);

            NOW if it does what it did on my end then it should NOT be plotting an SMA of AAPL but of the main series.

            Comment


              #7
              Originally posted by molecool View Post
              Hi Sledge - all you have to do is to add a second series to your strategy:

              Add("AAPL" ,PeriodType.Minute, 5); // assuming you're using a 5-min chart, you can do this dynamically

              //Then you create a 5-period SMA:

              SMA mySMA = SMA(Closes[1], 5);

              // plot it:

              Add(mySMA);

              NOW if it does what it did on my end then it should NOT be plotting an SMA of AAPL but of the main series.

              OK, it does it for me too.

              I added the code in Initialize, and then added my strategy to the chart.

              In the chart indicator list, you can see where the SMA was added, and it was related to "ES 12-12" and not FB (secondary series).

              I even tried to use Closes[2] (index out of range). Closes[0] is the same result you saw.

              I'm not sure what NT is doing behind the scenes.

              I can only suggest a work around if you really want to get by this right now.

              Here is some randome sample code I found:


              Code:
                      protected override void Initialize()
                      {
                          Add ( PeriodType.Minute,5);
                          Add(new Plot(Color.FromKnownColor(KnownColor.Orange), PlotStyle.Line, "Plot0"));
                          Add(new Plot(Color.FromKnownColor(KnownColor.Green), PlotStyle.Line, "Plot1"));
                          Overlay                = true;
                      }
              
                      /// <summary>
                      /// Called on each bar update event (incoming tick)
                      /// </summary>
                      protected override void OnBarUpdate()
                      {
                          // Use this method for calculating your indicator values. Assign a value to each
                          // plot below by replacing 'Close[0]' with your own formula.
                          if (CurrentBar<50) return;
                          
                          Plot0.Set(Close[0]);
                          Plot1.Set(SMA(BarsArray[1],20)[0]);
                      }

              Comment


                #8
                Thanks for taking the time to help, Siege - but I already wrote my work around earlier today and it works fine (as mentioned below - you may have missed it). The purpose of my post was to get clarification as to whether NT7 is supposed to be doing it this way or if there is a critical bug.

                By the way *your* workaround using the BarsArray works in many scenarios - assuming you only want to plot the values. The reason why I am doing this however is that I am plotting indicator values but also use them inside my strategy. I need to initialize my indicator in the Initialize() method so that I can get to the indicator's plot values in my OnBarUpdate() method. Calling the indicator there would be very inefficient.

                Anyway, I hope this gets some attention by the NT team tomorrow.

                Comment


                  #9
                  Would love to hear from the NT7 team on this one.

                  Comment


                    #10
                    Looks like a bug to me ?

                    Originally posted by molecool View Post
                    Would love to hear from the NT7 team on this one.
                    Well it is hard to believe, but the entire problem arises from defining mySMA with Closes[1] !! For some strange reason, do it that way, and Closes just stop being processed. Period!

                    The way that I got it to work was to define mySMA with Close, then modify it in OnStartUp().

                    Code:
                    [FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]protected [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]override [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]void[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] Initialize()[/FONT]
                    [FONT=Courier New]{[/FONT]
                    [FONT=Courier New]CalculateOnBarClose = [/FONT][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]true[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New];[/FONT]
                     
                    [FONT=Courier New]Add([/FONT][FONT=Courier New][COLOR=#800000][FONT=Courier New][COLOR=#800000]"AAPL"[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New], PeriodType.Minute, [/FONT][FONT=Courier New][COLOR=#800080][FONT=Courier New][COLOR=#800080]5[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]);[/FONT]
                     
                    [FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]this[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New].mySMA = SMA(Close, [/FONT][FONT=Courier New][COLOR=#800080][FONT=Courier New][COLOR=#800080]5[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]);[/FONT]
                     
                    [FONT=Courier New]Add([/FONT][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]this[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New].mySMA);[/FONT]
                    [FONT=Courier New]}[/FONT]
                     
                    [FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]protected [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]override [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]void[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] OnStartUp()[/FONT]
                    [FONT=Courier New]{[/FONT]
                    [FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]this[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New].mySMA = SMA(Closes[[/FONT][FONT=Courier New][COLOR=#800080][FONT=Courier New][COLOR=#800080]1[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]], [/FONT][FONT=Courier New][COLOR=#800080][FONT=Courier New][COLOR=#800080]5[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]);[/FONT]
                    [FONT=Courier New][COLOR=#008000][FONT=Courier New][COLOR=#008000]// this.mySMA = SMA(BarsArray[1], 5);[/COLOR][/FONT]
                    [/COLOR][/FONT][FONT=Courier New]}[/FONT]
                    Heck, it is broken even if you first initialize using Closes[1], then try to modify it in OnStartUp(). IOW, initializing mySMA with Closes[1] is an absolute process breaker on processing Closes thereafter.

                    Very weird. I consider it to be a bug, but they are probably going to tell us that as they never advised us to use named instances, we are in unsupported territory.

                    NT support, kindly take another look at this. If you require it, the files below demonstrate the problem. You will see that the ONLY difference is in how mySMA is inititialized.
                    Attached Files
                    Last edited by koganam; 11-19-2012, 06:36 PM.

                    Comment


                      #11
                      Definitely a bug

                      Very clever! I didn't think of doing it this way - excellent detective work.

                      Well I for one consider this to be a critical bug. Passing a series into a constructor should be a basic exercise, especially if the generated code offers it.

                      Thanks for spending time on this, very much obliged.

                      Comment


                        #12
                        Thanks for the report and effort guys - we will look into the scenarios kindly provided and get back to you.
                        BertrandNinjaTrader Customer Service

                        Comment


                          #13
                          I think it should be an easy fix. The fact that setting the second series in the onStartUp() method is successful suggests to me that there is a wrong reference in your constructors. Anyway, we can work around it for now but a fix in the next release would be very much appreciated.

                          Comment


                            #14
                            Thanks for your patience on this one - this is unfortunately not a straightforward fix as you're hitting an expected race condition here as the bars object is not available yet in the Initialize(). I've added this to our list to look into for our next major update, but can't give you an ETA or commitment at this point yet.
                            BertrandNinjaTrader Customer Service

                            Comment

                            Latest Posts

                            Collapse

                            Topics Statistics Last Post
                            Started by cmtjoancolmenero, Yesterday, 03:58 PM
                            1 response
                            17 views
                            0 likes
                            Last Post NinjaTrader_Gaby  
                            Started by benmarkal, Yesterday, 12:52 PM
                            3 responses
                            23 views
                            0 likes
                            Last Post NinjaTrader_Gaby  
                            Started by helpwanted, Today, 03:06 AM
                            1 response
                            20 views
                            0 likes
                            Last Post sarafuenonly123  
                            Started by Brevo, Today, 01:45 AM
                            0 responses
                            11 views
                            0 likes
                            Last Post Brevo
                            by Brevo
                             
                            Started by pvincent, 06-23-2022, 12:53 PM
                            14 responses
                            244 views
                            0 likes
                            Last Post Nyman
                            by Nyman
                             
                            Working...
                            X