• 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

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 Bionian, Today, 10:10 AM
                2 responses
                17 views
                0 likes
                Last Post NinjaTrader_ShawnB  
                Started by D Trader, Today, 01:41 PM
                0 responses
                8 views
                0 likes
                Last Post D Trader  
                Started by rexmie888, Today, 04:00 AM
                1 response
                9 views
                0 likes
                Last Post NinjaTrader_ShawnB  
                Started by terminatorT1000, Today, 01:23 AM
                1 response
                13 views
                0 likes
                Last Post NinjaTrader_ShawnB  
                Started by Tradarr, Today, 12:25 PM
                0 responses
                8 views
                0 likes
                Last Post Tradarr
                by Tradarr
                 
                Working...
                X