Plotting stock prices with SharpShooter, or Tips and tricks for SharpShooter charts

Andrew Kazyrevich
Did you know you can write 3 lines of code and get a nice stock prices SharpShooter chart with data downloaded straight from Yahoo Finance?
 
Here’s the code:

   
   var today = DateTime.Today;
   var prices = new Yahoo().DownloadPrices("GOOG", today.AddYears(-9), today);

   new PricesDialog(prices).ShowDialog();

And here’s the chart displaying Google OHLC price movements:
 


 

How to use this?

Assuming your app is configured under .NET Framework 4 (and not Client Profile), and your project references System.Windows.Forms, all you need is to install SharpShooter.Finance Nuget package:
 

 
And then you just write the code snippet from the beginning of the article. Alternatively, you can flick through source code on BitBucket.
 
I could have literally stopped here, but the intransgressible rules of blogging etiquette require me to share some little gotchas you may have to trudge through if you’d decide to implement a similar chart ;)

Tips and tricks for building SharpShooter charts.

Tip 1. You can use the same data source for several ChartArea elements on the same chart. That’s how the two charts are implemented in the example – shouldn’t be difficult, just select a chart from SharpShooter Collection, and paste another area via Insert | Chart | ChartArea.
 
Tip 2. It’s important to have AutoArgument assigned to false because otherwise your X axis will have consecutive numbers (which is often not what you want):
 

 
Tip 3. To format your X axis labels, you need to click on the series’ DataSource, and select Expression for the argument. Here’s how it looks for DateTime formatting in our case:
 

 
Tip 4. You can use expressions in values as well:
 

 
Tip 5. Generally, you cannot predict how many data points will your chart have, so the steps on the axes should be in percentage:
 

 
Tip 6. If you want your “plugin” report to be universally accessible, you’re better off using InlineReportSlot, so that the report layout would get stored within your project.
 
Finally, to download prices from Yahoo Finance, use http://chart.yahoo.com/table.csv… – more details on this here!

Summary.

Providing SharpShooter support with “building blocks” from Nuget seems like a very interesting idea, although the tasks those “blocks” solve should be rather generic – financial theme looks like a good fit here!
 
There is, however, a bit of lack of API support for such generality from SharpShooter side – eg, a lot of things are not customizable from the outside (chart title, axis names, colors, docking style, etc). I guess, for such level of control one may want to look into SharpShooter widgets, which can be parametrized.
 
But even in this form Nuget approach seems to be in demand – downloads statistics speaks for itself! Hopefully, SharpShooter.Finance will be a useful asset to the group ;)
 

October 28th, 2012

Leave a Comment