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

Multiple Time Frame Issue

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

    Multiple Time Frame Issue

    I should state up front that the totality of my coding experience consists of simple modifications to three NT8 indicators (and even one of those required outside intervention). Therefore, if I'm missing something obvious, please excuse my ignorance.

    Thanks to AlanP, I was able to create this ATR which uses an SMA for calculation (attached). I'm now trying to get that indicator to always use daily bars for calculation, but I'm doing something wrong. I thought I'd added the second data series correctly (see code below) and pointed my variables to it, but 2 issues keep cropping up. First, I can't compile the code as it is because three variables (double high0, double low0, and close1) are throwing an error that says indexing can't be applied to a double. If I change the values of those variables to Highs [1][0], Lows[1][0], and Closes[1][0] instead of High[1][0], Low[1][0], and Close[1][0], I can get it to compile, but the indicator is blank when I apply it to a chart.

    I'll paste my code below. Any guidance would be greatly appreciated.

    public class awoATRdaily : Indicator
    {
    private Series <double> trueRangeSeries;
    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    Description = NinjaTrader.Custom.Resource.NinjaScriptIndicatorDe scriptionATR;
    Name = "AWO ATR Daily";
    IsSuspendedWhileInactive = true;
    Period = 5;

    AddPlot(Brushes.DarkCyan, NinjaTrader.Custom.Resource.NinjaScriptIndicatorNa meATR);

    trueRangeSeries = new Series<double>(this);
    }
    else if (State==State.Configure)
    {
    AddDataSeries(BarsPeriodType.Day, 1);
    }
    }

    protected override void OnBarUpdate()
    {
    double high0 = High[1][0];
    double low0 = Low[1][0];

    if (CurrentBar == 0)
    Value[0] = high0 - low0;
    else
    {
    double close1 = Close[1][0];
    double trueRange = Math.Max(Math.Abs(low0 - close1), Math.Max(high0 - low0, Math.Abs(high0 - close1)));
    trueRangeSeries[0] = trueRange;
    Value[0] = SMA(trueRangeSeries,Period)[0];
    }
    }

    #2
    Ooops. Forgot the attachement

    Here's the original indicator I'm making modifications to in case that's helpful for reference.
    Attached Files

    Comment


      #3
      CurrentBar can be -1, which will cause your else clause to attempt assignments to non-existent objects such as Value[0]. You should make sure that CurrentBar is a valid bar. For example, you can place this as the first line of the OnBarUpdate() method:
      if (CurrentBar < 0) return ;

      Comment


        #4
        Thanks, tradesmart.

        I tried the change, but I'm still getting the same result.

        Comment


          #5
          I think I realized why I need that "s" on the end of my values. I think its because I need to access the series first and then it will look for the specified bar within the series. So, I have an indicator that compiles but still outputs nothing.

          Based on tradesmart's comment, I'm going to go back over the help guide material on bars checks. Maybe that's the root of my problem. I don't understand it at this point, but it'll give me something to focus on while I wait to see if anyone has further guidance.

          Comment


            #6
            Now I'm really stuck

            I tried to add a bars check at the beginning of the OnBarUpdate method to make sure the indicator was reading from the daily bars, but that didn't seem to make a difference. I'm attaching the indicator in its current state if anyone cares to take a closer look.
            Attached Files

            Comment


              #7
              Hello MRToby,

              Thank you for your note.

              You should have a current bar check for both data series.

              Adding,
              Code:
              if(CurrentBars[1] <10 || CurrentBars[0] <10) return;
              On the first line under OnBarUpdate, above,

              Code:
              if (BarsInProgress == 1);
              Should resolve the issue.

              Please let us know if you need further assistance.
              Alan P.NinjaTrader Customer Service

              Comment


                #8
                Give the attached script a try to see if it does what you'd like.

                It prints frequently to the output window so you can see the values for validation purposes.

                More info here: http://ninjatrader.com/support/helpG...gSeriesObjects
                Attached Files

                Comment


                  #9
                  OK! Now this is getting good :-) we're not there yet, but I can see light at the end of the tunnel. I'll explain more in a second, but first…

                  Tradesmart, thanks so much for taking the time to code that modification for me. I'm trying to work my way through it so I can understand the logic better. Parts of it are still a bit over my head, but I'm beginning to understand it more clearly.

                  Alan, once again you've come through. I changed that one line of code and suddenly I had a plot. However, that has revealed some other issues.

                  1. I have to load a minimum of 15 days on my intraday chart in order to plot a value for the current day. I like to be able to see values for the last 10 days, which means I really have to load 25 days on my intraday chart. I guess I can live with that, but if there's a way to tighten that up a little bit by changing something, I'd appreciate any advice.

                  2. I discovered an error in my original indicator. I meant for the period (default is 5) to reference bars 1–5, but it appears to be referencing bars 0–4 which is throwing off my math. I'm going to see if I can figure out how to change that, but if you have a quick answer, let me know.

                  3. There are a couple other anomalous things, but I won't know for sure until I fix the math issue.

                  Thank you both so much! I can't tell you how much I appreciate your time and instruction.

                  Comment


                    #10
                    In my sample code, I set BarsRequiredToPlot = 5 in SetDefaults and then in OnBarUpdate there's a test to make sure that both data series have enough bars to meet that requirement. I did this instead of hard-coding 10, as Alan had shown earlier. Which BarsInProgress you choose will determine how often the plots appear.
                    Code:
                    protected override void OnBarUpdate()
                    {
                        if (CurrentBars[0] <= BarsRequiredToPlot || CurrentBars[1] <= BarsRequiredToPlot) return;
                    //  if (BarsInProgress == 1) // if you want to plot only at each day's close
                        if (BarsInProgress == 0) // if you want to plot at the close of each intraday bar
                        { ...
                    You can edit the BarsRequiredToPlot as you see fit. Of course, to get a valid SMA, you will need at least its Period number of bars.

                    Comment


                      #11
                      Almost There

                      Thanks tradesmart. I was wondering how the two approaches related. I'm going to use BarsRequiredToPlot since that will be more obvious to me if I come back to mess with this in the future for any reason.

                      2 other things are confusing me:

                      1. My math was coming out all wrong when I created my trueRangeSeries double under State.SetDefaults like this:

                      trueRangeSeries = new Series<double>(this);
                      }
                      else if (State==State.Configure)
                      {
                      AddDataSeries(BarsPeriodType.Day, 1);
                      }
                      but the method you used of doing it under State.Historical and loading it up with a random indicator caused everything to work out right. That's the part I'm still trying to get my head around.

                      2. Even though BarsRequiredToPlot is set to 5, nothing will plot unless I have 9 days of data loaded, and, even then, the current day value will be incorrect. If I load 15 days, the current day value is correct, but there are values for several preceding days that are not correct. I assume that's because the SMA is calculating with some empty values included, but I don't know for sure. How do I get those inaccurate values to stop outputting, and why do I have to load so many days of data if the BarsRequiredToPlot is only set at 5? Shouldn't that mean I get an accurate value for the current day if I load 6 days of data on my intraday chart?

                      Comment


                        #12
                        Update

                        Here's the current version.

                        I created my Series object like this:
                        else if (State == State.Historical)
                        {
                        trueRangeSeries = new Series<double>(BarsArray[1]);
                        }
                        instead of
                        else if (State == State.Historical)
                        {
                        trueRangeSeries = new Series<double>(SMA(BarsArray[1], 50));
                        }
                        because it seems cleaner and more efficient to my novice brain. If that's going to give me trouble for some reason, just let me know and I can change it back. So far, it doesn't seem to impact the calculations negatively.

                        I do still have this problem though:
                        2. Even though BarsRequiredToPlot is set to 5, nothing will plot unless I have 9 days of data loaded, and, even then, the current day value will be incorrect. If I load 15 days, the current day value is correct, but there are values for several preceding days that are not correct. I assume that's because the SMA is calculating with some empty values included, but I don't know for sure. How do I get those inaccurate values to stop outputting, and why do I have to load so many days of data if the BarsRequiredToPlot is only set at 5? Shouldn't that mean I get an accurate value for the current day if I load 6 days of data on my intraday chart?
                        Attached Files

                        Comment


                          #13
                          Hello MRToby,

                          In the support department at NinjaTrader we do not create, debug, or modify code for our clients. This is so that we can maintain a high level of service for all of our clients as well as our partners.

                          You can also contact a professional NinjaScript Consultants who would be eager to create or modify this script at your request or assist you with your script. Please let me know if you would like our business development follow up with you with a list of professional NinjaScript Consultants who would be happy to create this script or any others at your request.

                          Please let us know if you need further assistance.
                          Alan P.NinjaTrader Customer Service

                          Comment


                            #14
                            Sorry

                            Sorry Alan. Didn't mean to cross any boundaries. I'm trying to learn to code my own indicators and I'm here trying to learn. It just helps me to have practical things to focus my questions around rather than asking general conceptual questions.

                            Comment


                              #15
                              2. Even though BarsRequiredToPlot is set to 5, nothing will plot unless I have 9 days of data loaded, and, even then, the current day value will be incorrect. If I load 15 days, the current day value is correct, but there are values for several preceding days that are not correct. I assume that's because the SMA is calculating with some empty values included, but I don't know for sure. How do I get those inaccurate values to stop outputting, and why do I have to load so many days of data if the BarsRequiredToPlot is only set at 5? Shouldn't that mean I get an accurate value for the current day if I load 6 days of data on my intraday chart?
                              The problem is due to the fact that SMA needs data from trueRangeSeries that does not exist for the first 5 bars. So, instead of waiting to populate trueRangeSeries simultaneously with the SMA, you need to populate it earlier, as soon as you have its required number of bars, which is 2 (prior and current). Something like this ought to get you going:
                              Code:
                              if (CurrentBars[1] > 0
                                 ...calculate trueRange and assign to trueRangeSeries[0]
                              then
                              Code:
                              if (CurrentBars[1] >= BarsRequiredToPlot)
                              ... Value[0] = SMA ...

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by algospoke, Today, 06:40 PM
                              0 responses
                              10 views
                              0 likes
                              Last Post algospoke  
                              Started by maybeimnotrader, Today, 05:46 PM
                              0 responses
                              7 views
                              0 likes
                              Last Post maybeimnotrader  
                              Started by quantismo, Today, 05:13 PM
                              0 responses
                              7 views
                              0 likes
                              Last Post quantismo  
                              Started by AttiM, 02-14-2024, 05:20 PM
                              8 responses
                              168 views
                              0 likes
                              Last Post jeronymite  
                              Started by cre8able, Today, 04:22 PM
                              0 responses
                              10 views
                              0 likes
                              Last Post cre8able  
                              Working...
                              X