Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Strategy User Defined Methods Question

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

    Strategy User Defined Methods Question

    Why does this method return an error that "..all code paths do not return a value"

    public bool uptrend (int period)
    {
    int trendup = 0;
    for(int barsAgo = 0; barsAgo < 8;barsAgo++)
    {


    if(Close[barsAgo] >= EMA(period)[barsAgo])
    {
    trendup = trendup + 1;
    if( trendup > 4)
    return true;
    return false;

    }

    }
    }

    #2
    Originally posted by gdavis74 View Post
    Why does this method return an error that "..all code paths do not return a value"

    public bool uptrend (int period)
    {
    int trendup = 0;
    for(int barsAgo = 0; barsAgo < 8;barsAgo++)
    {


    if(Close[barsAgo] >= EMA(period)[barsAgo])
    {
    trendup = trendup + 1;
    if( trendup > 4)
    return true;
    return false;

    }

    }
    }

    Hi

    I think the problems lie with your use of private/public bools and 'return'.

    1) A public bool (one which is also declared in Properties, must have a small first letter in Variables and is meant so that you can switch between true and false in the Indicator Box.

    It seems that. for your use here, you need to declare a private bool in Variables as follows:

    Code:
    private bool Uptrend  = false;
    (What is the function of the (int period)?)

    2) Within the 'if' statement, put something like this:

    if( trendup > 4)
    {
    uptrend = true;
    else uptrend = false;
    }

    The 'return' method is used on bars when you don't want the indicator to calculate at all, such as in:

    Code:
    if (CurrentBar < Period) return;
    to allow it to access the minimum number of previous bars, otherwise it won't plot.

    Comment


      #3
      Originally posted by gdavis74 View Post
      Why does this method return an error that "..all code paths do not return a value"

      public bool uptrend (int period)
      {
      int trendup = 0;
      for(int barsAgo = 0; barsAgo < 8;barsAgo++)
      {


      if(Close[barsAgo] >= EMA(period)[barsAgo])
      {
      trendup = trendup + 1;
      if( trendup > 4)
      return true;
      return false;

      }

      }
      }
      Your outermost block does not return. Whereas it is true that your logic is human-readable correct; the compiler really cannot handle that nicety. Move your return false statement into the outer block, and if necessary, use a break to reach that return statement, if you need to cut processing.

      Comment


        #4
        Hi gdavis74

        Listen to Koganam: he knows far, far more about these things than I do!

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by Geovanny Suaza, 02-11-2026, 06:32 PM
        0 responses
        650 views
        0 likes
        Last Post Geovanny Suaza  
        Started by Geovanny Suaza, 02-11-2026, 05:51 PM
        0 responses
        370 views
        1 like
        Last Post Geovanny Suaza  
        Started by Mindset, 02-09-2026, 11:44 AM
        0 responses
        109 views
        0 likes
        Last Post Mindset
        by Mindset
         
        Started by Geovanny Suaza, 02-02-2026, 12:30 PM
        0 responses
        574 views
        1 like
        Last Post Geovanny Suaza  
        Started by RFrosty, 01-28-2026, 06:49 PM
        0 responses
        577 views
        1 like
        Last Post RFrosty
        by RFrosty
         
        Working...
        X