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

how to put some methods into a shared file, and let my indicator and strategy refer

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

    how to put some methods into a shared file, and let my indicator and strategy refer

    Hi,

    I have finished programming an indicator. The indicator basically contains a bunch of conditions and outputs some bool type variables to indicate entry/exit.

    Now I'm programming a strategy. I wonder if you can briefly teach me how to put some functions that output bool type in a separate file, and let my indicator refer to it. That way if I need to change my method, I don't need to change both indicator and strategy files.

    #2
    You will need to expose these bools publicly which can then be read directly from the strategy. You would not need to write to a separate file with this method.

    Please see our Reference Sample on Exposing indicator values that are not plots

    MatthewNinjaTrader Product Management

    Comment


      #3
      Hi Thanks for reply.

      I tried the same as the example below.

      Code:
                  Add(MACD(12, 26, 9));
                  Add(SampleBoolSeries());
      But the problem is that my strategy is a multi-timeframe strategy.

      I can only add indicator to the primary data series, but can't add to my secondary data series.

      Is there a way to work around this?

      Comment


        #4
        Hi Leon, what you experience is expected - if you wish to Add() an indicator working on another series, please do those calcs in the added indicator itself.
        BertrandNinjaTrader Customer Service

        Comment


          #5
          Originally posted by NinjaTrader_Bertrand View Post
          if you wish to Add() an indicator working on another series, please do those calcs in the added indicator itself.
          Sorry, what does this mean?

          So say if I firstly added a secondary series by:
          Add(PeriodType.Ticks, 500);

          Then how do I run the indicator SampleBoolSeries() on that secondary series?

          Comment


            #6
            Hello,

            The Add() method would only be used for visual charting purposes. If you wanted to visualize this indicator on the chart from a secondary data series, you would need to manually add the indicator to your chart.

            However you can still reference the indicator methods in your strategy and specify which data series it is being calculated from

            Taking the SampleBoolSeries for example, the conditional logic would read:

            Code:
            if(SampleBoolSeries([B]BarsArray[1][/B]).BullIndication[0])
            				EnterLong();
            By using BarsArray[1], we are specifying that this indicator is calculated from the first Add() data series you have in your strategy.

            More information on BarsArray can be found below:

            MatthewNinjaTrader Product Management

            Comment


              #7
              Hi I think my question would really be: how to build a strategy and indicator referencing a external library? Is there an example for it?

              Comment


                #8
                Hello,

                It is possible to have your indicator or strategy read from an external file.

                Please see our Reference Sample on Using StreamReader to read from a text file


                MatthewNinjaTrader Product Management

                Comment


                  #9
                  Originally posted by NinjaTrader_Matthew View Post
                  Hello,

                  The Add() method would only be used for visual charting purposes. If you wanted to visualize this indicator on the chart from a secondary data series, you would need to manually add the indicator to your chart.

                  However you can still reference the indicator methods in your strategy and specify which data series it is being calculated from

                  Taking the SampleBoolSeries for example, the conditional logic would read:

                  Code:
                  if(SampleBoolSeries([B]BarsArray[1][/B]).BullIndication[0])
                                  EnterLong();
                  By using BarsArray[1], we are specifying that this indicator is calculated from the first Add() data series you have in your strategy.

                  More information on BarsArray can be found below:

                  http://www.ninjatrader.com/support/h.../barsarray.htm
                  Thank you. So does it mean that it does not matter whether "Add(SampleBoolSeries());" is added into Initialize(), the strategy will work the same?


                  Code:
                          protected override void Initialize()
                          {
                              // Add our indicators to the strategy for charting purposes
                              Add(MACD(12, 26, 9));
                              Add(SampleBoolSeries());
                              
                              CalculateOnBarClose = true;
                          }

                  Comment


                    #10
                    Hello leontancfa,
                    Thanks for your note and I am replying for Matthew.

                    Yes, the strategy will work fine even if you do not Add the indicator in the initialize section of the strategy.
                    JoydeepNinjaTrader Customer Service

                    Comment


                      #11
                      what is the benefit of having a private variable,
                      Code:
                              #region Variables
                              private BoolSeries aBC;
                              #endregion
                      and then have a public property?
                      Code:
                              public BoolSeries ABC
                              {
                                  get { return aBC; }
                              }
                      what could go wrong if I just make the variable public, and get the bool value by MyIndicator.aBC[0]?

                      Comment


                        #12
                        Originally posted by leontancfa View Post
                        what is the benefit of having a private variable,
                        Code:
                                #region Variables
                                private BoolSeries aBC;
                                #endregion
                        and then have a public property?
                        Code:
                                public BoolSeries ABC
                                {
                                    get { return aBC; }
                                }
                        what could go wrong if I just make the variable public, and get the bool value by MyIndicator.aBC[0]?
                        Mainly because it is the best practice in most cases.

                        ref: http://msdn.microsoft.com/en-us/library/w86s7x04.aspx

                        Comment


                          #13
                          Thank you. so I read that reference. I saw one reason is to let outside the indicator only read, but not set the value of the inner variable.

                          Did I miss other reasons?

                          Comment


                            #14
                            Originally posted by leontancfa View Post
                            Thank you. so I read that reference. I saw one reason is to let outside the indicator only read, but not set the value of the inner variable.

                            Did I miss other reasons?
                            That main import of separating Properties from the value of the backing store is in this paragraph:

                            "Properties have many uses: they can validate data before allowing a change; they can transparently expose data on a class where that data is actually retrieved from some other source, such as a database; they can take an action when data is changed, such as raising an event, or changing the value of other fields."

                            The funny thing about programming is that you may think at this time that you do not need any of those abilities, so you can dispense with them and just use a public variable. In my experience, however, sooner or later, if one continues development, one will find that he/she wants to do something which would be very easy to do, if only Properties had been used in the first place.

                            Best practices get dubbed as such, because the collective wisdom of those who have come before us, have found out the hard way, that not doing things in that manner, often causes regrets down the road.

                            Just my $0.02.

                            Comment

                            Latest Posts

                            Collapse

                            Topics Statistics Last Post
                            Started by sidlercom80, 10-28-2023, 08:49 AM
                            168 responses
                            2,261 views
                            0 likes
                            Last Post sidlercom80  
                            Started by Barry Milan, Yesterday, 10:35 PM
                            3 responses
                            10 views
                            0 likes
                            Last Post NinjaTrader_Manfred  
                            Started by WeyldFalcon, 12-10-2020, 06:48 PM
                            14 responses
                            1,428 views
                            0 likes
                            Last Post Handclap0241  
                            Started by DJ888, 04-16-2024, 06:09 PM
                            2 responses
                            9 views
                            0 likes
                            Last Post DJ888
                            by DJ888
                             
                            Started by jeronymite, 04-12-2024, 04:26 PM
                            3 responses
                            41 views
                            0 likes
                            Last Post jeronymite  
                            Working...
                            X