Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Accessing an Indicator dataseries from a strategy

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

    Accessing an Indicator dataseries from a strategy

    Is it possible to access an Indicators dataseries from a strategy that is from a calculation and NOT a plot. If so how? I'm trying to access percent retracement of previous swing.
    Thanks

    #2
    Hello kenb2004,
    Thanks for your post and I am happy to assist you.

    Yes, you can access the dataseries that is not a Plot from another strategy.

    Please refer to this example for further details http://www.ninjatrader.com/support/f...ead.php?t=4991

    Please let me know if I can assist you any further.
    JoydeepNinjaTrader Customer Service

    Comment


      #3
      I am familiar with the SampleBoolSeries method for accessing a
      BullIndication or BearIndication state, but how would that apply to a dataseries value. If you replaced DataSeries for BoolSeries in this Sample Indicator would SampleBoolSeries.myDataSeries[0] in a Strategy give you the value of the dataseries for the current bar?

      Comment


        #4
        Hello kenb2004,
        Yes, that is the procedure and the basic concept is the same.

        A basic code would look like this
        In variable
        Code:
        private DataSeries myDataSeries;
        in Initialize
        Code:
        myDataSeries = new DataSeries(this);
        assign a value in say OnBarUpdate
        Code:
        myDataSeries.Set(Close[0]);
        in property
        Code:
        [Browsable(false)]
        [XmlIgnore()]
        public DataSeries MyDataSeries
        {
              // We need to call the Update() method to ensure our exposed variable is in up-to-date.
              get  {return myDataSeries; }
        }
        Call the data series from the strategy as
        Code:
        double myDataSeriesValue = SampleBoolSeries().MyDataSeries[0];
        Please let me know if I can assist you any further.
        JoydeepNinjaTrader Customer Service

        Comment


          #5
          ...We need to call the Update() method to ensure our exposed variable is in up-to-date.

          How is this done?

          Comment


            #6
            Hello kenb2004,
            No, update is not needed for any Series object.


            Please let me know if I can assist you any further
            JoydeepNinjaTrader Customer Service

            Comment


              #7
              double myDataSeriesValue = SampleBoolSeries().MyDataSeries[0];

              Where in the strategy code does this go? OnBarUpdate?

              Comment


                #8
                Hello kenb2004,
                Yes, you can access the same from OnBarUpdate.

                Please let me know if I can assist you any further.
                JoydeepNinjaTrader Customer Service

                Comment


                  #9
                  I have successfully called the new dataseries in the strategy and printed the running %retracement to the Output window by putting the following code in OnBarUpdate

                  double Pct_Up_Ret = Swing.Pct_up_ret[0]; // Swing Dataseries for Percent Up Retracement
                  double PctUpRet = Math.Round(Pct_Up_Ret*100);
                  Print(Time[
                  0] + " - New Strategy Up Move Retracement = "+PctUpRet+"%");

                  however when I try to use it as a condition:

                  if (PctUpRet[0] >= 50)

                  I get an error "The name 'PctUpRet' does not exist in the current context. How can it not exist if it prints the running %retracement accurately?
                  Thanks
                  Last edited by kenb2004; 02-20-2012, 04:22 PM.

                  Comment


                    #10
                    Originally posted by kenb2004 View Post
                    I have successfully called the new dataseries in the strategy and printed the running %retracement to the Output window by putting the following code in OnBarUpdate

                    double Pct_Up_Ret = Swing.Pct_up_ret[0]; // Swing Dataseries for Percent Up Retracement
                    double PctUpRet = Math.Round(Pct_Up_Ret*100);
                    Print(Time[
                    0] + " - New Strategy Up Move Retracement = "+PctUpRet+"%");

                    however when I try to use it as a condition:

                    if (PctUpRet[0] >= 50)

                    I get an error "The name 'PctUpRet' does not exist in the current context. How can it not exist if it prints the running %retracement accurately?
                    Thanks
                    It looks like you have a scope error. Are you sure that you are trying to access the variable within the scope that it was declared?

                    Comment


                      #11
                      Hey koganam

                      Thanks for your input, but I really don't know what a scope error is. The dataseries is a percent rounded to a whole percent of the previous up swing. When I use the above print command it follows perfectly the running percentage retracement. The error says that it does not exist in the current context. I'm assuming "context" is the operative word. I'm at a loss.

                      Comment


                        #12
                        Originally posted by kenb2004 View Post
                        Hey koganam

                        Thanks for your input, but I really don't know what a scope error is. The dataseries is a percent rounded to a whole percent of the previous up swing. When I use the above print command it follows perfectly the running percentage retracement. The error says that it does not exist in the current context. I'm assuming "context" is the operative word. I'm at a loss.
                        I believe that you are running this possibly in the OnBarUpdate() event handler? If you do not mind publicly posting, at least the whole OnBarUpdate() code, I am sure that we can come to a quick resolution. If the code must remain secret, then I guess we shall just have to wait until you can send the code directly to NT support.

                        Comment


                          #13
                          The code is part of a multi-purpose template so I'll give you what I believe is pertinent.

                          protected override void OnBarUpdate()
                          {
                          SwingDataSeries();
                          Buy_1();
                          }
                          #region SwingDataSeries //*******************************************
                          private void SwingDataSeries()
                          {
                          if (RedDn){ // this bool is true when up move ends and starts drawing down swing
                          double Pct_Up_Ret = Swing.Pct_up_ret[0]; // Swing Dataseries for Percent Up Retracement
                          double PctUpRet = Math.Round(Pct_Up_Ret*100);
                          Print(Time[0] + " - New Strategy Up Move Retracement = "+PctUpRet+"%");
                          } }
                          #endregion
                          #region Buy_1 //*******************************************
                          private void Buy_1()
                          {
                          if (PctUpRet >= 50) // this line generates the error
                          {
                          CancelSell1_Entry(); // Cancel Sell1 Entry Order
                          CancelSell1_PTST(); // Cancel Sell1 Profit and Stop
                          CancelSell1_Exit(); // Cancel Sell_1 Exit Order
                          // CancelSell1_BE(); // Cancel Sell_1 Break Even Order
                          // buy1entry = EnterLongLimit(0, true, 1, Close[0] + EntryOffset * TickSize, "Buy_1");
                          DrawArrowUp("ArrowUp" + CurrentBar, false, 0, Low[0] + -2 * TickSize, Color.Lime);
                          DrawDot("My Up dot" + CurrentBar, false, 0, Close[0], Color.Lime);
                          } }
                          #endregion

                          Comment


                            #14
                            In post #8 Joydeep recommends putting the Indicator's access code in OnBarUpdate. This seemed to work for printing to Output window but not for a condition.

                            Comment


                              #15
                              Originally posted by kenb2004 View Post
                              The code is part of a multi-purpose template so I'll give you what I believe is pertinent.

                              protected override void OnBarUpdate()
                              {
                              SwingDataSeries();
                              Buy_1();
                              }
                              #region SwingDataSeries //*******************************************
                              private void SwingDataSeries()
                              {
                              if (RedDn){ // this bool is true when up move ends and starts drawing down swing
                              double Pct_Up_Ret = Swing.Pct_up_ret[0]; // Swing Dataseries for Percent Up Retracement
                              double PctUpRet = Math.Round(Pct_Up_Ret*100);
                              Print(Time[0] + " - New Strategy Up Move Retracement = "+PctUpRet+"%");
                              } }
                              #endregion
                              #region Buy_1 //*******************************************
                              private void Buy_1()
                              {
                              if (PctUpRet >= 50) // this line generates the error
                              {
                              CancelSell1_Entry(); // Cancel Sell1 Entry Order
                              CancelSell1_PTST(); // Cancel Sell1 Profit and Stop
                              CancelSell1_Exit(); // Cancel Sell_1 Exit Order
                              // CancelSell1_BE(); // Cancel Sell_1 Break Even Order
                              // buy1entry = EnterLongLimit(0, true, 1, Close[0] + EntryOffset * TickSize, "Buy_1");
                              DrawArrowUp("ArrowUp" + CurrentBar, false, 0, Low[0] + -2 * TickSize, Color.Lime);
                              DrawDot("My Up dot" + CurrentBar, false, 0, Close[0], Color.Lime);
                              } }
                              #endregion
                              Emphasis mine. PctUpRet is declared in the SwingDataSeries() method/function. That makes it local in scope to that function: it does not exist, as soon as the function closes and returns.

                              If you want to use a variable that you will either pass to a function, or modify in a function, and then access elsewhere in the code, you want to declare it as a class variable, and just ensure that when processed, it is not creating or being subjected to unintended side-effects.

                              A class variable can be declared anywhere outside a function or event handler. Good practice is to declare it in the "Variables" section, so that all declarations are organized in one place.

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by argusthome, 03-08-2026, 10:06 AM
                              0 responses
                              79 views
                              0 likes
                              Last Post argusthome  
                              Started by NabilKhattabi, 03-06-2026, 11:18 AM
                              0 responses
                              45 views
                              0 likes
                              Last Post NabilKhattabi  
                              Started by Deep42, 03-06-2026, 12:28 AM
                              0 responses
                              29 views
                              0 likes
                              Last Post Deep42
                              by Deep42
                               
                              Started by TheRealMorford, 03-05-2026, 06:15 PM
                              0 responses
                              32 views
                              0 likes
                              Last Post TheRealMorford  
                              Started by Mindset, 02-28-2026, 06:16 AM
                              0 responses
                              66 views
                              0 likes
                              Last Post Mindset
                              by Mindset
                               
                              Working...
                              X