Click here to Skip to main content
15,885,309 members
Articles / Programming Languages / C# 4.0

An Introduction to Real-Time Stock Market Data Processing

Rate me:
Please Sign up or sign in to vote.
4.96/5 (64 votes)
20 May 2013CPOL24 min read 332.6K   57.7K   202  
Discusses how stock market trading works, the different types of market data available, and provides a code example with sample data that processes a market data feed
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SparkAPI.Market
{

    /// <summary>
    /// Provides a view of an order book where orders are aggregated by price level
    /// </summary>
    public class AggregatedLimitOrderBook
    {

        /// <summary>Bid order price level list</summary>
        public List<LimitOrderBookPriceLevel> Bid { get; private set; }

        /// <summary>Ask order price level list</summary>
        public List<LimitOrderBookPriceLevel> Ask { get; private set; }

        /// <summary>
        /// AggregatedLimitOrderBook constructor
        /// </summary>
        /// <param name="orderBook">Limit order book</param>
        public AggregatedLimitOrderBook(LimitOrderBook orderBook) : this(orderBook, -1)
        {
        }

        /// <summary>
        /// AggregatedLimitOrderBook constructor
        /// </summary>
        /// <param name="orderBook">Limit order book</param>
        /// <param name="maximumPriceLevels">Number of aggregate price levels to calculate</param>
        public AggregatedLimitOrderBook(LimitOrderBook orderBook, int maximumPriceLevels)
        {
            Bid = GeneratePriceLevels(orderBook.Bid, maximumPriceLevels);
            Ask = GeneratePriceLevels(orderBook.Ask, maximumPriceLevels);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="orderList"></param>
        /// <param name="maximumPriceLevels"></param>
        /// <returns></returns>
        private static List<LimitOrderBookPriceLevel> GeneratePriceLevels(IEnumerable<LimitOrder> orderList, int maximumPriceLevels)
        {
            List<LimitOrderBookPriceLevel> result = new List<LimitOrderBookPriceLevel>();
            LimitOrderBookPriceLevel priceLevel = null;
            int currentPrice = 0;
            int levelIndex = 0;
            foreach (LimitOrder order in orderList)
            {

                //Update price level
                if (order.Price != currentPrice)
                {
                    levelIndex++;
                    if (priceLevel != null) result.Add(priceLevel);
                    priceLevel = new LimitOrderBookPriceLevel();
                    priceLevel.Price = order.Price;
                    currentPrice = order.Price;
                }
                priceLevel.Count++;
                priceLevel.Volume += order.Volume;

                //Exit if maximium price level is reached
                if (levelIndex > maximumPriceLevels) break;

            }
            return result;
        }

    }

}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Australia Australia
Paul Francis currently works as a senior engineer at The Trade Desk.

He holds an undergraduate Honours degree in Finance, and is near completion of a Ph.D. in Market Microstructure, specialising in order flow modelling, and market data processing, reconstruction and analytics.

He is also the creator of Sharp Spark (Spark API SDK), an open source component designed to facilitate the processing of real-time market data from the Spark API: http://sourceforge.net/projects/sparkapi

Paul lives in Sydney, Australia.

Comments and Discussions