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

Strategy and Indicator superclass

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

    Strategy and Indicator superclass

    I am programming a helper class to handle logging to file which I want to do to capture my debug output and stacktraces instead of using Print() to the Output window, and to handle opening and closing the file etc.

    I would like to use various Strategy class and Indicator class variables and functions such as Name, Instrument, TickSize, BarsArray, Print etc etc.

    Is there a super class I can use so I don't have to duplicate so much code in my helper class to access that stuff?

    Second question: is logging to file going to be added to NT8?

    Thanks

    #2
    The simplest way to log this information to an external file would be through following this StreamWriter example: http://www.ninjatrader.com/support/f...ead.php?t=3475

    While you could create your own class to do this I think it would be simpler to follow this example.

    Alternatively you can also send message straight to the log file but keep in mind this can cause a performance hit: http://www.ninjatrader.com/support/h...x.html?log.htm

    In regards to NinjaTrader 8 I do not have this information available at this time.
    LanceNinjaTrader Customer Service

    Comment


      #3
      Hi Lance,
      thanks for the swift response.

      I used that StreamWriter example a while back to start logging information to file when I first required it. However I find that I have developed a lot of strategies and indicators since then and I want to wrap it all up into one class so that I can really reduce the amount of boiler-plate code I use to do that.

      Putting it in an extra class means I have one central place to change it all, so if I improve it, then all my strategies and indicators will be improved in one hit.

      It means in new code I write, I will only need to instantiate my LogHelper with the data specific to that strategy / indicator, call the .log() method in the OnBarUpdate etc when required, and then have one line in my Termination() event to clean up the LogHelper which will flush and close the file pointer.

      Reading in between the lines, it sounds like I'm going to get the usual "not supported" response - so if any non-NT forum member with a deeper knowledge can help, that'd be great.

      Comment


        #4
        You could likely create an indicator that would handle this for you. You would have to pass the needed values to the indicator and have that indicator write out to the file. The main issue I see with this is if two scripts wrote to the same file at the same time, it would cause an error. You could possibly generate a random text file name so that each indicator instance would use a different write location.

        To generate a unique string: http://www.ninjatrader.com/support/h...gyuniqueid.htm

        This is speculation on my part, I have not tested the above.

        But you are correct, going down your proposed route would be unsupported.
        LanceNinjaTrader Customer Service

        Comment


          #5
          OK thanks for confirming that.

          One of the reasons why I want the Strategy and the Indicator class is so that I can call Instrument, Indicator.Name, Indicator.PeriodType and Indicator.Period so that I can construct a file name based on the chart and the indicator names and properties. That would avoid having random file names and would make it easy to find the right log file.

          I also want to call Print("info info info") to let me have the option still of writing my logging to the Output window - the option is a parameter on the Strat or Indi that's using the LogHelper class and the choice is passed in to the constructor.

          It's almost complete, I've written the whole thing for strategies, but now I want to use it in indicators as well but of course all this info and the Print function are on a totally different object now.

          I guess I can pass in both a Strategy object and an Indicator object and test to see which is not null wherever I want access to one, but that sucks if it turns out there's a superclass.

          Comment


            #6
            There isn't one that I'm aware of. You could also try asking here: http://ninjatrader.com/support/forum...ad.php?t=22435
            LanceNinjaTrader Customer Service

            Comment


              #7
              Originally posted by adamus View Post
              OK thanks for confirming that.

              One of the reasons why I want the Strategy and the Indicator class is so that I can call Instrument, Indicator.Name, Indicator.PeriodType and Indicator.Period so that I can construct a file name based on the chart and the indicator names and properties. That would avoid having random file names and would make it easy to find the right log file.

              I also want to call Print("info info info") to let me have the option still of writing my logging to the Output window - the option is a parameter on the Strat or Indi that's using the LogHelper class and the choice is passed in to the constructor.

              It's almost complete, I've written the whole thing for strategies, but now I want to use it in indicators as well but of course all this info and the Print function are on a totally different object now.

              I guess I can pass in both a Strategy object and an Indicator object and test to see which is not null wherever I want access to one, but that sucks if it turns out there's a superclass.
              So why not simply write a method in a partial class that concatenates that information and and passes it directly (via the calling parameters) to the indicator that you write which uses the StreamWriter?

              Comment


                #8
                Originally posted by koganam View Post
                So why not simply write a method in a partial class that concatenates that information and and passes it directly (via the calling parameters) to the indicator that you write which uses the StreamWriter?
                Because the LogHelper encapsulates the writing and the choice of destination (to Indicator.Print() or to the StreamWriter). I want to avoid implementing that in every strategy or indicator that uses my LogHelper, so only have to write this to do logging:

                Code:
                logHelper.log("Done on startup - BarsReq==" + BarsRequired);
                I want to pass all the relevant info into the constructor, e.g. user parameters

                PS: just realised Kogonam - are you effectively answering my question with the negative - there is no common superclass - or are you unaware as Lance and I are?
                Last edited by adamus; 07-18-2013, 04:45 AM.

                Comment


                  #9
                  Hello adamus,

                  Both Classes Strategy and Indicator are base classes and should not have a Super Class that included both of them inside of NinjaTrader. A partial class maybe the best way to incorporate what you are trying to do this.

                  Happy to be of further assistance.
                  JCNinjaTrader Customer Service

                  Comment


                    #10
                    Maybe I'm missing your point, but what is the advantage of using a partial class when my aim is to write code operating on functions and variables that are only analagous?

                    NinjaTrader.Strategy.Strategy.Print() and NinjaTrader.Indicator.Indicator.Print() can never be interchangeable as far as I can see, if there is no superclass.

                    Comment


                      #11
                      Hello adamus,

                      Mainly this would be to split a class definition of the two classes so that you can call them in a single script.

                      While, they are both separate functions of their base Class their functionality is still the exact same, and still take in the same parameters no matter if you call Print() from a Strategy or an Indicator.

                      Happy to be of further assistance.
                      JCNinjaTrader Customer Service

                      Comment


                        #12
                        Originally posted by adamus View Post
                        Because the LogHelper encapsulates the writing and the choice of destination (to Indicator.Print() or to the StreamWriter). I want to avoid implementing that in every strategy or indicator that uses my LogHelper, so only have to write this to do logging:

                        Code:
                        logHelper.log("Done on startup - BarsReq==" + BarsRequired);
                        I want to pass all the relevant info into the constructor, e.g. user parameters

                        PS: just realised Kogonam - are you effectively answering my question with the negative - there is no common superclass - or are you unaware as Lance and I are?
                        I believe that both the Strategy and Indicator classes are base classes, so there will probably be no superclass.
                        ... so that I can call Instrument, Indicator.Name, Indicator.PeriodType and Indicator.Period so that I can construct a file name based on the chart and the indicator names and properties. That would avoid having random file names and would make it easy to find the right log file.
                        That belongs in a method in a partial class. Call it public string ChartInfo() {} to return a string which you will use as the filename for the logger to write to, and pass that name along to the logHelper class as part of the constructor, when you call it. If you use a named instance of the logHelper, you can then explicitly call its methods and identifiers directly.

                        Comment


                          #13
                          Originally posted by koganam View Post
                          That belongs in a method in a partial class. Call it public string ChartInfo() {} to return a string which you will use as the filename for the logger to write to, and pass that name along to the logHelper class as part of the constructor, when you call it. If you use a named instance of the logHelper, you can then explicitly call its methods and identifiers directly.
                          OK I can see the sense in that but I also want to encapsulate calls to Indicator.Print() or Strategy.Print() and Indicator.Log(msg, level) and Strategy.Log(msg, level)

                          I think the best way is still to hold onto the Indicator or the Strategy object and then check which one is not null to access Print and Log etc. I can't see how partial classes would help there.

                          Comment


                            #14
                            Originally posted by adamus View Post
                            OK I can see the sense in that but I also want to encapsulate calls to Indicator.Print() or Strategy.Print() and Indicator.Log(msg, level) and Strategy.Log(msg, level)
                            Encapsulate where?

                            Comment


                              #15
                              In the LogHelper class - so that I don't have to write it repeatedly in the indicators and strategies that use it.

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by suroot, 02-25-2017, 04:43 AM
                              11 responses
                              2,548 views
                              0 likes
                              Last Post Zilvercat  
                              Started by Rogers101, 05-05-2024, 11:30 AM
                              16 responses
                              50 views
                              0 likes
                              Last Post Rogers101  
                              Started by ninza33, Today, 12:31 PM
                              2 responses
                              10 views
                              0 likes
                              Last Post ninza33
                              by ninza33
                               
                              Started by Bobin, 03-12-2024, 08:51 AM
                              15 responses
                              482 views
                              0 likes
                              Last Post fiddich
                              by fiddich
                               
                              Started by Skifree, Today, 11:21 AM
                              4 responses
                              16 views
                              0 likes
                              Last Post Skifree
                              by Skifree
                               
                              Working...
                              X