Click here to Skip to main content
15,885,366 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 SparkAPI.Common.Logging;

namespace SparkAPI.Data.Markets
{

    /// <summary>
    /// Provides a live event feed via the Spark API for the entire market
    /// </summary>
    /// <remarks>
    /// This feed calls the Spark.GetNextExchangeEvent() which is a synchronous method. This means when you call the method, it will 
    /// not return until it recieves an event or the timeout period is reached. By specifying a -1 timeout, we are telling it
    /// to wait until the end of the day. This structure means that RaiseEvent() method is only called when an event is
    /// received, but the logic must be run on a separate thread if we want other parts of the application to keep responding.
    /// </remarks>
    public class ApiMarketEventFeed : ApiEventFeedBase
    {

        /// <summary>Exchange identifier</summary>
        public string Exchange { get; private set; }

        /// <summary>
        /// ApiMarketEventFeed constructor
        /// </summary>
        /// <param name="exchange">Exchange sybmol</param>
        public ApiMarketEventFeed(string exchange)
        {
            Exchange = exchange;
        }

        /// <summary>
        /// Initiate data feed
        /// </summary>
        public override void Execute()
        {

            //Connect to Spark API if required
            ApiControl.Instance.Connect();

            //Get instance to exchange
            Spark.Exchange exchangeRef;
            if (ApiFunctions.GetSparkExchange(Exchange, out exchangeRef))
            {

                //Request all events for current day
                Spark.Event sparkEvent = new Spark.Event();
                if (Spark.GetAllExchangeEvents(ref exchangeRef, ref sparkEvent))
                {
                    while (Spark.GetNextExchangeEvent(ref exchangeRef, ref sparkEvent, -1))     //Specifying -1 timeout will keep it waiting until end of day
                    {
                        RaiseEvent(new EventFeedArgs(sparkEvent, Spark.TimeToDateTime(sparkEvent.Time)));
                    }
                }

                //Release memory at end of day
                Spark.ReleaseCurrentEvents();

            }

        }

    }
}

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