SIGN UP MEMBER LOGIN:    
ARTICLE

Realtime Stock Quotes into Excel using .NET

Posted by Mike Gold Articles | Financial Applications September 03, 2001
I may seem a little Bullish on .NET, but I was able to create this application fairly quickly in C#.
Reader Level:
Download Files:
 

I may seem a little Bullish on .NET, but I was able to create this application fairly quickly in C# thanks to the rich library and ease of connecting to COM applications like Excel.  Those of you who are Visual C++ programmers will be greatly relieved that it is finally as easy as using Visual Basic to talk to the COM interfaces of applications and ActiveX controls in C#.  This article is based upon the code and article written by Dipal Choksi.  You may want to refer to this article to see how the stock quotes are captured from the web. I've added the port of the quotes to Excel as well as a timer that retrieves the quotes every 10 seconds.  You can change this interval to whatever time you wish by changing the Interval property in timer1.

Below is the code that initializes the Excel Application Object so you can use it like any other class:

First I had to import the Excel library. This can either be done by adding a reference or adding the statement below:

using Excel;

I then added the code to instantiate an Excel Application object:

private Excel.Application ExcelObj = new Excel.Application();

Next we need to show Excel as a visible object and add a workbook to populate:

ExcelObj.Visible = true;
ExcelObj.Workbooks.Add(XlWBATemplate.xlWBATWorksheet);

Now we are ready to use Excel to display our stock quotes.  Each time the timer tick event is entered we print out a stock quote to the spreadsheet  (Note:  Alot of the codein this method  is taken from the article by Dipal Choksi :

private void timer1_Tick(object sender, System.EventArgs e)
{
string strQuotes;
int i;
strQuotes = "";
for(i=0;i<strQuoteArray.Length;i++)
{
// Retrieve the stock information from the web site
string strResult = GetQuoteFromWebSite(strQuoteArray[i]);
string[] temp = strResult.Split(separator) ;
//Check if the string array returned has more than one elements
//since if there are less than one elements then a exception must have been returned
if(temp.Length >1)
{
//The WebService returns a lot of information about the stock,
//We only show the relevant portions .
WriteExcelQuote(i, strQuoteArray[i], temp[1]);
}
else
{
strerror ="Error :"+ strResult ;
//set the error label
strQuotes = strQuotes + strerror + System.Environment.NewLine;
}
}
Counter ++;
}

The routine below takes the stock quote and the stock name and populates the excel spreadsheet. 

private void WriteExcelQuote(int index, string symbol, string quote)
{
Workbooks workbooks = ExcelObj.Workbooks;
// get the workbook from the existing spreadsheet
_Workbook workbook = workbooks.get_Item(1);
// get the collection of sheets in the workbook
Sheets sheets = workbook.Worksheets;
// get the first and only worksheet from the collection
// of worksheets
_Worksheet worksheet = (_Worksheet) sheets.get_Item(1);
// form a range with a single cell based on the index of the quote
// and the Incremented Counter.
char rangeChar = (char)(index + 65);
string strCell = rangeChar + Convert.ToString(Counter);
Range range2 = worksheet.get_Range(strCell, strCell);
// if its the first quote, populate the cell with the symbol
// otherwise populate the cell with the quote for the symbol
if (Counter == 1)
range2.Value = symbol;
else
range2.Value = quote;
}

Note there is not much code here.  The hardest part about populating an Excel Cell is forming the letter-number combination to put the quote in the correct row and column.  The index is converted to a letter (A - Z) and is concatenated with the ongoing Counter incremented each time in the timer event.  The cell names come out to Row Column representations such as "A4" (row 4, column 1)  or "B5" (row 5. column 2).  This letter number combination uses the get_Range function in the worksheet to obtain a range to populate.  In our case we are getting a range for a single cell (e.g.  "A1" to "A1"). This code could be altered to populate an array of cells by getting a larger range, if for some reason you need to populate the cells alot faster for a large range of quotes.

Extras

Once you've populated an excel spreadsheet there are many things you can do with it.  For example you can create a graph or write some more C# code to analyze your data or even populate a database.  Hope this lesson gives you another way to think about your portfolio and sparks some possibilities for organizing your trading with .NET.

Login to add your contents and source code to this article
share this article :
post comment
 

hai mike..., i'm developing this process excel format into Database. the Excel format is accept only NSE Stocks, But BSE Stocks are Not available. so i need a tool for BSE Stock Display here. Is there any tool to add the BSE Stocks, if u have plz kindly send me the tool.

Posted by prakash pandian May 02, 2011

I opened the zip files and thought that there would be some instructions on how to use this application.   To the less advanced programmers they need a bit more direction, very difficult compared to VB

Posted by brian crone Jan 27, 2010

SIR,

           BUT HOW TO USE THIS IN EXCEL I AM NOT GETTING

Posted by vishal joshi Dec 24, 2009

I'm not sure what technology you are looking at using, but this gives you some info on using RTD in excel which is great for stocks (real time style updates) in excel.

http://realtimeconnect.thorusproductions.com/

Posted by garth howell Jun 30, 2009

RTD in Excel is a great technology if you want rapid updates. There's some info on the technology here, plus a library if you want to try it out:
 http://realtimeconnect.thorusproductions.com/

As always, it depends on the solution that you are looking for.

Posted by garth howell Jun 30, 2009
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
    Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Become a Sponsor