Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

If statement evaluating to true when it shouldn't?

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

    If statement evaluating to true when it shouldn't?

    Hi guys,

    Here's the code and the output:

    Code:
    Print("==========");
    Print(Times[0][0]);
    Print("Position.MarketPosition: " + Position.MarketPosition);
    Print("Closes[0][0]: " + Closes[0][0]);
    Print("Opens[0][0]): " + Opens[0][0]);
    Print(Closes[0][0]>Opens[0][0]);
    //if we're long and the close of the primary data series is greater than the open of the primary data series do something
    if (Position.MarketPosition == MarketPosition.Long && (Closes[0][0]>Opens[0][0]));
    {
         Print("we actually are getting in here");
    }
    Output:
    ==========
    2/3/2022 9:14:56 AM
    Position.MarketPosition: Flat
    Closes[0][0]: 4521
    Opens[0][0]): 4522
    False
    we actually are getting in here
    This is in OnBarUpdate() in BarsInProgress == 0.

    There are no other prints in the code and the way I see this is that it knows that the close is less than the open and it knows that we're flat yet it's still getting into the if statement.

    The secondary data series is a tick data series.

    I am absolutely perplexed.

    #2
    Hello WalterSkinner,

    Thank you for your post.

    You have a semicolon after the if statement so everything in the curly braces is being seen as just another statement that should get printed:

    Code:
    Print("==========");
    Print(Times[0][0]);
    Print("Position.MarketPosition: " + Position.MarketPosition);
    Print("Closes[0][0]: " + Closes[0][0]);
    Print("Opens[0][0]): " + Opens[0][0]);
    Print(Closes[0][0]>Opens[0][0]);
    //if we're long and the close of the primary data series is greater than the open of the primary data series do something
    if (Position.MarketPosition == MarketPosition.Long && (Closes[0][0]>Opens[0][0]))[B]; // <-- semicolon shouldn't be there[/B]
    {
    Print("we actually are getting in here");
    }
    If you remove that semicolon you should see the print as you would expect:

    Code:
    Print("==========");
    Print(Times[0][0]);
    Print("Position.MarketPosition: " + Position.MarketPosition);
    Print("Closes[0][0]: " + Closes[0][0]);
    Print("Opens[0][0]): " + Opens[0][0]);
    Print(Closes[0][0]>Opens[0][0]);
    //if we're long and the close of the primary data series is greater than the open of the primary data series do something
    if (Position.MarketPosition == MarketPosition.Long && (Closes[0][0]>Opens[0][0]))
    {
    Print("we actually are getting in here");
    }
    I can't tell you how many times I've done a similar thing and it's maddening to look for, so I totally understand your confusion!

    Please let us know if we may be of further assistance to you.

    Comment


      #3
      What a relief I thought I was losing my mind thank you!

      Works great now.

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by Geovanny Suaza, 02-11-2026, 06:32 PM
      0 responses
      647 views
      0 likes
      Last Post Geovanny Suaza  
      Started by Geovanny Suaza, 02-11-2026, 05:51 PM
      0 responses
      369 views
      1 like
      Last Post Geovanny Suaza  
      Started by Mindset, 02-09-2026, 11:44 AM
      0 responses
      108 views
      0 likes
      Last Post Mindset
      by Mindset
       
      Started by Geovanny Suaza, 02-02-2026, 12:30 PM
      0 responses
      572 views
      1 like
      Last Post Geovanny Suaza  
      Started by RFrosty, 01-28-2026, 06:49 PM
      0 responses
      573 views
      1 like
      Last Post RFrosty
      by RFrosty
       
      Working...
      X