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?
Comment