Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Get & use lowest low and highest day, from a user input start time of the curernt day

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

    Get & use lowest low and highest day, from a user input start time of the curernt day

    I'm trying to code an indicator to get & use lowest low and highest day, from a user input start time of the current day. the indicator will run all through the session and replot new highest high (hh) and Lowest low (ll) as they are formed. Once I have these updating levels i can do various things, plot lines at them, alerts if crossed etc.

    I've tried the following code below. The barsAgo variable is producing the correct bars ago from the input start time, but the hh and ll produced are totally off, they sare not even in the range of the day. Can you please le tme know what is wrong with the MIN MAX statements and how I can fix it.

    amespace NinjaTrader.NinjaScript.Indicators
    {
    public class MyCustomIndicator1 : Indicator
    {
    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    Description = @"Enter the description for your new custom Indicator here.";
    Name = "MyCustomIndicator1";
    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;


    }
    else if (State == State.Configure)
    {
    }
    }

    protected override void OnBarUpdate()
    {


    int barsAgo = CurrentBar - Bars.GetBar(new DateTime (Time[0].Year, Time[0].Month, Time[0].Day, StartHour, StartMinute, 0));

    Print(barsAgo);

    double ll = MIN(Low, CurrentBar - barsAgo +1)[CurrentBar] ;
    double hh = MAX(High, CurrentBar - barsAgo +1)[CurrentBar] ;

    Print(ll);

    Print(hh);

    Draw.HorizontalLine(this, "alertlinehh", hh , Brushes.Yellow);
    Draw.HorizontalLine(this, "alertlinell", ll , Brushes.Yellow);

    }

    #region Properties

    [Range(0,23)]
    [NinjaScriptProperty]
    [Display(Name="Start hour", Description = "Enter start hour, Military time format 0 - 23", Order=1, GroupName="Parameters")]
    public int StartHour
    { get; set; }

    [Range(0, 59)]
    [NinjaScriptProperty]
    [Display(Name="Start minute", Description = "Enter start minute(s) 0 - 59",Order=2, GroupName="Parameters")]
    public int StartMinute
    { get; set; }


    #endregion

    #2
    Hello b16_aln,

    If you are trying to make a MIN/MAX for a period, you would need to exclude the CurrentBar from your syntax:

    Code:
    MIN(Low, [B]CurrentBar[/B] - barsAgo +1)[[B]CurrentBar[/B]] ;
    I believe this should instead be:

    Code:
    MIN(Low,  barsAgo)[0] ;
    using [CurrentBar] specifically would be a problem by its self, this is taking the first bar of the chart value. If you want the current value or right now in processing you would use [0] BarsAgo.

    The period being used would also not likely be correct, did you intend to use the entire bar set excluding the number of bars ago you selected? or was the intention to only use a period from the marked BarsAgo?

    I look forward to being of further assistance.

    Comment


      #3
      Thanks Jesse,

      When I do that I get the following error:

      Value of property "Period" of Ninjascript "MIN" and not in valid range between 1 and 2147483647.
      Last edited by b16_aln; 07-24-2019, 09:17 AM.

      Comment


        #4
        Hello b16_aln,

        The error would be correct, -11 is not positive and is not within the range. You need to use a positive number for the period.

        You can try reversing your subtraction or use Math.Abs() with your calculation to make an absolute value.

        Code:
        Print(barsAgo);
        Print(Math.Abs(barsAgo));
        I look forward to being of further assistance.

        Comment


          #5
          So your suggested script change which causes an error is correct? I'm lost, please explain.

          Comment


            #6
            Hello b16_aln,

            The error came because you are not using a valid value, not due to the suggestion I had made.

            -11 is not a valid period or BarsAgo, NinjaTrader works in positive values. This would be for indexes, BarsAgo or Periods.

            I look forward to being of further assistance.

            Comment


              #7
              Originally posted by NinjaTrader_Jesse View Post
              Hello b16_aln,

              If you are trying to make a MIN/MAX for a period, you would need to exclude the CurrentBar from your syntax:

              Code:
              MIN(Low, [B]CurrentBar[/B] - barsAgo +1)[[B]CurrentBar[/B]] ;
              I believe this should instead be:

              Code:
              MIN(Low, barsAgo)[0] ;
              using [CurrentBar] specifically would be a problem by its self, this is taking the first bar of the chart value. If you want the current value or right now in processing you would use [0] BarsAgo.

              The period being used would also not likely be correct, did you intend to use the entire bar set excluding the number of bars ago you selected? or was the intention to only use a period from the marked BarsAgo?

              I look forward to being of further assistance.
              Jesse, it was my was the intention to only use a period from the marked BarsAgo.

              Comment


                #8
                Originally posted by NinjaTrader_Jesse View Post
                Hello b16_aln,

                The error came because you are not using a valid value, not due to the suggestion I had made.

                -11 is not a valid period or BarsAgo, NinjaTrader works in positive values. This would be for indexes, BarsAgo or Periods.

                I look forward to being of further assistance.
                The barsAgo figure isn;t bars ago figure being printed is over 100.
                Last edited by b16_aln; 07-24-2019, 09:17 AM.

                Comment


                  #9
                  In case anyone is interested, the answer is not to use the MAX and use this instead:

                  High[HighestBar(High,barsAgo)]

                  Comment


                    #10
                    Hello b16_aln,

                    Jesse, it was my was the intention to only use a period from the marked BarsAgo.
                    Thank you for clarifying, in that case the CurrentBar does need removed. CurrentBar represents the Index of the current processing bar, this is rarely used with Indicators. In most situations you will see a Period, LookbackPeriod or BarsAgo used. These are all going to be a positive value starting from 0 and will relate to where you are in processing. For example if you are on CurrentBar 10, 0 BarsAgo would be CurrentBar 10. A Lookback period of 5 would account for the last 5 bars, looking back from bar 10.


                    The barsAgo figure isn;t -11, the bars ago figure being printed is over 100.
                    Are you referring to the print I had shown? The Print I had provided would hold no relation to the value your indicator is using, you would still need to modify that code to ensure it gets a positive value passed in for the period.

                    From the error all I can see is that a value of -11 was passed to the indicator as its Period:
                    Value of property "Period" of Ninjascript "MIN" is -11
                    Did you change the MIN indicator to take just the bars ago you are calculating?
                    Are you certain that calculation is resulting in a positive value?

                    There are a few examples of using the MIN with a Period in the following link:





                    I look forward to being of further assistance.

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                    0 responses
                    580 views
                    0 likes
                    Last Post Geovanny Suaza  
                    Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                    0 responses
                    335 views
                    1 like
                    Last Post Geovanny Suaza  
                    Started by Mindset, 02-09-2026, 11:44 AM
                    0 responses
                    102 views
                    0 likes
                    Last Post Mindset
                    by Mindset
                     
                    Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                    0 responses
                    554 views
                    1 like
                    Last Post Geovanny Suaza  
                    Started by RFrosty, 01-28-2026, 06:49 PM
                    0 responses
                    552 views
                    1 like
                    Last Post RFrosty
                    by RFrosty
                     
                    Working...
                    X