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 values of Custom indicator in a strategy

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

    exposing values of Custom indicator in a strategy

    Hi There

    How can I expose values from a custom indicator inside of a strategy?

    #2
    Hello GKonheiser,

    The value can be exposed from the indicator as a public variable.

    However, a strategy cannot be called from another script. Should the strategy receive a value from an indicator, this can be used in the strategy but would not be something that could be called from a different script.

    Exposing a value in the indicator can by done by creating a public variable that uses a get and set to return a private variable within the script.

    Please take a look at the SMA indicator included with NinjaTrader.
    Tools -> Edit NinjaScript -> Indicator... -> SMA -> OK

    On line 27 the private variable is created for the period. On lines 61 to 67 this private variable period is exposed as a public variable.
    The attribute tag [GridCategory("Parameters")] is optional and allows the variable to appear in the overloads as well as the Indicator window. When not used, the value is accessible from calling the script and value. IndicatorName().PublicVariableName
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Hi Chelsea,

      Ok so in my indicator I have a Get Set:-

      Code:
       private double plus2SD;
      		public double Plus2SD
              {
                  get { return plus2SD; }
                  set { plus2SD = value; }
              }
      		
      		
      		private double minus2SD;
      		public double Minus2SD
              {
                  get { return minus2SD; }
                  set { minus2SD = value; }
              }
      Then I set the value the private member in OnBarUpDate of the indicator:

      Code:
      protected override void OnBarUpdate()
              {	
      			if( CurrentBars[0] < sDPeriod)
      				return;		
      			
      			avg = Average(averageBase, sDPeriod);
      			
      			sndDev = Deviation(deviationBase, sDPeriod);
      
      			
      				Values[0].Set( avg );
      				Values[1].Set( avg + 2*sndDev );
      				Values[2].Set( avg - 2*sndDev );
      				Values[3].Set( avg + 2.5*sndDev );
      				Values[4].Set( avg - 2.5*sndDev );
      				Values[5].Set( avg + 3*sndDev );
      				Values[6].Set( avg - 3*sndDev );
      				Values[7].Set( avg + 4*sndDev );
      				Values[8].Set( avg - 4*sndDev );
      				Values[9].Set( avg + 5*sndDev );
      				Values[10].Set( avg - 5*sndDev );
      				Values[11].Set( avg + 6*sndDev );
      				Values[12].Set( avg - 6*sndDev );
      				Values[13].Set( avg + 7*sndDev );
      				Values[14].Set( avg - 7*sndDev );	
      			
      				plus2SD = Values[1][0];
      				minus2SD = Values[2][0];
      			
      		// Print("Date" + Time[0].Day + "/"+ Time[0].Month +"  Time " + Time[0].Hour + " " + Time[0].Minute + " Current Bar 10 min " + CurrentBars[0] + " +2SD  "  + Values[1] + " -2SD " + Values[2]);
              }

      I then access the value from my Strategy but it returns 0:

      Code:
      Print("2SD from indic " + StDev(averageBase, deviationBase, sdPeriod).Plus2SD);

      Comment


        #4
        GKonheiser,

        A call to Update(); is also needed in the get to update the value before returning the object.

        An example is attached to demonstrate.
        Attached Files
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Originally posted by NinjaTrader_ChelseaB View Post
          Hello GKonheiser,

          The value can be exposed from the indicator as a public variable.

          However, a strategy cannot be called from another script. Should the strategy receive a value from an indicator, this can be used in the strategy but would not be something that could be called from a different script.

          Exposing a value in the indicator can by done by creating a public variable that uses a get and set to return a private variable within the script.

          Please take a look at the SMA indicator included with NinjaTrader.
          Tools -> Edit NinjaScript -> Indicator... -> SMA -> OK

          On line 27 the private variable is created for the period. On lines 61 to 67 this private variable period is exposed as a public variable.
          The attribute tag [GridCategory("Parameters")] is optional and allows the variable to appear in the overloads as well as the Indicator window. When not used, the value is accessible from calling the script and value. IndicatorName().PublicVariableName

          Hi ChelseaB,
          I created an Indicator script with custom button that allowed me to place an ATM order, with great help from Calonious. However, now I need to change the Stop and Target (2 each). Is there a way for me to do this inside indicator script? OR passing the new Stop levels, Target levels, atmStrategyID and orderID from indicator to strategy to do?

          Comment


            #6
            Hello chrishyewll,

            In general, the Atm Strategy Template is where all settings for modifying orders should go.

            If you are wanting to have custom control over the movements of protective orders, I would not recommend using an Atm Strategy. Instead, I would recommend submitting the protective orders and modifying them.

            Below is a link to an example.


            There is no documentation or support for this, but you might try getting a collection of the stop orders with <atmStrategy>.GetStopOrders() and try an account.Change() on the order.

            Or you may try monitoring all orders on the <account>.OrderUpdate event (or looping through all of the <account>.Orders) and change working stop orders.
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              Originally posted by NinjaTrader_ChelseaB View Post
              Hello chrishyewll,

              In general, the Atm Strategy Template is where all settings for modifying orders should go.

              If you are wanting to have custom control over the movements of protective orders, I would not recommend using an Atm Strategy. Instead, I would recommend submitting the protective orders and modifying them.

              Below is a link to an example.


              There is no documentation or support for this, but you might try getting a collection of the stop orders with <atmStrategy>.GetStopOrders() and try an account.Change() on the order.

              Or you may try monitoring all orders on the <account>.OrderUpdate event (or looping through all of the <account>.Orders) and change working stop orders.
              Hi Chelsea,
              Thanks for the reference above. I forgot to mention that I am using NT7 while the example in the link is using NT8. Do you have NT7 example?
              Thanks ahead.

              Comment


                #8
                Hello chrishyewll,

                I overlooked this is an NT7 thread.

                Unfortunately no, I don't have any suggestions for NinjaTrader 7.

                With NT7 placing orders or using the account object outside of a NinjaScript Strategy in any other type of script is far outside of the area of NS support.

                This thread will remain open for any community members that would like to assist.

                You can also contact a professional NinjaScript Consultant who would be eager to create or modify this script at your request or assist you with your script. The NinjaTrader Ecosystem has affiliate contacts who provide educational as well as consulting services. Please let me know if you would like our business development follow up with you with a list of affiliate consultants who would be happy to create this script or any others at your request.
                Chelsea B.NinjaTrader Customer Service

                Comment


                  #9
                  Originally posted by NinjaTrader_ChelseaB View Post
                  Hello chrishyewll,

                  I overlooked this is an NT7 thread.

                  Unfortunately no, I don't have any suggestions for NinjaTrader 7.

                  With NT7 placing orders or using the account object outside of a NinjaScript Strategy in any other type of script is far outside of the area of NS support.

                  This thread will remain open for any community members that would like to assist.

                  You can also contact a professional NinjaScript Consultant who would be eager to create or modify this script at your request or assist you with your script. The NinjaTrader Ecosystem has affiliate contacts who provide educational as well as consulting services. Please let me know if you would like our business development follow up with you with a list of affiliate consultants who would be happy to create this script or any others at your request.
                  Hi Chelsea,
                  Thanks for you infor. I managed to find ways to go around it by creating a few different template with different preset stoploss and targets. I use script to decide which template to use so that it won't immediately get stopped out the moment I enter the market. Thanks!

                  Comment


                    #10
                    Hi,

                    Can you update this one, as it is not compatible with NT8, pretty please?



                    Thanks,

                    Comment


                      #11
                      Hello nothingbutprofits,

                      There is now a code sample in the NinjaTrader 8 help guide under Update().
                      https://ninjatrader.com/support/help...nt8/update.htm

                      Please let me know if you need this example copied and pasted into an indicator.
                      Chelsea B.NinjaTrader Customer Service

                      Comment


                        #12
                        Originally posted by NinjaTrader_ChelseaB View Post
                        Hello nothingbutprofits,

                        There is now a code sample in the NinjaTrader 8 help guide under Update().
                        https://ninjatrader.com/support/help...nt8/update.htm

                        Please let me know if you need this example copied and pasted into an indicator.
                        Yes. We have the error message for strategy:

                        No overload for method 'Indicator' takes 0 arguments

                        Code:
                        //Strategy
                        //-------------------
                        
                        protected override void OnBarUpdate()
                        {
                        base.OnBarUpdate();
                        
                        Print("Indicator tripleValue: " + Indicator().tripleValue.ToString() ); //error message here..."No overload for method 'Indicator' takes 0 arguments"
                        
                        if (CurrentBars[0] < BarsRequiredToTrade) return;
                        
                        }

                        Comment


                          #13
                          Hello nothingbutprofits,

                          Calling Indicator() would be the issue. You would need to actually use your indicators name. There are no indicators called Indicator (as Indicator is a base class not an actual indicator).

                          Attached is the example from the help guide copied into a script (ExposedValIndiTest) and a host script (HostExposedValIndiTest) that calls it.
                          Attached Files
                          Chelsea B.NinjaTrader Customer Service

                          Comment


                            #14
                            Originally posted by NinjaTrader_ChelseaB View Post
                            Hello nothingbutprofits,

                            Attached is the example from the help guide copied into a script (ExposedValIndiTest) and a host script (HostExposedValIndiTest) that calls it.
                            Thanks for the attached zip file as it's very clear for all of us.

                            Both HostExposedValIndiTest and ExposedValIndiTest indicators are working well.

                            Now for this step I'm trying to make it work:

                            I added ExposedValIndiTest to NT8, and then copied Print("ExposedValIndiTest().TripleValue: " + ExposedValIndiTest().TripleValue ); to my strategy.

                            MACDCustom Indicator:
                            ============================
                            Code:
                            protected override void OnBarUpdate()
                            {
                            
                            tripleValue = SMA(20)[0] * 3;//added here for testing to be read by strategy
                            
                            if (CurrentBar == 0)
                            {
                            fastEma[0] = Input[0];
                            slowEma[0] = Input[0];
                            macdSeries[0] = 0;
                            macdAverage[0] = 0;
                            return;
                            }
                            
                            // Calculate MACD first
                            CalculateMACD();
                            
                            UpdateBarsLines();
                            
                            UpdateBackgroundPanelColors();
                            CrossingMarker();
                            UpdateDirection();
                            UpdateMACDPosition();
                            
                            }
                            
                            //added for testing on allowing for tripleValue variable to read by strategy on every OnBarUpdate()
                            public double TripleValue
                            {
                            get
                            {
                            //call OnBarUpdate before returning tripleValue
                            Update();
                            return tripleValue;
                            }
                            }

                            AutoStrategy Strategy:
                            ============================
                            Code:
                            protected override void OnBarUpdate()
                            {
                            base.OnBarUpdate();
                            
                            Print("ExposedValIndiTest().TripleValue: " + ExposedValIndiTest().TripleValue ); // <- this one works fine.
                            
                            Print("MACDCustom().TripleValue: " + MACDCustom().TripleValue );// <- this one has "No overload for method 'MACDCustom' takes 0 arguments." error.
                            
                            if (CurrentBars[0] < BarsRequiredToTrade)
                            return;
                            
                            if (MACDCustom(........)
                            EnterLong();
                            }
                            I must have missed something. What cause No overload for method error?

                            Comment


                              #15
                              Hello nothingbutprofits,

                              Are there other public variables (possibly using the NinjaScriptProperty attribute) within the scope of the MACDCustom indicator class?

                              Overloads are how you would call the indicator based on the public properties using the NinjaScriptProperty attribute.

                              Below is a link to the help guide on NinjaScriptProperty Attribute.
                              https://ninjatrader.com/support/help...yattribute.htm

                              Like SMA(14), this requires a Period overload parameter (declared publicly within the SMA class and uses the NinjaScriptProperty attribute).
                              https://ninjatrader.com/support/help...simple_sma.htm

                              MACD(2,14,3), this requires a Fast, Slow, and Smooth overload parameters (also public using NinjaScriptProperty).
                              https://ninjatrader.com/support/help...gence_macd.htm
                              Last edited by NinjaTrader_ChelseaB; 10-05-2021, 02:13 PM.
                              Chelsea B.NinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by alifarahani, Today, 09:40 AM
                              0 responses
                              1 view
                              0 likes
                              Last Post alifarahani  
                              Started by Gerik, Today, 09:40 AM
                              0 responses
                              1 view
                              0 likes
                              Last Post Gerik
                              by Gerik
                               
                              Started by RookieTrader, Today, 09:37 AM
                              0 responses
                              4 views
                              0 likes
                              Last Post RookieTrader  
                              Started by KennyK, 05-29-2017, 02:02 AM
                              3 responses
                              1,282 views
                              0 likes
                              Last Post NinjaTrader_Clayton  
                              Started by AttiM, 02-14-2024, 05:20 PM
                              11 responses
                              184 views
                              0 likes
                              Last Post NinjaTrader_ChelseaB  
                              Working...
                              X