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上的对象)的最小结束日期和最大开始日期? –