Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Calling an indicator from another.

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

    Calling an indicator from another.

    Hello!

    I have two seperate indicators. Let's say, both indicators draw arrows on the chart.

    Is it possible to call the first indicator from the second one? Is yes, could someone give me some tips?
    Here is the idea:
    From the first indicator, I have:

    HTML Code:
     if(X > Y)
     {
       DrawArrow(); 
       bool flag1 = true;
     }
    From the second indicator, I have:

    HTML Code:
    if(X1 > Y1 && flag1)
     {
       DrawArrow(); 
     }


    How can I combine both indicators? Is it even possible?

    I would appreciate any help.

    Best regards

    #2
    Hello Stanfillirenfro,

    Yes you can do that but the indicator you call won't be visible or draw anything, that would only be useful to get plot values from the other indicator. You would just call the other indicator by its name, for example

    double val = SMA(12)[0] calls the SMA indicator and can be used from any other indicator but the SMA does not appear visually, you just get its plot value.
    JesseNinjaTrader Customer Service

    Comment


      #3
      Many thanks NinjaTrader_Jesse for your reply.

      Both indicators are printing arrows on the chart seperately. No problem when they are attached on the chart seperately. In the code below; I have imported only one indicator to test.

      I am having the following error message:
      Indicator 'myNewIndicator': Error on calling 'OnBarUpdate' method on bar 500: Object reference not set to an instance of an object.
      I have the following code:
      HTML Code:
      public class myNewIndicator : Indicator
       {
            #region Variables
             private ImportedIndicator myImportedIndicator;
             #endregion
      }
      ​
      HTML Code:
      protected override void OnBarUpdate()
       {
           Print("myImportedIndicator" + " Size " + ":  " + myImportedIndicator.val1 + " : " + Time[0]);
      
      }
      ​
      Which object reference is not set to an instance? What should I understand with this statement?

      Tnaks in advance!

      Comment


        #4
        Hello Stanfillirenfro,

        You need to set the indicator value to an instance, I would suggest making a condition that uses the indicators plot using the builder and then click view code. That will show the necessary code.
        JesseNinjaTrader Customer Service

        Comment


          #5
          Many thanks NinjaTrader_Jesse for your reply.

          In (State == State.DataLoaded), I have:

          HTML Code:
          else if (State == State.DataLoaded)
          {
              myImportedIndicator = ImportedIndicator();​
          }
          After compilation, I have the error message:
          No overload for method ImportedIndicator`takes o arguments
          Could you please point out what is wrong?

          Thanks

          Comment


            #6
            Hello Stanfillirenfro,

            Where are you seeing the error, can you take a screenshot? The error does not seem to be a normal error that would happen based on the code given.
            JesseNinjaTrader Customer Service

            Comment


              #7
              Many thanks NinjaTrader_Jessefor your reply.

              The NinjaScript Editor has one panel for the codes. If the compilation pups out an error, a second panel appears below the code panel. I am seeing the error message on this second panel.

              I have also copied the parameters of the importedIndicator in the constructor of myImportedIndicator = ImportedIndicator(para1, para2, para3....);
              The error message changes to
              No overload for method ImportedIndicator`takes xxx arguments

              Comment


                #8
                Hello NinjaTrader_Jesse!

                I can call a second indicator from the first one, but the arraws are not drawn as expected.
                In order to test the behaviour of my host indicator, I have attached the first indicator on the chart and the results are as expected. Afterwards, I have called this indicator from the host indicator and the results are weird.

                I have the followings:
                First indicator:
                Region #Variables:
                HTML Code:
                private Series<bool> flag1;
                private Series<bool> flag2;​
                in (State == State.DataLoaded):
                HTML Code:
                flag1 = new Series<bool>(this);
                flag2 = new Series<bool>(this);​
                #Properties:
                HTML Code:
                [Browsable(false)]
                [XmlIgnore]
                public Series<bool> Flag1
                {
                get { return flag1; }
                }
                
                [Browsable(false)]
                [XmlIgnore]
                public Series<bool> Flag2
                {
                get { return flag2; }
                }​
                In protected override void OnBarUpdate()
                HTML Code:
                if(Open[0] < Close[0] && Open[0] != Low[0] && Open[1] > Close[1] && Low[0] < Low[1])
                {
                  Draw.ArrowUp(this, "ArrowL"+CurrentBar, true, Time[0], Low[0] - 4, Brushes.AliceBlue);
                  flag1[0] = (true);
                  flag2[0] = (false);
                }​
                With this code, I have the arrows drawn on the chart as expected.

                For the host indicator:
                Region #Variables:
                HTML Code:
                private Test1 myTest1;
                in (State == State.DataLoaded)
                HTML Code:
                myTest1 = Test1();
                In protected override void OnBarUpdate()
                HTML Code:
                if(myTest1.Flag1[0] == true)
                {
                 Draw.ArrowUp(this, "ArrowL"+CurrentBar, true, Time[0], Low[0] - 4, Brushes.AliceBlue);
                }​​​​​​
                The problem is that with the code of the host indicator, I have random arrows on the chart. I was expecting to have the arrows on the same bars like in the case of the first indicator.

                I would really appreciate if you could have a look on the differents parts of my codes and check please if each part is at its place or not. I am not figuring out why the host indicator is giving unexpected results.

                Many thanks in advance.
                Last edited by Stanfillirenfro; 01-23-2025, 04:16 AM.

                Comment


                  #9
                  Hello Stanfillirenfro,

                  When calling an indicator in code you won't visually see anything it draws, only the primary indicator will draw. If you need both indicators to draw you would need to code a single indicator that includes the code from both indicators so it can draw on the chart.

                  If you are trying to determine how your code is working you can use Print statements in your code. https://support.ninjatrader.com/s/ar...language=en_US
                  JesseNinjaTrader Customer Service

                  Comment


                    #10
                    Many thanks NinjaTrader_Jesse for your reply.

                    No, I do not need both indicators to be drawn on the chart. The idea was to combine several indicators in a single one by calling them in a host indicator. Maby I did not explain well, but that was the idea. I want to call some indicators in a host indicator so that I can manage them only by checking the boxes in the indicator dilog box to show them or not.

                    I have also printed a condition,
                    HTML Code:
                    Print("myTest1.Flag1[0]" + " Size " + ":  " + myTest1.Flag1[0] + " : " + Time[0]);
                    in the condition
                    HTML Code:
                    if(myTest1.Flag1[0] == true)
                    , but I am not having anything on the output window. Impossible do proceed with it.


                    Comment


                      #11
                      Hello Stanfillirenfro,

                      If there is nothing in the output window that would indicate that part of your code is not being reached. You can call other indicators from your indicator like the SMA example I had provided in post 2, you simply call the indicator by its name and access the plot or series you wanted from that instance.
                      JesseNinjaTrader Customer Service

                      Comment


                        #12
                        Thanks NinjaTrader_Jesse for your reply.

                        I thinks I need the plot values instead, But I have arrows on the indicator I want to import. I can import ISeries(), but how to import the values of an arrow? This is new for me.

                        Could you please guide me a bit?

                        Thanks!

                        Comment


                          #13
                          Hello Stanfillirenfro,

                          You can't import arrows from the other indicator, it would be best to just make a plot that has a constant value of 0 and then 1 when a signal is observed. If you need prices you could do the same with a price of 0 or a price when the event occurs.

                          JesseNinjaTrader Customer Service

                          Comment


                            #14
                            Thanks NinjaTrader_Jesse for your help.

                            FINALLY!!!
                            The best way was to use the plots.
                            The problem is solved!

                            Many many thanks again!!!

                            Comment

                            Latest Posts

                            Collapse

                            Topics Statistics Last Post
                            Started by Preacher, 09-20-2021, 10:11 AM
                            13 responses
                            1,001 views
                            0 likes
                            Last Post Gary2751  
                            Started by N1tr0, 01-02-2025, 11:25 AM
                            8 responses
                            114 views
                            0 likes
                            Last Post N1tr0
                            by N1tr0
                             
                            Started by fincabayano, Today, 05:58 AM
                            0 responses
                            10 views
                            0 likes
                            Last Post fincabayano  
                            Started by omribidi, 02-06-2025, 04:39 AM
                            2 responses
                            31 views
                            0 likes
                            Last Post omribidi  
                            Started by Aileenapu, Today, 05:34 AM
                            0 responses
                            5 views
                            0 likes
                            Last Post Aileenapu  
                            Working...
                            X