• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Calculate historical input once, not in OnBarUpdate

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

    Calculate historical input once, not in OnBarUpdate

    Hello,

    I have a simple beginner's question. I need for an indicator some input based on historical data, i.e. prior day's Low/High/Open/close. This input stays unchanged during "normal" bar updates. I can't calculate this input in Initialize() because the chart and historical data are not loaded at his time yet. I won't calculate this input in OnBarUpdate() for better performance. The history will be not changed with each tick, it makes no sense to calculate old OLHC' over and over again.

    What is the guide for such situations?

    At the moment I have only idea with a status variable, which is set to 1 after first (and last) calculation and once set to 1 the calculation is avoided. I don't know where this variable should be declared and with which visibility. When it will be reset? During loading of indicator for some chart?

    #2
    fel17, the idea with a status flag is good, since you need the bar data you would still calculate in OnBarUpdate() but only if the flag would allow to do so, so for example at the start of every session / day or week (depending on what you need).

    Generally for allocating any custom resources you can also use the OnStartUp() method only called once and not multiple like Initialize() would -

    http://www.ninjatrader.com/support/h.../onstartup.htm
    BertrandNinjaTrader Customer Service

    Comment


      #3
      Thanks for quick response.
      I try to do it with OnStartUp() but it seems that Bars.GetDayBar(1) is not available at of time. Like in Initialize(). Bars.GetDayBar(1) is null in my case... In OnBarUpdate() it isn't the case.

      I'd like to prefer do not to use status flag. Is there another overridable method which can be use for this?

      If I should use a status variable - should I declare and set it to 0 in the Variables section of my class? As public or private? The variable should be calculated only once per day.

      Comment


        #4
        Yes, for accessing bars data you would need to be at least at the first OnBarUpdate() bar (so CurrentBar == 0).

        The status variable would be the way to go, private scope with initial value of 0 should be fine.

        You can reset for example at Bars.FirstBarOfSession daily.
        BertrandNinjaTrader Customer Service

        Comment


          #5
          Hi,

          I'd like to extend my question.

          I'm looking for a method which is called at begin of a session (session = trading day).
          Maybe OnStartUp() is the right one, but I'm not sure whether it's called once or once per day?

          Usually I don't close NT7 during the week. But I close my data feed provider every evening and so disconnect NT from data provider (beside of many dis/reconnects during the day, because of network problems).

          My question is: will OnStartUp() run every morning on connect or only once on Monday?
          What's about initialize()?
          Any alternatives, something like OnSessionStart()?

          Comment


            #6
            fel17, there would be unfortunately no dedicated OnSessionStart() - but you can check for the session start in your OnBarUpdate() with - http://www.ninjatrader.com/support/h...rofsession.htm

            Initialize() can be called multiple times, so not a good place to calculate items. OnStartUp() is called after Initialize() but before the first OnBarUpdate() after you've enabled the script.
            BertrandNinjaTrader Customer Service

            Comment


              #7
              Hi Bertrand,

              thanks for hints.

              FirstBarOfSession probably isn't suitable for my need. I.e. if I connect with NT at 9:00 and trading started at 8:00 then I have in the 15 miin chart four bars already. My current bar isn't FirstBarOfSession.

              Initialize:
              You wrote:
              Initialize() can be called multiple times
              in the manual:
              The Initialize() method is called once when starting a strategy.
              My question is: in which situations NT calls Initialiaze()?
              Only after the start of NT and first load of charts?
              After each connect to data provider and auto loading of data?
              Once per day during first connect of this day?

              Basically I'm looking for an idea to generate a signal/alert only once a day, if any. I don't want to generate this alert many times after each disconnect/reconnect during one day. And I will "forget" generated signals in the past, yesterday or earlier. Even if NT has not been closed last days.

              Do I need a status date variable or there are better (built in) methods?

              Comment


                #8
                fel17, please also be aware this additional tip in the Init help page -

                'Although this method is called once on the start of an indicator or strategy, there are times that this method is also called (Opening an indicator dialog) when you may not expect it to'

                You would need to save the state of the signal then externally, for example to a text file that you read in again if you restart the strategy / reconnect. This should you allow you to persist the daily signal state as needed.
                BertrandNinjaTrader Customer Service

                Comment


                  #9
                  Originally posted by NinjaTrader_Bertrand View Post
                  fel17, please also be aware this additional tip in the Init help page -

                  'Although this method is called once on the start of an indicator or strategy, there are times that this method is also called (Opening an indicator dialog) when you may not expect it to'
                  Funny... The question is still unanswered: when NT starts indicator or strategy? After start of NT, each connect, first connect of the day?
                  What are other "times that this method is also called", beside of Opening an indicator dialog?

                  Originally posted by NinjaTrader_Bertrand View Post
                  You would need to save the state of the signal then externally, for example to a text file that you read in again if you restart the strategy / reconnect. This should you allow you to persist the daily signal state as needed.
                  Hmm, sounds like a lot of effort... Should I use standard C# libraries for this?
                  No way to save this timestamp persistent in NT?
                  And the question again: where I should read the file? When an indicator is restarted?

                  Could you ask someone from second level?

                  Comment


                    #10
                    fel17, I'm the lead here for NinjaScript related items - the main point is - you cannnot and should not expect the Initialize() to be only called once, for anything critial here OnStartUp() was added to NT7, it would be called only once before the first OnBarUpdate() is seen by your script.

                    We state this as Initialize() is also called across all scripts, so for example if you have a strategy running and then add another one > Initialize is called not only for the to be added script, but across all.

                    If you would like to get access to the connection event, please work with -

                    http://www.ninjatrader.com/support/h...tionstatus.htm

                    For your initial question: there's no global variable you could populate with the state for the day unfortunately, the calculations inside your script are being rerun and can retrigger as you start a new instance of it. A saving of the daily state could be done for example via txt files -

                    http://www.ninjatrader.com/support/f...ead.php?t=3477
                    BertrandNinjaTrader Customer Service

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by CDXTrader, Today, 12:29 AM
                    0 responses
                    2 views
                    0 likes
                    Last Post CDXTrader  
                    Started by TopGun13, Yesterday, 10:35 PM
                    0 responses
                    2 views
                    0 likes
                    Last Post TopGun13  
                    Started by Audiofail, Yesterday, 05:40 AM
                    6 responses
                    18 views
                    0 likes
                    Last Post willwin
                    by willwin
                     
                    Started by jerblaster23, Yesterday, 03:42 PM
                    1 response
                    13 views
                    0 likes
                    Last Post NinjaTrader_ChelseaB  
                    Started by brian meade, Yesterday, 01:39 PM
                    2 responses
                    11 views
                    0 likes
                    Last Post brian meade  
                    Working...
                    X