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

Day of the week & Two days ago High & Low

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

    Day of the week & Two days ago High & Low

    Hello,

    I'm trasitioning from Metastock to NT and slowly adapting to the syntax's big differences.

    I'm attempting to create an indicator that will calculate and plot the High and Low from two days ago and also calculate and plot the Monday High and Low.

    My Metastock script for this is:
    {Start Of Day}
    SOD := DayOfMonth()<>Ref(DayOfMonth(),-1);
    Hi := HighestSince(1,SOD,H);
    Lo := LowestSince(1,SOD,L);

    {2 Days ago High/Low}
    B4YestHigh := ValueWhen(2,SOD,Ref(Hi,-1));
    B4YestLow := ValueWhen(2,SOD,Ref(Lo,-1));

    {Monday High/Low}
    MonHigh := ValueWhen(1,DayOfWeek()=1,Hi);
    MonLow := ValueWhen(1,DayOfWeek()=1,Lo);

    After much searching & reading, I gather that for NT I should use the following functions:
    Bars.Get.DayBar(2).High
    Bars.Get.DayBar(2).Low
    DayOfWeek.Monday

    But I'm getting lost with these NT functions and NT syntax as I try to recreate the same Metastock script.

    Any help is greatly appreciated.

    #2
    Hello 2Look4me,

    Thank you for your post.

    I utilized CurrentDayOHL (http://www.ninjatrader.com/support/h...nt_day_ohl.htm) and PriorDayOHLC (http://www.ninjatrader.com/support/h...r_day_ohlc.htm) in the example below:
    Code:
    			// Calculate The Bar Number for Tuesday
    			if(Time[0].DayOfWeek == DayOfWeek.Tuesday)
    			{
    				int bar = CurrentBar;
    			}
                            // High and Low Since Start Of Day
    			double highSOD = CurrentDayOHL().CurrentHigh[0];
    			double lowSOD = CurrentDayOHL().CurrentLow[0];
    			// High and Low for 2 Days Ago
    			double high2DA = PriorDayOHLC().PriorHigh[Bars.BarsSinceSession + 1];
    			double low2DA = PriorDayOHLC().PriorLow[Bars.BarsSinceSession + 1];
    			// High and Low for Monday
    			double highMON = PrioDayOHLC().PriorHigh[CurrentBar - bar];
    			double lowMON = PriorDayOHLC().PriorLow[CurrentBar - bar];

    Comment


      #3
      Patrick, thanks for rewriting the MS script to NT script.
      For the following code I'm getting an error.

      // High and Low for Monday
      double highMON = PriorDayOHLC().PriorHigh[CurrentBar - Bar];
      double lowMON = PriorDayOHLC().PriorLow[CurrentBar - Bar];

      Error: NinjaTrader.Data.Bar is a type but is used like a variable

      The syntax seems to be correct as well as being a double var, so why the error CS0118?

      Comment


        #4
        Hello 2Look4me,

        The variable needs to be "bar" not "Bar", or you could name it something else.

        Comment


          #5
          Hi Patrick,

          A) If the script already has
          if (Close[0] > Close[Math.Min(CurrentBar, 1)])
          return;

          But why I still get an error in the log tab?:
          Error on calling "OnBarUpdate" method for indicator "aaTest" on bar 81: You are accessing an index with a value that is invalid since its out of range. I.E. accessing a series [barsAgo] with a value of 5 when there are only 4 bars on the chart.

          B) I get the compile error: The name "bar" does not exist in the current context. Why if int bar = CurrentBar is already defined?
          double highMON = PriorDayOHLC().PriorHigh[CurrentBar - bar];
          double lowMON = PriorDayOHLC().PriorLow[CurrentBar - bar];

          C) Are the variables highSOD and lowSOD used in the sript calculations?
          double highSOD = CurrentDayOHL().CurrentHigh[0];
          double lowSOD = CurrentDayOHL().CurrentLow[0];

          Thanks for all your help.

          Comment


            #6
            Hello,

            Thank you for the questions.

            Error on calling "OnBarUpdate" method for indicator "aaTest" on bar 81: You are accessing an index with a value that is invalid since its out of range. I.E. accessing a series [barsAgo] with a value of 5 when there are only 4 bars on the chart.
            This message is telling you exactly what is happening. you are accessing an index that does not yet exist.

            Your first check
            Code:
            if (Close[0] > Close[Math.Min(CurrentBar, 1)])
            would not be a good way to make sure there are enough bars, you can instead use the CurrentBar alone to verify there are enough bars like the following:
            Code:
            if(CurrentBar < 10) return;
            you would need to verify all of your items that have index locations to see if 10 will cover how far you are going back, you would want to adjust this number to the amount of bars that need to be on the chart before you start your processing.

            I get the compile error: The name "bar" does not exist in the current context.
            Where have you defined the variable bar? this would need to be either in your Variables section for it to have script scope or declared in the scope of where it is being used.

            Here is an example that would cause the error

            Code:
            if(something == somethingElse)
            {
            int bar = 0;
            }
            
            if(otherThing == something)
            {
            Print(bar);
            }
            In this example bar is declared in the first if statement giving it the scope of just the first if statement. for this to be used in both it would need to be declared outside of both if statements.

            C) Are the variables highSOD and lowSOD used in the script calculations?
            double highSOD = CurrentDayOHL().CurrentHigh[0];
            double lowSOD = CurrentDayOHL().CurrentLow[0];
            I am unsure on this question, I don't see this in your prior posts can you please clarify what the question is with these?

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

            Comment


              #7
              I appreciate the detailed explanation as it gives me more insight about Ninjascript.
              This is the script I entered to calculate the High & Low from 2 days ago and the Monday High & Low

              #region Variables
              int bar;
              #endregion

              OnBarUpdate()

              // Calculate The Bar Number for Tuesday
              if(CurrentBar < 8) return;

              if(Time[0].DayOfWeek == DayOfWeek.Tuesday)
              {
              int bar = CurrentBar;
              }

              // High and Low Since Start Of Day
              double highSOD = CurrentDayOHL().CurrentHigh[0];
              double lowSOD = CurrentDayOHL().CurrentLow[0];

              // High and Low for 2 Days Ago
              double high2DA = PriorDayOHLC().PriorHigh[Bars.BarsSinceSession + 1];
              double low2DA = PriorDayOHLC().PriorLow[Bars.BarsSinceSession + 1];

              // High and Low for Monday
              double highMON = PriorDayOHLC().PriorHigh[CurrentBar - bar];
              double lowMON = PriorDayOHLC().PriorLow[CurrentBar - bar];

              A) compile error CS0135
              "bar" conflicts with the declaration "Ninja.Trader.Indicator.aaTest.bar"

              B) log tab error:
              Error on calling "OnBarUpdate" method for indicator "aaTest" on bar 8: You are accessing an index with a value that is invalid since its out of range. I.E. accessing a series [barsAgo] with a value of 5 when there are only 4 bars on the chart.

              I've tried adjusting the items that have index locations, but keep getting the same error but on different bar.

              Comment


                #8
                Hello 2Look4me,

                Thank you for your response.

                The int bar is defined twice in your example, it only needs to be defined once. For example:
                Code:
                #region Variables
                [B]private int bar = 0;[/B]
                ...
                Code:
                // Calculate The Bar Number for Tuesday
                if(Time[0].DayOfWeek == DayOfWeek.Tuesday)
                {
                [B]bar = CurrentBar;[/B]
                We also need to verify that bar is not 0 (meaning a Tuesday has not occurred) before assigning our values for Monday:
                Code:
                // High and Low for Monday
                if(bar!=0)
                {
                double highMON = PriorDayOHLC().PriorHigh[CurrentBar - bar];
                double lowMON = PriorDayOHLC().PriorLow[CurrentBar - bar];
                }
                With the use of Bars.BarsSinceSession we need to verify that there are enough bars loaded when we state Bars.BarsSinceSession + 1. So we can check for a number of sessions to complete before checking for values:
                Code:
                        #region Variables
                        private int sessionCount = 0;
                ....
                Code:
                			if(Bars.FirstBarOfSession)
                			{
                				sessionCount++;
                			}
                			if(sessionCount < 2)
                				return;
                The above would be in place of the CurrnetBar < 8 line in your current code.

                Comment


                  #9
                  Hi Patrick,

                  In following your suggestions I've made the necessary changes, but I still get a compile error at the end.

                  #region Variables

                  private int bar = 0;
                  private int sessionCount = 0;

                  #endregion

                  OnBarUpdate()

                  // Calculate The Bar Number for Tuesday
                  if(Bars.FirstBarOfSession) {
                  sessionCount++;
                  }
                  if (sessionCount < 2)
                  return;

                  if(Time[0].DayOfWeek == DayOfWeek.Tuesday)
                  {
                  bar = CurrentBar;
                  }

                  // High and Low Since Start Of Day
                  double highSOD = CurrentDayOHL().CurrentHigh[0];
                  double lowSOD = CurrentDayOHL().CurrentLow[0];

                  // High and Low for 2 Days Ago
                  double high2DA = PriorDayOHLC().PriorHigh[Bars.BarsSinceSession + 1];
                  double low2DA = PriorDayOHLC().PriorLow[Bars.BarsSinceSession + 1];

                  // High and Low for Monday
                  if (bar !=0 ) {
                  double highMON = PriorDayOHLC().PriorHigh[CurrentBar - bar];
                  double lowMON = PriorDayOHLC().PriorLow[CurrentBar - bar];
                  }

                  Compile error:
                  The name "lowMON" does not exist in the current context
                  the name "highMON" does not exist in the current context

                  I tried including a lowMON and a highMON in the variables section, but the compile errors are more.

                  Thanks in advance for all your assistance

                  Comment


                    #10
                    Hello 2Look4me,

                    Thank you for your response.

                    Please attach the .cs file to your response so we may investigate this matter further. The .cs file will be located under (My) Documents\NinjaTrader 7\bin\Custom and then Indicator for indicators or Strategy for strategies.

                    Comment


                      #11
                      I'm enclosing the file attachment. Thanks
                      Attached Files

                      Comment


                        #12
                        2Look4me, the attached version should compile fine, you would need to make sure to define the variables with class and not just local scope.
                        Attached Files
                        BertrandNinjaTrader Customer Service

                        Comment


                          #13
                          Works great! If I also want to apply the indicator to a daily chart instead of just intraday what changes are needed in the script?

                          Patrick/Bertrand, thanks for your patience, time and all your detailed suggestions. I've learned so much from this first time scripting NT from Metastock.

                          Comment


                            #14
                            2Look4me, you're welcome - for a daily chart you don't want to work with the CurrentDayOHL / PriorDayOHLC methods as those will only function on intraday data. You can instead directly access the High and Low bars series to give you the values needed, you can apply an index to those to reference data from the past (i.e. High[2]).
                            BertrandNinjaTrader Customer Service

                            Comment


                              #15
                              That's true, didn't think of it. Thanks

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by quantismo, Yesterday, 05:13 PM
                              2 responses
                              15 views
                              0 likes
                              Last Post quantismo  
                              Started by maybeimnotrader, Yesterday, 05:46 PM
                              4 responses
                              23 views
                              0 likes
                              Last Post maybeimnotrader  
                              Started by frankthearm, Today, 09:08 AM
                              6 responses
                              25 views
                              0 likes
                              Last Post frankthearm  
                              Started by adeelshahzad, Today, 03:54 AM
                              5 responses
                              33 views
                              0 likes
                              Last Post NinjaTrader_BrandonH  
                              Started by stafe, 04-15-2024, 08:34 PM
                              7 responses
                              32 views
                              0 likes
                              Last Post NinjaTrader_ChelseaB  
                              Working...
                              X