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

strategy that calls indicator, and indicator adds bars????

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

    strategy that calls indicator, and indicator adds bars????

    When a strategy calls an indicator which adds bars in the indicator's initialize() method, what is happening? To me it seems that NT is selectively ignoring the indicator's request to add bars. Take this basic example:

    An indicator compares an instrument's 5 minute bars to its 1 minute bars with some calculation. A strategy then enters positions using this indicator.

    From what I deduced based on the NT documentation, I would have expected program flow to be the following: Strategy Initialize() --> Strategy OnBarUpdate() --> Indicator initialize() (5 minute bars added here) --> Strategy OnBarUpdate <--> Indicator OnBarUpdate

    But that is not happening because the program doesn't work. So,
    1) It would be VERY helpful if someone could clarify the program flow of strategy and indicator method calls that I have above.
    2) When is the best time to add the 5 minute bars in my example?
    2) Do the same bars need to be added in BOTH the indicator's Initialize() and the strategy's Initialize() methods? And how does that affect the numbering of bars objects, since now it seems like the same bars have been added twice and are redundant?

    #2
    Hello MM345, and thank you for your question.

    Indicator and Strategy OnBarUpdate calls are always asynchronous and truly event driven. This is true whether we have a single strategy with multiple time frames, or an indicator and a strategy with multiple time frames. There are therefore no documented guarantees as far as ordering.

    That said, I have created a strategy and an indicator, which I have attached. Their output from my log file is below. Repeating code from my strategy and initializer's Initialize and OnBarUpdate methods in your code, and analyzing your own logs, should allow you to determine how your strategy typically orders events.

    You may notice that OnBarUpdate was called in my output before Initialize was. Initialize is guaranteed to be called for your strategy while you are configuring its properties in the properties screen, but it is possible for OnBarUpdate to be called after enabling the strategy and before Initialize is again called. This is why I mentioned no guarantees can be made. While you can rely on an Indicator's Initialize method being called before the Indicator's OnBarUpdate method being called, and a Strategy's Initialize method being called before the Strategy's OnBarUpdate method is called, you must treat indicator and strategy events as though they may happen in any order.

    The only way to ensure that your strategy can read the indicator's series correctly, is if your strategy has an OnBarUpdate that is more granular (i.e. uses a shorter time period) than any of your indicator's series. In other words, if your indicator has 1 and 5 minute bars, I would strongly consider running e.g. 10 second bars in your Strategy.

    Here is an excerpt from the multi-time frame section of the strategy guide. Even though you may be attempting to use the same time series, likely the primary series, in your indicator and strategy, your indicator and strategy each have a unique copies of the primary data series, and these concepts therefore apply.

    Since a NinjaScript is truly event driven, the OnBarUpdate() method is called for every bar update event for each Bars object added to a script. This model provides the utmost flexibility. For example, you could have multiple trading systems combined into one strategy dependent on one another. Specifically, you could have a 1 minute MSFT Bars object and a 1 minute AAPL Bars object, process different trading rules on each Bars object and check to see if MSFT is long when AAPL trading logic is being processed.
    Log output.


    08-Aug-16 06:47:40|1|128|Enabling NinjaScript strategy 'ExampleStrategy1554554/a23abde3c9bb4fc6b02c9cd9dba23330' : On starting a real-time strategy - StrategySync=SubmitImmediately SyncAccountPosition=False EntryHandling=AllEntries EntriesPerDirection=1 StopTargetHandling=PerEntryExecution ErrorHandling=StopStrategyCancelOrdersClosePositio ns ExitOnClose=True/ triggering 30 before close Set order quantity by=Strategy ConnectionLossHandling=KeepRunning DisconnectDelaySeconds=10 CancelEntryOrdersOnDisable=False CancelExitOrdersOnDisable=True CalculateOnBarClose=True MaxRestarts=4 in 5 minutes
    08-Aug-16 06:47:40|1|4|Strategy OBU called
    08-Aug-16 06:47:40|1|4|Indicator OBU called
    08-Aug-16 06:47:40|1|4|Indicator OBU called
    08-Aug-16 06:47:40|1|4|Indicator OBU called

    (30 more times)

    08-Aug-16 06:47:40|1|4|Indicator OBU called
    08-Aug-16 06:47:40|1|4|Indicator OBU called
    08-Aug-16 06:47:40|1|4|Indicator OBU called
    08-Aug-16 06:47:40|1|4|MyValue: 2167.25
    08-Aug-16 06:47:40|1|4|Strategy OBU called
    08-Aug-16 06:47:40|1|4|Indicator OBU called
    08-Aug-16 06:47:40|1|4|MyValue: 2167.25
    08-Aug-16 06:47:40|1|4|Strategy OBU called
    08-Aug-16 06:47:40|1|4|Indicator OBU called
    08-Aug-16 06:47:40|1|4|MyValue: 2167.25

    (repeats last 3 lines many times)


    08-Aug-16 06:47:59|1|4|In strategy initialize, about to create indicator
    08-Aug-16 06:47:59|1|4|Indicator initialize called
    08-Aug-16 06:47:59|1|4|In strategy initialize, just called toTest constructor
    08-Aug-16 06:47:59|1|4|In strategy initialize, about to create indicator
    08-Aug-16 06:47:59|1|4|Indicator initialize called
    08-Aug-16 06:47:59|1|4|In strategy initialize, just called toTest constructor
    08-Aug-16 06:48:00|1|4|Strategy OBU called
    08-Aug-16 06:48:00|1|4|Indicator OBU called
    08-Aug-16 06:48:00|1|4|MyValue: 2178.5
    08-Aug-16 06:48:00|1|4|Strategy OBU called
    08-Aug-16 06:48:00|1|4|Indicator OBU called
    08-Aug-16 06:48:00|1|4|MyValue: 2178.5
    08-Aug-16 06:48:00|1|4|Strategy OBU called
    08-Aug-16 06:48:00|1|4|Indicator OBU called
    08-Aug-16 06:48:00|1|4|MyValue: 2178.75

    (last 3 lines repeated until strategy disabled)
    Please let us know if there are any other ways we can help.
    Attached Files
    Jessica P.NinjaTrader Customer Service

    Comment


      #3
      One thing I forgot to mention. You will want to initialize indicators like this

      reference = YourIndicatorName(parameter1, parameter2)

      And not

      reference = new YourIndicatorName()

      The latter may prevent indicators from being available during OnBarUpdate
      Jessica P.NinjaTrader Customer Service

      Comment


        #4
        Thanks. That really clarifies the program flow questions I had.

        I'm still not clear on two things:
        1) If there are bars that are only needed for the indicator's calculations, then why do they also have to be added to the strategy? For instance, if my 5 minute bars are only used in the Indicator's OnBarUpdate() method, then why won't my program work without me also adding them to the strategy?

        2) Are Bar objects numbered independently in the indicator and strategy? Take this example:
        -- Strategy is run on a 1 minute chart of AAPL
        -- Strategy initialize() adds 2 mintute bars for AAPL
        -- Indicator initialize() adds 5 minute bars for MSFT

        Would this correctly reflect the bar numbering:
        Strategy:
        0 = 1 min AAPL
        1 = 2 min AAPL

        Indicator:
        0 = 1 min AAPL
        1 = 5 min MSFT?

        Comment


          #5
          Thanks for the additional questions, I am happy to clarify further

          Originally posted by MM345 View Post
          If there are bars that are only needed for the indicator's calculations, then why do they also have to be added to the strategy? For instance, if my 5 minute bars are only used in the Indicator's OnBarUpdate() method, then why won't my program work without me also adding them to the strategy?
          If you just want information from your indicator, you don't have to. You can simply make your indicator's data series public. Many strategies access child indicator's Values array.

          If, however, you want your strategy's OnBarUpdate to trigger at least as often as your indicator's OnBarUpdate is triggered, your strategy's OnBarUpdate is only going to be sensitive to its own data series.

          If you would like your strategy's data series to be independent from your indicator's, this is why I recommended using a more granular data series in your strategy, or configuring your strategy with its Calculate property set to NT8 CalculationMode.OnEachTick, or in NT7, CalculateOnBarClose = false.

          The idea is that if your strategy is updating more frequently than its child indicators, it will always be able to access child indicator data as it comes in.

          Originally posted by MM345 View Post
          Are Bar objects numbered independently in the indicator and strategy? Take this example:
          -- Strategy is run on a 1 minute chart of AAPL
          -- Strategy initialize() adds 2 mintute bars for AAPL
          -- Indicator initialize() adds 5 minute bars for MSFT

          Would this correctly reflect the bar numbering:
          Strategy:
          0 = 1 min AAPL
          1 = 2 min AAPL

          Indicator:
          0 = 1 min AAPL
          1 = 5 min MSFT?
          This is exactly right.

          Within a strategy or indicator, BarsArray[1] , Closes[1], Times[1], CurrentBars[1] , and the like will all refer to the first data series you added, and all of those at the [0] index will refer to the primary data series. From there, the 0th member of each array is always the most recent data, and the (CurrentBars[n])'th member of each array is the oldest data available.

          The numbers used in a strategy are completely independent of those used in a child indicator. To get a child indicator's numbers, you may use ChildIndicator.CurrentBars[n] , where n relates to the child indicator's bar indices.
          Jessica P.NinjaTrader Customer Service

          Comment


            #6
            Hi Jessica,

            I don't mean to hijack MM345's post, but I'm also struggling for last few days trying to get my strategy to fire in NT7.

            I've created a method within my custom indicator based on some conditions that will pass a trigger value (signal = 1), which I in turn try to access via my strategy script, but it's not reading it at all.

            I've followed suggestion about making strategy chart more granular (lower timeframe/tick) than indicator chart, but still same behavior. The weird thing about this whole ordeal is that I'm using same approach for another strategy (different indicator), and it works perfect there but here it looks like OnBarUpdate from the strategy is still being called before OnBarUpdate for the indicator.

            Any help or guidance you can give would be greatly appreciated.

            Thank you.

            Comment


              #7
              Hello successfulmike,

              Would it be possible to either post a stripped-down version of your code here, or mail it to platformsupport[at]ninjatrader[dot]com so that we may try to test it on our end?
              Jessica P.NinjaTrader Customer Service

              Comment


                #8
                Ok, I've sent the scripts (strategy and indicator) to the email address you referenced. Thanks Jessica

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by jclose, Today, 09:37 PM
                0 responses
                4 views
                0 likes
                Last Post jclose
                by jclose
                 
                Started by WeyldFalcon, 08-07-2020, 06:13 AM
                10 responses
                1,413 views
                0 likes
                Last Post Traderontheroad  
                Started by firefoxforum12, Today, 08:53 PM
                0 responses
                10 views
                0 likes
                Last Post firefoxforum12  
                Started by stafe, Today, 08:34 PM
                0 responses
                10 views
                0 likes
                Last Post stafe
                by stafe
                 
                Started by sastrades, 01-31-2024, 10:19 PM
                11 responses
                169 views
                0 likes
                Last Post NinjaTrader_Manfred  
                Working...
                X