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

User-Defined classes in Strategy

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

    User-Defined classes in Strategy

    Hello All,
    I would like to create an object that knows how to print itself (it's attributes) on the primary chart when it is created. My object, when called, does not have/refer-to the same CurrentBar value as it's parent.

    Here is sample code that reproduces my problem.
    Code:
    #region Using declarations
    using System;
    using System.ComponentModel;
    using System.Diagnostics;
    using System.Drawing;
    using System.Drawing.Drawing2D;
    using System.Xml.Serialization;
    using NinjaTrader.Cbi;
    using NinjaTrader.Data;
    using NinjaTrader.Indicator;
    using NinjaTrader.Gui.Chart;
    using NinjaTrader.Strategy;
    #endregion
    
    // This namespace holds all strategies and is required. Do not change it.
    namespace NinjaTrader.Strategy
    {
        /// <summary>
        /// Enter the description of your strategy here
        /// </summary>
        [Description("This is the main Strat...the 'parent' class")]
        public class MyCustomStrategy : Strategy
        {
            #region Variables
            // Wizard generated variables
            // User defined variables (add any user defined variables below)
            MyClass myClass;
            #endregion
    
            /// <summary>
            /// This method is used to configure the strategy and is called once before any strategy method is called.
            /// </summary>
            protected override void Initialize()
            {
                CalculateOnBarClose = true;
            }
    
            /// <summary>
            /// Called on each bar update event (incoming tick)
            /// </summary>
            protected override void OnBarUpdate()
            {
                //Print(CurrentBar);
                if(CurrentBar == 25){
                    Print("From OnBarUpdate() CurrentBar is:" + CurrentBar);
                    myClass = new MyClass();
                }
            }
    
            #region Properties
            #endregion
        }
        //this is the object, instansiated by MyCustomStrategy, that should
        //be able to draw itself when created via "new"...
        public class MyClass : Strategy
        {
            public     MyClass(){
                Print("Why is this not 25 as well??");
                Print("From MyClass() CurrentBar is:" + CurrentBar);
            }
        }
    }
    and the output:

    From OnBarUpdate() CurrentBar is:25
    Why is this not 25 as well??
    From MyClass() CurrentBar is:0

    Thank you for any guidance you can provide,
    -Billy

    #2
    billyb,

    Unfortunately creating classes is outside the scope of what we can offer support for. It is a much more advanced C# concept than what we can cover. Perhaps some of the more advanced C# users will assist you though. Good luck.
    Josh P.NinjaTrader Customer Service

    Comment


      #3
      Hello Billy,

      defining and applying own classes the way you depicted puts you into big trouble. MyClass() inherits from base class Strategy but is instantiated at a arbitrary point in time (CurrentBar == 25 in your case). Everything is out of control. I wouldn't do that.

      Regards
      Ralph

      Comment


        #4
        Thanks for the quick replies!

        Well, Ralph, I would really like to be able to create these MyClass objects as they contain specific information regarding things that occurred in the incoming data flow at the specific moment that they were created. I am keeping a list of them in MyCustomStrategy so that I can walk back through them on occasion.

        It would be possible to handle the chart drawing from MyCustomStrategy, at the same time that the MyClass object is created, thereby avoiding the issue, but that seems a bit messy. The MyClass objects can get created in many places in the (real) MyCustomStrategy and it's cumbersome to repete the drawing code in each of these locations (but not impossible). If the object could draw itself, the code would be much cleaner.

        Do you happen to have a suggestion as to how I might accomplish this? Maybe by defining MyClass differently or with a different inheritance?

        TIA,
        -Billy

        Comment


          #5
          Originally posted by billyb View Post
          Thanks for the quick replies!

          Well, Ralph, I would really like to be able to create these MyClass objects as they contain specific information regarding things that occurred in the incoming data flow at the specific moment that they were created. I am keeping a list of them in MyCustomStrategy so that I can walk back through them on occasion.

          It would be possible to handle the chart drawing from MyCustomStrategy, at the same time that the MyClass object is created, thereby avoiding the issue, but that seems a bit messy. The MyClass objects can get created in many places in the (real) MyCustomStrategy and it's cumbersome to repete the drawing code in each of these locations (but not impossible). If the object could draw itself, the code would be much cleaner.

          Do you happen to have a suggestion as to how I might accomplish this? Maybe by defining MyClass differently or with a different inheritance?

          TIA,
          -Billy
          Why do you need instantiate a new object?
          If you need to perform a routine of drawing actions, you just could do a separate method and call it at the moment you need.

          Comment


            #6
            Hello Billy,

            would like to understand the intention of MyClass in more detail, because it looks like a special case to me. Here is how I understood it:
            You create instances of MyClass at certain points in time and you store special informations with these instances by construction. Later on (somewhen) you execute drawing-methods of these instances and in turn they are plotting certain informations on the chart? Is that correct?

            Regards
            Ralph

            Comment


              #7
              Hi Ralph,
              You are almost correct! My thought goes like this:

              1) At the close of some unknown bar, a number of conditions have been met that are of interest to my strategy. In my earlier example this happened supposedly on the 25 bar.
              2) I store the Open, High, Low and Close of the bar that triggered this as well as two calculated prices.
              3) I must now start 2 horizontal lines on the chart that will continue extending through each subsequent bar close until the strategy notices that once again those special conditions have been met - maybe on the 87th bar for instance.
              4) I then create another instance of MyClass, store the OHLC and 2 new calculated prices in it.
              5) I start 2 new horizontal lines at the new calculated prices while leaving the old lines in place (they do not get extended anymore either).

              I must be able to look back periodically at previously stored "calculated prices"

              My idea is that if each class instance is maintaining it's own drawing routines and it's own lines, then I wouldn't have to manage the "tag" field for each one...the class instances would do that themselves.

              Does this make any sense?
              -Billy

              Comment


                #8
                Originally posted by billyb View Post
                Hi Ralph,
                You are almost correct! My thought goes like this:

                1) At the close of some unknown bar, a number of conditions have been met that are of interest to my strategy. In my earlier example this happened supposedly on the 25 bar.
                2) I store the Open, High, Low and Close of the bar that triggered this as well as two calculated prices.
                3) I must now start 2 horizontal lines on the chart that will continue extending through each subsequent bar close until the strategy notices that once again those special conditions have been met - maybe on the 87th bar for instance.
                4) I then create another instance of MyClass, store the OHLC and 2 new calculated prices in it.
                5) I start 2 new horizontal lines at the new calculated prices while leaving the old lines in place (they do not get extended anymore either).

                I must be able to look back periodically at previously stored "calculated prices"

                My idea is that if each class instance is maintaining it's own drawing routines and it's own lines, then I wouldn't have to manage the "tag" field for each one...the class instances would do that themselves.

                Does this make any sense?
                -Billy
                Seems to me you want to overcomplicate things.
                I would use two plots. If conditions are met on bar 25 I would assign those two calculated values to plots.
                On the following bar plots would be assigned the previous bar's value until conditions are met again.

                Comment


                  #9
                  Hello Billy,

                  yes it makes sense. Simplest way to do that would be to define a Structure (i.e. a Rectangle is a Structure) without inheritance from the Indicator base class. You would hand over your parent-indicator's this-operator to the constructor instead. This way you have fully access to your parent-indicator's public interface. And you could implement your drawing routines in the context of your parent-indicator.

                  If you follow roonius's proposal you would store your values and calculation results in a container and use that container for drawing purpose and later access.

                  Either way, it is a design desicion how to structure code.

                  Regards
                  Ralph

                  Comment


                    #10
                    Thanks Ralph - passing the parent's this pointer did the trick!
                    -Billy

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by wzgy0920, 04-20-2024, 06:09 PM
                    2 responses
                    26 views
                    0 likes
                    Last Post wzgy0920  
                    Started by wzgy0920, 02-22-2024, 01:11 AM
                    5 responses
                    32 views
                    0 likes
                    Last Post wzgy0920  
                    Started by wzgy0920, Yesterday, 09:53 PM
                    2 responses
                    49 views
                    0 likes
                    Last Post wzgy0920  
                    Started by Kensonprib, 04-28-2021, 10:11 AM
                    5 responses
                    192 views
                    0 likes
                    Last Post Hasadafa  
                    Started by GussJ, 03-04-2020, 03:11 PM
                    11 responses
                    3,234 views
                    0 likes
                    Last Post xiinteractive  
                    Working...
                    X