Click here to Skip to main content
15,884,298 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.5K   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.Diagnostics;
using SparkAPI.Common.Design;

namespace SparkAPI.Data
{

    /// <summary>
    /// Spark connection status
    /// </summary>
    internal enum SparkConnectionStatus
    {
        Disconnected = 0,
        Connecting = Spark.SPARK_CONNECTING,
        Connected = Spark.SPARK_CONNECTED,
        LoggedIn = Spark.SPARK_LOGGED_IN
    }

    /// <summary>
    /// Controls the initialisation and connection management to the underlying Spark API
    /// </summary>
    internal class ApiControl
    {

        private readonly object _syncLock = new Object();
        private bool _isInitiated = false;

        /// <summary>
        /// APIControl constructor (can only be instanced via Common.Singleton.Instance())
        /// </summary>
        private ApiControl()
        {
        }

        /// <summary>
        /// Get instance of singleton APIControl
        /// </summary>
        internal static ApiControl Instance
        {
            get
            {
                return Singleton<ApiControl>.Instance;
            }
        }

        /// <summary>
        /// Returns Spark connection status
        /// </summary>
        internal SparkConnectionStatus ConnectionStatus
        {
            get
            {
                return (SparkConnectionStatus)Spark.GetConnectionStatus();
            }
        }

        /// <summary>
        /// Initialise Spark component
        /// </summary>
        /// <remarks>
        /// The Spark API must be initalised before use otherwise many functions will not work correctly, but it won't throw an exception 
        /// and will just return an invalid result. 
        /// </remarks>
        internal void Initialise()
        {
            lock (_syncLock)
            {
                if (!_isInitiated)
                {
                    Spark.Init();
                    Spark.SetOptions(Spark.OPTION_ALL_EXCHANGE_EVENTS);
                    Spark.SetCacheDir(@"D:\Temp", 4000);
                    _isInitiated = true;
                }
            }
        }

        /// <summary>
        /// Open connection to Spark API
        /// </summary>
        /// <returns></returns>
        internal bool Connect()
        {
            bool result = true;
            lock (_syncLock)
            {
                try
                {

                    if (ConnectionStatus == SparkConnectionStatus.Disconnected)
                    {

                        //Instance Spark object
                        Debug.WriteLine(DateTime.Now.ToString("HH:mm:ss.fff") + "\t" + "Initiating Spark connection...");
                        Initialise();

                        //Connect to Spark server
                        ApiCredentials credentials = ApiCredentials.LoadFromFile();
                        if (credentials == null) throw new ApplicationException("Spark API security credentials not available");
                        Spark.SetLogin(credentials.Username, credentials.Password);
                        if (!Spark.Connect())
                        {
                            Debug.WriteLine("Can't connect: {0}", Spark.DescribeError(Spark.GetLastError()));
                            result = false;
                        }
                        Debug.WriteLine(DateTime.Now.ToString("HH:mm:ss.fff") + "\t" + "Spark connection initiated");

                        //Wait to allow Spark API to data access
                        System.Threading.Thread.Sleep(1000);

                    }
                }
                catch (Exception ex)
                {
                    Debug.WriteLine("ERROR: APIControl - ", ex.Message);
                    result = false;
                }
            }
            return result;
        }

        /// <summary>
        /// Close connection to Spark API
        /// </summary>
        internal void Disconnect()
        {
            lock (_syncLock)
            {
                if (ConnectionStatus != SparkConnectionStatus.Disconnected)
                {
                    Spark.Disconnect();
                }
                Spark.Exit();
                _isInitiated = false;
            }
        }

    }
}

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