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

Exposing Value of Indicator In Strategy

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

    Exposing Value of Indicator In Strategy

    I have 4 indicators I wrote which if true will color a bar so when they're applied to a chart I know they're working. So currently in my strategy I have the indicators set to the chart, the bars are painting, however the values are not returning. Is there anything you can suggest or things I should be checking to make sure the values are set up to be exposed properly?

    Currently I have in OnBarUpdate()
    Val30 = Val3[0]
    Val40 = Val4[0]

    if (condition)
    paint bar
    Val1[0] = true
    Val 2[0] = false
    Val3[0] = 0
    Val4[0] = 1

    Then later those variables are exposed as public doubles. Because of the way it's written if the bar is painted then the Val30 and Val40 should be available as a value back in the strategy, right? The values are set to print to the output window but despite the bars coloring no value is coming back from the indicators. Is there anything I should be looking at to ensure that everything is set up right?

    #2
    Hello itsthefriz,
    Normally you need plots to be able to use in strategy, so if you expose plots (e.g. with values 0 or 1) it should work. Other variables won't be available for use in strategy. Hope it helps!

    Comment


      #3
      Hi s.kinra, that's fair, I should be able to plot these if that's the better way of returning a value, I was under the impression a public variable could be moved between indicator and strategy but maybe that's wrong?

      Comment


        #4
        Hi itsthefriz, thanks for posting.

        The public property is available to the strategy through the dot operator e.g. MyInicatorInstance.MyVariable. We also have an example here showing how to expose values that are not plots:



        Kind regards,
        -ChrisL
        Chris L.NinjaTrader Customer Service

        Comment


          #5
          Hi Chris, thanks for the help, that was actually the example I used to create the public variables. The variable is accessible via the object browser however the only value ever returned to the strategy is 0.

          Did a little more debugging by setting the variables equal to 2 in OnStateChange however the value printed to the terminal is still 0. At the minute I cannot say whether the indicator is returning a value of 1 to the public variables within the indicator. I assume it's safe to say that the value from the indicator is at least over-writing the value 2 from when the variable was declared?

          Comment


            #6
            Hi itsthefriz,

            The public property should be available. E.g. the SMA indicator has a public Integer property and we are able to access the period value e.g.

            Code:
            private SMA _SMA;
            else if (State == State.DataLoaded)
            {
                _SMA = SMA(14);
            }
            protected override void OnBarUpdate()
            {
                Print(_SMA.Period); //prints 14
            }
            In the SMA indicator we have:

            #region Properties
            [Range(1, int.MaxValue), NinjaScriptProperty]
            [Display(ResourceType = typeof(Custom.Resource), Name = "Period", GroupName = "NinjaScriptParameters", Order = 0)]
            public int Period
            { get; set; }
            #endregion
            Chris L.NinjaTrader Customer Service

            Comment


              #7
              As far as I can tell they're all set up that way with the region properties "exposing" the value. All values are defined as doubles. The first 2 pictures are the indicator which is showing the setup, where the variables are defined, and how they're returned.

              Given how large the strategy is I can't send snips of the code in place but the third picture shows the variables defined in the State.DataLoaded. Prior to that they are attached to an indicator within State.DataLoaded. The 4th picture shows the Print statement within the OnBarUpdate function

              If it's easier I can send the strategy and indicators in an email with however I have far too much time put into this strategy which I intend to sell to place it all up here.
              Attached Files

              Comment


                #8
                Hi, does it update if you add [NinjaScriptProperty] as a property decorator?

                Chris L.NinjaTrader Customer Service

                Comment


                  #9
                  Would this be below the region Properties of the indicator? It looks like that's just for parameters?
                  Attached Files

                  Comment


                    #10
                    Chris,

                    Just to add on some additional information I noticed that when the indicator is manually added to the chart by right clicking and adding the indicator the bar color changes. When the indicator is assigned to the chart via the strategy the bars do not print. I believe this may be where the problem lies. If there's anything you can think of that I should be looking into to ensure that everything is assembled correctly I would really appreciate it.

                    Comment


                      #11
                      Hello itsthefriz,

                      In the strategy how are you using the indicator? Could you provide a small sample? If you are not calling the indicator from OnBarUpdate that is very likely what is causing it to not work in that situation.
                      JesseNinjaTrader Customer Service

                      Comment


                        #12
                        Hi Jesse,

                        No problem, I'll walk you through the entire path of the indicator from initialization through OnBarUpdate() and will be referring to the indicator as My_Indicator. Secondly since it's being typed out I'm eliminating some of the formatting to make this easier to follow. Hopefully the missing brackets and semi-colon's don't end up giving you an aneurysm haha jk. The strategy does compile, enables, and prints values to my output window.

                        In Public Class Strategy indicator and parameters are initialized

                        private NinjaTrader.NinjaScript.Indicators.My_Folder.My_In dicator PB_ER;
                        private double Parameter1
                        private double Parameter2


                        In State.DataLoaded reference to indicator created and variable is assigned (note: I've moved variable assignment from State.DataLoaded into OnBarUpdate() and neither place returns the needed values)

                        if(PB_ER == null)
                        PB_ER = My_Indicator(Parameter1, Parameter2)
                        AddChartIndicator(PB_ER)

                        PB_ER_Long_1m = My_Indicator.Indicator_Long_Val

                        in OnBarUpdate
                        Print("Indicator Value: " + PB_ER_Long_1m)

                        Comment


                          #13
                          Hello itsthefriz,

                          I don't specifically see what the problem is, do you see the same issue if you try to use the indicator that comes with the sample Chris had linked to? It could relate to something with your strategy, if so you should see the same problem swapping the indicator out with the sample indicator. If so you could compare the strategy from that sample with what you have to get a better idea of the reason.



                          JesseNinjaTrader Customer Service

                          Comment


                            #14
                            Hi Jesse,

                            That's the confusing part, the strategy/indicator SampleBoolSeries was what I copied from. I'll play with the SampleBoolSeries strategy to see if it can expose the value and I'll get back to you guys in the next few days

                            Comment


                              #15
                              Can you update that zip file that will work with NT8? Reason: We have the error message in Strategy:

                              "No overload for method MACDCustomindicator takes 0 arguments."

                              See here: (NT7 version)

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Belfortbucks, Today, 09:29 PM
                              0 responses
                              6 views
                              0 likes
                              Last Post Belfortbucks  
                              Started by zstheorist, Today, 07:52 PM
                              0 responses
                              7 views
                              0 likes
                              Last Post zstheorist  
                              Started by pmachiraju, 11-01-2023, 04:46 AM
                              8 responses
                              151 views
                              0 likes
                              Last Post rehmans
                              by rehmans
                               
                              Started by mattbsea, Today, 05:44 PM
                              0 responses
                              6 views
                              0 likes
                              Last Post mattbsea  
                              Started by RideMe, 04-07-2024, 04:54 PM
                              6 responses
                              33 views
                              0 likes
                              Last Post RideMe
                              by RideMe
                               
                              Working...
                              X