Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

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 md4866, 05-01-2024, 08:15 PM
          2 responses
          18 views
          0 likes
          Last Post md4866
          by md4866
           
          Started by samish18, Today, 12:20 PM
          0 responses
          7 views
          0 likes
          Last Post samish18  
          Started by thread, 04-15-2024, 11:58 PM
          3 responses
          48 views
          0 likes
          Last Post Georg1o
          by Georg1o
           
          Started by leojimenezp, 04-20-2024, 05:49 PM
          4 responses
          50 views
          0 likes
          Last Post leojimenezp  
          Started by nicthe, Today, 09:24 AM
          1 response
          8 views
          0 likes
          Last Post nicthe
          by nicthe
           
          Working...
          X