Created
October 28, 2014 02:42
-
-
Save sudipto80/5c53f9d53c5372cdb4c8 to your computer and use it in GitHub Desktop.
Squirrel Example : Live Stock Analytics
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Stock symbols of companies for which you want to run this | |
string[] symbols = { "AAPL", "GOOG", "MSFT" }; | |
Dictionary<string, Table> stocks = new Dictionary<string, Table>(); | |
string template = @"http://real-chart.finance.yahoo.com/table.csv?s=[Symbol]&d=8&e=4&f=2014&g=d&a=0&b=2&c=1962&ignore=.csv"; | |
foreach (var symb in symbols) | |
{ | |
WebClient wc = new WebClient(); | |
wc.DownloadFile(template.Replace("[Symbol]", symb), "temp" + symb + ".csv"); | |
stocks.Add(symb, DataAcquisition.LoadCSV("temp" + symb + ".csv").Top(30)); | |
} | |
//Creating the symbol column. | |
symbols.ToList().ForEach(s => stocks[s].AddColumn("Symbol", new List<string>(Enumerable.Repeat(s, 30)))); | |
//Merging the results using LINQ. | |
Table allStocks = symbols.Select(ticker => stocks[ticker]) | |
.Aggregate((first, second) => first.Merge(second)); | |
//Adding the column "Diff" programmatically. | |
allStocks.AddColumn(columnName: "Diff", formula: "[Open] - [Close]", decimalDigits: 4); | |
//Preparing to write the result in a HTML file. | |
StreamWriter sw = new StreamWriter("temp.htm"); | |
string htmlTable = allStocks | |
//Sort by the difference in descending order | |
.SortBy("Diff", SortDirection.Descending) | |
//Taking top 20 entries | |
.Top(20) | |
//Pick only these columns | |
.Pick("Symbol", "High", "Close") | |
//Returning the HTML table representation | |
.ToHTMLTable(); | |
sw.WriteLine(htmlTable); | |
sw.Close(); | |
System.Diagnostics.Process.Start("temp.htm"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment