Announcement

Collapse
No announcement yet.

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 Geovanny Suaza, 02-11-2026, 06:32 PM
0 responses
579 views
0 likes
Last Post Geovanny Suaza  
Started by Geovanny Suaza, 02-11-2026, 05:51 PM
0 responses
334 views
1 like
Last Post Geovanny Suaza  
Started by Mindset, 02-09-2026, 11:44 AM
0 responses
101 views
0 likes
Last Post Mindset
by Mindset
 
Started by Geovanny Suaza, 02-02-2026, 12:30 PM
0 responses
554 views
1 like
Last Post Geovanny Suaza  
Started by RFrosty, 01-28-2026, 06:49 PM
0 responses
551 views
1 like
Last Post RFrosty
by RFrosty
 
Working...
X