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

How to access the instant value of indicator?

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

    How to access the instant value of indicator?

    Hi folks,

    My indicate calculates its value by referring to other indicators. I can see its value is ticking before a price candle stick is completed. In practice, I want to have access to this value that is still changing to guesstimate those potential next moves that it could be. It could be a little bit late to defer the decisions until the completion of current bar.

    The question is: how to access this "instant" value of indicators? I know Indicator[0] is refer to the latest indicator value of last completed bar. But how about its latest value when the bar is not completed yet?

    Thank you very much!


    Best Regards
    David

    #2
    If the indicator is calculating when CalculateOnBarClose=False, then it will also
    be updating the plot values inside those plot series on every tick.

    It must be doing this already, otherwise you would not see those chart updates
    on every tick, per your description.

    What that means is that something like EMA(21)[0] will have its value at BarsAgo
    index '[0]' constantly updated, on every tick.

    So, inside your own OnBarUpdate, you would just use EMA(21)[0] everywhere
    in your own calculations, even in COBC-aware code per my post here.

    EMA(21)[0] is guaranteed to return the most current value from EMA(21),
    even if the current bar has not closed -- this means, on every tick, the value
    available at EMA(21)[0] can be treated as if it were the value at bar close.

    See that? Any tick could be the last tick of the bar, so when processing every
    tick using CalculateOnBarClose=False, EMA(21)[0] is always considered to
    be up to date -- so just use it as-is.

    The only time that may not be what you want is on the first tick of a new bar.
    In that specific case, the values at Open[0], High[0], Low[0], and Close[0] are
    all the same, so you may need to use '[1]' indexing to get the actual value of
    the last closed bar.

    [In more detail:
    When COBC=False and FirstTickOfBar is true, then to guarantee you are
    looking at the last calculated EMA value for the last closed bar, you would
    use EMA(21)[1]. But you only care about that kind of '[1]' index shifting when
    COBC=False -- and you only do that because you know that using the shifted
    value at EMA(21)[1] is more important to your calculations than EMA(21)[0].
    The point is: CalculateOnBarClose slightly redefines the meaning of what the
    value at BarsAgo index '[0]' really means -- but, generally speaking, you can
    go a long ways coding indicators before you need to know or care about
    those details. CaclulateOnBarClose gets deep quickly, and caring about
    it real deeply usually means you're doing something somewhat advanced.]

    But I digress.

    So, in general, you don't have to worry much about "instant" values -- they
    are there automatically when CalculateOnBarClose=False. Start studying
    some builtin indicators like @EMA.cs, @CCI.cs, and @MACD.cs -- those
    indys don't give a rat's patooty about the CalculateOnBarClose setting, but
    yet, you'll notice most indicators work just fine regardless of COBC set to
    True or False -- only a very few require COBC=False to function properly.

    What I mean is,
    The code in the builtin indicators tend to care more about having enough
    bars for calculations, thus you see constant checks using CurrentBar. What
    you hardly see any of is checks for CalculateOnBarClose or FirstTickOfBar.
    Yet all the builtin indicators work just fine with COBC=False -- what does that
    tell you? Most builtin indicators don't care much about CalculateOnBarClose,
    so you don't need to care that much either.

    So, write your code using normal accesses, like EMA(21)[0] or High[0] or
    Close[0], etc, and just realize those '[0]' accesses (aka BarsAgo indexes)
    are giving you the most current values, regardless of the COBC setting.

    Then, set all your current plot values, like MyPlot1.Set(currVal), using whatever
    calculation is needed to define 'currVal'. That is, your formula for CurrVal should
    use whatever indicator values and/or OHLC values is needed, using whatever
    '[0]' or '[1]' or '[n]' BarsAgo indexing that is needed -- per normal -- just like the
    formulas in the builtin indicators are doing.

    Finally, you asked,

    Originally posted by sinpeople View Post
    The question is: how to access this "instant" value of indicators? I know Indicator[0] is refer to the latest indicator value of last completed bar. But how about its latest value when the bar is not completed yet?
    The answer is: you do the same thing, you use Indicator[0] -- or more
    accurately, you would use Indicator(args)[0]. In your code, the "instant"
    value is accessed the same way as the EndOfBar value -- it is the value
    of the CalculateOnBarClose setting that defines what "instant" means --
    not the access code.

    The '[0]' is a BarsAgo index against a series (not an array) and it will
    always give you the latest calculated value, regardless of the current
    CalculateOnBarClose setting -- which I think underpins your question.

    You could also try studying this reference sample for some answers.

    I can't seem to locate the help page that had clear discussions of
    CalculateOnBarClose, and what False meant for BarsAgo indexing.
    Sorry, I thought one existed, but I can't find it.

    Maybe Support folks know a good reference for turning a COBC
    newbie into a COBC expert?

    Bookmark this Table of Contents page for more reference samples, this
    page has no external links that point to it anymore -- strange, but true.
    Last edited by bltdavid; 08-28-2020, 12:44 AM.

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by StockTrader88, 03-06-2021, 08:58 AM
    44 responses
    3,966 views
    3 likes
    Last Post jhudas88  
    Started by rbeckmann05, Today, 06:48 PM
    0 responses
    4 views
    0 likes
    Last Post rbeckmann05  
    Started by rhyminkevin, Today, 04:58 PM
    4 responses
    54 views
    0 likes
    Last Post dp8282
    by dp8282
     
    Started by iceman2018, Today, 05:07 PM
    0 responses
    5 views
    0 likes
    Last Post iceman2018  
    Started by lightsun47, Today, 03:51 PM
    0 responses
    8 views
    0 likes
    Last Post lightsun47  
    Working...
    X