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

Helping out to build PVI

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

    Helping out to build PVI

    Hello,

    I am new to Ninja Scripting and trying to build the PVI indicator.

    Formula can be found at http://www.incrediblecharts.com/tech...lume_index.php

    I started of but the code does not work:
    Code:
    protected override void OnBarUpdate()
            {
                // Use this method for calculating your indicator values.
           			
    			
    			// PVI (today) = PVI (yesterday) + [(Ct-Cy) / Cy] x PVI (yesterday)
    			if (Volume[0] > Volume[1]) {
        			myPVI.Set(0,(myPVI[1]+((Close[0] - Close[1]) / Close[1]) * myPVI[1]));				 
    			}
    
    			if (Volume[0] <= Volume[1]) {    				
    				myPVI.Set(0,myPVI[1]);
    			}
    		
    					
    			Plot0.Set(myPVI[0]);		
    			Plot2.Set(myPVI[1]);
    			
            }
    Please can someone help out.

    #2
    You need to ensure you have enough bars before you can check an index of [1]. Please see this tip: http://www.ninjatrader-support2.com/...ead.php?t=3170
    Josh P.NinjaTrader Customer Service

    Comment


      #3
      Originally posted by NinjaTrader_Josh View Post
      You need to ensure you have enough bars before you can check an index of [1]. Please see this tip: http://www.ninjatrader-support2.com/...ead.php?t=3170
      So now total bar check added,but only showing a yellow horizontal line?

      Code:
      protected override void OnBarUpdate()
              {
                  // Use this method for calculating your indicator values.
             		
      			// Check the total bars in the data series
      			if (CurrentBar < 1) {
              		return;
      			}
      			
      			// PVI (today) = PVI (yesterday) + [(Ct-Cy) / Cy] x PVI (yesterday)
      			if (Volume[0] > Volume[1]) {				
          			myPVI.Set(0,(myPVI[1]+((Close[0] - Close[1]) / Close[1]) * myPVI[1]));				 
      			}
      
      			if (Volume[0] <= Volume[1]) {    				
      				myPVI.Set(0,myPVI[1]);
      			}
      							
      			Plot0.Set(myPVI[0]);		
      			//Plot2.Set(SMA(myPVI[0],255));
      			
              }

      Comment


        #4
        It will plot whatever you set. If you set the same value over and over then you will get a horizontal line. Please check your calculations.
        Josh P.NinjaTrader Customer Service

        Comment


          #5
          Originally posted by NinjaTrader_Josh View Post
          It will plot whatever you set. If you set the same value over and over then you will get a horizontal line. Please check your calculations.
          You are right that is the issue, but I do not know how to translate
          PVI (today) = PVI (yesterday) + [(Ct-Cy) / Cy] x PVI (yesterday)

          into Ninja script

          Comment


            #6
            The issue is you need an initial value for your PVI. Without one it will just be 0 and if its 0 your calculation essentially is 0+0 = 0.
            Josh P.NinjaTrader Customer Service

            Comment


              #7
              Originally posted by NinjaTrader_Josh View Post
              The issue is you need an initial value for your PVI. Without one it will just be 0 and if its 0 your calculation essentially is 0+0 = 0.

              Where to set this value?

              I put myPVI.Set(1,100) in Initialize(). Wrong place?

              Comment


                #8
                Nope. You just need to have one instance of your calculation where you have a value to go in with.

                if (CurrentBar == 0)
                myPVI.Set(100);
                Josh P.NinjaTrader Customer Service

                Comment


                  #9
                  Originally posted by NinjaTrader_Josh View Post
                  Nope. You just need to have one instance of your calculation where you have a value to go in with.

                  if (CurrentBar == 0)
                  myPVI.Set(100);
                  Still no luck. No line is showing up.

                  Code:
                  #region Using declarations
                  using System;
                  using System.ComponentModel;
                  using System.Diagnostics;
                  using System.Drawing;
                  using System.Drawing.Drawing2D;
                  using System.Xml.Serialization;
                  using NinjaTrader.Cbi;
                  using NinjaTrader.Data;
                  using NinjaTrader.Gui.Chart;
                  #endregion
                  
                  // This namespace holds all indicators and is required. Do not change it.
                  namespace NinjaTrader.Indicator
                  {
                      /// <summary>
                      /// Positive Volume Index (by Norman Fosback)
                      /// </summary>
                      [Description("Positive Volume Index (by Norman Fosback) - dumb money")]
                      public class PVI : Indicator
                      {
                          #region Variables
                          // Wizard generated variables
                              private int requiredBars = 2; // Default setting for RequiredBars
                              private int period = 255; // Default setting for Period
                          // User defined variables (add any user defined variables below)
                  			private DataSeries myPVI; // Define the PVI Data Series variable
                          #endregion
                  
                          /// <summary>
                          /// This method is used to configure the indicator and is called once before any bar data is loaded.
                          /// </summary>
                          protected override void Initialize()
                          {
                              Add(new Plot(Color.FromKnownColor(KnownColor.Orange), PlotStyle.Line, "Plot0"));
                              Add(new Plot(Color.FromKnownColor(KnownColor.Green), PlotStyle.Line, "Plot2"));
                              
                  			// Create a DataSeries object and assign it to the variable 
                  			myPVI = new DataSeries(this); 
                  			//myPVI.Set(1,100);
                  			
                  			CalculateOnBarClose	= true;
                              Overlay				= false;
                              PriceTypeSupported	= false;
                          }
                  
                          /// <summary>
                          /// Called on each bar update event (incoming tick)
                          /// </summary>
                          protected override void OnBarUpdate()
                          {
                              // Use this method for calculating your indicator values.
                         		
                  			// Check the total bars in the data series
                  			if (CurrentBar < 1) {
                          		return;
                  			}
                  			if(CurrentBar == 0) {
                  				myPVI.Set(100);
                  			}
                  			
                  			// PVI (today) = PVI (yesterday) + [(Ct-Cy) / Cy] x PVI (yesterday)
                  			if (Volume[0] > Volume[1]) {				
                      			myPVI.Set(0,(myPVI[1]+((Close[0] - Close[1]) / Close[1]) * myPVI[1]));				 
                  			}
                  
                  			if (Volume[0] <= Volume[1]) {    				
                  				myPVI.Set(0,myPVI[1]);
                  			}
                  							
                  			Plot0.Set(myPVI[0]);		
                  			//Plot2.Set(SMA(myPVI[0],255));
                  			
                          }
                  
                          #region Properties
                          [Browsable(false)]	// this line prevents the data series from being displayed in the indicator properties dialog, do not remove
                          [XmlIgnore()]		// this line ensures that the indicator can be saved/recovered as part of a chart template, do not remove
                          public DataSeries Plot0
                          {
                              get { return Values[0]; }
                          }
                  
                          [Browsable(false)]	// this line prevents the data series from being displayed in the indicator properties dialog, do not remove
                          [XmlIgnore()]		// this line ensures that the indicator can be saved/recovered as part of a chart template, do not remove
                          public DataSeries Plot2
                          {
                              get { return Values[1]; }
                          }
                  
                          [Description("Required bars")]
                          [Category("Parameters")]
                          public int RequiredBars
                          {
                              get { return requiredBars; }
                              set { requiredBars = Math.Max(2, value); }
                          }
                  
                          [Description("")]
                          [Category("Parameters")]
                          public int Period
                          {
                              get { return period; }
                              set { period = Math.Max(1, value); }
                          }
                          #endregion
                      }
                  }
                  
                  #region NinjaScript generated code. Neither change nor remove.
                  // This namespace holds all indicators and is required. Do not change it.
                  namespace NinjaTrader.Indicator
                  {
                      public partial class Indicator : IndicatorBase
                      {
                          private PVI[] cachePVI = null;
                  
                          private static PVI checkPVI = new PVI();
                  
                          /// <summary>
                          /// Positive Volume Index (by Norman Fosback) - dumb money
                          /// </summary>
                          /// <returns></returns>
                          public PVI PVI(int period, int requiredBars)
                          {
                              return PVI(Input, period, requiredBars);
                          }
                  
                          /// <summary>
                          /// Positive Volume Index (by Norman Fosback) - dumb money
                          /// </summary>
                          /// <returns></returns>
                          public PVI PVI(Data.IDataSeries input, int period, int requiredBars)
                          {
                              checkPVI.Period = period;
                              period = checkPVI.Period;
                              checkPVI.RequiredBars = requiredBars;
                              requiredBars = checkPVI.RequiredBars;
                  
                              if (cachePVI != null)
                                  for (int idx = 0; idx < cachePVI.Length; idx++)
                                      if (cachePVI[idx].Period == period && cachePVI[idx].RequiredBars == requiredBars && cachePVI[idx].EqualsInput(input))
                                          return cachePVI[idx];
                  
                              PVI indicator = new PVI();
                              indicator.BarsRequired = BarsRequired;
                              indicator.CalculateOnBarClose = CalculateOnBarClose;
                              indicator.Input = input;
                              indicator.Period = period;
                              indicator.RequiredBars = requiredBars;
                              indicator.SetUp();
                  
                              PVI[] tmp = new PVI[cachePVI == null ? 1 : cachePVI.Length + 1];
                              if (cachePVI != null)
                                  cachePVI.CopyTo(tmp, 0);
                              tmp[tmp.Length - 1] = indicator;
                              cachePVI = tmp;
                              Indicators.Add(indicator);
                  
                              return indicator;
                          }
                  
                      }
                  }
                  
                  // This namespace holds all market analyzer column definitions and is required. Do not change it.
                  namespace NinjaTrader.MarketAnalyzer
                  {
                      public partial class Column : ColumnBase
                      {
                          /// <summary>
                          /// Positive Volume Index (by Norman Fosback) - dumb money
                          /// </summary>
                          /// <returns></returns>
                          [Gui.Design.WizardCondition("Indicator")]
                          public Indicator.PVI PVI(int period, int requiredBars)
                          {
                              return _indicator.PVI(Input, period, requiredBars);
                          }
                  
                          /// <summary>
                          /// Positive Volume Index (by Norman Fosback) - dumb money
                          /// </summary>
                          /// <returns></returns>
                          public Indicator.PVI PVI(Data.IDataSeries input, int period, int requiredBars)
                          {
                              return _indicator.PVI(input, period, requiredBars);
                          }
                  
                      }
                  }
                  
                  // This namespace holds all strategies and is required. Do not change it.
                  namespace NinjaTrader.Strategy
                  {
                      public partial class Strategy : StrategyBase
                      {
                          /// <summary>
                          /// Positive Volume Index (by Norman Fosback) - dumb money
                          /// </summary>
                          /// <returns></returns>
                          [Gui.Design.WizardCondition("Indicator")]
                          public Indicator.PVI PVI(int period, int requiredBars)
                          {
                              return _indicator.PVI(Input, period, requiredBars);
                          }
                  
                          /// <summary>
                          /// Positive Volume Index (by Norman Fosback) - dumb money
                          /// </summary>
                          /// <returns></returns>
                          public Indicator.PVI PVI(Data.IDataSeries input, int period, int requiredBars)
                          {
                              if (InInitialize && input == null)
                                  throw new ArgumentException("You only can access an indicator with the default input/bar series from within the 'Initialize()' method");
                  
                              return _indicator.PVI(input, period, requiredBars);
                          }
                  
                      }
                  }
                  #endregion

                  Comment


                    #10
                    Please use the regular .Set() instead of trying to use the one designed to set prior values.

                    DataSeries.Set(value) and not DataSeries.Set(barsAgo, value)

                    Other than that you will just have to step through your code slowly and figure out its logic to make it work. Please use Print() at each step to see what your values are.
                    Josh P.NinjaTrader Customer Service

                    Comment


                      #11
                      Originally posted by NinjaTrader_Josh View Post
                      Please use the regular .Set() instead of trying to use the one designed to set prior values.

                      DataSeries.Set(value) and not DataSeries.Set(barsAgo, value)

                      Other than that you will just have to step through your code slowly and figure out its logic to make it work. Please use Print() at each step to see what your values are.
                      Do I need a additional software or will print() display it to the chart?

                      Comment


                        #12
                        Print() puts it into the Output Window. Tools->Output Window.
                        Josh P.NinjaTrader Customer Service

                        Comment


                          #13
                          PVI and NVI first working version

                          I just finished my first Ninja Trader indicator code. Please feel free to comment on the Positive Volume Index (PVI) indicator and Negative Volume Index (NVI) indicator.

                          cheers,
                          pingpong
                          Attached Files

                          Comment


                            #14
                            Originally posted by pingpong View Post
                            I just finished my first Ninja Trader indicator code. Please feel free to comment on the Positive Volume Index (PVI) indicator and Negative Volume Index (NVI) indicator.

                            cheers,
                            pingpong
                            pingpong,

                            Good Effort. I remember my first indicator.

                            My Thoughts,

                            I believe it is important to comment out the print statements before uploading indicators.

                            Could the displays on the price screen be put on the same side?

                            Are the percentages suppose to add up to 100 percent?

                            RJay
                            RJay
                            NinjaTrader Ecosystem Vendor - Innovative Trading Solutions

                            Comment


                              #15
                              Originally posted by rt6176 View Post
                              pingpong,

                              Good Effort. I remember my first indicator.

                              My Thoughts,

                              I believe it is important to comment out the print statements before uploading indicators.

                              > OK, will do next time.

                              Could the displays on the price screen be put on the same side?

                              > Not clear what you mean?

                              Are the percentages suppose to add up to 100 percent?
                              > No

                              PVI > MA(PVI) 79% bull 21% bear
                              PVI < MA(PVI) 33% bull 67% bear
                              NVI > MA(NVI) 96% bull 4% bear
                              NVI < MA(NVI) 47% bull 52% bear

                              He study appointed the chance a bull markted will happen based on the table above.

                              RJay
                              Are the percentages suppose to add up to 100 percent?
                              > No

                              PVI > MA(PVI) 79% bull 21% bear
                              PVI < MA(PVI) 33% bull 67% bear
                              NVI > MA(NVI) 96% bull 4% bear
                              NVI < MA(NVI) 47% bull 52% bear

                              He study appointed the chance a bull markted will happen based on the table above.

                              RJay[/quote]

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by alifarahani, Today, 09:40 AM
                              3 responses
                              15 views
                              0 likes
                              Last Post NinjaTrader_Jesse  
                              Started by RookieTrader, Today, 09:37 AM
                              4 responses
                              17 views
                              0 likes
                              Last Post RookieTrader  
                              Started by PaulMohn, Today, 12:36 PM
                              0 responses
                              3 views
                              0 likes
                              Last Post PaulMohn  
                              Started by love2code2trade, 04-17-2024, 01:45 PM
                              4 responses
                              39 views
                              0 likes
                              Last Post love2code2trade  
                              Started by junkone, Today, 11:37 AM
                              3 responses
                              22 views
                              0 likes
                              Last Post NinjaTrader_ChelseaB  
                              Working...
                              X