试图创建结构列表

时间:2013-09-22 21:33:02

标签: c# struct

使用C#。我没有得到任何编译错误,但它似乎没有按计划工作。有没有什么不正确的我有这个列表<>结构设置如下?..

public struct StockEntry
{         
    public string Name { get; set; }
    public PeriodType Period { get; set; }
    public int Value { get; set; }
    public int Count { get; set; }
}

List<StockEntry> _stocks = new List<StockEntry>(); 

protected override void Initialize()
{ 
    //5min price bars
    _stocks.Add(new StockEntry { Name = "ABC", Period = PeriodType.Minute, Value = 5, Count = 0 } );
    _stocks.Add(new StockEntry { Name = "ACE", Period = PeriodType.Minute, Value = 5, Count = 0 } );
    _stocks.Add(new StockEntry { Name = "ACN", Period = PeriodType.Minute, Value = 5, Count = 0 } );
    _stocks.Add(new StockEntry { Name = "ADT", Period = PeriodType.Minute, Value = 5, Count = 0 } );
    _stocks.Add(new StockEntry { Name = "SCTY", Period = PeriodType.Minute, Value = 5, Count = 0 } );

    //1min price bars
    _stocks.Add(new StockEntry { Name = "ABC", Period = PeriodType.Minute, Value = 1, Count = 0 } );
    _stocks.Add(new StockEntry { Name = "ACE", Period = PeriodType.Minute, Value = 1, Count = 0 } );
    _stocks.Add(new StockEntry { Name = "ACN", Period = PeriodType.Minute, Value = 1, Count = 0 } );
    _stocks.Add(new StockEntry { Name = "ADT", Period = PeriodType.Minute, Value = 1, Count = 0 } );
    _stocks.Add(new StockEntry { Name = "SCTY", Period = PeriodType.Minute, Value = 1, Count = 0 } );
}

**以下添加部分* *

它拥有Count的任何地方都是关键领域,我不确定它是否实际上正在计算我正在努力实现的交易数量。

protected override void OnBarUpdate()
{   
    // for loop to iterate each stock through the required 
    // entry conditions. "series" is just a 5min price bar 
    // of each instrument,   "series + 5" is 1min of each 
    // instrument.  Hoping the first stock I added above 
    // takes Index 1, then the next stock added takes Index 2 and so on.                
    for (int series = 0; series < 5; series++)
    {
        if (BarsInProgress == series + 5) //OnBarUpdate called for 1min bars
        {    
            var stockEntry = _stocks[series];
            bool enterTrade = false;

            if (stockEntry.Count < 1)//if 0 entries
            {
                enterTrade = true;
            }
            else // if 1 or more entries make sure 2 price bars or 10min has elapsed before entering another trade
            {
                enterTrade = BarsSinceEntry(series, "", 0) > 2;
            } 

            if (enterTrade)
            {  
                // Condition for Long Entry, fast MA cross above slow MA & current price > high of bar at cross
                if (SMA(BarsArray[series],Fast)[1] > SMA(BarsArray[series],Slow)[1] && 
                    SMA(BarsArray[series],Fast)[2] < SMA(BarsArray[series],Slow)[2] && 
                    Closes[series + 5][0] > Highs[series][1] + distance && 
                    SMA(BarsArray[series],Slow)[1] > SMA(BarsArray[series],Slow)[2] + .01)
                {        
                    EnterLong(200); //enter long 200 shares

                    // store/track that a trade for the current stock has 
                    // taken place so that it can process through the condition 
                    // above that requests how many trades have already taken place.
                    stockEntry.Count++;                     }
            }
        }
    }
}    

2 个答案:

答案 0 :(得分:0)

正如Hans建议的那样,尝试在Initialise方法之外移动_stocks变量。见下面的例子。

List<StockEntry> _stocks = new List<StockEntry>(); // Make it a class variable, not a method variable.           

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

答案 1 :(得分:0)

您希望类型StockEntry的每个变量或列表项包含名称,期间,值和计数的组合吗?或者您是否希望每个此类变量或列表项都包含对具有名称,期间,值和计数的实体的引用?结构类型具有前者的含义;类类型具有后一种含义。

如果有一个结构列表,并且希望修改列表项的一部分,则必须读出列表项,进行更改,然后将其写回。如果列表包含对可变类类型的引用,则可以从列表中读取引用,然后修改它引用的对象,而不必更新列表。这通常很方便,但这样的便利是有代价的:如果列表的目的是封装项目中保存的信息,则必须确保列表中的每个项目在任何地方都保留唯一的引用在宇宙中StockEntry之外的那个时候,当一个人积极使用相关条目时。这通常意味着将列表中的信息暴露给外部代码的唯一安全方法是将该信息复制到其他对象。

我建议一个好的模式是定义一个类,如:

class SimpleHolder<T>
{
  public T Value;
  SimpleHolder() {}
  SimpleHolder(T val) {Value = val;}
}

然后使用List<SimpleHolder<StockItem>>。如果您然后定义SimpleHolder<StockItem> stockEntry;,您将能够说stockEntry.Value.Count++;并让它更新列表中保存的实体,但也可以让方法返回StockItem作为手段返回其中包含的数据而不必暴露持有它的实体。