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

Tip: CalculateBarOnClose can hurt you. How to think about it.

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

    Tip: CalculateBarOnClose can hurt you. How to think about it.

    Hi,
    When you are testing your indicator, remember to test with both
    CalculateBarOnClose = true and CalculateBarOnClose = false;

    This has been the cause of so many weird bugs for me I thought I'd summarise.

    Background
    Bar Numbering scheme
    Bars are numbered from Left to Right.
    The First bar in a series is 0, rising to the most recent bar = "n"
    The number of the bar can be obtained using the Bars.CurrentBar property.
    Most other properties also use this numbering scheme. Including ChartControl.FirstBarPainted & ChartControl.LastBarPainted these are really handy when overriding the PLOT method.
    Data Series Indexing Scheme
    The DataSeries that hold the Bars are Indexed from Right to Left.
    The most recent bar in a dataSeries has an Index of 0 eg: Close[0]. As you scroll left you move move toward the oldest bar eg: Close[ n].
    The maths to display your values on the screen

    To convert from one to the other
    So the way you map the values in the data series (ie Close[n]) to the bar displayed on the screen is to subtract your index from CurrentBar eg: Close[CurrentBar - cntBar]

    How CalculateBarOnClose hurts you.
    If CalculateBarOnClose = false the CurrentBar property is the same as described above & it all works well.

    Beware if CalculateBarOnClose = true.
    The CurrentBar property is 1 less than it would be if CalculateBarOnClose = false. You need to change your algorithm to compensate for that.

    This is because the trader has told you they don't want to see the bar that is forming. So there is 1 less bar you are supposed to draw. But the number of bars since you started are still the same. Its just the CurrentBar value is 1 less than the total number of bars. So implicitly your calculation becomes "(Close[ (CurrentBar-1) - cntBar]" & your index result of the rightmost, partially complete bar becomes "-1". This is invalid index value for the array. You are not supposed to access it nor display your indicator at that position. If you do, you'll see an error message in the log & your indicator will not display.
    To prevent this issue always check with something like
    Code:
     
    if ((CurrentBar - cntBar) < 0)
    continue;
    Slightly bigger sample showing iterating thru the bars & displaying thier values inside a PLOT method.
    Code:
     
    // ---< For each bar that is displayed on the screen >--- 
    for(int cntBar = ChartControl.FirstBarPainted; cntBar <= ChartControl.LastBarPainted; cntBar++) 
    { 
    // if (!ChartControl.ShowBarsRequired && cntBar < BarsRequired)
    // continue; 
    if ((CurrentBar - cntBar) < 0)
    continue;
     
    float x = ChartControl.GetXByBarIdx(cntBar); 
    graphics.DrawString( CurrentBar.ToString(), textFont, textBrush, x, y3, stringFormat); 
    graphics.DrawString( cntBar.ToString(), textFont, textBrush, x, y2, stringFormat); 
    graphics.DrawString( (CurrentBar - cntBar).ToString(), textFont, textBrush, x, y, stringFormat);
    }
    I hope this saves you some time.
    Dave
    Last edited by David Lean; 12-08-2009, 01:12 AM.

Latest Posts

Collapse

Topics Statistics Last Post
Started by GussJ, 03-04-2020, 03:11 PM
12 responses
3,239 views
0 likes
Last Post Leafcutter  
Started by AveryFlynn, Today, 04:57 AM
0 responses
5 views
0 likes
Last Post AveryFlynn  
Started by RubenCazorla, 08-30-2022, 06:36 AM
3 responses
79 views
0 likes
Last Post PaulMohn  
Started by f.saeidi, Yesterday, 12:14 PM
9 responses
25 views
0 likes
Last Post f.saeidi  
Started by Tim-c, Today, 03:54 AM
0 responses
5 views
0 likes
Last Post Tim-c
by Tim-c
 
Working...
X