Tutorial
February 7, 2021

Introducing IEXSharp: How to Get Market Data Using C# and .NET

The IEXSharp library is built to help you integrate financial data into your application with C# and the .NET framework.

Cloud icon

Victor Lee

https://iexcloud.io/community/blog/introducing-iexsharp-how-to-get-market-data-using-c-and-net
@iexcloud
https://iexcloud.io/community/blog/introducing-iexsharp-how-to-get-market-data-using-c-and-net
@iexcloud

When integrating financial data from IEX Cloud into your application, you can choose from a range of implementation options.

While many users may opt to integrate with the API directly, the IEX Cloud community also develops and supports a range of third-party libraries that make integration easier within different environments.

The IEXSharp library is built to help you integrate financial data into your application with C# and the .NET framework. This introduction will take you through:

  • A brief introduction to C# and .NET
  • An overview of the IEXSharp library
  • Installation instructions
  • Examples and sample code for using the library

Why the C# language?

The reasons I love C# are too numerous to list. Many of them are technical, so I donʼt want to bore you with the details. But at a high level, C# is highly versatile, and can be used in mobile apps, cloud services, desktop apps, and games. It has a static typing system which makes it easy to write and iterate, while also being very fast at runtime. This allows for the processing and analysis of large volumes of data much more quickly than many other languages. Because of this, C# is a popular language in finance and leveraged widely for creating windows desktop apps.

C# runs on the .NET framework, as do multiple other languages such as the functional language F#. The image below from the .NET Conf 2020 illustrates it best. The .NET framework is fast and easy to work with. It has been voted the most loved framework of any language. Being fully open source, anyone can look at and improve the code. It has a large ecosystem behind it, which means it continues to evolve at a rapid pace.

IEXSharp .Net ecosystem momentum

Source:

https://twitter.com/sinclairinat0r/status/1326194304117837824

Doesnʼt .NET only work on Microsoft Windows?

.NET core now runs on many operating systems, including MacOS and many flavors of Linux.

Since .NET has been out for almost two decades, the different flavors can be confusing. Previously, there was the .NET framework, .NET Core, .NET Standard, Xamarin, and Mono.

With the release of .NET 5.0, everything has now been unified into a single runtime.

Introducing IEXSharp!

IEXSharp combines the vast data of IEX Cloud with the power and flexibility of .NET. It handles the tedious work (authenticating, parsing JSON, etc.) and lets you play with the data right away. IEXSharp supports all the endpoints offered by IEX Cloud, including Premium Data.

IEXSharp targets .NET Standard 2.0, which means it will work with .NET 5.0, but also any of the legacy .NET flavors that you happen to be using if youʼre not ready to upgrade right away. Learn more about .NET Standard here

IEXSharp is open source, so all the source code can be found at our main page.

Installing and setting up IEXSharp

To install IEXSharp, itʼs usually easiest to use an IDE – or an integrated development environment. An IDE refers to software for building applications that brings together frequently-used developer tools into one graphical user interface (GUI). One of the most popular IDEs is Visual Studio.

Visual Studio Community, Visual Studio Code, and Visual Studio for Mac are free. The screenshots below use Visual Studio Community. As for IEXSharp, itʼs installed using a package management system called NuGet.

Create an IEX Cloud account

To begin accessing data from IEX Cloud, youʼll first need to create an account. You can choose from different plans depending on your usage – including a free plan for simple use cases and experimentation, all the way up to a Business plan for teams and companies. Create your account here.

If youʼre new to the platform, itʼs recommended that you first review the resources below before requesting any data.

Check out the Getting Started guide to get a basic overview of IEX Cloud.

Be sure to review how credits work. Credits are the fundamental units used to access data and make API calls on IEX Cloud. Every API endpoint has a “data weight” that indicates how many credits are used every time you make that API call. Once you start using live production data, youʼll be charged credits.

Learn more about credits

To begin accessing data, youʼll need to get provisioned with an IEX Cloud API token. Your token can then be found in your Console under the API Tokens tab. To access live production data, you should use your public token, which begins with pk_.

Get your API token

If you want to start with unlimited free test data in our sandbox, which is recommended for newer users, use the token beginning with “Tpk.”

Now that Iʼve got everything set up, how do I get started?

Step 1: Create a new project. Choose Console App (.NET Core) as the type of project.

IEXSharp create a new project screenshot

Step 2: Right click on the dependencies and choose “Manage NuGet Packages.”

IEXSharp manage NuGet packages screenshot

Step 3: In the NuGet window that pops up, select the “Browse” tab. Then type in the search term “iexsharp.” There should only be one result. Click on the little down arrow (↓) on the right to install the package.

Select IEXSharp in NuGet screenshot

Step 4: Paste the following code into Program.cs. You will need to replace the "publishableToken" and "secretToken" with your own from the IEX Cloud Console in the API Tokens tab. Also, set the useSandBox to true or false. depending on the tokens you choose to use. Note that when using the sandbox, IEX Cloud will return obfuscated (or scrambled) values. This is useful for testing purposes.

Example One: Getting Historical Stock Prices

The code sample below shows an example of how you might access historical stock prices from IEX Cloudʼs Historical Prices endpoint.

It begins with several using statements. These tell the program what namespaces to use. Then, we declare our own namespace, IEXSharpTestConsole. We name the main class, Program. Since this a console app, it executes the method named Main()

Next, there is another using statement, but this time it handles disposing of IEXCoudClient when it is no longer needed. We create the IEXCloudClient by calling its constructor. Then await the response from querying the Historical Prices endpoint by calling HistoricalPriceAsync().

The response that comes back can either be an error message, in which case you will write to the console, or it contains data. We loop through the data with a foreach loop. The timestamp for each OHLC can be extracted using the GetTimestampInEST(); method. We then write the results to the console.

Click the green “play” button on top to run your code.

IEXSharp manage NuGet packages screenshot

You should get output like below:

Now that you have this data, you can begin doing analysis or graphing with it. Alternatively, you can use Entity Framework Core to save the data to a database.

Learn more

Example Two: Getting Corporate Actions and Company Data

Now weʼll explore getting other types of data. IEX Cloud doesnʼt just supply stock prices. It also gives you access to a wide range of interesting data, such as corporate actions and CEO pay.

This code sample contains the same using, namespace, and class declarations as above. This time, you await the response from querying the CEO Compensation Endpoint. by calling CeoCompensationAsync(). Since the data returned is a single item, you donʼt need to loop through it this time in order write out the interesting aspects.

Replace the contents of your Program.cs with the following:

Now hit debug. The results should be something like this:

Streaming Data

Okay, now that we showed a couple examples of single data calls, you might be wondering if there is a way to stream updated values rather than just a onetime snapshot.

IEX Cloud enables you to stream real-time data* for stock prices, forex rates, and crypto data. IEXSharp can handle the work of setting up a streaming connection for you.

This code sample contains the same using, namespace, and class. This time, in addition to creating an instance of IEXCloudClient, we also create a sseClient by calling QuoteStream(). This corresponds to the stocksUS streaming endpoint.

Here, youʼll hook into two of the event handlers on sseClient: Error and MessageReceived. These allow you to determine what happens every time one of these events fires. For Error, just write out the error message details. If a message is received successfully, loop through the quotes and write them all out. After setting the event handlers, you can start streaming. While the streaming is ongoing, you can have your program do additional work in the meantime. In this example, the additional work is just writing out “example work.”. When the app is done and ready to stop streaming, it can just call sseClient.Close().

When we run the code above, this produces:

This sounds like too much work. Can I just start with a prebuilt sample? And something with a GUI instead of the boring command line?

An IEXSharpcommunity member, XamlBrewer, created a sample app using IEXSharp that you can try out here

It demonstrates how to set up watch lists, display live stock data, and format news articles with images. All of this in a full GUI app. Note that this app targets UWP so it is Windows only. For details, you can read his blog post here.

IEXSharp manage NuGet packages screenshot

Closing thoughts

The examples above should give you a good start in building your next app powered by IEX Cloud. Feel free to explore the library, as many more endpoints are available. The ones I have used in these examples have only barely touched the surface.

Canʼt get one of the examples to work, or encountered a bug with the library?

File an issue and post any questions at https://github.com/vslee/IEXSharp/issues

Found a bug with the data?

Reach out to IEX Cloudʼs support team at support@iexcloud.io

Looking for a way to contribute?

We welcome code contributions! Whether itʼs fixing a bug or writing a new feature, weʼre excited to welcome you to our community. Join us at https://github.com/vslee/IEXSharp

Victor Lee is the creator of IEXSharp. He grew up in Northern California and lives in Reno, Nevada. While programming is not his day job, it is his passion. His goal is to make programming more interesting for new and aspiring developers.

* Real-time stock prices from the IEX Cloud API are different from real-time stock data made available through direct connection with IEX Exchange. Learn more.