Announcement

Collapse
No announcement yet.

barstate_newbar question

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

  • barstate_newbar question

    I have the following code in my script
    if(getBarState()==BARSTATE_NEWBAR && getCurrentBarIndex() == 0) {
    var vMA = MA.getValue(External.MA);
    }

    where MA uses an external function which provides a 5 period Moving Average of the close price.

    Can you please confirm that the BARSTATE_NEWBAR if statement above does the following:

    Wait for first tick of a new bar then calculate the 5period MA of 5 PREVIOUS bars (i.e. bars -1 to -5) and not the current (new) bar close?

    Because I think if I reload the study it slightly changes the value, making me think that in fact it calculates the 5 periods as the last 4 bars and the close of the brand new bar (which is actually the open price at the time...)?

  • #2
    Hello grantanderson,

    At newbar, the first tick of that bar is the 5th price used for the calculation. If you want to get the prior bar's value at newbar you could just do,

    var vMA = MA.getValue(External.MA, -1);

    If close() is the price source for the study and you only allow the formula to use the first tick of each bar to calculate the MA it will be different when you reload. This is because close() at newbar in real time mode is the first tick of that bar, which is the same as the open() for that bar. At newbar on historical bars during a reload, close() now gives you the actual close of the bar. Does this help?
    Jason K.
    Project Manager
    eSignal - an Interactive Data company

    EFS KnowledgeBase
    JavaScript for EFS Video Series
    EFS Beginner Tutorial Series
    EFS Glossary
    Custom EFS Development Policy

    New User Orientation

    Comment


    • #3
      thanks for that! Yes that's what I thought was happening so it all makes sense now.

      Comment

      Working...
      X