-1

使用 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++;                     }
            }
        }
    }
}    
4

2 回答 2

0

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

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

我建议一个很好的模式是定义一个类似的类:

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

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

于 2013-09-25T15:45:57.127 回答
0

正如 Hans 建议的那样,尝试将 _stocks 变量移到 Initialise 方法之外。请参见下面的示例。

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 } );
    ....
}
于 2013-09-22T22:14:26.653 回答