Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

How do I create my own class and instantiate it with it's own private variables?

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

    How do I create my own class and instantiate it with it's own private variables?

    Hi. Please see the below post from Michael.

    I want to create my own class, and instantiate it in a Indicator or Strategy, but have private variables in it. I can't seem to do that in an AddOn.

    Here is what I want to do..

    Here is the class I want to create

    namespace NinjaTrader.NinjaScript
    {

    public static class TestMe
    {
    private NinjaScript ns; // ns container to print
    private NinjaScriptBase nsb; // nsb container
    private int TestInt;


    public static void TestInit (this NinjaScript tns, NinjaScriptBase tnsb, int SetTestInt)
    {
    ns = tns;
    nsb = tnsb;
    TestInt = SetTestInt;
    }

    public static void TestMeDraw1(this NinjaScript ns, NinjaScriptBase nsb, double TickSize, double y)
    {
    DrawingTools.Draw.Dot(nsb, "testDot", true, 0, TickSize + y, Brushes.Red);
    }

    public static void TestMeDraw2(double TickSize, double y)
    {
    DrawingTools.Draw.Dot(tnsb, "testDot", true, 0, TickSize + y, Brushes.Red);
    }
    }
    }

    Then create and run it from an Indicator/Strategy.

    TestMe.TestInit (this, this);
    TestMe.TestMeDraw1(this, this, TickSize, High[0]);

    // new code that I want to run - ns and nsb are then set for this indicator from the TestInit
    TestMe.TestMeDraw(TickSize, High[0]);

    I would then put a bunch of code in that class.

    How do I do this, and where do I put the code? I tried in a AddOn, and I get errors saying it all has to be static....

    Thanks

    Originally posted by NinjaTrader_MichaelM View Post
    Hello DaFish,

    The issue is that NinjaScriptBase is not available from within the NinjaScript ns you pass into the method. Here is an example of a workaround:
    Code:
    namespace NinjaTrader.NinjaScript
    {
    	public static class TestMe
    	{
    		public static void TestMeDraw(this NinjaScript ns, NinjaScriptBase nsb, double TickSize, double y)
    		{
    			DrawingTools.Draw.Dot(nsb, "testDot", true, 0, TickSize + y, Brushes.Red);
    		}
    	}
    }
    and then in your indicator/strategy/whatever:
    Code:
    TestMe.TestMeDraw(this, this, TickSize, High[0]);
    Please let me know if you have any further questions.

    #2
    Hello DaFish,

    Thank you for writing in. I am currently investigating this issue and will update you as soon as possible.

    Thank you for your patience in the meantime.
    Michael M.NinjaTrader Quality Assurance

    Comment


      #3
      Hello DaFish,

      Thank you for your patience. I was able to get the following code working successfully as an AddOn:
      Code:
      public static class TestPassNS
      {
          public static NinjaScriptBase nsb;
          public static NinjaScript ns;
          public static void passNS(this NinjaScript passns, NinjaScriptBase passnsb)
          {
              ns = passns;
              nsb = passnsb;
          }
          public static void drawStuff(double at_y)
          {
              DrawingTools.Draw.Dot(nsb, "MyDot", false, 0, at_y, Brushes.Aqua);
          }
      }
      and then in my indicator:
      Code:
      if(CurrentBar == 0)
      {
          TestPassNS.passNS(this, this);
      }
      else if (CurrentBar > 1)
      {
          TestPassNS.drawStuff(Low[0]);
      }
      Please let me know if you have any questions or if I may be of further assistance.
      Michael M.NinjaTrader Quality Assurance

      Comment


        #4
        You are awesome Michael... Thanks a lot.

        In case anybody wants to see and use the code. I am using this like I would have with UserDefinedMethods. Works great.
        Attached Files

        Comment


          #5
          Hello DaFish,

          I am glad to hear you were able to get everything working. It is always my pleasure.

          Thank you for providing your code to other users who may experience similar situations.

          Please let me know if I may be of further assistance anytime and have a great weekend!
          Michael M.NinjaTrader Quality Assurance

          Comment


            #6
            Applying indicators

            Hi Michael, I have adopted your code

            Q: How would I include indicators such as ATR or ADX in the methods?

            Thanks in advance,

            steve

            Comment


              #7
              Hello stevenev,

              Can you please clarify what you would like to do with the indicators?

              You would not be able to call an indicator from an AddOn.
              Zachary G.NinjaTrader Customer Service

              Comment


                #8
                Hi. See my post #4 with the attached code in. I show an indicator that YOU write being called in there.

                This AddOn will perform the function of the user defined methods in NT7. THis way you can have your own common code - this is NOT meant to call system indicators - unless what you are saying is that you want to call ATR or ADX in the AddOn to calc values inside your own methods - is that what you are asking?

                I just checked my code. I don't think I call any system indicators inside my AddOn. What I do, is I calculate CCI, FisherTransform, stochastics.... inside my indicator, then pass in the dataseries to the AddOn. I do it that way because I am always using CCI for lots of things. This way I calculate it once, then pass the dataseries to where I need it.

                I hope this helps.

                Originally posted by stevenev View Post
                Hi Michael, I have adopted your code

                Q: How would I include indicators such as ATR or ADX in the methods?

                Thanks in advance,

                steve

                Comment


                  #9
                  I have run into problems with this setup, and I really appreciate your help.

                  I implemented the code here. In Strategy backtesting.. when I run it against a single instrument, everything works. When I run it against a list of instruments (which worked fine individually) I get out of range index errors. I used try/catch to isolate them.

                  I am not a c# expert. I am thinking somethings don't get reset properly when moving from symbol to symbol so the problem occurs.

                  As an alternative, I tried the partial class solution but that did not work, and I reported it here: http://ninjatrader.com/support/forum...ad.php?t=81647

                  Thanks in advance for your help.

                  Comment


                    #10
                    HI make sure you have enough data loaded on the chart, and make sure you are checking for:

                    // Checks to ensure all Bars objects contain enough bars before beginning
                    if (
                    (CurrentBars[0] <= BarsRequired) ||
                    (CurrentBars[1] <= BarsRequired) ||
                    (CurrentBars[2] <= BarsRequired) ||
                    (CurrentBars[3] <= BarsRequired) ||
                    (CurrentBars[4] <= BarsRequired) ||
                    (CurrentBars[5] <= BarsRequired))
                    {
                    return;
                    }

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by fitspressoburnfat, Today, 04:25 AM
                    0 responses
                    2 views
                    0 likes
                    Last Post fitspressoburnfat  
                    Started by Skifree, Today, 03:41 AM
                    1 response
                    4 views
                    0 likes
                    Last Post Skifree
                    by Skifree
                     
                    Started by usazencort, Today, 01:16 AM
                    0 responses
                    1 view
                    0 likes
                    Last Post usazencort  
                    Started by kaywai, 09-01-2023, 08:44 PM
                    5 responses
                    604 views
                    0 likes
                    Last Post NinjaTrader_Jason  
                    Started by xiinteractive, 04-09-2024, 08:08 AM
                    6 responses
                    23 views
                    0 likes
                    Last Post xiinteractive  
                    Working...
                    X