Reputation: 1136
Here is the form
Tuple<dynamic, dynamic> pair = new Tuple<dynamic, dynamic>() =
{({ Symbol = a, Close = 1.0, Date = 2/1/2016, Vol = 0 }, { Symbol = b, Close = 1.6, Date = 2/1/2016, Vol = 0 })}
{({ Symbol = a, Close = 1.0, Date = 2/1/2016, Vol = 0 }, { Symbol = c, Close = 2.0, Date = 2/1/2016 , Vol = 0 })}
{({ Symbol = b, Close = 1.6, Date = 2/1/2016, Vol = 0 }, { Symbol = c, Close = 2.0, Date = 2/1/2016, Vol = 0 })}
{({ Symbol = a, Close = 1.1, Date = 2/2/2016, Vol = 0 }, { Symbol = b, Close = 1.2, Date = 2/2/2016, Vol = 0 })}
{({ Symbol = a, Close = 1.1, Date = 2/2/2016, Vol = 0 }, { Symbol = c, Close = 2.1, Date = 2/2/2016, Vol = 0 })}
{({ Symbol = b, Close = 1.2, Date = 2/2/2016, Vol = 0 }, { Symbol = c, Close = 2.1, Date = 2/2/2016, Vol = 0 })}
{({ Symbol = a, Close = 1.4, Date = 2/3/2016, Vol = 0 }, { Symbol = b, Close = 1.4, Date = 2/3/2016, Vol = 0 })}
{({ Symbol = a, Close = 1.4, Date = 2/3/2016, Vol = 0 }, { Symbol = c, Close = 1.4, Date = 2/3/2016, Vol = 0 })}
{({ Symbol = b, Close = 1.4, Date = 2/3/2016, Vol = 0 }, { Symbol = c, Close = 1.4, Date = 2/3/2016, Vol = 0 })}
{({ Symbol = a, Close = 1.7, Date = 2/4/2016, Vol = 0.214285714285714 }, { Symbol = b, Close = 1.8, Date = 2/4/2016, Vol = 0.218217890235992 })}
{({ Symbol = a, Close = 1.7, Date = 2/4/2016, Vol = 0.214285714285714 }, { Symbol = c, Close = 2.7, Date = 2/4/2016, Vol = 0.464743364189122 })}
{({ Symbol = b, Close = 1.8, Date = 2/4/2016, Vol = 0.218217890235992 }, { Symbol = c, Close = 2.7, Date = 2/4/2016, Vol = 0.464743364189122 })}
{({ Symbol = a, Close = 1.8, Date = 2/5/2016, Vol = 0.122450941145067 }, { Symbol = b, Close = 1.2, Date = 2/5/2016, Vol = 0.169725025739105 })}
{({ Symbol = a, Close = 1.8, Date = 2/5/2016, Vol = 0.122450941145067 }, { Symbol = c, Close = 1.2, Date = 2/5/2016, Vol = 0.301649178342484 })}
{({ Symbol = b, Close = 1.2, Date = 2/5/2016, Vol = 0.169725025739105 }, { Symbol = c, Close = 1.2, Date = 2/5/2016, Vol = 0.301649178342484 })}
......
......
I want to select the pair with minimal abstract of Vol
(min|Vol1 - Vol2|) everyday after '2/3/2016'(The previous date can be default anything )
The expected form as follow:
{({ Symbol = a, Close = 1.7, Date = 2/4/2016, Vol = 0.214285714285714 }, { Symbol = b, Close = 1.8, Date = 2/4/2016, Vol = 0.218217890235992 }), Volspread = XXXX}
{({ Symbol = b, Close = 1.2, Date = 2/5/2016, Vol = 0.169725025739105 }, { Symbol = c, Close = 1.2, Date = 2/5/2016, Vol = 0.301649178342484 }), Volspread = XXXX}
.......
.......
Here Volspread
is just the min abs respectively.
Upvotes: 2
Views: 355
Reputation: 37299
For selecting the items with the min
difference in each group:
var result = pairs
.Where(pair => pair.Item1.Date > new DateTime(2016, 2, 3))
.Select(pair => new { Item = pair, Diff = Math.Abs(pair.Item1.Vol - pair.Item2.Vol) })
.GroupBy(pair => pair.Item.Item1.Date)
.SelectMany(group => group.Where(item => item.Diff.Equals(group.Min(i => i.Diff))).Select(item => item.Item))
.ToList();
And the testing data:
List<Tuple<dynamic, dynamic>> pairs = new List<Tuple<dynamic, dynamic>>
{
new Tuple<dynamic,dynamic>( new { Symbol = "a", Close = 1.0, Date = new DateTime(2016,2,1), Vol = 0 }, new { Symbol = "b", Close = 1.6, Date = new DateTime(2016,2,1), Vol = 0 }),
new Tuple<dynamic,dynamic>( new { Symbol = "a", Close = 1.0, Date = new DateTime(2016,2,1), Vol = 0 }, new { Symbol = "c", Close = 2.0, Date = new DateTime(2016, 2, 1), Vol = 0 }),
new Tuple<dynamic,dynamic>( new { Symbol = "b", Close = 1.6, Date = new DateTime(2016,2,1), Vol = 0 }, new { Symbol = "c", Close = 2.0, Date = new DateTime(2016, 2, 1), Vol = 0 }),
new Tuple<dynamic,dynamic>( new { Symbol = "a", Close = 1.1, Date = new DateTime(2016,2,2), Vol = 0 }, new { Symbol = "b", Close = 1.2, Date = new DateTime(2016,2,2), Vol = 0 }),
new Tuple<dynamic,dynamic>( new { Symbol = "a", Close = 1.1, Date = new DateTime(2016,2,2), Vol = 0 }, new { Symbol = "c", Close = 2.1, Date = new DateTime(2016,2,2), Vol = 0 }),
new Tuple<dynamic,dynamic>( new { Symbol = "b", Close = 1.2, Date = new DateTime(2016,2,2), Vol = 0 }, new { Symbol = "c", Close = 2.1, Date = new DateTime(2016,2,2), Vol = 0 }),
new Tuple<dynamic,dynamic>( new { Symbol = "a", Close = 1.4, Date = new DateTime(2016,2,3), Vol = 0 }, new { Symbol = "b", Close = 1.4, Date = new DateTime(2016,2,3), Vol = 0 }),
new Tuple<dynamic,dynamic>( new { Symbol = "a", Close = 1.4, Date = new DateTime(2016,2,3), Vol = 0 }, new { Symbol = "c", Close = 1.4, Date = new DateTime(2016,2,3), Vol = 0 }),
new Tuple<dynamic,dynamic>( new { Symbol = "b", Close = 1.4, Date = new DateTime(2016,2,3), Vol = 0 }, new { Symbol = "c", Close = 1.4, Date = new DateTime(2016,2,3), Vol = 0 }),
new Tuple<dynamic,dynamic>( new { Symbol = "a", Close = 1.7, Date = new DateTime(2016,2,4), Vol = 0.214285714285714 }, new { Symbol = "b", Close = 1.8, Date = new DateTime(2016,2,4), Vol = 0.218217890235992 }),
new Tuple<dynamic,dynamic>( new { Symbol = "a", Close = 1.7, Date = new DateTime(2016,2,4), Vol = 0.214285714285714 }, new { Symbol = "c", Close = 2.7, Date = new DateTime(2016,2,4), Vol = 0.464743364189122 }),
new Tuple<dynamic,dynamic>( new { Symbol = "b", Close = 1.8, Date = new DateTime(2016,2,4), Vol = 0.218217890235992 }, new { Symbol = "c", Close = 2.7, Date = new DateTime(2016,2,4), Vol = 0.464743364189122 }),
new Tuple<dynamic,dynamic>( new { Symbol = "a", Close = 1.8, Date = new DateTime(2016,2,5), Vol = 0.122450941145067 }, new { Symbol = "b", Close = 1.2, Date = new DateTime(2016,2,5), Vol = 0.169725025739105 }),
new Tuple<dynamic,dynamic>( new { Symbol = "a", Close = 1.8, Date = new DateTime(2016,2,5), Vol = 0.122450941145067 }, new { Symbol = "c", Close = 1.2, Date = new DateTime(2016,2,5), Vol = 0.301649178342484 }),
new Tuple<dynamic,dynamic>( new { Symbol = "b", Close = 1.2, Date = new DateTime(2016,2,5), Vol = 0.169725025739105 }, new { Symbol = "c", Close = 1.2, Date = new DateTime(2016,2,5), Vol = 0.301649178342484 }),
};
Before edit:
You can do this:
//Remove irrelevant records and calculate diff
var result = pairs
.Where(item => item.Item1.Date > new DateTime(2016, 2, 3))
.Select(item => new { Item = item, Diff = Math.Abs(item.Item1.Vol - item.Item2.Vol) });
//Find min diff
var minDiff = result.Min(item => item.Diff);
//Get only records with min diff
result = result.Where(item => item.Diff.Equals(minDiff)).ToList();
Another option is to use the nuget library of MoreLinq
for MinBy
Upvotes: 1