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 share Indicator data with the strategy

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

    how to share Indicator data with the strategy

    Hi - this is a newbie question. I have an indicator that by itself is generating trade signals with additional data like stop-loss, take-profit, risk-reward etc.. I am trying to build a strategy but not sure how to invoke the indicator and how to read the output from it.. In short trying to figure out how to modify the indicator to be able to use inside a strategy code..

    Any examples code will be super helpful.

    #2
    Hello jpkulkarni,

    You will need to set the signal to a plot and then call the indicator from the strategy and use the plot value.

    Below is a link to a support article on adding plots in indicators to use with strategies.


    And a link to an example.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Thanks Chelsea.. What is needed to initialize series added by each AddPlot line? I am lost as to why I am unable to assign values to the plot series.. Here is the what I have done.. When I debug below code, the assignment code in PostSignal method doesn't increase the value of the series.Count.. What am I dowing wrong?

      Sinerely,
      JK
      --

      State.Configure section
      Code:
      AddPlot(Brushes.Transparent, "Signal");
      AddPlot(Brushes.Transparent, "Entry");
      AddPlot(Brushes.Transparent, "StopLoss");
      AddPlot(Brushes.Transparent, "TakeProfit");
      OnBarUpdate method
      Code:
      Signal[0] = 0;
      Entry[0] = 0;
      StopLoss[0] = 0;
      TakeProfit[0] = 0;
      PlotSignal method (bullish object holds the signal values)
      Code:
      Signal[0] = 1;
      Entry[0] = bullish.Entry;
      StopLoss[0] = bullish.StopLoss;
      TakeProfit[0] = bullish.TakeProfit;
      Properties Section
      Code:
      [Browsable(false)]
      [XmlIgnore]
      public Series<double> Signal // +1 for Bullish Entry; -1 for Bearish Entry
      {
      get { return Values[2]; } // this starts with Values[2] because I have two AddPlots earlier in the code that are working fine
      }
      
      [Browsable(false)]
      [XmlIgnore]
      public Series<double> Entry
      {
      get { return Values[3]; }
      }
      
      [Browsable(false)]
      [XmlIgnore]
      public Series<double> StopLoss
      {
      get { return Values[4]; }
      }
      
      [Browsable(false)]
      [XmlIgnore]
      public Series<double> TakeProfit
      {
      get { return Values[5]; }
      }​

      ​​

      Comment


        #4
        Ideally, the OnbarUpdate should keep on initializing new element of the Signa, Entry, StopLoss and TakeProfit series on each bar update.. That is not happening. Also, the code where assigning actual values when I do detect a signal, then also it is not actually assigning the value. When I try to print the values after assignment, below is the error i get.

        Code:
        Indicator 'JKID': Error on calling 'OnBarUpdate' method on bar 24: Index (zero based) must be greater than or equal to zero and less than the size of the argument list.

        Comment


          #5
          Hello jpkulkarni,

          Calling AddPlot() automatically adds a series to the Values collection.

          From the help guide:
          "When this method is called to add a plot, an associated Series<double> object is created held in the Values collection.​"



          Printing Values.Count in State.DataLoaded would provide the total number of plot series.

          Note the series are Series<double> objects and each value must be assigned a double value.
          Further, the collection starts at 0. With 4 calls to AddPlot() this would create series Values[0] through Values[3]. Values[4] and Values[5] won't exist.

          When assigning values, are you doing this from OnBarUpdate()?
          Chelsea B.NinjaTrader Customer Service

          Comment


            #6
            Yes. Assigning values as part of OnBarUpdate(). Also I have total 6 plots in the code, only the 1st 2 are working, these 4 are not.

            Comment


              #7
              Hello jpkulkarni,

              To confirm the code you have shared in post # 3 is not the actual code of the script?

              Comment out all code in OnBarUpdate().

              At the top add simple assignments and print the values.

              Values[0][0] = 1;
              Print(string.Format("{0} | Values[0][0]: {1}", Time[0], Values[0][0]));
              Values[0][1] = 2;
              Print(string.Format("{0} | Values[0][1]: {1}", Time[0], Values[0][1]));
              Values[0][2] = 3;
              ​​​​​​​Print(string.Format("{0} | Values[0][2]: {1}", Time[0], Values[0][2]));
              Values[0][3] = 4;
              ​​​​​​​Print(string.Format("{0} | Values[0][3]: {1}", Time[0], Values[0][3]));
              Values[0][4] = 5;
              ​​​​​​​Print(string.Format("{0} | Values[0][4]: {1}", Time[0], Values[0][4]));
              Values[0][5] = 6;
              ​​​​​​​Print(string.Format("{0} | Values[0][5]: {1}", Time[0], Values[0][5]));

              Save the output to a text file and include this with your next post.
              Chelsea B.NinjaTrader Customer Service

              Comment


                #8
                The OnConfigure() method below gets called from the State.OnConfigure section of my code. The 1st two plots were getting correctly generated (until this runtime issue happened). I will try your code next and share the output.

                JK

                Click image for larger version

Name:	image.png
Views:	21
Size:	467.6 KB
ID:	1292677

                Comment


                  #9
                  I added below code (changed to Values[2] instead of values[0])

                  Click image for larger version

Name:	image.png
Views:	33
Size:	445.1 KB
ID:	1292686
                  I get below output

                  Code:
                  2/22/2023 5:00:00 PM | Values[2][0]: 1
                  Indicator 'JKID': Error on calling 'OnBarUpdate' method on bar 0: You are accessing an index with a value that is invalid since it is out-of-range. I.E. accessing a series [barsAgo] with a value of 5 when there are only 4 bars on the chart.
                  ​

                  Comment


                    #10
                    Hello jpkulkarni,

                    I've actually made a mistake in that print and was adjusting the bars ago indexes and not the plot series indexes.

                    Please print this (without modification) instead:
                    Code:
                    Values[0][0] = 1;
                    Print(string.Format("{0} | Values[0][0]: {1}", Time[0], Values[0][0]));
                    Values[1][0] = 2;
                    Print(string.Format("{0} | Values[1][0]: {1}", Time[0], Values[1][0]));
                    Values[2][0] = 3;
                    Print(string.Format("{0} | Values[2][0]: {1}", Time[0], Values[2][0]));
                    Values[3][0] = 4;
                    ​​​​​​​Print(string.Format("{0} | Values[3][0]: {1}", Time[0], Values[3][0]));
                    Values[4][0] = 5;
                    ​​​​​​​Print(string.Format("{0} | Values[4][0]: {1}", Time[0], Values[4][0]));
                    Values[5][0] = 6;
                    ​​​​​​​Print(string.Format("{0} | Values[5][0]: {1}", Time[0], Values[5][0]));

                    Note, Values[2][1] (the 3rd plot from 1 bar ago) will not exist on the first bar as there is no bar 1 bar ago.

                    The print I provided tests each plot one by one, and on the current bar.
                    Chelsea B.NinjaTrader Customer Service

                    Comment


                      #11
                      Alright -- added below code and looks like its initializing correctly and growing per tick as expected.

                      Click image for larger version

Name:	image.png
Views:	22
Size:	598.8 KB
ID:	1292699

                      Get below output

                      Code:
                      [4990] 2/22/2024 3:25:00 PM | Values[0][0]: 1 | Values[0].Count: 4995
                      2/22/2024 3:25:00 PM | Values[1][0]: 2 | Values[1].Count: 4995
                      2/22/2024 3:25:00 PM | Values[2][0]: 3 | Values[2].Count: 4995
                      2/22/2024 3:25:00 PM | Values[3][0]: 4 | Values[3].Count: 4995
                      2/22/2024 3:25:00 PM | Values[4][0]: 5 | Values[4].Count: 4995
                      2/22/2024 3:25:00 PM | Values[5][0]: 6 | Values[5].Count: 4995
                      [4991] 2/22/2024 3:26:00 PM | Values[0][0]: 1 | Values[0].Count: 4995
                      2/22/2024 3:26:00 PM | Values[1][0]: 2 | Values[1].Count: 4995
                      2/22/2024 3:26:00 PM | Values[2][0]: 3 | Values[2].Count: 4995
                      2/22/2024 3:26:00 PM | Values[3][0]: 4 | Values[3].Count: 4995
                      2/22/2024 3:26:00 PM | Values[4][0]: 5 | Values[4].Count: 4995
                      2/22/2024 3:26:00 PM | Values[5][0]: 6 | Values[5].Count: 4995
                      [4992] 2/22/2024 3:27:00 PM | Values[0][0]: 1 | Values[0].Count: 4995
                      2/22/2024 3:27:00 PM | Values[1][0]: 2 | Values[1].Count: 4995
                      2/22/2024 3:27:00 PM | Values[2][0]: 3 | Values[2].Count: 4995
                      2/22/2024 3:27:00 PM | Values[3][0]: 4 | Values[3].Count: 4995
                      2/22/2024 3:27:00 PM | Values[4][0]: 5 | Values[4].Count: 4995
                      2/22/2024 3:27:00 PM | Values[5][0]: 6 | Values[5].Count: 4995
                      [4993] 2/22/2024 3:28:00 PM | Values[0][0]: 1 | Values[0].Count: 4995
                      2/22/2024 3:28:00 PM | Values[1][0]: 2 | Values[1].Count: 4995
                      2/22/2024 3:28:00 PM | Values[2][0]: 3 | Values[2].Count: 4995
                      2/22/2024 3:28:00 PM | Values[3][0]: 4 | Values[3].Count: 4995
                      2/22/2024 3:28:00 PM | Values[4][0]: 5 | Values[4].Count: 4995
                      2/22/2024 3:28:00 PM | Values[5][0]: 6 | Values[5].Count: 4995
                      [4994] 2/22/2024 3:29:00 PM | Values[0][0]: 1 | Values[0].Count: 4996
                      2/22/2024 3:29:00 PM | Values[1][0]: 2 | Values[1].Count: 4996
                      2/22/2024 3:29:00 PM | Values[2][0]: 3 | Values[2].Count: 4996
                      2/22/2024 3:29:00 PM | Values[3][0]: 4 | Values[3].Count: 4996
                      2/22/2024 3:29:00 PM | Values[4][0]: 5 | Values[4].Count: 4996
                      2/22/2024 3:29:00 PM | Values[5][0]: 6 | Values[5].Count: 4996
                      [4995] 2/22/2024 3:30:00 PM | Values[0][0]: 1 | Values[0].Count: 4997
                      2/22/2024 3:30:00 PM | Values[1][0]: 2 | Values[1].Count: 4997
                      2/22/2024 3:30:00 PM | Values[2][0]: 3 | Values[2].Count: 4997
                      2/22/2024 3:30:00 PM | Values[3][0]: 4 | Values[3].Count: 4997
                      2/22/2024 3:30:00 PM | Values[4][0]: 5 | Values[4].Count: 4997
                      2/22/2024 3:30:00 PM | Values[5][0]: 6 | Values[5].Count: 4997
                      [4996] 2/22/2024 3:31:00 PM | Values[0][0]: 1 | Values[0].Count: 4998
                      2/22/2024 3:31:00 PM | Values[1][0]: 2 | Values[1].Count: 4998
                      2/22/2024 3:31:00 PM | Values[2][0]: 3 | Values[2].Count: 4998
                      2/22/2024 3:31:00 PM | Values[3][0]: 4 | Values[3].Count: 4998
                      2/22/2024 3:31:00 PM | Values[4][0]: 5 | Values[4].Count: 4998
                      2/22/2024 3:31:00 PM | Values[5][0]: 6 | Values[5].Count: 4998

                      Comment


                        #12
                        Hello jpkulkarni,

                        This lets us know the plot Series<double> objects have been initialized properly and added to the Values collection and are accessible.

                        Try adding below the existing prints:

                        Signal[0] = 1234;

                        Print(string.Format("{0} | Signal[0]: {1}", Time[0], Signal[0]));
                        Chelsea B.NinjaTrader Customer Service

                        Comment


                          #13
                          Thanks for all the help, Chelsea. I was able to figure out the issue. I was not in the series but my debug code :-(

                          Anyway - have a different problem now. My indicator is operating on OnBarUpdate and the Strategy is also operating on OnBarUpdate.. So, I have a timing issue now. The Indicator OnBarUpdate is completing after the part of the code in my strategy which is checking for the signal. Is there a way for me to wait till Indicator OnBarUpdate method is complete before I check for the signal in the indicator ?

                          Comment


                            #14
                            Hello jpkulkarni,

                            Calling the indicator from the strategy should cause the indicator's OnBarUpdate() to run first.

                            Comment out all other code and print the time of the bar and a label for the indicator and strategy.

                            In the indicator:

                            Print(string.Format("{0} | Indicator", Time[0]));

                            In the strategy:

                            Print(string.Format("{0} | MyIndicatorValue[0]: {1}", Time[0], MyIndicatorValue[0]));
                            Print(string.Format("{0} | Strategy", Time[0]));

                            (Replace MyIndicatorValue[0] with the name of your actual indicator);

                            Save the output to a text file and attach this to your next post.
                            Chelsea B.NinjaTrader Customer Service

                            Comment


                              #15
                              I was able to call update() as 1st thing in OnBarUpdate() on the indicator and get it to behave correctly. Thanks for all your help. I have started a new thread for a new issue since (cannot start NT) .. Hopefully I can recover from current issue without losing a lot of progress.

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Karado58, 11-26-2012, 02:57 PM
                              8 responses
                              14,825 views
                              0 likes
                              Last Post Option Whisperer  
                              Started by Option Whisperer, Today, 09:05 AM
                              0 responses
                              1 view
                              0 likes
                              Last Post Option Whisperer  
                              Started by cre8able, Yesterday, 01:16 PM
                              3 responses
                              11 views
                              0 likes
                              Last Post cre8able  
                              Started by Harry, 05-02-2018, 01:54 PM
                              10 responses
                              3,204 views
                              0 likes
                              Last Post tharton3  
                              Started by ChartTourist, Today, 08:22 AM
                              0 responses
                              6 views
                              0 likes
                              Last Post ChartTourist  
                              Working...
                              X