Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Accessing an Indicator dataseries from a strategy

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

    #16
    Thanks again for your input. The end goal is to replace "Close[0]" with the equivalent price of a 50% retracement in the buy1entry IOrder. How would you recommend accomplishing this, since PctUpRet is only a percentage and not a price?
    Thanks
    Last edited by kenb2004; 02-21-2012, 05:54 AM.

    Comment


      #17
      Hello kenb2004,
      As koganam said, it’s a scope error. Please make the PctUpRet a private field like

      Code:
      [B][COLOR="Green"]#region Variable
      double PctUpRet = 0;
      #endregion[/COLOR][/B]
      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
      [B][COLOR="SeaGreen"]PctUpRet[/COLOR][/B] = 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
      @ koganam - thanks for your input.

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

      Comment


        #18
        Thanks, that solved the error problem. The other issue is being able to assign buy1entry to the PRICE of the 50% retracement. Any thoughts?
        Thanks

        Comment


          #19
          Hello kenb2004,
          Use the print function extensively in the code to get the values and use the same appropriately.

          Please refer to this post on how to debug your code http://www.ninjatrader.com/support/f...ead.php?t=3418

          If you have any specific NinjaScript questions would be happy to assist you further.
          JoydeepNinjaTrader Customer Service

          Comment


            #20
            Originally posted by kenb2004 View Post
            Thanks, that solved the error problem. The other issue is being able to assign buy1entry to the PRICE of the 50% retracement. Any thoughts?
            Thanks
            In order to calculate the percent retrace, you must have used values for the SwingHigh and SwingLow, so you know those values a priori. The 50% retrace is (SwingHigh + SwingLow)/2.

            Comment


              #21
              Where can I put the following code so I can use the double "UpSwingPts" in ANY method, ANYWHERE in my strategy?
              Thanks

              double UpSwingPts = Swing.Upswing_pts[0]; // Up Swing Points from Indicator DataSeries
              Print(Time[0] + " - Strategy Up Swing Points = "+UpSwingPts+" Pts");

              Comment


                #22
                Hello kenb2004,
                Thanks for writing in and I am happy to assist you.
                Please make the scope of the variable to private. For example

                Code:
                #region Variables
                       private double UpSwingPts = 0;
                       // other variables
                #endregion
                //call the same in OnBarUpdate
                Code:
                protected override void OnBarUpdate()
                {
                UpSwingPts = Swing.Upswing_pts[0]; // Up Swing Points from Indicator DataSeries


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

                Comment


                  #23
                  Originally posted by kenb2004 View Post
                  Where can I put the following code so I can use the double "UpSwingPts" in ANY method, ANYWHERE in my strategy?
                  Thanks

                  double UpSwingPts = Swing.Upswing_pts[0]; // Up Swing Points from Indicator DataSeries
                  Print(Time[0] + " - Strategy Up Swing Points = "+UpSwingPts+" Pts");
                  In order to access a variable from anywhere in the code, you need to declare the variable as a class variable, You do this by putting the declaration, along with others in the "Variables" region.
                  Code:
                  #region Variables
                         private double UpSwingPts = 0;
                         // other variables
                  #endregion
                  You then access the code from within any eventhandler or method by simply assigning or accessing it directly.

                  Code:
                  AnyEventHandlerOrMethod()
                  {
                  UpSwingPts = SomeCalculation; // assigns value, or
                  double SomeLocalVariable = UpSwingPts (operator) SomethingElse; //access the value to use in another calculation.
                  }

                  Comment


                    #24
                    Thank you, that was very helpful. Could you tell me why you chose private double instead of double or public double?
                    Thanks

                    Comment


                      #25
                      Hello kenb2004,
                      It is more sort of a C# query. Please refer to this post http://stackoverflow.com/questions/6...ed-and-nothing

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

                      Comment


                        #26
                        Originally posted by kenb2004 View Post
                        Thank you, that was very helpful. Could you tell me why you chose private double instead of double or public double?
                        Thanks
                        It is again a matter of scope. Best practice is to declare variables as narrowly as possible, but no narrower. That avoids unintended side effects. By declaring it private, it is not accessible outside the class, and the processor does not use the overhead necessary to expose the variable where it is not needed; apart from which, no code outside the control of the class can alter the value at all.

                        Comment


                          #27
                          Thanks again for your input.

                          Comment


                            #28
                            hi! I just want to check with you, I am trying to use the data series,

                            I follow the procedure in declaring data series.
                            publicclass MACDCross : Strategy
                            {
                            #region Variables
                            private DataSeries myDataSeries;

                            #endregion

                            protectedoverridevoid Initialize()
                            {
                            CalculateOnBarClose =
                            true;

                            myDataSeries =
                            new DataSeries (this);

                            }

                            but when I run it gives me an error "myDataSeries" does not exist.

                            how can I solve this problem?

                            Comment


                              #29
                              Hello xedrick12,

                              Thanks for your post and welcome to the forums!

                              I copied your code and it compiles without error. It is possible that in the code you compiled that there may be a typo or hidden character in the names. Please copy what you posted into a new file and try again.

                              Also, when you compile, NinjaTrader compiles ALL scripts at the same time so it is also possible that the error could be from another source file. If you continue to get the error, take note of the file name listed on the left column of the error. If it is different than the file you are working in you will need to open that file to correct the error (or delete the file) and recompile.

                              Comment


                                #30
                                hi Joy! thanks for your comment.
                                the error is NinjaTrader.Strategy.DataSeries does not contain a constructor that takes '1' arguments.

                                what does it mean?

                                Comment

                                Latest Posts

                                Collapse

                                Topics Statistics Last Post
                                Started by argusthome, 03-08-2026, 10:06 AM
                                0 responses
                                111 views
                                0 likes
                                Last Post argusthome  
                                Started by NabilKhattabi, 03-06-2026, 11:18 AM
                                0 responses
                                59 views
                                0 likes
                                Last Post NabilKhattabi  
                                Started by Deep42, 03-06-2026, 12:28 AM
                                0 responses
                                38 views
                                0 likes
                                Last Post Deep42
                                by Deep42
                                 
                                Started by TheRealMorford, 03-05-2026, 06:15 PM
                                0 responses
                                42 views
                                0 likes
                                Last Post TheRealMorford  
                                Started by Mindset, 02-28-2026, 06:16 AM
                                0 responses
                                78 views
                                0 likes
                                Last Post Mindset
                                by Mindset
                                 
                                Working...
                                X