Click here to Skip to main content
15,895,283 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 333K   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.IO;
using System.Xml.Serialization;

namespace SparkAPI.Common.Serialization
{

    /// <summary>
    /// Provides generic methods to save and load a serializable object to and from XML 
    /// </summary>
    /// <typeparam name="T">Object type to be serialized</typeparam>    
    public class XmlSerializer<T> where T : class, new()
    {

        /// <summary>
        /// Reads and deserializes object from specified xml data text
        /// </summary>
        /// <param name="xml">XML text</param>
        public static T DeserializeFromString(string xml)
        {
            T result;
            if (string.IsNullOrEmpty(xml))
            {
                result = new T();
            }
            else
            {
                XmlSerializer serializer = new XmlSerializer(typeof(T));
                using (StringReader reader = new StringReader(xml))
                {
                    result = (T)serializer.Deserialize(reader);
                }
            }
            return result;
        }

        /// <summary>
        /// Reads and deserializes object from specified file name
        /// </summary>
        /// <param name="fileName">Schema list file name</param>
        public static T DeserializeFromFile(string fileName)
        {
            T result = null;
            if (System.IO.File.Exists(fileName))
            {
                string xmlData = System.IO.File.ReadAllText(fileName);
                result = DeserializeFromString(xmlData);
            }
            return result;
        }

        /// <summary>
        /// Serializes and saves object to specified file name
        /// </summary>
        /// <param name="item"></param>
        /// <param name="fileName"></param>
        public static void SerializeToFile(T item, string fileName)
        {
            System.IO.FileStream fileStream = new System.IO.FileStream(fileName, System.IO.FileMode.Create);
            System.Xml.Serialization.XmlSerializer sr = new System.Xml.Serialization.XmlSerializer(item.GetType());
            sr.Serialize(fileStream, item);
            fileStream.Close();
        }

        /// <summary>
        /// Serializes and saves object to specified file name
        /// </summary>
        /// <param name="item"></param>
        public static string SerializeToString(T item)
        {
            StringWriter writer = new StringWriter();
            System.Xml.Serialization.XmlSerializer sr = new System.Xml.Serialization.XmlSerializer(item.GetType());
            sr.Serialize(writer, item);
            return writer.ToString();
        }

    }
}

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