0

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]++;
}
}
 }
            } 
}
zirjeo
  • 21
  • 1
  • 4

2 Answers2

0

The same way you declared variable1 and variable2....

public class MAcrossLong : Strategy
{
   private int variable1 = 0;
   private int variable2 = 0;
   private Dictionary<string, StockEntry> _stocks;

   protected override void Initialize()
   {
      _stocks.Add("ABC", new StockEntry { Name = "ABC", Period = PeriodType.Minute, Value = 5, Count = 0 } );
   }

   protected override void OnBarUpdate()
   {
      _stocks["ABC"].Name = "new name"; // Or some other code with _stocks
   }
}

To fix the error inside the OnBarUpdate() that you recently added, you need to switch to a foreach loop and use a KeyValuePair<string, StockEntry> iterator. You can read more on them here, here, and here.

It should look something like this:

foreach(KeyValuePair<string, StockEntry> stock in _stocks)
{
   string ticker = stock.Key;
   StockEntry stockEntry = stock.Value;
   // Or some other actions with stock
}
Community
  • 1
  • 1
awudoin
  • 541
  • 2
  • 8
  • I added the OnBardUpdate() code to my original post at the bottom of the post. I posted the other errors I'm getting now. Is it a simple fix? thanks – zirjeo Sep 19 '13 at 15:26
  • Your added portion is a separate problem and should be posted as a new question. The problem lies in that you don't access a dictionary via an index like normal arrays or lists. You access via its key which you defined as a string. Look into `foreach` loops and use `KeyValuePair` as the iterator. – awudoin Sep 19 '13 at 16:30
  • So use for each and replace series with KeyValuePair ? – zirjeo Sep 19 '13 at 17:21
  • I'm still struggling with this a bit. If one of my conditions for entering a stock trade is..if (SMA(BarsArray[series],Fast)[1] > SMA(BarsArray[series],Slow)[1]) staying in line with what you have above how can I adjust this and have each stock iterate through this condition? I assume I can't use BarsArray any longer. And would "series" be replaced with "stock" that your using above? – zirjeo Sep 20 '13 at 01:59
  • I'm not 100% sure what you're asking. Read up on the links I provided so you understand how `foreach` and `KeyValuePair` work. If you still have trouble post in a new question with what you have tried and why it's not working because this is a completely separate topic from the original. – awudoin Sep 20 '13 at 02:38
  • Instead of using a string for the dictionary key, im going to use int like so..Dictionary _stocks = new Dictionary(); and then add stocks to the dictionary as.. _stocks.Add(0, new StockEntry { Name = "ABC", Period = PeriodType.Minute, Value = 5, Count = 0 } ); This way I can still iterate with the index values and not have to change my whole script. You think that will work? – zirjeo Sep 20 '13 at 16:53
  • If you want to use an `int` as an index then use a `List` instead of a `Dictionary`. This will allow you to iterate with a standard `for` loop. – awudoin Sep 20 '13 at 17:34
  • The struct im using above will not work? – zirjeo Sep 20 '13 at 19:35
  • This is the last time I'm going to say it and respond because it is obvious you haven't read the documentation like I keep referring to.... You can use the struct. A `List` is generic just like a `Dictionary`. – awudoin Sep 20 '13 at 20:25
  • No need to get rude, I did read the documentation thanks. Just see no reason to have to fix what's not broken. The docs discuss complicated ways that I don't need such as using a keyvalue, and a simple foreach loops that I'm already aware of but I don't need. All this has done is just wasted my time. Thanks for tryn to help though. – zirjeo Sep 20 '13 at 21:26
0

You need to declare _stocks in a class level scope. Since you have declared it within the Initialize method its visibility becomes local to that method. So you should declare it along with variable1 and variable2 like

private int variable1 = 0;
private int variable2 = 0;
private Dictionary<string, StockEntry> _stocks;

You might need to look into the Access modifiers and Variable and Method Scopes for a better understanding

Carbine
  • 7,849
  • 4
  • 30
  • 54
  • I added the OnBardUpdate() code to my original post at the bottom of the post. I posted the other errors I'm getting now. Is it a simple fix? thanks – zirjeo Sep 19 '13 at 15:26