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

Count Max or Min? Today is lowest low in X days?

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

    Count Max or Min? Today is lowest low in X days?

    Hi there, I am looking to create an indicator that would indicate what todays Max or Min count would be. For example.

    Is today a 20 day low lr a 50 day low or a 218 day low. I would like the indicator to plot in a new panel not the price panel so overlay= false.

    I cant get my head around how to count. Can anyone point me to an example or method to do this?

    Thanks,

    #2
    Hello stalt,

    Thank you for writing in.

    You can use the MIN() and MAX() methods to find the lowest/highest value over a specified period.




    You can then compare the value that is returned from this method to the low/high value of the current day bar to check if today was a 20 day low/high. You can do this with the 50 and 218 as well.

    Here's an example to check if today is a 20 day low (ensure to run on daily or 1440 minute bars):
    Code:
    protected override void OnBarUpdate()
    {
         // if today's low value is the 20 day low, do something
         if (MIN(Low, 20)[0] == Low[0])
              // do something
    }
    Please, let us know if we may be of further assistance.
    Zachary G.NinjaTrader Customer Service

    Comment


      #3
      Close. I would like it to plot each day on a daily chart. The values indicated are not what I am looking for, but simply the plot if that makes sense. on a red candle day it might be 1 if we got another red candle it would be 2 etc then in larger pull backs it would indicate 50 or 70 or 87 or 88 for that specific day. Does that explain it better? I don't quite get how to access the historical days to get the count.

      Comment


        #4
        Hello stalt,

        Just to clarify, you would like to essentially increment a counter by one every time a bar closes below the close of the previous bar and plot the value, correct?

        Here's a way this can be done:

        Code:
        private int lowerDayCount = 0;
        
        protected override void OnBarUpdate()
        {
                // we need to check that we have at least two bars on our chart before trying to
                // access the previous bar
                // note the CurrentBar value of the first bar is 0, the next 1, etc.
        	if (CurrentBar < 1)
        		return;
        
                // if the current bar has closed lower than the previous bar, we'll increment our
                // counter
        	
                // otherwise, we are going to reset the counter back to 0 since the current bar
                // closed at the same price or higher to reset the count
        	if (Close[0] < Close[1])
        		lowerDayCount++;
        	else
        		lowerDayCount = 0;
        	
                // we'll plot the value of lowerDayCount
        	Value.Set(lowerDayCount);
        }
        Zachary G.NinjaTrader Customer Service

        Comment


          #5
          Closer,

          If it closes lower than the day before, but also at a new X day low, I want the value of X.

          Comment


            #6
            Hello stalt,

            To clarify further:
            1. You would like to check the lowest close over a period of x
            2. x is incremented every time day bar closes
            3. If the current bar closes lower than the previous bar's close AND if the current close price is the x day lowest close, plot the value of x


            Is this correct?
            Zachary G.NinjaTrader Customer Service

            Comment


              #7
              No I am not interested in the look back period. I have attached n image which may help describe what I am trying to build

              Comment


                #8
                Originally posted by stalt View Post
                No I am not interested in the look back period. I have attached n image which may help describe what I am trying to build

                https://www.dropbox.com/s/s8k0bspefn...56-29.jpg?dl=0
                1. Create an object that holds the LowValue (containing the Low of the relevant bar) and the CurrentBar on which it occurs.
                2. Set up a List.
                3. Add to the List as these swing points are validated.
                4. Use OrderBy to order/sort the values.(That means that you have to bring LINQ into scope.)
                5. Find the immediate LowValue in the sorted IEnumerable that is lower than the value of the current low.
                6. Use the corresponding CurrentBar values to calculate the delta and output it to the chart where you want it to be.

                You can use this as an example of how to do it.

                ref: https://msdn.microsoft.com/en-us/lib...or=-2147217396

                Comment


                  #9
                  OK this is above my capabilities at this point, but thanks for the info. I'll need to do some more research. Thanks

                  Comment


                    #10
                    Solution, probably a bit late

                    Hi !

                    This thread is old, and so, i write this code with NT8, yet, i do not think that there would be any differences in NT7, so, since i was looking to create something similar to what stalt wanted, and the method of kogonam seemed a bit complex, i build a simple thing that allow to do fully what stalt wanted (if i understand well what he wanted), the code is below, and i put the explanations in //

                    Code:
                    if (max[0] != max[1] || min[0] != min[1])
                    //to make the code lighter, so that we dont have to do the whole process of the while, at each bar or at each price update
                    {
                    //since either the max or the min has been changed, the bar value will likely be change, so we reset the Count to 0 
                    	CountBarOfMax = 0;
                    	CountBarOfMin = 0;
                    					
                    //What we do here for the Max and Min, is that, since we have a value for the Max and Min,
                    //and yet, we have no idea at what bar number it has been done, if a bar is below
                    //our max or our min, we will add 1 bar value, then use this new value as the lookback
                    //period for the High/Low, and we are going to stop the operation, as soon as a Low for
                    //the min, or a High for the Max, as hit the Max or Min Value, which then mean, that
                    //the min or max has happened on this bar, and with our count, we have the bar value saved
                    
                    		while (High[CountBarOfMax] < max[0])
                    		{
                    		CountBarOfMax++	;
                    		}
                    					
                    		while (Low[CountBarOfMin] > min[0])
                    		{
                    		CountBarOfMin++	;
                    		}
                    					
                    	maxCurrentBarValue = CountBarOfMax;
                    	minCurrentBarValue = CountBarOfMin;
                    	}
                    			
                    //then you do not need this part, but i use this, so that no matter if the direction of our max-min
                    //is down or up, we have the bar value between max-min, yet, if in your example you want
                    //to know the bar value of your min for example, you will just take the minCurrentBarValue
                    //variable, and it will tell you how many bar ago did the low of your min happened.	
                    
                    BarValueofMaxtoMin = Convert.ToInt32(Math.Max((maxCurrentBarValue - minCurrentBarValue), (minCurrentBarValue - maxCurrentBarValue)));
                    Hope this can help, even with almost 2 year of delay !

                    Comment


                      #11
                      Well almost 4 years later I finally got back to this! I have migrated mostly to Ninja Trader 8 but have come up with the following indicator that indicates if we are at a new X day low or an X day high. in intervals of 10 (today is a new 10 day low, today is a new 20 day low, today is a new 30 day low or today is a new 10 day high, today is a new 20 day high , today is a new 30 day high). I have maxed both at 100 so the max it will measure is a max new 100 day low or high. Originally I was looking at the exact days (today is a new 26 day low, but for my testing purposes that is probably a bit too specific. her is the code if you are still interested. Its not exactly elegant, but does what I want it to do. any thoughts on cleaning up the code I would love the feedback.

                      public class daysMINMAX : Indicator
                      {
                      //variables to hold all MIN indicators
                      private MIN min10;
                      private MIN min20;
                      private MIN min30;
                      private MIN min40;
                      private MIN min50;
                      private MIN min60;
                      private MIN min70;
                      private MIN min80;
                      private MIN min90;
                      private MIN min100;

                      //variables to hold all Min Values
                      private double min10T;
                      private double min20T;
                      private double min30T;
                      private double min40T;
                      private double min50T;
                      private double min60T;
                      private double min70T;
                      private double min80T;
                      private double min90T;
                      private double min100T;




                      //variable to hold the current MIN Value
                      private double myminVal;

                      //variables to hold all MAX indicators
                      private MAX max10;
                      private MAX max20;
                      private MAX max30;
                      private MAX max40;
                      private MAX max50;
                      private MAX max60;
                      private MAX max70;
                      private MAX max80;
                      private MAX max90;
                      private MAX max100;

                      //variables to hold all MAX values
                      private double max10T;
                      private double max20T;
                      private double max30T;
                      private double max40T;
                      private double max50T;
                      private double max60T;
                      private double max70T;
                      private double max80T;
                      private double max90T;
                      private double max100T;




                      //variable to hold the current MAX Value
                      private double mymaxVal;

                      private double curVal;

                      protected override void OnStateChange()
                      {
                      if (State == State.SetDefaults)
                      {
                      Description = @"Enter the description for your new custom Indicator here.";
                      Name = "daysMINMAX";
                      Calculate = Calculate.OnBarClose;
                      IsOverlay = false;
                      DisplayInDataBox = true;
                      DrawOnPricePanel = true;
                      DrawHorizontalGridLines = true;
                      DrawVerticalGridLines = true;
                      PaintPriceMarkers = true;
                      ScaleJustification = NinjaTrader.Gui.Chart.ScaleJustification.Right;
                      //Disable this property if your indicator requires custom values that cumulate with each new market data event.
                      //See Help Guide for additional information.
                      IsSuspendedWhileInactive = true;
                      AddPlot(Brushes.Red,"DaysLow");
                      AddPlot(Brushes.Green,"DaysHigh");
                      }
                      else if (State == State.Configure)
                      {
                      }
                      else if (State == State.DataLoaded)
                      {
                      //get MIN values
                      min10 = MIN(10);
                      min20 = MIN(20);
                      min30 = MIN(30);
                      min40 = MIN(40);
                      min50 = MIN(50);
                      min60 = MIN(60);
                      min70 = MIN(70);
                      min80 = MIN(80);
                      min90 = MIN(90);
                      min100 = MIN(100);

                      //get MAX values
                      max10 = MAX(10);
                      max20 = MAX(20);
                      max30 = MAX(30);
                      max40 = MAX(40);
                      max50 = MAX(50);
                      max60 = MAX(60);
                      max70 = MAX(70);
                      max80 = MAX(80);
                      max90 = MAX(90);
                      max100 = MAX(100);



                      }
                      }

                      protected override void OnBarUpdate()
                      {
                      //Add your custom indicator logic here.

                      //Get MIN Values for today
                      min10T= min10[0];
                      min20T= min20[0];
                      min30T= min30[0];
                      min40T= min40[0];
                      min50T= min50[0];
                      min60T= min60[0];
                      min70T= min70[0];
                      min80T= min80[0];
                      min90T= min90[0];
                      min100T= min100[0];

                      //Get MAX Values for today
                      max10T= max10[0];
                      max20T= max20[0];
                      max30T= max30[0];
                      max40T= max40[0];
                      max50T= max50[0];
                      max60T= max60[0];
                      max70T= max70[0];
                      max80T= max80[0];
                      max90T= max90[0];
                      max100T= max100[0];




                      if (CurrentBar < 205)
                      {
                      return;
                      }

                      //logic to determine if today is a new X day Low

                      if (Close[0]== min10T && min10T>min20T)
                      {
                      myminVal=10*(-1);
                      }

                      else if(Close[0]== min20T && min20T>min30T)
                      {
                      myminVal=20*(-1);
                      }
                      else if(Close[0]== min30T && min30T>min40T)
                      {
                      myminVal=30*(-1);
                      }
                      else if(Close[0]== min40T && min40T>min50T)
                      {
                      myminVal=40*(-1);
                      }
                      else if(Close[0]== min50T && min50T>min60T)
                      {
                      myminVal=50*(-1);
                      }
                      else if(Close[0]== min60T && min60T>min70T)
                      {
                      myminVal=60*(-1);
                      }
                      else if(Close[0]== min70T && min70T>min80T)
                      {
                      myminVal=70*(-1);
                      }
                      else if(Close[0]== min80T && min80T>min90T)
                      {
                      myminVal=80*(-1);
                      }
                      else if(Close[0]== min90T && min90T>min100T)
                      {
                      myminVal=90*(-1);
                      }

                      else if(Close[0]== min100T)
                      {
                      myminVal=100*(-1);
                      }



                      else
                      {
                      myminVal= 0;
                      }

                      //logic to determine if today is a new X day High

                      if (Close[0]== max10T && max10T<max20T)
                      {
                      mymaxVal=10;
                      Print(Time[0].ToString("yyyy/MM/dd")+"mymaxval is :"+mymaxVal);
                      }

                      else if(Close[0]== max20T && max20T<max30T)
                      {
                      mymaxVal=20;
                      Print(Time[0].ToString("yyyy/MM/dd")+"mymaxval is :"+mymaxVal);
                      }
                      else if(Close[0]== max30T && max30T<max40T)
                      {
                      mymaxVal=30;
                      }
                      else if(Close[0]== max40T && max40T<max50T)
                      {
                      mymaxVal=40;
                      }
                      else if(Close[0]== max50T && max50T<max60T)
                      {
                      mymaxVal=50;
                      }
                      else if(Close[0]== max60T && max60T<max70T)
                      {
                      mymaxVal=60;
                      }
                      else if(Close[0]== max70T && max70T<max80T)
                      {
                      mymaxVal=70;
                      }
                      else if(Close[0]== max80T && max80T<max90T)
                      {
                      mymaxVal=80;
                      }
                      else if(Close[0]== max90T && max90T<max100T)
                      {
                      mymaxVal=90;
                      }

                      else if(Close[0]== max100T)
                      {
                      mymaxVal=100;
                      }

                      else
                      {
                      mymaxVal= 0;
                      Print(Time[0].ToString("yyyy/MM/dd")+"mymaxval is :"+mymaxVal);
                      }


                      //Logic for plots..Green if above 0(New Highs) and red if below Zero( New Lows)
                      DaysLow[0]=myminVal;
                      DaysHigh[0]=mymaxVal;

                      if(myminVal< 0)
                      PlotBrushes[0][0] = Brushes.Red;
                      else if(mymaxVal>0)
                      PlotBrushes[0][0] = Brushes.Green;
                      else
                      PlotBrushes[0][0] = Brushes.Gray;


                      Print(Time[0].ToString("yyyy/MM/dd"));
                      Print(Value[0]);
                      Print(Value[1]);
                      Print(Close[0]);
                      }

                      public Series<double> DaysLow
                      {
                      get { return Values[0]; }
                      }

                      public Series<double> DaysHigh
                      {
                      get { return Values[1]; }
                      }


                      }
                      }

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by CortexZenUSA, Today, 12:53 AM
                      0 responses
                      1 view
                      0 likes
                      Last Post CortexZenUSA  
                      Started by CortexZenUSA, Today, 12:46 AM
                      0 responses
                      1 view
                      0 likes
                      Last Post CortexZenUSA  
                      Started by usazencortex, Today, 12:43 AM
                      0 responses
                      5 views
                      0 likes
                      Last Post usazencortex  
                      Started by sidlercom80, 10-28-2023, 08:49 AM
                      168 responses
                      2,265 views
                      0 likes
                      Last Post sidlercom80  
                      Started by Barry Milan, Yesterday, 10:35 PM
                      3 responses
                      11 views
                      0 likes
                      Last Post NinjaTrader_Manfred  
                      Working...
                      X