Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Variable does not exist in current context

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

    Variable does not exist in current context

    I'm a complete newbie to C programming, so I'm probably missing something obvious.

    I'm trying to create a high water mark that captures the highest point reached in a long position. So, I create a variable named highWaterMark when the stock qualifies for entry, and then update it as the price increases. Here are the important parts of the code:

    ...
    protected override void OnBarUpdate()
    {
    double highWaterMark;
    if (Historical) return;
    if (entry condition for strategy)
    {
    EnterLong();
    // initialize high water mark to the entry price
    highWaterMark = GetCurrentBid();
    }
    if (Position.MarketPosition == MarketPosition.Long && Close[0] > highWaterMark)
    {
    highWaterMark = Close[0];
    }
    ...
    }


    The compiler flags the last if statement with the error "The name 'highWaterMark' does not exist in the current context". If the last if statement is commented out, everything works fine. If the last if statement is replaced with an assignment statement like "highWaterMark = Close[0];", then it compiles. Why can I change the value of highWaterMark but not reference it in an if statement? Am I initializing the variable in the wrong location?

    #2
    Originally posted by egan857 View Post
    I'm a complete newbie to C programming, so I'm probably missing something obvious.

    I'm trying to create a high water mark that captures the highest point reached in a long position. So, I create a variable named highWaterMark when the stock qualifies for entry, and then update it as the price increases. Here are the important parts of the code:

    ...
    protected override void OnBarUpdate()
    {
    double highWaterMark;
    if (Historical) return;
    if (entry condition for strategy)
    {
    EnterLong();
    // initialize high water mark to the entry price
    highWaterMark = GetCurrentBid();
    }
    if (Position.MarketPosition == MarketPosition.Long && Close[0] > highWaterMark)
    {
    highWaterMark = Close[0];
    }
    ...
    }


    The compiler flags the last if statement with the error "The name 'highWaterMark' does not exist in the current context". If the last if statement is commented out, everything works fine. If the last if statement is replaced with an assignment statement like "highWaterMark = Close[0];", then it compiles. Why can I change the value of highWaterMark but not reference it in an if statement? Am I initializing the variable in the wrong location?
    On each bar update, you are redeclaring highWaterMark without initializing it. You want to make highWaterMark a class variable, as it is to retain its value between updates.

    Comment


      #3
      Oops. I definitely don't want to reinitialize the variable on each bar update. So, here's where I show my C ignorance: how do I declare this as a class variable?

      Comment


        #4
        Originally posted by egan857 View Post
        Oops. I definitely don't want to reinitialize the variable on each bar update. So, here's where I show my C ignorance: how do I declare this as a class variable?
        Remove it from where it is now, and add it to the "Variables" region. That is the neatest way, but you can declare it outside of any event handler.

        Comment


          #5
          Yesss!!! Thank you very much! It seems so simple, but that's all it took to solve the problem. I have tested it out and the high water mark is updating properly.

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by Geovanny Suaza, 02-11-2026, 06:32 PM
          0 responses
          589 views
          0 likes
          Last Post Geovanny Suaza  
          Started by Geovanny Suaza, 02-11-2026, 05:51 PM
          0 responses
          342 views
          1 like
          Last Post Geovanny Suaza  
          Started by Mindset, 02-09-2026, 11:44 AM
          0 responses
          103 views
          0 likes
          Last Post Mindset
          by Mindset
           
          Started by Geovanny Suaza, 02-02-2026, 12:30 PM
          0 responses
          555 views
          1 like
          Last Post Geovanny Suaza  
          Started by RFrosty, 01-28-2026, 06:49 PM
          0 responses
          552 views
          1 like
          Last Post RFrosty
          by RFrosty
           
          Working...
          X