Click here to Skip to main content
15,896,425 members
Articles / Programming Languages / C#

Volume Weighted Average Price (VWAP) Algorithm

Rate me:
Please Sign up or sign in to vote.
4.94/5 (11 votes)
11 Jan 20063 min read 137.8K   1.9K   76  
Calculates Volume Weighted Average Price for Financial Time series.
using System;
using System.Collections;
using System.ComponentModel;

namespace Fabrefactum
{

	/// <summary>
	/// A datetime-ordinated time series
	/// </summary>
	[SerializableAttribute()]
	public class TimeSeries: IEnumerable
	{
		private class TimeSeriesEnumerator: IEnumerator
		{
			private TimeSeries fArray;

			private int fIndex;

			object IEnumerator.Current
			{
				get
				{ 
					return fArray[fIndex];
				}
			}

			public TimeSeriesEnumerator(TimeSeries Array)
			{
				fArray = Array;
				fIndex = -1;
			}

			bool IEnumerator.MoveNext()
			{
				fIndex++;
				if (fIndex >= fArray.Count)
				{
					return false;
				}
				else
				{
					return true;
				}
			}

			void IEnumerator.Reset()
			{
				fIndex = -1;
			}
		}

		protected internal SortedList fArray = new SortedList();

		protected string fName;

		protected string fTitle;

		protected bool fChanged = false;

		[DescriptionAttribute("")]
		[CategoryAttribute("Description")]
		public string Name
		{
			get
			{
				return fName;
			}

			set
			{
				fName = value;
			}
		}

		[DescriptionAttribute("")]
		[CategoryAttribute("Description")]
		public string Title
		{
			get
			{
				return fTitle;
			}

			set
			{
				fTitle = value;
			}
		}

		[BrowsableAttribute(false)]
		public virtual int Count
		{
			get
			{
				return fArray.Count;
			}
		}

		[BrowsableAttribute(false)]
		public virtual DateTime FirstDateTime
		{
			get
			{
				if (Count <= 0)
				{
					throw new ApplicationException("Array has no elements");
				}
				else
				{
					return GetDateTime(0);
				}
			}
		}

		[BrowsableAttribute(false)]
		public virtual DateTime LastDateTime
		{
			get
			{
				if (Count <= 0)
				{
					throw new ApplicationException("Array has no elements");
				}
				else
				{
					return GetDateTime(Count - 1);
				}
			}
		}

		[BrowsableAttribute(false)]
		public virtual int FirstIndex
		{
			get
			{
				return 0;
			}
		}

		[BrowsableAttribute(false)]
		public virtual int LastIndex
		{
			get
			{
				return Count - 1;
			}
		}

		[BrowsableAttribute(false)]
		public virtual int RealCount
		{
			get
			{
				return Count;
			}
		}

		public virtual object this [int Index]
		{
			get
			{
				return fArray.GetByIndex(Index);
			}
		}

		public virtual double this [int Index, int Row]
		{
			get
			{
				return (double)fArray.GetByIndex(Index);
			}
					
		}
 
		public object this [DateTime DateTime]
		{
			get
			{
					int i = GetIndex(DateTime);
				if (i != -1)
				{
					return fArray.GetByIndex(i);}
				else
				{
					return null;
				}
			}

			set
			{
				Add(DateTime, value);
			}
		}

		/// <summary>
		/// Empty Constructor
		/// </summary>
		public TimeSeries()
		{
		}

		/// <summary>
		/// Constructor with Name and Title
		/// </summary>
		/// <param name="Name"></param>
		/// <param name="Title"></param>
		public TimeSeries(string Name, string Title)
		{
			fName = Name;
			fTitle = Title;
		}

		public TimeSeries(string Name)
		{
			fName = Name;
		}

		public virtual TimeSeries Clone()
		{
			return Clone(0, Count - 1);
		}

		/// <summary>
		/// Clones a TimeSeries
		/// </summary>
		/// <param name="Index1"></param>
		/// <param name="Index2"></param>
		/// <returns></returns>
		public virtual TimeSeries Clone(int Index1, int Index2)
		{

			TimeSeries timeSeries = new TimeSeries(fName,fTitle);
			if (Count == 0)
			{
				return timeSeries;
			}
			for (int i = Index1; i <= Index2; i++)
			{
				timeSeries.Add(GetDateTime(i), this[i]);
			}
			return timeSeries;
		}

	    /// <summary>
		/// Clears the entire TimeSeries of data
		/// </summary>
		public virtual void Clear()
		{
			fArray.Clear();
			fChanged = true;
		}

		public bool Contains(DateTime DateTime)
		{
			return fArray.Contains(DateTime);
		}

		public bool Contains(int Index)
		{
			if (Index < 0 || Index > Count - 1)
			{
				return false;
			}
			else
			{
				return true;
			}
		}

		/// <summary>
		/// Add an element using a date as an index
		/// </summary>
		/// <param name="DateTime"></param>
		/// <param name="Object"></param>
		public void Add(DateTime DateTime, object Object)
		{
			fArray[DateTime] = Object;
			fChanged = true;
		}

		/// <summary>
		/// Remove an element using a data as index
		/// </summary>
		/// <param name="DateTime"></param>
		public virtual void Remove(DateTime DateTime)
		{
			fArray.Remove(DateTime);
			fChanged = true;
		}

		public virtual void Remove(int Index)
		{
			fArray.RemoveAt(Index);
			fChanged = true;
		}

		/// <summary>
		/// Gets the date time of a particular index
		/// </summary>
		/// <param name="Index"></param>
		/// <returns></returns>
		public virtual DateTime GetDateTime(int Index)
		{
			return (System.DateTime)fArray.GetKey(Index);
		}

			/// <summary>
		/// Finds an object and returns its index
		/// </summary>
		/// <param name="Item"></param>
		/// <returns></returns>
		public virtual int GetIndex(object Item)
		{
			return fArray.IndexOfValue(Item);
		}

		public virtual void Print()
		{
			for (int i = 0; i < Count; i++)
			{
				Console.WriteLine(String.Concat(GetDateTime(i), " ", this[i]));
			}
		}

	
		IEnumerator IEnumerable.GetEnumerator()
		{
			return new TimeSeriesEnumerator(this);
		}
	}

}

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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United States United States
Andrew Peters is a systems developer interested in non-trivial trading systems and financial systems architecture. He is currently focused on realtime, high performance multi-threaded applications running on the server and the desktop.

After a 4 year stint in China learning Mandarin and Tibetan, Andrew returned to the US to learn more about enterprise development and financial markets. While in China, he translated meetings between demure Communist officials and angry American businessmen, served coffee and fetid tofu in his 'BaiSuiFang' Coffee Shop, started Fabrefactum Software and was generally laughed at for his stupid jokes in Chinese.

He currently helps the pricing/analytics team hack on code at Chatham Financial, an interest rate and foreign exchange derivative consulting company.

Comments and Discussions