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

OnStartUp() in UserDefinedIndicatorMethods?

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

    OnStartUp() in UserDefinedIndicatorMethods?

    Hello!

    I'v tried to use the OnStartUp() method in my UserDefinedIndicatorMethods file, but it doesn't work since it seems to be called after the OnBarUpdate() method is called by usual indicators.

    I have created a custom method that uses a List that I would like to get populated only once "on start up" before this method is called by any indicator. How can I trigger a single call "on start up" (to another custom method) that parses my input textfile to the List?

    A second question is if it's ok to use StreamReader in a method as shown in the outline below?. Is it correct to Dispose StreamReader at the end of the method?



    Code:
    using System.Collections.Generic;
    using System.IO;
    #endregion
    
    
    namespace NinjaTrader.Indicator
    {
        partial class Indicator
        {
            private List <HolidaySession> holidaySessionList;  // Declare variable to store List
            private string holidaySessionTextFile2012     = "HolidaySessionLists2012.txt";  // Input text file name
            
            protected override void OnStartUp()
            {
                // Call method to populate holidaySessionList
                holidaySessionList = ParseHolidaySessionInputTextFile(holidaySessionTextFile2012);
                
            }
            
            // Method to parse input text file
            public List<HolidaySession> ParseHolidaySessionInputTextFile(string fileName)
            {
                List<HolidaySession> holidaySessionListTemp = new List<HolidaySession>();
                System.IO.StreamReader sr = new System.IO.StreamReader(fileLocation);
                
               // Loop to parse text file
    
    
                // Disposes resources used by the StreamReader
                if (sr != null)
                {
                    sr.Dispose();
                    sr = null;
                }
    
                return holidaySessionListTemp;
                
            }
    
             // Get Holiday Adjusted Session Begin and End Time in Local Time Zone
             public void SessionGetNextTrueBeginEnd(DateTime day, out DateTime sessionBegin, out DateTime sessionEnd)
             {
                  //List <HolidaySession> holidaySessionList is built in OnStartup()
             }
    
            
        }
    
    }
    Best Regards
    posieidon_sthlm
    Last edited by poseidon_sthlm; 09-09-2012, 10:49 AM.

    #2
    Hello,

    OnStartup should be called once before the first OnBarUpdate method call. Not that it only works with things that inherit the IndicatorBase method.

    Do you perhaps have something I can use for testing here? I'd be happy to look further into it.
    Adam P.NinjaTrader Customer Service

    Comment


      #3
      Hello again!

      After some more hours of trouble shooting I can finally pinpoint my problem and provide an as simple as possible example for you to reproduce.

      The problem seems to be that the OnStartUp() method in the myUserDefinedIndicatorMethods file for some reason conflicts with the OnStartUp() method of the calling indicator. If the OnStartUp() section is present in the calling indicator, then NT throws
      Error on calling 'OnBarUpdate' method for indicator 'TestExistsInSessionTemplateList' on bar 0: Object reference not set to an instance of an object.
      .

      I want to use OnStartUp() in the method file as well as in the indicator. How can I solve this issue?

      This code in the calling indicator, where OnStartUp() is called, throws the error:.
      Code:
      namespace NinjaTrader.Indicator
      {
          public class TestExistsInSessionTemplateList : Indicator
          {
              private string sessionTemplateName;
              
              protected override void Initialize()
              {
                  Overlay = true;
              }
              
              
      [COLOR=Red]        protected override void OnStartUp()    // Error when OnBarUpdate() called from indicator 
              {
              }[/COLOR]
              
      
              protected override void OnBarUpdate()
              {
                  if (CurrentBar == 0)
                  {
                      sessionTemplateName = Bars.Session.TemplateName;            // Get the session template name of the chart
                      Print("sessionTemplateName: " + sessionTemplateName);
                      
                      bool test =  ExistsInSessionTemplateList(sessionTemplateName);     // Call method
                      
                      Print("");
                      Print("ExistsInSessionTemplateList: " + test);
                  }
              }
          }
      }
      This code in the calling indicator, where OnStartUp() is NOT called, works as expected.
      Code:
      namespace NinjaTrader.Indicator
      {
          public class TestExistsInSessionTemplateList : Indicator
          {
              private string sessionTemplateName;
              
              protected override void Initialize()
              {
                  Overlay = true;
              }
              
      [COLOR=Green]        /*
              protected override void OnStartUp()    // No Error when OnBarUpdate() not called 
              {
              }
              */[/COLOR]
      
              protected override void OnBarUpdate()
              {
                  if (CurrentBar == 0)
                  {
                      sessionTemplateName = Bars.Session.TemplateName;            // Get the session template name of the chart
                      Print("sessionTemplateName: " + sessionTemplateName);
                      
                      bool test =  ExistsInSessionTemplateList(sessionTemplateName);     // Call method
                      
                      Print("");
                      Print("ExistsInSessionTemplateList: " + test);
                  }
              }
          }
      }
      Note also that initializing the variable "sessionTemplateList" in one line doesn't solve the probelm.
      HTML Code:
      private List <string> sessionTemplateList = new List <string>();
      . The called method will in this case always return "false", also when the correct output would be "true".


      Assembly with two files provided:
      1) TestExistsInSessionTemplateList (Indicator)
      2) TestMyUserDefinedIndicatorMethods (Method file)

      Best Regards
      poseidon_sthlm
      Attached Files
      Last edited by poseidon_sthlm; 09-11-2012, 02:30 AM.

      Comment


        #4
        posiedon,

        Generally we would recommend defining that method from within your indicator.

        Code:
        public class TestExistsInSessionTemplateList : Indicator
         {
        
        protected override void OnStartUp()
        {			
        	// Call method to populate sessionTemplateList only once "on start up"
        	sessionTemplateList = ParseSessionTemplateList(sessionTemplateInputArray);
        }
        
        }
        A partial class is for splitting a class up into separate parts, however note that an indicator inherits as follows :

        TestExistsInSessionTemplateList : Indicator

        I.e. it inherits the Indicator class which is defined under-the-hood. I believe it is an abstract class or an interface (I am actually not 100% sure), however you are creating this partial class to work with that particular under-the-hood object so its likely you are getting this behavior due to something in the internal design. If you would like maybe a bit more detail on how things work I would suggest looking at the following indicator.



        There is an IndicatorBase class I had to use here to grab arbitrary, functional indicators using a little reflection. It may help you to see how I am grabbing these objects. I had to use the OnStartUp method to get them due to the way NT works under-the-hood, and I had to use SharpDevelop to look at objects in memory to see if I was doing it right.

        Unfortunately I don't have source code access, and we are entering unsupported territory here but it may help if you get something like VisualStudio or Sharp Develop and look at whats going on in memory when you are running through your indicator at run-time. For example, I found out that I couldn't create new indicator objects from within the Initialize() method. It may help as well to learn a bit about how things are working under-the-hood.

        ICSharpCode has 19 repositories available. Follow their code on GitHub.


        Last edited by NinjaTrader_AdamP; 09-11-2012, 07:10 AM.
        Adam P.NinjaTrader Customer Service

        Comment


          #5
          Hello!
          Thanks for your time and your advice. I'm not a programmer but until now it has been rather easy to figure out what I need for my NT development with help from not least this excellent forum. I look forward to go deeper into programming in the future.

          Right now I need to solve my problem with calling a custom method "on start up" from the "partial class". Can you point me to any alternative solution, than using the OnStartUp() method? Is there som event that I can use to trigger the call to my custom method? The reason I need to trigger the call inside the "partail class" is that I'v written a method similar to Bars.Session.GetNextBeginEnd() that will be called as a method from several different indicators.

          I'v checked the indicator you have written, but I didn't recognize anything that could help me to solve my current issue. If I overlooked something, please give me another hint.

          In my opinion my provided example below shows that the use of OnStartUp() within the "partail class" can result in unexcepected behaviour. My example is not very "exotic", but rather code that you would expect if you develop a holiday session method or a news event method. Therefore I want to report this as a "bug".

          Update:
          I'v done some more trouble shooting. The general problem is that the OnStartUp() method in the "partial indicator" file is called after the OnBarUpdate() method of any indicator, if you also use OnStartUp() in at least one of the indicators attached to a chart or a strategy. This should be unexpected behaviour and can be verified easily with print statments.

          Printstatments from the code below:
          Code:
          OnStartUp -  Calling Indictor. CurrentBar: 0        DateTime.Now.Ticks: 634830625874391521
          OnBarUpdate -  Calling Indictor. CurrentBar: 0        DateTime.Now.Ticks: 634830625874471537
          OnStartUp - Indicator Methods. CurrentBar: 0     DateTime.Now.Ticks: 634830625874521547
          Code:
          public class TestOnBarUpdateInPartialClass : Indicator
              {
                     private bool done = false;
                      
                  protected override void Initialize()
                  {
                      Overlay = true;
                  }
                  
                  protected override void OnStartUp()
                  {
                      Print("OnStartUp -  Calling Indictor. CurrentBar: " + CurrentBar + "        DateTime.Now.Ticks: " + DateTime.Now.Ticks);
                  }
                  
                  protected override void OnBarUpdate()
                  {
                      if ( !done)
                      {
                          Print("OnBarUpdate -  Calling Indictor. CurrentBar: " + CurrentBar + "        DateTime.Now.Ticks: " + DateTime.Now.Ticks);
                          done = true;
                      }
                  }
              }
          
              partial class Indicator
              {
              
                  protected override void OnStartUp()
                  {
                      Print("OnStartUp - Indicator Methods. CurrentBar: " + CurrentBar + "     DateTime.Now.Ticks: " + DateTime.Now.Ticks);
                      
                  }
              }
          Best Regards
          poseidon_sthlm
          Last edited by poseidon_sthlm; 09-12-2012, 08:13 AM.

          Comment


            #6
            Hello,

            You could put the method into the file UserDefinedMethods.

            Here you would be able to access it from all indicators/strategies.



            -Brett

            Comment


              #7
              NinjaTrader_Brett, the issue I describe is with the UserDefinedMethods. (When I write "partial class" below I refer to the UserDefinedMethods file.) The general problem is that the OnStartUp() method in the UserDefinedMethods file is called after the OnBarUpdate() method of any indicator, if you also use OnStartUp() in at least one of the indicators attached to a chart or a strategy. Therefore it's not possible to initalize and populate a List only once to be used by other methods, as described in the post #1. Any suggestions? Is there an event one could use to trigger a call to a method before the OnBarUpdate() method is called, besides OnStartUp()?

              Best Regards
              poseidon_sthlm
              Last edited by poseidon_sthlm; 09-12-2012, 09:27 AM.

              Comment


                #8
                Hello,

                Thanks for the clarification.

                To do this you would need some sort of global variable that would only be instantiated once, unfortunately this is not supported by NinjaTrader as user defined or any strategy or indicator may get instantiated multiple times or may be hosted virtually by the NinjaTrader internal architecture.


                This is on developments list to look into the possibility of adding it in a future major version.

                Unfortunately for now it would be unsupported and I have been recommending that if there is some local data you only need once to write it to a text for for shared access between indicators/strategies.

                -Brett

                Comment


                  #9
                  Ok, thanks. I will have to find another solution.

                  Please rememeber to inform your development department about the unexpected behaviour concerning the OnStartUp() method in UserDefinedMethods. The general problem here is that the OnStartUp() method in the UserDefinedMethods file is called after the OnBarUpdate() method of any indicator, if you also use OnStartUp() in at least one of the indicators attached to a chart or a strategy.

                  Best Regards
                  poseidon_sthlm

                  Comment


                    #10
                    No problem, as far as the OnStartUp() you should not define this in the UserDefinedMethods.cs. This is only for user defined methods and not for NT OnStartUp() method which should only be placed in real indicators. This file is intended for you to use you own custom created methods here and share them between indicators. Such as if you wanted a method what would add two number and you intend to use that with many indicators.

                    public int AddTwoNumbers(int number1, int number2)
                    {
                    return number1 + number2;
                    }


                    -Brett

                    Comment


                      #11
                      Ok, thanks. I thought OnStartUp() would work also in UserDefinedIndicatorMethods, but I may have missunderstood your answer in post #2.

                      Best Regards
                      poseidon_sthlm

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by Segwin, 05-07-2018, 02:15 PM
                      14 responses
                      1,788 views
                      0 likes
                      Last Post aligator  
                      Started by Jimmyk, 01-26-2018, 05:19 AM
                      6 responses
                      837 views
                      0 likes
                      Last Post emuns
                      by emuns
                       
                      Started by jxs_xrj, 01-12-2020, 09:49 AM
                      6 responses
                      3,293 views
                      1 like
                      Last Post jgualdronc  
                      Started by Touch-Ups, Today, 10:36 AM
                      0 responses
                      13 views
                      0 likes
                      Last Post Touch-Ups  
                      Started by geddyisodin, 04-25-2024, 05:20 AM
                      11 responses
                      63 views
                      0 likes
                      Last Post halgo_boulder  
                      Working...
                      X