Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Writing a bool Method. Getting error CS0161.

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

    Writing a bool Method. Getting error CS0161.


    How do I solve the error?

    protected ISeries<double> argument1;
    protected ISeries<double> argument2;
    private int lookBackPeriod;

    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    Description = @"Enter the description for your new custom Indicator here.";
    Name = "aRectsDemo";
    Calculate = Calculate.OnBarClose;
    IsOverlay = true;
    DisplayInDataBox = true;
    DrawOnPricePanel = true;
    DrawHorizontalGridLines = false;
    DrawVerticalGridLines = false;
    PaintPriceMarkers = false;
    ScaleJustification = NinjaTrader.Gui.Chart.ScaleJustification.Right;
    //Disable this property if your indicator requires custom values that cumulate with each new market data event.
    //See Help Guide for additional information.
    IsSuspendedWhileInactive = true;
    Ticktimeframe = 1000;
    }
    else if (State == State.Configure)
    {
    AddDataSeries(Data.BarsPeriodType.Tick, Ticktimeframe);
    }
    }

    protected override void OnBarUpdate()
    {

    WasAbove(Close, SMA(20), 20);

    }

    protected bool WasAbove (ISeries<double> argument1, ISeries<double> argument2, int lookBackPeriod)
    {
    for (int i = 0; i < lookBackPeriod; i++)
    {
    if (argument1[i] > argument2[i])
    {
    return true;
    }

    else
    return false;
    }
    }

    #2
    Hello jamestrader21x,

    If you want to learn about that error specifically you can search for any error code you see in the platform on google, we are using C# so you can find the explanation in MSDN or also other websites like stackoverflow.

    Your method does not return in all paths, it always has to return something so it would need to be written like the following:

    Code:
    private bool WasAbove (ISeries<double> argument1, ISeries<double> argument2, int lookBackPeriod)
    {
        bool result = false;
        for (int i = 0; i < lookBackPeriod; i++)
        {
            if (argument1[i] > argument2[i])
            {
                 result = true;
             }
        }
        return result;
    }
    as a side note you are currently not getting the return value of the method in OnBarUpdate, you could use a variable:

    Code:
    bool result = WasAbove(Close, SMA(20), 20);

    Comment


      #3
      I checked online before posting. I couldn't find a satisfying answer. I appreciate the help. By the way, how did you post that code in like that? I've always wonder how people do that on this forum.

      Comment


        #4
        Hello jamestrader21x,

        To add code blocks you need to click the A in the top right of the forum text editor (advanced editor) and then there is a hash tag: # in the toolbar, you select the text and then press the button.

        Comment


          #5
          Thanks for the help! I got my code updated. Even overloaded it. I don't know why Ninja Trader doesn't have a 'WasAbove/Below' method. CrossAbove/Below only covers entry above/below thresholds. Sometimes ISeries<doubles> stay above threshold for quite awhile; making CrossAbove/Below useless or you have to extend the look back period. Again, thanks for the help!

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by Geovanny Suaza, 02-11-2026, 06:32 PM
          0 responses
          556 views
          0 likes
          Last Post Geovanny Suaza  
          Started by Geovanny Suaza, 02-11-2026, 05:51 PM
          0 responses
          324 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
          545 views
          1 like
          Last Post Geovanny Suaza  
          Started by RFrosty, 01-28-2026, 06:49 PM
          0 responses
          547 views
          1 like
          Last Post RFrosty
          by RFrosty
           
          Working...
          X