Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Error for 'OnBarUpdate' method on bar 20;

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

    Error for 'OnBarUpdate' method on bar 20;

    I am getting this error even after my script passes compiling. I go to add it to the chart and not only do the lines not show up but in the log's I get the above error. I have tried to adjust the coding but to no avail.

    Here is the code. Any help would be great as I have beat my head on the wall for 2 days.

    namespace NinjaTrader.NinjaScript.Indicators
    {
    public class Lynn50percent : Indicator
    {
    private double swingHigh = 0;
    private double swingLow = 0;
    private double retracementLevel = 0;
    private double priceDifference = 0;

    // Flag to detect if price has crossed the retracement level
    private bool priceHa****Retracement = false;

    // Period to look back for swing highs and lows
    private int lookBackPeriod = 20;

    [Range(1, int.MaxValue), NinjaScriptProperty]
    public int LookBackPeriod
    {
    get { return lookBackPeriod; }
    set { lookBackPeriod = value; }
    }

    protected override void OnBarUpdate()
    {
    // Ensure that enough bars are available for the calculations
    if (CurrentBar < lookBackPeriod)
    return;

    // 1. Calculate swing high and swing low based on the look-back period
    int swingHighIndex = HighestBar(High, lookBackPeriod);
    int swingLowIndex = LowestBar(Low, lookBackPeriod);

    // If the indices are valid, update the swingHigh and swingLow values
    if (swingHighIndex >= 0 && swingLowIndex >= 0)
    {
    swingHigh = High[swingHighIndex];
    swingLow = Low[swingLowIndex];
    }

    // 2. Calculate the 50% retracement level
    retracementLevel = swingLow + (swingHigh - swingLow) * 0.5;

    // 3. Check if price has crossed the retracement level
    if (Close[0] <= retracementLevel && !priceHa****Retracement)
    {
    priceHa****Retracement = true; // Mark that price has hit the retracement level
    }

    // 4. If price has hit the retracement level, calculate the price difference
    if (priceHa****Retracement)
    {
    if (Close[0] > retracementLevel)
    {
    // Calculate price difference from swing high (for long position)
    priceDifference = Math.Abs(Close[0] - swingHigh);
    }
    else
    {
    // Calculate price difference from swing low (for short position)
    priceDifference = Math.Abs(Close[0] - swingLow);
    }
    }

    // 5. Plot the Swing High, Swing Low, and 50% Retracement Level
    // Plot the Swing High
    Draw.HorizontalLine("SwingHigh", swingHigh, Brushes.Blue);
    // Plot the Swing Low
    Draw.HorizontalLine("SwingLow", swingLow, Brushes.Blue);
    // Plot the 50% Retracement Level
    Draw.HorizontalLine("RetracementLevel", retracementLevel, Brushes.Red);

    // 6. If price has crossed the retracement level, display the price difference on the chart
    if (priceHa****Retracement)
    {
    string priceDiffText = "Price Diff: " + priceDifference.ToString("0.00");

    // Create a unique label ID by appending CurrentBar
    string labelId = "PriceDiffLabel_" + CurrentBar;

    // Adjust the y-position for visibility of the text
    double yPosition = High[0] + (swingHigh - swingLow) * 0.1;

    // Draw the text label on the chart with the unique label
    Draw.Text(this, labelId, priceDiffText, 0, yPosition, Brushes.Black);
    }
    }
    }
    }​

    #2
    Hello pipsinthehood,

    Thank you for your post.

    Can you please provide a screenshot of the full error message? "Error for 'OnBarUpdate' method on bar 20:" the log usually will provide further details after the colon on what error your strategy is hitting.

    I look forward to assisting further.

    Comment


      #3
      Here you go.

      Click image for larger version

Name:	image.png
Views:	205
Size:	2.7 KB
ID:	1325450

      Comment


        #4
        Hello pipsinthehood,

        This was posted in the NinjaTrader 7 section of the forums.
        To confirm, this is for NinjaTrader 7 correct?

        What is the specific line of code causing the error?

        If you are uncertain, print the line number above each method call, assignment, and branching command (if statement) in OnBarUpdate() and reproduce the error.

        The last print to appear should be one line above the line causing the error.

        Below is a link to a forum post on adding debugging prints to understand behavior.
        Chelsea B.NinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by Geovanny Suaza, 02-11-2026, 06:32 PM
        0 responses
        547 views
        0 likes
        Last Post Geovanny Suaza  
        Started by Geovanny Suaza, 02-11-2026, 05:51 PM
        0 responses
        323 views
        1 like
        Last Post Geovanny Suaza  
        Started by Mindset, 02-09-2026, 11:44 AM
        0 responses
        99 views
        0 likes
        Last Post Mindset
        by Mindset
         
        Started by Geovanny Suaza, 02-02-2026, 12:30 PM
        0 responses
        543 views
        1 like
        Last Post Geovanny Suaza  
        Started by RFrosty, 01-28-2026, 06:49 PM
        0 responses
        545 views
        1 like
        Last Post RFrosty
        by RFrosty
         
        Working...
        X