• 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

CalculateOnBarClose in strategy vs indicator

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

    CalculateOnBarClose in strategy vs indicator

    Here's the setup:
    I have an strategy with 2 data series, 1 of the series is 1-tick bars
    I also have an indicator with 2 data series, 1 of the series is 1-tick bars

    Here's what I'd like to do:
    1) I want the strategy to be able to use Level 1 data (bid, ask). In live trading I want my execution to be based on the current bid/ask data.

    2) The indicator doesn't require bid/ask data. It just needs the price of the last trade. Also, the indicator code requires COBC = true - even though the bars are 1-tick bars.


    My question:
    How do I get what I'd like to do? I need the strategy to react to changes in the bid/ask. My guess is that I need COBC = false to get the most current info but I'm not sure. Would I get all Level 1 changes in Last, Bid, Ask with a 1 tick series AND COBC = true?

    What is the difference between COBC = true on a 1 tick bar and COBC = false? Do both have all Level 1 info?

    Does NT consider 'tick data' to be a trade or is it every Level 1 change (i.e., all changes to the bid/ask)?

    If required, can I set the strategy to COBC = false and have it call an indicator that has COBC = true?

    #2
    Follow up question:

    Given:
    Add("ES 12-06", PeriodType.Tick, 1, MarketDataType.Ask);

    In live trading does this data series update on a change in the ask price or does it need to wait for a trade to occur?

    In live trading would I get a different outcome for this series if COBC = true or COBC = false?

    Comment


      #3
      Hi bluelou,

      The strategy will call the indicator on every tick if calculate on bar close is set to false.
      This means that the indicator while having CalculateOnBarClose set to true, will still work like its false (as the only difference is how often OnBarUpdate is run and what the last bar is which is supplied from the strategy data series).

      However, if you want the indicator to run once for each bar instead of on each tick, only call the indicator on the first tick of a bar and call the previous bar.
      if (FirstTickOfBar)
      {
      myIndicatorValue = SMA(19)[1];
      }
      Then use the saved value until the new bar.


      To get the current bid or ask in your strategy use:
      GetCurrentAsk()
      GetCurrentBid()


      If you add the series using the MarketDataType bid or ask you will get all the ticks for that series.

      As far as when a new tick is sent out, this comes from the data provider and not from NinjaTrader. I would inquire with the data provider when a new tick is sent.
      I think this is when there is a new trade for last, or when a new position is taken for bid and ask. I'm not positive on this.

      With a 1 tick series, there would be no difference between COBC false or true. There would be a difference if you use a data series with an interval larger than 1 tick.
      (COBC false runs on every tick, COBC true runs on every bar close)
      Chelsea B.NinjaTrader Customer Service

      Comment


        #4
        Cal and ChelseaB,
        I think this clears things up for me.

        Thanks!
        Lou

        Comment


          #5
          ChelseaB,
          FWIW, I've found FirstTickOfBar to be unreliable so I don't think it's a realistic option.

          Specifically, when using volume bars in live trading sometimes FTOB fails to flag a new bar. I've monitored this with Print() statements. It can happen in backtesting, too, but it's rare for FTOB to fail in that case.

          ~Lou

          Comment


            #6
            Hi bluelou,

            FirstTickOfBar will only work in real-time data and does not work in backtest.

            From the help guide:
            "Indicates if the incoming tick is the first tick of a new bar. This property is only of value in scripts that run tick by tick which is when the CalculateOnBarClose property is set to false.
            NOTE: This property should NOT be accessed outside of the OnBarUpdate() method."
            http://www.ninjatrader.com/support/h...ttickofbar.htm

            I have never found FirstTickOfBar to work incorrectly. Do you have an example "toy" script that can reproduce this?
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              ChelseaB,
              Yes, sorry, you are correct, FTOB has no effect on a backtest. No, sorry, I don't have any toy script. FWIW, I'd say it fails to mark a new bar around 1 out of 1000 bars. I have it in an indicator which I'm recoding so I can remove it.

              ~Lou

              Originally posted by NinjaTrader_ChelseaB View Post
              Hi bluelou,

              FirstTickOfBar will only work in real-time data and does not work in backtest.

              From the help guide:
              "Indicates if the incoming tick is the first tick of a new bar. This property is only of value in scripts that run tick by tick which is when the CalculateOnBarClose property is set to false.
              NOTE: This property should NOT be accessed outside of the OnBarUpdate() method."
              http://www.ninjatrader.com/support/h...ttickofbar.htm

              I have never found FirstTickOfBar to work incorrectly. Do you have an example "toy" script that can reproduce this?

              Comment


                #8
                Originally posted by NinjaTrader_ChelseaB View Post
                Hi bluelou,

                FirstTickOfBar will only work in real-time data and does not work in backtest.

                From the help guide:
                "Indicates if the incoming tick is the first tick of a new bar. This property is only of value in scripts that run tick by tick which is when the CalculateOnBarClose property is set to false.
                NOTE: This property should NOT be accessed outside of the OnBarUpdate() method."
                http://www.ninjatrader.com/support/h...ttickofbar.htm

                I have never found FirstTickOfBar to work incorrectly. Do you have an example "toy" script that can reproduce this?
                Well, that is what the literature says, and on the face of it it is correct. However, the more correct statement might be that if COBC = true, or in Backtest mode, then the FirstTickOfBar is the same as the "Only Tick Of Bar". ie., it will be the same as the only tick that activates processing on the bar.
                Last edited by koganam; 12-20-2014, 10:06 AM. Reason: Corrected logical statement.

                Comment


                  #9
                  Originally posted by NinjaTrader_ChelseaB View Post
                  Hi bluelou,

                  FirstTickOfBar will only work in real-time data and does not work in backtest.

                  From the help guide:
                  "Indicates if the incoming tick is the first tick of a new bar. This property is only of value in scripts that run tick by tick which is when the CalculateOnBarClose property is set to false.
                  NOTE: This property should NOT be accessed outside of the OnBarUpdate() method."
                  http://www.ninjatrader.com/support/h...ttickofbar.htm

                  I have never found FirstTickOfBar to work incorrectly. Do you have an example "toy" script that can reproduce this?
                  Hi,

                  I think the documentation on FirstTickOfBar should be expanded to explain how it works/doesn't work with regard to Historical data processing.

                  For example, let's say you want to back-fill a 1-Min indicator, built on a tick-by-tick basis, with historical tick data via:
                  Add(this.Instrument.FullName,PeriodType.Tick,1,Mar ketDataType.Last); //BarsInProgress==1

                  In this scenario, when Ninja is processing Historical data :

                  - it first calls OnBarUpdate for EACH tick of the primary bar (the 1-Min bar), with BarsInProgress==1 (the .Last series), and
                  - then calls OnBarUpdate for the LAST tick of the primary bar (the 1-Min bar) with BarsInProgress=0.

                  [ie, it will call OnBarUpdate twice for the LAST tick]

                  But, FirstTickOfBar is always true, which is incorrect when BarsInProgress==0.

                  Furthermore, a reference to Close[0] or Closes[0][0] when BarsInProgress==0 will refer to the LAST tick.

                  Comment


                    #10
                    Hello pbmi66,

                    Originally posted by pbmi66 View Post
                    Hi,

                    I think the documentation on FirstTickOfBar should be expanded to explain how it works/doesn't work with regard to Historical data processing.

                    For example, let's say you want to back-fill a 1-Min indicator, built on a tick-by-tick basis, with historical tick data via:
                    Add(this.Instrument.FullName,PeriodType.Tick,1,Mar ketDataType.Last); //BarsInProgress==1

                    In this scenario, when Ninja is processing Historical data :

                    - it first calls OnBarUpdate for EACH tick of the primary bar (the 1-Min bar), with BarsInProgress==1 (the .Last series), and
                    - then calls OnBarUpdate for the LAST tick of the primary bar (the 1-Min bar) with BarsInProgress=0.

                    [ie, it will call OnBarUpdate twice for the LAST tick]

                    But, FirstTickOfBar is always true, which is incorrect when BarsInProgress==0.

                    Furthermore, a reference to Close[0] or Closes[0][0] when BarsInProgress==0 will refer to the LAST tick.
                    In this situation, do you have Calculate on bar close set to false as says is required in the documentation?

                    Are you running this in historical data? (With historical data Calculate on bar close is always true. From the help guide:
                    When indicators or strategies are running on historical data, OnBarUpdate() is only called on the close of each historical bar even if this property is set to false
                    http://www.ninjatrader.com/support/h...onbarclose.htm
                    Chelsea B.NinjaTrader Customer Service

                    Comment


                      #11
                      COBC strategy - indicator

                      Hello,

                      what if the strategy is running COBC=true, is it then possible to have an indicator working with COBC=false?

                      And is it possible to have the indicator in another dataseries with different COBC

                      stragegy dataseries 1 minute with COBC=false and indicator used in the script with 2 Range and COBC=true
                      or
                      strategy dataseries 1 minute with COBC=true and indicator used in the script with 2 range and COBC=false?

                      Thank you
                      Tony

                      Originally posted by NinjaTrader_ChelseaB View Post
                      Hello pbmi66,



                      In this situation, do you have Calculate on bar close set to false as says is required in the documentation?

                      Are you running this in historical data? (With historical data Calculate on bar close is always true. From the help guide:
                      When indicators or strategies are running on historical data, OnBarUpdate() is only called on the close of each historical bar even if this property is set to false
                      http://www.ninjatrader.com/support/h...onbarclose.htm
                      Last edited by tonynt; 09-15-2015, 02:34 PM. Reason: typo

                      Comment


                        #12
                        tonynt,
                        I'm pretty sure that if you initialize the strategy with COBC = true then the indicators must also be COBC = true. But, we can leave it to the NT people to confirm.

                        Then for the indicators, it's a matter of putting them in the appropriate BarsInProgress. Of course, you can have a 1 quote or 1 trade bar, too.

                        Comment


                          #13
                          Hi Tony,

                          While NinjaTrader does not prevent an indicator from being set to Calculate on bar close as False while the calling script is set with Calculate on bar close is set to True, I have seen behavior where the updates to OnBarUpdate are out of order when this is the case. Our development discourages using different Calculate on bar close settings for a calling and callie script.

                          (The behavior is that you may see all of the historical updates of the indicator script all at once when the strategy starts instead of seeing the scripts alternate in OnBarUpdate() updates when processing historical data.)

                          If a strategy is set with Calculate on bar close as False, it will call the indicator with Calculate on bar close False as well.
                          Chelsea B.NinjaTrader Customer Service

                          Comment


                            #14
                            Hello Chelsea,

                            thank you for your reply. Let me do the question different please for understanding all this:

                            a.) I work with conditions in onmaketdata in my script-strategy. When in Initialize is COBC=true then the conditions in onmarketdata work nevertheless on tick (like COBC=false) Correct?

                            b.) when I use for one of the conditions in onmarketdata "&&FirstTickOfBar" will this work then with close of bar or not because in onmarketdata there is no FirstTickOfBar possible?

                            c.) when I have a script-strategy running lets say in a 5 min chart. And I add an indicator with 1 min chart. And lets say after 2 minutes the "1-minute-indicator" shows the condition - will this work after 2 minutes (with COBCtrue of 2nd 1-minute-bar) in my script or only after 5 minutes (with COBCtrue of the 5 min bar)? Accurate example I need it for: an entrylimitorder I have in the 5 min chart working should be cancelled with a condition from the 1-minute-chart. If the 1-minute-chart-condition is true after 2 minutes, will this cancel the limitorder after 2 minutes at end of the 1-minute-bar (COBCtrue of the 1 minute) or only after the close of the 5min bar where the strategy is on?

                            Thank you
                            Tony

                            Comment


                              #15
                              Hi Tony,

                              a) OnMarketData is similar to Calculate on bar close but includes more information and does not work historically (at all). With Calculate on bar close there is an update to OnBarUpdate for every last tick. With OnMarketData, there is update for every last, bid, and ask tick.

                              b) FirstTickOfBar does not work in OnMarketData, however, you could make your own variable that is a bool. Set the bool to false, until OnBarUpdate triggers, and then set it to true. The next tick in OnMarketData can see that is true and you can consider this update the first tick of the next bar. Then set the bool back to false waiting for the next bar to set it back to true.

                              c) If you are using a 5 minute chart and you have an indicator that is calling a 2 minute secondary series for calculations, you are still only going to get a value once every 5 minutes. An indicator plot is synced with the primary bar series (unless you use special code to sync this to a secondary series). In other words, the plot is only going to have 1 value for every 5 minute bar and won't have indexes for the 2 minute bars in between. So even though you can call the secondary series, you wouldn't have a place to store it since it has more bars than the larger time-frame. (Though again, you can sync a plot to a secondary series so that may be a workaround)

                              When it comes to liveUntilCancelled, the secondary timeframe can cause your order to be cancelled IF the secondary series is using the same instrument.
                              But you can of course manually cancel an order at anytime from any bars in progress.
                              Chelsea B.NinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by pstrusi, Today, 09:06 AM
                              0 responses
                              1 view
                              0 likes
                              Last Post pstrusi
                              by pstrusi
                               
                              Started by vpzdcv, Today, 02:31 AM
                              0 responses
                              9 views
                              0 likes
                              Last Post vpzdcv
                              by vpzdcv
                               
                              Started by YevhenShynkarenko, Today, 01:22 AM
                              1 response
                              18 views
                              0 likes
                              Last Post YevhenShynkarenko  
                              Started by ttodua, Today, 12:52 AM
                              0 responses
                              8 views
                              0 likes
                              Last Post ttodua
                              by ttodua
                               
                              Started by ttodua, Today, 12:50 AM
                              0 responses
                              8 views
                              0 likes
                              Last Post ttodua
                              by ttodua
                               
                              Working...
                              X