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

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 -

    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 -



                    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 -

                    BertrandNinjaTrader Customer Service

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by pechtri, 06-22-2023, 02:31 AM
                    8 responses
                    122 views
                    0 likes
                    Last Post Nyman
                    by Nyman
                     
                    Started by frankthearm, 04-18-2024, 09:08 AM
                    16 responses
                    64 views
                    0 likes
                    Last Post NinjaTrader_Clayton  
                    Started by habeebft, Today, 01:18 PM
                    1 response
                    5 views
                    0 likes
                    Last Post NinjaTrader_ChelseaB  
                    Started by benmarkal, Today, 12:52 PM
                    2 responses
                    13 views
                    0 likes
                    Last Post benmarkal  
                    Started by f.saeidi, Today, 01:38 PM
                    1 response
                    7 views
                    0 likes
                    Last Post NinjaTrader_BrandonH  
                    Working...
                    X