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

Countif

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

    Countif

    Hi,

    This is an indicator.

    I'm trying to count the number of bars between an event for a given number of bars:

    Code:
    		protected override void OnBarUpdate()
    		{
    			if (CountIf(() => (Close[0] / Open[0]) - 1, N) > Threshold);
    			
    				Value[0];
    			
    		}
    It wont compile? basically, "N" is the lookback period, and Threshold is the % gain close/open of the given bar. So for example, if 10 bars have gone, and the % gain is below .01 (threshold), I want a count of 10 bars/indicator value as "10."

    Thank you

    #2
    Originally posted by calhawk01 View Post
    Hi,

    This is an indicator.

    I'm trying to count the number of bars between an event for a given number of bars:

    Code:
    		protected override void OnBarUpdate()
    		{
    			if (CountIf(() => (Close[0] / Open[0]) - 1, N) > Threshold);
    			
    				Value[0];
    			
    		}
    It wont compile? basically, "N" is the lookback period, and Threshold is the % gain close/open of the given bar. So for example, if 10 bars have gone, and the % gain is below .01 (threshold), I want a count of 10 bars/indicator value as "10."

    Thank you
    so i got it to work, but small problem:

    Code:
    			if (CurrentBar > N+1)
    			{
    			
    				Value[0]= (CountIf(delegate {return (Math.Abs(Close[0]/Close[2] -1)) < ([B]0.001[/B]);},N)) ;
    			}
    above works; but when I use the bolded as a variable, the return variable values are ZERO;

    Code:
    			if (CurrentBar > N+1)
    			{
    			
    				Value[0]= (CountIf(delegate {return (Math.Abs(Close[0]/Close[2] -1)) < ([B]Threshold[/B]);},N)) ;
    			}
    so basically when I hard input the "threshold" value, the indicator works, but when I use the threshold variable, the indicator compiles but does not give me any values.

    The threshold is a "double" and min value on it is 0.

    Why could this be?

    Comment


      #3
      Code:
      // 
      // Copyright (C) 2017, NinjaTrader LLC <www.ninjatrader.com>.
      // NinjaTrader reserves the right to modify or overwrite this NinjaScript component with each release.
      //
      #region Using declarations
      using System;
      using System.Collections.Generic;
      using System.ComponentModel;
      using System.ComponentModel.DataAnnotations;
      using System.Linq;
      using System.Text;
      using System.Threading.Tasks;
      using System.Windows;
      using System.Windows.Input;
      using System.Windows.Media;
      using System.Xml.Serialization;
      using NinjaTrader.Cbi;
      using NinjaTrader.Gui;
      using NinjaTrader.Gui.Chart;
      using NinjaTrader.Gui.SuperDom;
      using NinjaTrader.Data;
      using NinjaTrader.NinjaScript;
      using NinjaTrader.Core.FloatingPoint;
      using NinjaTrader.NinjaScript.DrawingTools;
      #endregion
      
      // This namespace holds indicators in this folder and is required. Do not change it.
      namespace NinjaTrader.NinjaScript.Indicators
      {
      	/// <summary>
      	/// The Accumulation/Distribution (AD) study attempts to quantify the amount of volume flowing into or 
      	/// out of an instrument by identifying the position of the close of the period in relation to that period's high/low range.
      	/// </summary>
      	public class Counting : Indicator
      	{
      		protected override void OnStateChange()
      		{
      			if (State == State.SetDefaults)
      			{
      				Description					= NinjaTrader.Custom.Resource.NinjaScriptIndicatorDescriptionADL;
      				Name						= "Counting";
      				IsSuspendedWhileInactive	= true;
      				N					= 50;
      				Threshold					= 0;
      				Period					= 2;
      				DrawOnPricePanel			= false;
      				AddPlot(Brushes.DarkCyan, NinjaTrader.Custom.Resource.ADLAD);
      			}
      			else if (State == State.Historical)
      			{
      				if (Calculate == Calculate.OnPriceChange)
      				{
      					Draw.TextFixed(this, "NinjaScriptInfo", string.Format(NinjaTrader.Custom.Resource.NinjaScriptOnPriceChangeError, Name), TextPosition.BottomRight);
      					Log(string.Format(NinjaTrader.Custom.Resource.NinjaScriptOnPriceChangeError, Name), LogLevel.Error);
      				}
      			}
      		}
      		protected override void OnBarUpdate()
      		{
      			
      			if (CurrentBar > N+1)
      			{
      			
      				Value[0]= (CountIf(delegate {return (Math.Abs( (Close[0]/Close[2]) -1)) [B]> 0[/B];},N)) ;
      			}
      
      		}
      
      		#region Properties
      		[NinjaScriptProperty]
      		[Range(1, int.MaxValue)]
      		[Display(Name="N", Order=1, GroupName="Parameters")]
      		public int N
      		{ get; set; }
      		
      		[NinjaScriptProperty]
      		[Range(1, int.MaxValue)]
      		[Display(Name="Period", Order=2, GroupName="Parameters")]
      		public int Period
      		{ get; set; }
      
      		
      		[NinjaScriptProperty]
      		[Range(0, double.MaxValue)]
      		[Display(Name="Threshold", Order=3, GroupName="Parameters")]
      		public double Threshold
      		{ get; set; }
      		
      		[Browsable(false)]
      		[XmlIgnore()]
      		public Series<double> AD
      		{
      			get { return Values[0]; }
      		}
      		#endregion
      	}
      }
      
      #region NinjaScript generated code. Neither change nor remove.
      
      namespace NinjaTrader.NinjaScript.Indicators
      {
      	public partial class Indicator : NinjaTrader.Gui.NinjaScript.IndicatorRenderBase
      	{
      		private Counting[] cacheCounting;
      		public Counting Counting(int n, int period, double threshold)
      		{
      			return Counting(Input, n, period, threshold);
      		}
      
      		public Counting Counting(ISeries<double> input, int n, int period, double threshold)
      		{
      			if (cacheCounting != null)
      				for (int idx = 0; idx < cacheCounting.Length; idx++)
      					if (cacheCounting[idx] != null && cacheCounting[idx].N == n && cacheCounting[idx].Period == period && cacheCounting[idx].Threshold == threshold && cacheCounting[idx].EqualsInput(input))
      						return cacheCounting[idx];
      			return CacheIndicator<Counting>(new Counting(){ N = n, Period = period, Threshold = threshold }, input, ref cacheCounting);
      		}
      	}
      }
      
      namespace NinjaTrader.NinjaScript.MarketAnalyzerColumns
      {
      	public partial class MarketAnalyzerColumn : MarketAnalyzerColumnBase
      	{
      		public Indicators.Counting Counting(int n, int period, double threshold)
      		{
      			return indicator.Counting(Input, n, period, threshold);
      		}
      
      		public Indicators.Counting Counting(ISeries<double> input , int n, int period, double threshold)
      		{
      			return indicator.Counting(input, n, period, threshold);
      		}
      	}
      }
      
      namespace NinjaTrader.NinjaScript.Strategies
      {
      	public partial class Strategy : NinjaTrader.Gui.NinjaScript.StrategyRenderBase
      	{
      		public Indicators.Counting Counting(int n, int period, double threshold)
      		{
      			return indicator.Counting(Input, n, period, threshold);
      		}
      
      		public Indicators.Counting Counting(ISeries<double> input , int n, int period, double threshold)
      		{
      			return indicator.Counting(input, n, period, threshold);
      		}
      	}
      }
      
      #endregion
      Got my last "threshold" variable to work but I just dont believe this indicator is counting correctly. If you notice in this code (bolded), i'm trying to calculate the # of occurances where close[0]/close[x bars ago] -1 is greater than 0, this should give me a tons of "counts" but for some reason its only giving me 2. I think something is wrong with my code when it comes to

      Code:
      CurrentBar > N+1
      or

      Code:
      N
      //the lookbackperiod.

      can someone please help? I basically want to load up a chart, say for X days and look at each bar, and if the percentage change from the two points is greater than X, I want to count.If it's not, then I want the counter to decrease

      Comment


        #4
        Is there a way to make the look back period on countif() to be the max bar loaded in a chart?

        Comment


          #5
          Hello,

          Thank you for the post.

          I took a look at what you have so far and wanted question the use of CountIf here, you said
          if the percentage change from the two points is greater than X, I want to count.If it's not, then I want the counter to decrease
          I am not sure CountIf will be good for this specifically as this is only going to return true or false and will either increment or not but will never decrease. If you also want to decrement the count, you would likely be better off just removing the CountIf and making this into an if statement.

          Code:
          private int myCount;
          
          protected override void OnBarUpdate()
          {
          	if((Math.Abs( (Close[0]/Close[2]) -1)) > 0)
          	{
          		myCount++;
          	} else {
          		myCount--;
          	}
          	Value[0]= myCount;
          }
          Otherwise to continue using the CountIf with the count of the bars, you could use CurrentBar as the BarsAgo to encompass all data.

          I look forward to being of further assistance.
          JesseNinjaTrader Customer Service

          Comment


            #6
            Originally posted by NinjaTrader_Jesse View Post
            Hello,

            Thank you for the post.

            I took a look at what you have so far and wanted question the use of CountIf here, you said


            I am not sure CountIf will be good for this specifically as this is only going to return true or false and will either increment or not but will never decrease. If you also want to decrement the count, you would likely be better off just removing the CountIf and making this into an if statement.

            Code:
            private int myCount;
            
            protected override void OnBarUpdate()
            {
            	if((Math.Abs( (Close[0]/Close[2]) -1)) > 0)
            	{
            		myCount++;
            	} else {
            		myCount--;
            	}
            	Value[0]= myCount;
            }
            Otherwise to continue using the CountIf with the count of the bars, you could use CurrentBar as the BarsAgo to encompass all data.

            I look forward to being of further assistance.

            Thanks.

            Ok so I only use NT for research purposes. And I haven't used it in a while and just started using N8. I've been using n8 without any issues for a month, but I didn't really program anything. Since two days ago, I started making strategies and indicators. And specially, I've noticed that since I started testing the stratified in strategy analyzer, my entire computer and N8 is extremely slow if I have n8 open. What's going on? I can say one thing, one my strategy generates a random variable.. could it be that even if that strategy isn't open, it's constantly generating a variable in the background? Below are my system specs:

            Processor: intel Xeon CPU 2.20ghx
            Memory: 3 Gb
            System type: 64 bit

            VPS: windows server 2008 r2 standard.
            2.53 GB FREE SPace

            Comment


              #7
              Hello,

              Thank you for the reply.

              It is certainly possible that something your script is doing is causing the slowdown, have you at this point tried the sample strategies that come with the platform to see if the computer behaves in a similar way? The SampleMACrossOver is a good test subject as this has very minimal logic, it could let you know if the slowdown is something your logic is creating or relates to the performance of your computer.

              You noted that your strategy generates a random variable; if the script is not being executed that code should also not be executing. I would doubt this is related but depending on what logic you are using that could possibly be related. It would be really difficult to say from the details I have so far. The best solution would be to perform some tests against simple logic to see if this is related to your logic first, and then if so further refine your logic to see why that is the case. If this is related to your logic, you may need to backtrack some by commenting out some logic and retest to find the cause.

              I look forward to being of further assistance.
              JesseNinjaTrader Customer Service

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by michi08, 10-05-2018, 09:31 AM
              5 responses
              741 views
              0 likes
              Last Post NinjaTrader_ChelseaB  
              Started by The_Sec, Today, 02:29 PM
              0 responses
              2 views
              0 likes
              Last Post The_Sec
              by The_Sec
               
              Started by tsantospinto, 04-12-2024, 07:04 PM
              4 responses
              62 views
              0 likes
              Last Post aligator  
              Started by sightcareclickhere, Today, 01:55 PM
              0 responses
              1 view
              0 likes
              Last Post sightcareclickhere  
              Started by Mindset, 05-06-2023, 09:03 PM
              9 responses
              261 views
              0 likes
              Last Post ender_wiggum  
              Working...
              X