Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Error on calling OnBarUpdate method on bar 50

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

    Error on calling OnBarUpdate method on bar 50

    Hello All,

    I have recently encountered a problem where my code is not running as I run into an error that reads: "Error on calling OnBarUpdate method on bar 0"

    I do have a fold loop in the code but I thought I took all necessary precautions to squash any bugs.
    Compared to other coding languages, I am relatively new to C#, and this portion of my development is very confusing.

    I have included my code here:

    Code:
    protected override void OnBarUpdate()
    {
    if (BarsInProgress != 0)
    return;
    if (CurrentBars[0] < LengthCCI)
    return;
    else
    {
    if (CurrentBar == 0)
    Value[0] = 0;
    else
    {
    double input0 = Input[0];
    double high0 = High[0];
    double low0 = Low[0];
    
    ATRval = ATR(ST_nATR)[0];
    ATRCCI = (ATR(LengthATR)[0]) * AtrFactor;
    
    UP = ((high0)+(low0))/2 + (ST_Atr_Mult * ATRval);
    DN = ((high0)+(low0))/2 + ((-(ST_Atr_Mult)) * ATRval);
    
    // CCI_ATR measures distance from the mean. Calculates a trend
    // line based on that distance using ATR as the locator for the line.
    
    bar = CurrentBars[0];
    
    price = (Close[0] + Low[0] + High[0]);
    
    //This is for calculating LinDev
    double x_bar = ((SMA(High, LengthCCI))[0]+(SMA(Close, LengthCCI)[0])+(SMA(Low, LengthCCI)[0]));
    for(int i = 0; i < LengthCCI; i++)
    {
    TAD[0] = TAD[1] + Math.Abs((High[i]+Low[i]+Close[i])-(x_bar));
    
    if (LengthCCI > 0)
    lindev = (TAD[0]/LengthCCI);
    else
    lindev = 0;
    }
    
    //ATR vals
    
    ATRval = ATR(ST_nATR)[0];
    ATRCCI = (ATR(LengthATR)[0]) * AtrFactor;
    
    if (Close[0] < ST[1])
    {
    ST[0] = UP;
    }
    else
    {
    ST[0] = DN;
    }
    
    if (lindev == 0.0)
    {
    myCCI = 0;
    }
    else
    {
    myCCI = ((price - x_bar) / lindev / 0.015);
    }
    
    if (myCCI > 0)
    {
    MT1[0] = (Math.Max(MT1[1], Median[0] - ATRCCI));
    }
    else
    {
    MT1[0] = (Math.Min(MT1[1], Median[0] + ATRCCI));
    }
    
    // Alignment of Supertrend and CCI ATR indicators
    if ((Close[0]> ST[0]) & (Close[0] > MT1[0]))
    {
    Stade[0] = (StateUp);
    }
    else if ((Close[0] < ST[0]) & (Close[0] < MT1[0]))
    {
    Stade[0] = StateDn;
    }
    else
    Stade[0] = Stade[1];
    /*newState = HighestAll(if State <> State[1] then bar else 0);*/
    if ((Stade[0] != Stade[1]))
    newState = CurrentBar;
    else
    newState = 0;
    // Combined Signal Approach - Supertrend and ATR CCI
    
    if (bar >= newState)
    {
    CSAplot[0] = MT1[0];
    Print(CSAplot[0]);
    
    }
    else
    {
    CSAplot[0] = Double.NaN;
    Print(CSAplot[0]);
    }
    
    
    /* Buy/Sell Arrows */
    if ((Stade[0] == StateUp) & (Stade[1] != StateUp))
    {
    BuySignalArrow[0] = (0.998 * MT1[0]);
    
    }
    else
    {
    BuySignalArrow[0] = Double.NaN;
    }
    
    if ((Stade[0] == StateDn) & (Stade[1] != StateDn))
    {
    SellSignalArrow[0] = (1.002 * MT1[0]);
    
    }
    else
    {
    SellSignalArrow[0] = Double.NaN;
    
    }
    
    }

    #2
    Hello ArcticBiscuit

    The error states that it happened on the first bar so you would need to figure out which specific line is throwing the error. You can start by commenting out the later lines of code in OnBarUpdate so you can further isolate the specific line.

    This could relate to your initial if conditions with the else, generally the code you have should look like this:


    Code:
    protected override void OnBarUpdate()
    {
    if (BarsInProgress != 0)
    return;
    
    if (CurrentBars[0] < LengthCCI)
    {
    Value[0] = 0; // set 0 on all bars before LengthCCI
    return;
    }
    
    double input0 = Input[0];
    double high0 = High[0];
    double low0 = Low[0];
    
    ....

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by NullPointStrategies, Yesterday, 05:17 AM
    0 responses
    55 views
    0 likes
    Last Post NullPointStrategies  
    Started by argusthome, 03-08-2026, 10:06 AM
    0 responses
    132 views
    0 likes
    Last Post argusthome  
    Started by NabilKhattabi, 03-06-2026, 11:18 AM
    0 responses
    73 views
    0 likes
    Last Post NabilKhattabi  
    Started by Deep42, 03-06-2026, 12:28 AM
    0 responses
    45 views
    0 likes
    Last Post Deep42
    by Deep42
     
    Started by TheRealMorford, 03-05-2026, 06:15 PM
    0 responses
    49 views
    0 likes
    Last Post TheRealMorford  
    Working...
    X