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

Print() and public const inside a class

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

    Print() and public const inside a class

    I've encountered two problems that I have been unable to solve in my searches of this forum and the MSDN site.

    1. Print() inside a class

    When debugging the code inside class 'bar' in indicator 'foo', I cannot use the Print() method. I get error CS0038, "Cannot access a non-static member of outer type 'NinjaTrader.Indicator.IndicatorBase' via nested type 'NinjaTrader.Indicator.foo.bar'.

    If I try to create an instance of IndicatorBase to use internally in class 'bar' to have access to Print(), I get error CS0144, "Cannot creat an instance of the abstract class or interface 'NinjaTrader.Indicator.IndicatorBase'.

    Is there any way to send anything to the Output window from inside a class? Use stdout or stderr with fprintf() or something?

    2. public named constants inside a class

    Inside indicator 'foo', I have a public class 'bar'. The indicator 'foo' creates several different instances of 'bar'. Class 'bar' includes several public exposed variables that can show current status as some pre-defined named constants defined in the indicator 'foo'. For example, I have this constant in 'foo':

    public const int TYPE_FORMING = 3;

    All well and good, my instances of 'bar' refer to these named constants without problems.

    I run into problems when trying to reference these public constants by name from another indicator. Say I have another indicator 'xyz' that calls 'foo' and needs access to the exposed values inside all the 'bar' instances within 'foo'. How do access the public constant by name in 'xyz'? Suppose inside foo there's an instance of bar called 'b'. Then in 'xyz' I tried for example:
    Code:
    //variables
    private foo f;
    
    protected void override OnStartUp() {
        f = foo();
    }
    
    protected void override OnBarUpdate() {
        // error CS0176, TYPE_FORMING cannot be accessed with an instance reference, qualify it with a type name instead
        if (f.b.type == f.TYPE_FORMING) {
        // etc.
        }
    }
    When attempt to correct this according to the MSDN documentation I get another error:
    Code:
        if (f.b.type == foo.TYPE_FORMING) // error CS0119, conflict with the foo() method
    I could always just use the integer values for these constants. I would prefer to use their names because it makes my code more readable.

    -Alex
    Last edited by anachronist; 12-20-2011, 01:23 PM.

    #2
    Hello,

    Thanks for the post.

    Unfortunately this falls under an area we are unable to assist on, and I dont have any recommendations for your custom class work. I would recommend using everything inside the base indicator class and not creating your own classes would be my recommendation and what we are setup for.

    -Brett

    Comment


      #3
      OK, I kind of expected that, but it was worth a shot to ask.

      It seems to defeat the purpose of having an OOP language like C# as the scripting language if the recommendation is to avoid using the features of the language.

      I guess my other option for not having Print() is to buy Visual Studio to do my debugging.

      Regarding my question #2 above, it seems I can put all my const declarations in a user defined method file, and they will be accessible to any indicator. So that problem is solved... I still wish I had some way to send strings to the output window from inside a class, though.

      -Alex
      Last edited by anachronist; 12-20-2011, 07:36 PM.

      Comment


        #4
        Alex,

        Print() is actually an NT method, which is what is causing you problems. It can only be used inside the Indicator and Strategy base classes.

        That said, there are other options that can be used depending on your needs for debugging. You can make your own "Output window" from a Windows Form with a TextBox.

        You can also use a MessageBox:
        MessageBox.Show("SomeText");
        This also has the advantage of acting like a "BreakPoint". While halting execution of the code can help sometimes, it can also adversely effect things in a multi-threaded environment, so it depends on your needs.

        In that case, you can always try the Trace class such as:
        Trace.WriteLine("SomeText");
        In addition to WriteLine there are other methods/properties that can be useful from that class. The output from the Trace class can be seen in the trace file. One word of caution if you use this method, if you ever need help from NT and they want you to send in the trace file littered with your debug lines, they probably will (and rightfully so) tell you to get rid of your debug lines before they would want to look at it. There is also a utility from SysInternals called "dbgview" that will read those trace lines as they are written.

        One last word of caution ALL of these methods are undocumented/unsupported so don't even think of asking for help from NT with them. They should be attempted only by those that are experienced and comfortable to go it alone.

        Hope this helps.

        VT

        Comment


          #5
          Thanks VTrader. I'm a 35 year veteran computer programmer, mostly working in C++, PHP, and some Java, but almost nothing on Windows platforms except some scientific Visual Basic macros for Excel. My exposure to C# has been only through NinjaTrader. It's good to know there are alternative ways to send output to a window. I'll work with those (although I am mystified why NinjaTrader won't expose a handle to the Output window). If I ever need a breakpoint badly enough, I'll probably forego the MessageBox and just buy Visual Studio and do my NinjaScript debugging in that.
          -Alex

          Comment


            #6
            Followup:
            Originally posted by VTtrader View Post
            Print() is actually an NT method, which is what is causing you problems. It can only be used inside the Indicator and Strategy base classes.
            What if I forced inheritance on my custom class by appending ":Indicator" or ":Strategy" to the class declaration?

            It seems to work. The custom class needs to provide empty override methods for Initialize() and OnBarUpdate(). Then Print() works inside the custom class, although it doesn't seem to be able to print NinjaTrader properties like CurrentBar.ToString() -- it prints as "-1" on each bar.

            -A
            Last edited by anachronist; 04-27-2012, 04:42 PM.

            Comment


              #7
              This is 100% unsupported however you might check it out.

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by RookieTrader, Today, 09:37 AM
              3 responses
              14 views
              0 likes
              Last Post NinjaTrader_ChelseaB  
              Started by kulwinder73, Today, 10:31 AM
              0 responses
              5 views
              0 likes
              Last Post kulwinder73  
              Started by terofs, Yesterday, 04:18 PM
              1 response
              23 views
              0 likes
              Last Post terofs
              by terofs
               
              Started by CommonWhale, Today, 09:55 AM
              1 response
              3 views
              0 likes
              Last Post NinjaTrader_Erick  
              Started by Gerik, Today, 09:40 AM
              2 responses
              7 views
              0 likes
              Last Post Gerik
              by Gerik
               
              Working...
              X