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

How to Get Daily Range?

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

    How to Get Daily Range?

    Hi,

    Maybe, this question was asked previously and I have also searched the thread but without much success. A help would be appreciated.

    I would like to calculate only the Daily Range, for that I need to know the High and Low price. I'm writing the following code and trying two methods. Which one is more efficient? I find Range function is cleaner but throws an error. Am I doing something wrong?

    for (int Loop = 1; Loop <= 10; Loop++) {
    if (Bars.GetDayBar(Loop) != null) {
    if (Bars.GetDayBar(Loop).High > Bars.GetDayBar(Loop).Low) { //bear day
    _ADR = Bars.GetDayBar(Loop).High - Bars.GetDayBar(Loop).Low;
    }
    elseif (Bars.GetDayBar(Loop).High < Bars.GetDayBar(Loop).Low) { //bull day
    _ADR = Bars.GetDayBar(Loop).Low - Bars.GetDayBar(Loop).High;
    }


    _DebugStr =
    "#" + Loop.ToString() + " Date: " + Bars.GetDayBar(Loop).Time.ToString("DD-MMM-YYYY") + " High:" + Bars.GetDayBar(Loop).High.ToString() +
    " Low:" + Bars.GetDayBar(Loop).Low.ToString();

    Print(_DebugStr);


    _DebugStr = Range(Closes[
    1])[Loop].ToString();
    //throws an error Error on calling 'OnBarUpdate' method for indicator 'IndicatorName' on bar 24: Index was outside the bounds of the array.
    Print(_DebugStr);
    }
    }

    Also, I would like to know about the possibility of writing some code based on Session Change instead of OnBarUpdate() event? Because, my calculation is based on Daily Bar and I doesn't need to calculate on every tick. If this is not possible any other ideas would be appreciated. I may be plotting this indicator on 5 minute chart or 1 minute.

    Cheers
    YFx

    #2
    Originally posted by yameen.forex View Post
    Hi,

    Maybe, this question was asked previously and I have also searched the thread but without much success. A help would be appreciated.

    I would like to calculate only the Daily Range, for that I need to know the High and Low price. I'm writing the following code and trying two methods. Which one is more efficient? I find Range function is cleaner but throws an error. Am I doing something wrong?

    for (int Loop = 1; Loop <= 10; Loop++) {
    if (Bars.GetDayBar(Loop) != null) {
    if (Bars.GetDayBar(Loop).High > Bars.GetDayBar(Loop).Low) { //bear day
    _ADR = Bars.GetDayBar(Loop).High - Bars.GetDayBar(Loop).Low;
    }
    elseif (Bars.GetDayBar(Loop).High < Bars.GetDayBar(Loop).Low) { //bull day
    _ADR = Bars.GetDayBar(Loop).Low - Bars.GetDayBar(Loop).High;
    }


    _DebugStr =
    "#" + Loop.ToString() + " Date: " + Bars.GetDayBar(Loop).Time.ToString("DD-MMM-YYYY") + " High:" + Bars.GetDayBar(Loop).High.ToString() +
    " Low:" + Bars.GetDayBar(Loop).Low.ToString();

    Print(_DebugStr);


    _DebugStr = Range(Closes[
    1])[Loop].ToString();
    //throws an error Error on calling 'OnBarUpdate' method for indicator 'IndicatorName' on bar 24: Index was outside the bounds of the array.
    Print(_DebugStr);
    }
    }

    Also, I would like to know about the possibility of writing some code based on Session Change instead of OnBarUpdate() event? Because, my calculation is based on Daily Bar and I doesn't need to calculate on every tick. If this is not possible any other ideas would be appreciated. I may be plotting this indicator on 5 minute chart or 1 minute.

    Cheers
    YFx
    The Range() is cleaner and probably faster. You need to validate the DataSeries though,

    Code:
     
    if (CurrentBars[1] > -1) _DebugStr = Range(Closes[1])[Loop].ToString();

    Comment


      #3
      In your first example, isn't High always > Low by definition?

      In your second example, because you are attempting to inspect a Close value of 1 bar ago, Ninja will throw an error on bar 0, since there is no bar before it. You need something like this in OnBarUpdate()
      Code:
      if (CurrentBars[0] < 1) return;

      Comment


        #4
        Thanks for the response, Konganam.

        I'm still unable to print the values, and am getting the error (Commented in the code). This is how my code looks like now.

        string _DebugStr = string.Empty;

        for (int Loop = 1; Loop <= 10; Loop++) {
        if (Bars.GetDayBar(Loop) != null) {
        if (CurrentBars[1] > -1)
        _DebugStr = Range(Closes[
        1])[Loop].ToString();

        //Error on calling 'OnBarUpdate' method for indicator 'IndicatorName' on bar 24: Index was outside the bounds of the array.
        Print(_DebugStr);
        }
        }


        Can you please take a look.. I just need to know the range of the last N days based on Daily candle. I should be able to print in any Timeframe chart.
        Last edited by yameen.forex; 11-25-2012, 05:32 AM.

        Comment


          #5
          Originally posted by yameen.forex View Post
          Thanks for the response, Konganam.

          I'm still unable to print the values, and am getting the error (Commented in the code). This is how my code looks like now.

          string _DebugStr = string.Empty;

          for (int Loop = 1; Loop <= 10; Loop++) {
          if (Bars.GetDayBar(Loop) != null) {
          if (CurrentBars[1] > -1)
          _DebugStr = Range(Closes[
          1])[Loop].ToString();

          //Error on calling 'OnBarUpdate' method for indicator 'IndicatorName' on bar 24: Index was outside the bounds of the array.
          Print(_DebugStr);
          }
          }


          Can you please take a look.. I just need to know the range of the last N days based on Daily candle. I should be able to print in any Timeframe chart.
          Maybe I am not understanding what you are trying to achieve? Do you have more than one bar series?

          Comment


            #6
            I need to get the range of Daily Candle for the last 8 days. How to achieve this?

            Comment


              #7
              Originally posted by yameen.forex View Post
              I need to get the range of Daily Candle for the last 8 days. How to achieve this?
              One way is to add a daily bars data stream and take the Range off of it, which is why I was asking if you have more than one bar series. You can loop through this if you want.

              Code:
              [FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]protected [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]override [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]void[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] Initialize()[/FONT]
              [FONT=Courier New]{[/FONT]
               [FONT=Courier New]Add(PeriodType.Day, [/FONT][FONT=Courier New][COLOR=#800080][FONT=Courier New][COLOR=#800080]1[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]);[/FONT]
              [FONT=Courier New]}[/FONT]
              Code:
              [FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]protected [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]override [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]void[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] [COLOR=black]OnBarUpdate()[/COLOR][/FONT]
              [FONT=Courier New][COLOR=black]{[/COLOR][/FONT]
              if[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] (CurrentBars[[/FONT][FONT=Courier New][COLOR=#800080][FONT=Courier New][COLOR=#800080]1[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]] > -[/FONT][FONT=Courier New][COLOR=#800080][FONT=Courier New][COLOR=#800080]1[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] && BarsInProgress == [/FONT][FONT=Courier New][COLOR=#800080][FONT=Courier New][COLOR=#800080]1[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]) Print([/FONT][FONT=Courier New][COLOR=#800000][FONT=Courier New][COLOR=#800000]" Daily Range: "[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] + Range(BarsArray[[/FONT][FONT=Courier New][COLOR=#800080][FONT=Courier New][COLOR=#800080]1[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]])[[/FONT][FONT=Courier New][COLOR=#800080][FONT=Courier New][COLOR=#800080]0[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]]);[/FONT]
              [FONT=Courier New]}[/FONT]
              The other way is to use the GetDayBar() as you have used it.

              Comment


                #8
                Thanks for your answer.

                Is there a way, not to perform calculation on every tick but calculate only on session change over or something like that? Previous day's candle values will not change. I think calculation on every tick would add up burden to the CPU. Any thoughts/ideas would be greatly apprecaited!

                Comment


                  #9
                  Originally posted by yameen.forex View Post
                  Thanks for your answer.

                  Is there a way, not to perform calculation on every tick but calculate only on session change over or something like that? Previous day's candle values will not change. I think calculation on every tick would add up burden to the CPU. Any thoughts/ideas would be greatly apprecaited!
                  NT natively provides that. Look up FirstTickOfBar and FirstBarOfSession.

                  Code:
                   
                  if (FirstTickOfBar && Bars.FirstBarOfSession)
                  {
                  //Do all the stuff that you want to do only once per session
                  }

                  Comment


                    #10
                    Thank you very much again. I will try this and post the results.

                    Cheers

                    Comment


                      #11
                      The Foreign exchange market goes based on international economic events and money goes. Just because the average retail store day trader can now only trade on low leverage instead of high leverage
                      All markets have boring times and wild times.


                      NTEP scales
                      Last edited by asto222; 12-15-2012, 08:45 AM.

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by andrewtrades, Today, 04:57 PM
                      1 response
                      5 views
                      0 likes
                      Last Post NinjaTrader_Manfred  
                      Started by chbruno, Today, 04:10 PM
                      0 responses
                      3 views
                      0 likes
                      Last Post chbruno
                      by chbruno
                       
                      Started by josh18955, 03-25-2023, 11:16 AM
                      6 responses
                      436 views
                      0 likes
                      Last Post Delerium  
                      Started by FAQtrader, Today, 03:35 PM
                      0 responses
                      7 views
                      0 likes
                      Last Post FAQtrader  
                      Started by rocketman7, Today, 09:41 AM
                      5 responses
                      19 views
                      0 likes
                      Last Post NinjaTrader_Jesse  
                      Working...
                      X