Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Class and override...problem

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

    Class and override...problem

    hi,
    in the follow there is the structure of my program :


    namespace NinjaTrader.NinjaScript.Indicators
    {
    public class ClassXX {
    //logical and
    // draw text
    }

    public class PriceAnalisys : Indicator
    {
    public ClassXX mt;
    .....
    .....

    protected override void OnBarUpdate()
    {
    //code
    }

    i try to use draw.text in the class Plot but it not see :
    -CurrentBar
    -TickSize..

    how i do to use it in my class ClassXX? Or it's not possible and i give to sent it by parameter....

    Thanks
    Last edited by turbofib; 07-23-2016, 12:14 PM.

    #2
    Hello,

    Thank you for the question.

    Only an Indicator or Strategy would have a CurrentBar or TickSize property, this includes the Draw methods as well.

    If you define a class:

    Code:
    public class ClassXX 
    {
    }
    This class is just a class and has no access to the same properties as a indicator or strategy. The reason a Indicator has CurrentBar etc.. is because of its Inheritance:

    Code:
    public class PriceAnalisys[B] : Indicator[/B]

    With that being said, the custom class could only accept passed values, for example you could create properties in the class and fill values in, finally return a value needed to Draw with. here is an example:

    Code:
    namespace NinjaTrader.NinjaScript.Indicators
    {
    	public class ClassXX 
            {
                public int CurrentBar { get; set; }
    
                public double GetMyValue()
                {
                    if(CurrentBar > 10)
                    {   
                         //do something that does not require NinjaScript properties
                         return 100;
                    }
                    return 50;
                }
            } 
    	
    	public class Test : Indicator
    	{
    		private ClassXX mt;
    		protected override void OnStateChange()
    		{
    			if (State == State.SetDefaults)
    			{
    				Name = "Test";
    				mt = new ClassXX();
    			}
    		}
    
    		
    		protected override void OnBarUpdate()
    		{
    			mt.CurrentBar = CurrentBar;
    			Draw.Line(this, "MyLine", 10, mt.GetMyValue(), 0, mt.GetMyValue(), Brushes.Blue);
    			
    		}
    	}
    }
    If you are trying to put logic in a class and then Draw the value this class generates, I would suggest using the above example instead. leave the NinjaScript specific logic in the original indicator, and move the Math or other logic to the class and return a value. The returned value could be used in the Draw syntax.

    There is a decent guide here showing some basic class use and defining Constructors as well which is a common use item with classes: http://www.techotopia.com/index.php/...ed_Programming


    I look forward to being of further assistance.
    JesseNinjaTrader Customer Service

    Comment


      #3
      i need to return different value in my class...

      You suggest me to call different method (in my class) or use ref(ref is command of c#).?

      Comment


        #4
        Hello,

        You could have the same method return different values of the same type. For values of different types you would need different methods or to return an object.

        It would be up to you and how you want to develop the class. C# is flexible, so long as what you are doing is valid C# syntax and works how you want it, there is really no wrong way.

        I look forward to being of further assistance.
        JesseNinjaTrader Customer Service

        Comment


          #5
          You said :

          This class is just a class and has no access to the same properties as a indicator or strategy. The reason a Indicator has CurrentBar etc.. is because of its Inheritance:



          Code:
          public class PriceAnalisys : Indicator
          i must to use RemoveDrawObject in my class (ClassXX)...but is not possible...
          To do this i think do as follow:


          public class PriceAnalisys : Indicator
          {
          public ClassXX mt;
          mt = new ClassXX (this); //i send istance to this indicator in costructors of my class
          ..
          }



          public class ClassXX { //my class

          private PriceAnalisys refIndicator; //Instance of Priceanalisys

          public ClassXX (PriceAnalisys main) ====> Costructor my class
          {
          refIndicator= main;
          }

          ......
          .....

          refIndicator.RemoveDrawObject(tagFc[i]); ===> i use RemoveDrawObject inside in my class..

          }


          Is possible to do this? ..or there will be problem..
          Last edited by turbofib; 08-09-2016, 01:53 AM.

          Comment


            #6
            Hello,

            That would be correct, again the Class is only a class and not a indicator so it would not have access to indicator methods.

            You could try passing an instance, but you would need to test and ensure that in all situations it does work correctly.

            You could also just use a return method to return the Tag of the object to be removed. An example of a return method that accepts a string and returns a string could be as follows:

            Code:
            public string CheckToRemoveObject(string tag)
            {
                 if(someCondition)
                 {
                      return tag; //just pass the tag through
                 }
                 return ""; // condition was not met, return a blank string to not remove anything.
            
            }
            
            
            RemoveDrawingObject(CheckToRemoveObject("testTag"));
            This call would either equate to:
            Code:
            RemoveDrawingObject("");
            or
            RemoveDrawingObject("testTag");
            I always suggest for simplicity to keep all indicator code in the indicator, but that is up to you how you want to structure the class.

            Please let me know if I may be of further assistance.
            JesseNinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by judysamnt7, 03-13-2023, 09:11 AM
            4 responses
            57 views
            0 likes
            Last Post DynamicTest  
            Started by ScottWalsh, Today, 06:52 PM
            4 responses
            36 views
            0 likes
            Last Post ScottWalsh  
            Started by olisav57, Today, 07:39 PM
            0 responses
            7 views
            0 likes
            Last Post olisav57  
            Started by trilliantrader, Today, 03:01 PM
            2 responses
            19 views
            0 likes
            Last Post helpwanted  
            Started by cre8able, Today, 07:24 PM
            0 responses
            9 views
            0 likes
            Last Post cre8able  
            Working...
            X