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

Number of bars in an interval

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

    Number of bars in an interval

    I am trying to find the number of bars between the current one and a specified date/time in the past. Assuming mainTrendTime has the specified date/time, here is my expression:

    mainTrendLength = Bars.GetBar(DateTime.Now) - Bars.GetBar(mainTrendTime) + 1;

    This works fine for a daily chart (I have not tried any other granularity yet) but it fails for a Market Analyzer. In the MA:
    • DateTime.Now works as expected
    • Bars.GetBar(DateTime.Now) fails with no message in the output window

    My questions:
    • Is there a better way to do what I need to do?
    • How can I make the function work in a Market Analyzer?

    Thanks,
    EV

    #2
    Hello EV,

    Thank you for your post.

    In the Market Analyzer we would need to set the # of bars to look back to a large enough number to encompass the search. Please right in your Market Analyzer > select Properties > change the # of bars to look back to the desired number. You will also need to ensure the Indicators Column is looking back enough bars as well by right clicking in the Market Analyzer > selecting Columns and your Indicator's Column.

    For information on the Market Analyzer please visit the following link: http://www.ninjatrader.com/support/h...t_analyzer.htm

    Comment


      #3
      Originally posted by ETFVoyageur View Post
      I am trying to find the number of bars between the current one and a specified date/time in the past. Assuming mainTrendTime has the specified date/time, here is my expression:

      mainTrendLength = Bars.GetBar(DateTime.Now) - Bars.GetBar(mainTrendTime) + 1;

      This works fine for a daily chart (I have not tried any other granularity yet) but it fails for a Market Analyzer. In the MA:
      • DateTime.Now works as expected
      • Bars.GetBar(DateTime.Now) fails with no message in the output window

      My questions:
      • Is there a better way to do what I need to do?
      • How can I make the function work in a Market Analyzer?

      Thanks,
      EV
      If you are seeking the span from the current bar, then why are you not using :
      Code:
      mainTrendLength = CurrentBar - Bars.GetBar(mainTrendTime) + 1;
      ?
      What you have written will have unpredictable results, as DateTime.Now is the time on the computer clock, and really has little to do with the time on the chart, except in realtime.

      Comment


        #4
        Patrick,

        Thank you for your suggestions. I checked and that figure was already big enough. I must say that I find the look-back settings confusing. I understand the need to have enough, but not too much, data for the sake of efficiency. My concerns are:

        First, using hard-coded numbers does not work well when an indicator is dynamic. In that case you have to just set big enough numbers to cover any eventuality. That would seem to defeat the original purpose.

        The second is that the concept is implemented in a way that I find confusing. I do not have a good conceptual grasp of why three settings are involved, or exactly what each does, so I just set them all big enough and pray. That is especially true since two of them have exactly the same label.
        • # of bars to look back (Market Analyzer | Properties...)
        • # of bars to look back (indicator configuration)
        • Maximum bars look back (indicator configuration)

        =====
        Thank you also for the link. The link points to a description of how to use the Market Analyzer.

        My question, however, is about why my custom indicator behaves differently in Market Analyzer than it does in a chart -- Bars.GetBar() works in the chart, but not in the Market Analyzer -- and how to implement the functionality I need so that it works properly in both places. The link does not point to any programming information that I noticed (other than F5 for refresh).

        Did I miss what you intended me to look at?

        --EV

        Comment


          #5
          Koganam,

          Thank you for your suggestion.

          First my goal -- I want to set the period for my indicator to the number of bars to get from the user-specified date/time to the most recent date/time available. I am just allowing the user to set the period for my indicator by calendar instead of a fixed number of bars. Doing it this way means the user can set the indicator to always be from the beginning of the current trend, or since the last market low point. The indicator will adjust with the passage of time (rather than the user having to continually reset the number of bars manually).
          • That period will be the same for all bars on the chart
          • That period changes only when the current date/time changes (e.g. tomorrow) or when the user changes the specified date/time.
          • Given that, I do not see how CurrentBar is relevant -- that changes with every bar on the chart. (Inspired by your suggestion, I have switched to using "Count", though.)

          Second, I believe my problem is that even if I could adopt your suggestion I would still depend on Bars.GetBar() -- and as best I can see that works when the indicator is in a chart, but not when the indicator is used for a column in a Market Analyzer. I need a solution that works for both. Specifically, the following statement works in my chart, but not in my Market Analyzer. How can I do the same thing in a Market Analyzer?

          int i = Bars.GetBar(mainTrendTime);
          --EV
          Last edited by ETFVoyageur; 01-27-2014, 06:45 AM.

          Comment


            #6
            I believe that my problem comes from trying to access the Bars object at a time it is not valid.

            How can I tell whether or not it is safe to access the Bars object?

            Thanks,
            EV

            Comment


              #7
              Originally posted by ETFVoyageur View Post
              I believe that my problem comes from trying to access the Bars object at a time it is not valid.

              How can I tell whether or not it is safe to access the Bars object?

              Thanks,
              EV
              Code:
              if (Bars == null) return; //as an escape filter
              or
              Code:
              if (Bars != null){//yada, yada, yada} //as a processing filter
              You might also want to check if ChartControl is null.

              Comment


                #8
                Hello EV,

                Thank you for your response.

                Would you be comfortable allowing me to test this indicator on my end? If so please either attach the indicator to your response or please send the file to support[at]ninjatrader[dot]com with 'ATTN: Patrick - 1002854' in the subject line and a reference to this thread in the body of the e-mail: http://www.ninjatrader.com/support/f...ad.php?t=63511

                Comment


                  #9
                  OK -- I believe I have it solved as well as NT permits ... it is functionally correct, although there are some cosmetic issues due to NT architecture (AFAIK).

                  The underlying problem is that I want to give the user the option to specify the computational range by giving a start date/time instead of a number of bars. That allows the indicator to automatically adjust itself so that it always computes from the user-specified date. The thought is that the user could specify the last market inflection point, or the start of a security's current trend. This would be an improvement over the user needing to manually change the number of bars with each new bar that comes in.

                  That means the indicator needs to be able to convert the date/time range between the date/time the user specified and now into a number of bars. The obvious way to do that is to use the indicator's own Bars object. [If anyone knows a better way, now would be a good time to speak up.]

                  The operational problem is that I override ToString() so that I can supply a more concise and meaningful signature -- which means ToString() needs access to the previously mentioned conversion -- in short, it needs access to the Bars object. And that is the problem -- NT can, and does, call ToString() before Bars is valid. The difference between my indicator's behavior in charts and in a Market Analyzer was due to the simple fact that MA and Chart have different calling patterns for ToString(). So I am left with a partial, but probably acceptable, solution:

                  1) I am fine computationally -- Bars is available for any real indicator computations. That has never been the problem. The entire problem is with when ToString() gets called.

                  2) ToString() works fine for charts because by the time it is called to get the legend for the indicator pane Bars is available. Verifying that (Bars!=null) works fine for that.

                  3) ToString() does not work properly for such things as the legend in the "Columns" dialog in the Market Analyzer -- Bars is not valid when ToString() is called for that purpose. My hack is to detect (Bars==null) and substitute the actual date/time string (instead of the number of bars). Clumsy and not very nice, but it is only cosmetic -- and I do not see how to do it any better with the current NT architecture. [If anyone knows a better way, please speak up now.] (Note: I realize you could make an argument that showing the data is a better choice anyway when Bars==null -- that is a matter of opinion and NT architecture should not force the choice.)

                  Bottom line is that the indicator is now functional in both environments. That is good enough for my own use. If I were a commercial, selling the indicator, I'd be pretty unhappy at the cosmetic problem -- it might look like a sign of poor quality to my customers, and would surely get bug reports.

                  Further note: if the string that should be displayed contains dynamic information, as in my case (# of bars derived from a date/time) then the current NT architecture of calling ToString() without Bars being valid is logically flawed. I understand the performance point, but ToString() is not called often enough for performance to matter.

                  --EV
                  Last edited by ETFVoyageur; 01-27-2014, 01:33 PM.

                  Comment


                    #10
                    Originally posted by NinjaTrader_PatrickH View Post
                    Hello EV,

                    Thank you for your response.

                    Would you be comfortable allowing me to test this indicator on my end? If so please either attach the indicator to your response or please send the file to support[at]ninjatrader[dot]com with 'ATTN: Patrick - 1002854' in the subject line and a reference to this thread in the body of the e-mail: http://www.ninjatrader.com/support/f...ad.php?t=63511
                    Patrick,

                    Does my summary handle what you wanted to know, or is there more that you are looking for? (We crossed in the mail -- I did not see y our posting until I made mine.)

                    --EV

                    Comment


                      #11
                      I hope someone can point me in the right direction.

                      My new indicator works, as previously detailed. It functions fine in a Market Analyzer. There is just one little thing -- when I configure to use the date/time I cannot save the template! The functionality works fine -- it determines the correct number of bars and uses that value. The only problem I know of is that I cannot save the template if this is turned on (I can save it fine as long as this is off).

                      I fully expect that it is my problem, but I am at a loss for how to even go looking for it. I have my trace debug stuff turned on, and I cannot see that any of the usual entry points are being called (Initialize(), ToString(), etc).

                      I look in the trace file and sure enough there is a problem -- but I cannot interpret what it says in any useful way. Here it is, in case it means more to one of you:

                      Code:
                      2014-01-27 13:50:39:689 in OnUnhandledThreadException
                      2014-01-27 13:50:39:692 *************** unhandled exception trapped ***************
                      2014-01-27 13:50:39:692 There was an error generating the XML document.
                      2014-01-27 13:50:39:692    at System.Xml.Serialization.XmlSerializer.Serialize(XmlWriter xmlWriter, Object o, XmlSerializerNamespaces namespaces, String encodingStyle, String id)
                         at System.Xml.Serialization.XmlSerializer.Serialize(XmlWriter xmlWriter, Object o)
                         at NinjaTrader.Cbi.Serializer.Serialize()
                         at NinjaTrader.MarketAnalyzer.UI.MarketAnalyzerControl.SaveToXml(XmlDocument document, XmlElement element, Boolean asTemplate)
                         at NinjaTrader.MarketAnalyzer.TemplateForm.bntOk_Click(Object sender, EventArgs e)
                         at System.Windows.Forms.Control.OnClick(EventArgs e)
                         at System.Windows.Forms.Button.OnClick(EventArgs e)
                         at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
                         at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
                         at System.Windows.Forms.Control.WndProc(Message& m)
                         at System.Windows.Forms.ButtonBase.WndProc(Message& m)
                         at System.Windows.Forms.Button.WndProc(Message& m)
                         at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
                         at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
                         at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
                      That's all the information I have. Any help on interpreting it, or suggestion for where else to look for information would be appreciated.

                      I understand it is unable to serialize ... but I do not know why. I guess I'll just have to go at it brute force, unless one of you can give me some info on what to look for.

                      Thanks,
                      EV
                      Last edited by ETFVoyageur; 01-27-2014, 04:12 PM.

                      Comment


                        #12
                        OK ... when at first you do not succeed, get a bigger hammer.

                        I did not realize that NT would try to serialize all properties unless explicitly told not to. Some non-parameter properties had the same problem as discussed earlier -- they used the Bars object. They should not have been being serialized, and when I turned that off all became fine.

                        Saving the template now works. I hope that is the last of the problems ... my worry is that I do not know enough to verify everything that I should .... Charts and Market Analyzers can now have their templates loaded and saved with the new indicator. Once loaded they function correctly. Sounds like a good start at least. Any other likely gotcha's?

                        --EV

                        Comment

                        Latest Posts

                        Collapse

                        Topics Statistics Last Post
                        Started by RDTrader16, Today, 10:19 PM
                        0 responses
                        5 views
                        0 likes
                        Last Post RDTrader16  
                        Started by gemify, 03-08-2023, 08:02 AM
                        9 responses
                        148 views
                        0 likes
                        Last Post culpepper  
                        Started by elirion, Today, 10:03 PM
                        0 responses
                        2 views
                        0 likes
                        Last Post elirion
                        by elirion
                         
                        Started by RaddiFX, Today, 09:55 PM
                        0 responses
                        9 views
                        0 likes
                        Last Post RaddiFX
                        by RaddiFX
                         
                        Started by Trader146, 03-29-2024, 01:22 PM
                        4 responses
                        27 views
                        0 likes
                        Last Post Trader146  
                        Working...
                        X