Skip to content

Instantly share code, notes, and snippets.

@sudipto80
Created October 28, 2014 02:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sudipto80/5c53f9d53c5372cdb4c8 to your computer and use it in GitHub Desktop.
Save sudipto80/5c53f9d53c5372cdb4c8 to your computer and use it in GitHub Desktop.
Squirrel Example : Live Stock Analytics
//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