-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathNewCode.cs
More file actions
75 lines (62 loc) · 2.38 KB
/
NewCode.cs
File metadata and controls
75 lines (62 loc) · 2.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Net;
namespace ClassLibrary1
{
public class NewCode
{
public static Uri MakeUrl(string symbol, DateTime dfrom, DateTime dto)
{
return new Uri("http://ichart.finance.yahoo.com/table.csv?s=" + symbol +
"&e=" + dto.Day.ToString() + "&d=" + dto.Month.ToString() + "&f=" + dto.Year.ToString() +
"&g=d&b=" + dfrom.Day.ToString() + "&a=" + dfrom.Month.ToString() + "&c=" + dfrom.Year.ToString() +
"&ignore=.csv");
}
private string Fetch(Uri url)
{
var req = WebRequest.Create(url);
using (var stream = req.GetResponse().GetResponseStream())
{
using (var reader = new StreamReader(stream))
{
return reader.ReadToEnd();
}
}
}
private IEnumerable<IEnumerable<string>> Decompose(string data)
{
char mark = '\n';
char mark2 = ',';
var result = from datedata in data.Split(mark)
where !string.IsNullOrEmpty(datedata)
select datedata.Split(mark2);
return result;
}
private IEnumerable<Tuple<DateTime, double>> ReFormat(IEnumerable<IEnumerable<string>> list)
{
Func<string, DateTime> parseDate =
dt => DateTime.ParseExact(dt, "yyyy-mm-dd", CultureInfo.InvariantCulture);
Func<string, double> parseRate = rt => Double.Parse(rt, CultureInfo.GetCultureInfo("en-US"));
var result = from sublist in list.Skip(1)
select new Tuple<DateTime, double>
(parseDate(sublist.ElementAt(0)), parseRate(sublist.ElementAt(4)));
return result;
}
public IEnumerable<Tuple<DateTime, double>> GetResult(Uri url)
{
var res1 = Fetch(url);
var res2 = Decompose(res1);
var res3 = ReFormat(res2);
return res3;
}
public Tuple<DateTime, double> DateMaxClose(IEnumerable<Tuple<DateTime, double>> mylist)
{
return mylist.Aggregate(
(sum, item) => (item.Item2 > sum.Item2) ? item : sum
);
}
}
}