Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Warning: Unreachable code detected

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

    Warning: Unreachable code detected

    Hello all,
    Currently writing an indicator and have ran into an issue I can't quite wrap my head around.

    What this function is doing is checking to see from the bar that started the indicator (let's call it bar "30") until the current bar (let's call it bar "53"), which should be the next step of the indicator, has any bar in between bar 30 and bar 53 had a high that's over the current EMA(21).

    What I expected to happen is if the high of the current bar is greater than the current EMA(21) then we return false because we do not want to continue; if it is not greater then we want to return true and continue with the indicator. But for some reason it's telling me that it can't get to the "return true;".

    I am obviously missing something simple but not sure what...please help...

    Code:
    protected bool canContinue (int n) {
    for (int i = n; i >= startLegNum; i--) {
    if (High[i] > ema21[0]) {
    return false;
    }
    
    return true;
    }
    }
    Thank you,
    Travis Sloneker

    #2
    Hello Travis, thanks for your post.

    It has the chance of it not doing the loop at all and there is no return statement outside of the FOR loop. E.g. if n == startLegNum it will not go into the FOR loop.

    Please let me know if I can assist any further.

    Comment


      #3
      Originally posted by NinjaTrader_ChrisL View Post
      Hello Travis, thanks for your post.

      It has the chance of it not doing the loop at all and there is no return statement outside of the FOR loop. E.g. if n == startLegNum it will not go into the FOR loop.

      Please let me know if I can assist any further.
      Chris,
      Thank you for your help; I have corrected it based on what you suggested but I am still not understanding why it is always returning true.

      Below is the updated code; anytime I try to add a "return false;" after the "if (ema21[0] > High[0])" or even put it in an else it tells me it can't reach it. I would expect if that condition isn't true the code would move to the next line which would be where the "return false;" would be; I'll show you what I mean in the second code block.

      Code Block #1:

      Code:
      protected bool canContinue (int n) {
        if (n > startLegNum) {
          for (int i = n; i >= startLegNum; i--) {
            Print("EMA: " + ema21[0]);
            Print("CH: " + High[0]);
            if (ema21[0] > High[0]) {
              return true;
            }
          }
        }
      
      return false;
      }
      Code Block #2:

      Code:
      protected bool canContinue (int n) {
        if (n > startLegNum) {
          for (int i = n; i >= startLegNum; i--) {
            Print("EMA: " + ema21[0]);
            Print("CH: " + High[0]);
            if (ema21[0] > High[0]) {
              return true;
            }
            return false;
          }
        }
      
      return false;
      }
      Last edited by travisloneker; 03-22-2021, 10:02 AM.

      Comment


        #4
        Hello Travis, thanks for your reply.

        It sounds like your condition is always, at some point, evaluating this as true:

        ema21[0] > High[0]

        Can you confirm this with your prints? I see you are already printing out ema21 and the High price. On the first iteration, it is evaluating that condition as true. This should be clear from the prints you have set up.

        Kind regards.



        Comment


          #5
          Originally posted by NinjaTrader_ChrisL View Post
          Hello Travis, thanks for your reply.

          It sounds like your condition is always, at some point, evaluating this as true:

          ema21[0] > High[0]

          Can you confirm this with your prints? I see you are already printing out ema21 and the High price. On the first iteration, it is evaluating that condition as true. This should be clear from the prints you have set up.

          Kind regards.


          Chris,
          I figured it out; I wasn't using the variable "i" to iterate through the bars leading back to the starting bar. Sometimes it literally just takes talking to someone!

          Thank you,
          Travis Sloneker

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by Geovanny Suaza, 02-11-2026, 06:32 PM
          0 responses
          663 views
          0 likes
          Last Post Geovanny Suaza  
          Started by Geovanny Suaza, 02-11-2026, 05:51 PM
          0 responses
          376 views
          1 like
          Last Post Geovanny Suaza  
          Started by Mindset, 02-09-2026, 11:44 AM
          0 responses
          110 views
          0 likes
          Last Post Mindset
          by Mindset
           
          Started by Geovanny Suaza, 02-02-2026, 12:30 PM
          0 responses
          575 views
          1 like
          Last Post Geovanny Suaza  
          Started by RFrosty, 01-28-2026, 06:49 PM
          0 responses
          580 views
          1 like
          Last Post RFrosty
          by RFrosty
           
          Working...
          X