Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

OnBarUpdate error

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

    OnBarUpdate error

    Hi
    I am doing some testing and looking at volume.
    The code below is a tiny modification of your example script blockvolume .


    private void CalculateMyBlockVolume(bool forceCurrentBar)
    {
    bool inTransition = State == State.Realtime && BarsArray[1].Count - 1 - CurrentBars[1] > 1;
    int whatBar = State == State.Historical || inTransition || Calculate != Calculate.OnBarClose || forceCurrentBar ? CurrentBars[1] : Math.Min(CurrentBars[1] + 1, BarsArray[1].Count - 1);

    if ((Instrument.MasterInstrument.InstrumentType == Cbi.InstrumentType.CryptoCurrency ? Core.Globals.ToCryptocurrencyVolume((long)BarsArra y[1].GetVolume(whatBar)) : BarsArray[1].GetVolume(whatBar)) >= BlockSize)
    {
    if (!inTransition && hasCarriedOverTransitionTick && !forceCurrentBar && Calculate == Calculate.OnBarClose)
    CalculateMyBlockVolume(true);

    hasCarriedOverTransitionTick = inTransition;
    blockValue += CountType == CountType.Volume ? (Instrument.MasterInstrument.InstrumentType == Cbi.InstrumentType.CryptoCurrency ? Core.Globals.ToCryptocurrencyVolume((long)BarsArra y[1].GetVolume(whatBar)) : BarsArray[1].GetVolume(whatBar)) : 1;



    if(Volume[0] != 0)// I tried also changing this to != null
    {
    mystr = Volume[0].ToString();
    mystr.Remove(2);
    Print(Volume[0].ToString() + " " + mystr);

    }

    Error on calling 'OnBarUpdate' method on bar -1: You are accessing an index with a value that is invalid since it is out-of-range. I.E. accessing a series [barsAgo] with a value of 5 when there are only 4 bars on the chart.

    Can you assist with where I am going wrong with this?
    If i change mystr.Remove to (1) - it works but doesn't alter the no of digits in the Print stmt.?

    edit - also what is bar -1; I am unsure how that can exist?
    Last edited by Mindset; 06-24-2020, 01:15 AM.

    #2
    Hello Mindset,

    Thanks for the post.

    The error is likely because of the length of the string, you are trying to remove starting at index 2, if the string does not have enough indexes for that it would fail. What specifically was the intention with the Remove(2)?

    The -1 means that happened before a primary bar processed. The current bar starts at -1 and will be 0 on the first processed bar. The logic is being called from the secondary series and likely had a problem before the primary bar processed.

    I look forward to being of further assistance.

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by CarlTrading, 03-31-2026, 09:41 PM
    1 response
    81 views
    1 like
    Last Post NinjaTrader_ChelseaB  
    Started by CarlTrading, 04-01-2026, 02:41 AM
    0 responses
    42 views
    0 likes
    Last Post CarlTrading  
    Started by CaptainJack, 03-31-2026, 11:44 PM
    0 responses
    64 views
    2 likes
    Last Post CaptainJack  
    Started by CarlTrading, 03-30-2026, 11:51 AM
    0 responses
    68 views
    0 likes
    Last Post CarlTrading  
    Started by CarlTrading, 03-30-2026, 11:48 AM
    0 responses
    55 views
    0 likes
    Last Post CarlTrading  
    Working...
    X