Using C# This is a basic question I'm sure. I'm getting the error.. "The name '_stocks' does not exist in the current context". I know this is because I declared the _stocks dictionary within the Initialize method. This makes the _stocks variable a local variable and only accessible within the Initialize method. I need to declare the _stocks variable as a field of the class (so it can be accessed by any method of the class). The other method as you will see below is OnBarUpdate(). How do I go about declaring the _stocks variable as a field of the class?
public class MAcrossLong : Strategy
{
//Variables
private int variable1 = 0
private int variable2 = 0
public struct StockEntry
{
public string Name { get; set; }
public PeriodType Period { get; set; }
public int Value { get; set; }
public int Count { get; set; }
}
protected override void Initialize()
{
Dictionary<string, StockEntry> _stocks = new Dictionary<string, StockEntry>();
_stocks.Add("ABC", new StockEntry { Name = "ABC", Period = PeriodType.Minute, Value = 5, Count = 0 } );
}
protected override void OnBarUpdate()
{
//_stocks dictionary is used within the code in this method. error is occurring within this method
}
}
**Added Portion....
I should probably just post the code within OnBarUpdate() because I'm now getting other errors... The best overloaded method match for 'System.Collections.Generic.Dictionary.this[string]' has some invalid arguments Argument '1': cannot convert from 'int' to 'string' Operator '<' cannot be applied to operands of type 'NinjaTrader.Strategy.MAcrossLong.StockEntry' and 'int'
protected override void OnBarUpdate()
{ //for loop to iterate each instrument through
for (int series = 0; series < 5; series++)
if (BarsInProgress == series)
{
var singleStockCount = _stocks[series];
bool enterTrade = false;
if (singleStockCount < 1)
{
enterTrade = true;
}
else
{
enterTrade = BarsSinceEntry(series, "", 0) > 2;
}
if (enterTrade)
{ // Condition for Long Entry here
EnterLong(200);
{
if(_stocks.ContainsKey(series))
{
_stocks[series]++;
}
}
}
}
}