class Bar
{
public DateTime Time { get; set; }
public double Price { get; set; }
}
class Instrument
{
public List<Bar> Bars { get; set; }
public string Name { get; set; }
public Instrument(string name, string path) {
// set the Bars list here reading from files
}
}
以下是我的兩個簡化版本。我正在爲貨幣創建一個自定義的回測平臺。目前的問題是在每臺儀器上沒有價格數據的地方切斷酒吧。Linq列表清單
我從XML文件讀取價格。
var xlinqBarLists = xlinqInstruments.Select(i => i.Bars);
這基本上是
List<List<Bar>> xlinqBarLists
通過每個單獨列表我想要做的就是循環,找到最新的開始日期和最早結束日期,然後砍掉那個時間窗口外的所有酒吧。我的黑客攻擊代碼是
var xlinqInstruments = root.Elements("instrument").Select(a =>
new Instrument(a.Element("name").Value, a.Element("path").Value));
var xlinqBarLists = xlinqInstruments.Select(i => i.Bars);
DateTime latestStartDate = DateTime.MinValue;
DateTime earliestEndDate = DateTime.MaxValue;
foreach (List<Bar> bars in xlinqBarLists)
{
if (bars.Min(b => b.Time) > latestStartDate)
latestStartDate = bars.Min(b => b.Time);
if (bars.Max(b => b.Time) < earliestEndDate)
earliestEndDate = bars.Max(b => b.Time);
}
foreach (List<Bar> barList in xlinqBarLists)
{
var timeWindowBars = from bar in barList
where bar.Time >= latestStartDate && bar.Time <= earliestEndDate
select bar;
// I need some way to overwrite the original Instrument.Bars property with timeWindowBars
// Suggestions?
}
我可以通過跳過foreach循環更快速高效地執行此操作嗎?
這應該屬於代碼審查:http://codereview.stackexchange.com/ –
你是否想要將每個樂器的'Bars'屬性設置爲'List''只包含那些屬於mi所有*'Bar'對象(包括其他'Instrument's上的對象)的最小結束日期和最大開始日期? –