Click here to Skip to main content
15,884,472 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.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Mime;
using System.Text;
using System.Threading;
using SparkAPI.Common.Logging;
using SparkAPI.Data.Markets;
using SparkAPI.Data.Securities;

namespace SparkConsole.Commands
{

    /// <summary>
    /// Export specified security data to destination folder
    /// </summary>
    /// <remarks>
    /// exportsecurity  {securityListFile}  {destinationFolder}     [-d:{n}]  [-t]
    ///     securityListFile    Text file containing list of securities to export
    ///     destinationFolder   Destination folder for data files
    ///     -d                  Days to export where n equals number of days
    ///     -t                  Include current day
    /// </remarks>
    public class ExportSecurityCommand : IConsoleCommand
    {
        /// <summary>Log reference</summary>
        private readonly ILogDefinition _log;

        /// <summary>
        /// ExportSecurityCommand constructor
        /// </summary>
        /// <param name="log"></param>
        public ExportSecurityCommand(ILogDefinition log)
        {
            _log = log;
        }

        /// <summary>
        /// Execute command 
        /// </summary>
        /// <param name="args"></param>
        public void Execute(string[] args)
        {

            //Validate minimum argument count
            if (args.Count() < 3) throw new ArgumentException("'exportsecurity' command requires at least two arguments");

            //Get arguments
            string securityFileList = args[1];
            string destinationFolder = args[2];

            //Get optional arguments
            string daysToExportArg = (args.Count() >= 4) ? args[3] : "-d:20";
            string includeCurrentDayArg = (args.Count() >= 5) ? args[4] : "";
            int daysToExport = int.Parse(daysToExportArg.Split(':')[1]);
            bool includeCurrentDay = (includeCurrentDayArg == "-t");

            //Validate security list
            if (!System.IO.File.Exists(securityFileList)) throw new ArgumentException("Security list file '" + securityFileList + "' does not exist");
            string[] symbols = File.ReadAllLines(securityFileList);

            //Export specified security list to file
            foreach (string symbol in symbols)
            {
                string[] symbolItems = symbol.Split('.');
                if (symbolItems.Count() == 1)
                {

                    //Export both exchanges to file (if available) if no exchange specified
                    ExportSecurityToFile(symbol, "ASX", destinationFolder, daysToExport, includeCurrentDay);
                    ExportSecurityToFile(symbol, "CXA", destinationFolder, daysToExport, includeCurrentDay);

                }
                else
                {

                    //Export security to file for specified exchange
                    ExportSecurityToFile(symbolItems[0], symbolItems[1], destinationFolder, daysToExport, includeCurrentDay);

                }
            }

        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="symbol"></param>
        /// <param name="exchange"></param>
        /// <param name="dataFolder"></param>
        /// <param name="daysToExport"></param>
        /// <param name="includeCurrentDay"></param>
        private void ExportSecurityToFile(string symbol, string exchange, string dataFolder, int daysToExport, bool includeCurrentDay)
        {
            var exportManager = new ApiSecurityEventExportManager(symbol, exchange, daysToExport, dataFolder, includeCurrentDay);
            exportManager.OnProgressUpdate += exportManager_OnProgressUpdate;
            exportManager.Export();
        }

        /// <summary>
        /// Executed when a progress update is sent from the export manager
        /// </summary>
        private void exportManager_OnProgressUpdate(object sender, SparkAPI.Common.Events.GenericEventArgs<string> e)
        {
            _log.WriteLine(e.Value);
        }

    }

}

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