Click here to Skip to main content
13,404,627 members (36,431 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


5 bookmarked
Posted 28 Dec 2012

A MetaStock Class Library in C#

, 28 Dec 2012
Rate this:
Please Sign up or sign in to vote.
Here, I present the actual C# code I use to interface with the database that I update every evening from my data vendor.

Metastock is a popular data format for storing end-of-day and intraday stock quotes data.

It's a very old format with a structure that is a total mess. I myself have spent a lot of time figuring how to properly read quotes in a jungle of different files and formats.

Here, I present the actual C# code I use to interface with the database that I update every evening from my data vendor.

Information on the metastock data structure is scattered over the web. I've collected what seemed to work and implemented as a C# class. A special thank you to Julian M Bucknall for providing the code to convert from MBF4 (Microsoft Binary Format single precision) to standard floating point IEEE number.

The library is organized as follows:

public struct MetaStockRow  
    public DateTime Day;  
    public double Open;  
    public double High;  
    public double Low;  
    public double Close;  
    public double Volume;  
    public double OpenInterest;  
  • The struct MetaStockRow stores one day of data
  • A set of MetaStockRow(s) forms a MetaStockTable, that is, the whole data for a stock symbol
  • A set of MetaStockTable(s) forms a MetaStockCollection, which is useful when you have to work with more than one stock at a time. A special feature of MetaStockCollection is the capability to intersect dates, that is, making sure that the stocks you are working with have the same data points (days).
  • MetaStockTables and MetaStockCollections are generated by the class MetaStockReader which is the main object that performs the low-level reading on the database files. 

How to use the code:

  • Create a MetaStockReader object
  • Use AddPath() to tell it where your metastock folders are located (usually there is more than one)
  • Create a MetaStockCollection object
  • Insert stocks in the collection with AddSymbol()
  • Trim and sync dates in the collection with FilterPeriod() and IntersectDates()
  • Get daily closes with GetCloseArray()

An example:

MetaStockReader MR = new MetaStockReader();       

MetaStockCollection ML = new MetaStockCollection();                

ML = ML.FilterPeriod(DateTime.Parse("01/01/2000"),DateTime.Parse("31/12/2011"));  
ML = ML.IntersectDates();  

double[] s1 = ML[0].GetCloseArray();  
double[] s2 = ML[1].GetCloseArray();

If you need to work with just one stock, you can avoid using MetaStockCollection and work directly with MetaStockTable objects and access to its MetaStockRow(s).

There is a little more in the code of what I've shown here, but if you take a look at the sources you can easily discover by yourself all the other features. I must warn you the code quality is rather poor, because I haven't spent much time in improving it. I stopped when it "worked" (shame on me!).

Download the C# source code here.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Antonino Porcino
Software Developer
Italy Italy
No Biography provided

You may also be interested in...


Comments and Discussions

QuestionReupload the Link Pin
Hai Ninh Hoang3-Jan-18 5:15
memberHai Ninh Hoang3-Jan-18 5:15 
QuestionDownload link is broken! Pin
DuduardoBSB9-Aug-16 16:09
memberDuduardoBSB9-Aug-16 16:09 
GeneralThe download link is broken! Pin
DuduardoBSB9-Aug-16 16:34
memberDuduardoBSB9-Aug-16 16:34 
QuestionVisual studio 2013 Pin
jaoaoooaoa9-May-15 11:49
memberjaoaoooaoa9-May-15 11:49 
AnswerRe: Visual studio 2013 Pin
Antonino Porcino9-May-15 21:47
memberAntonino Porcino9-May-15 21:47 
Questioninvalid MBF array Pin
kelzi20-Apr-14 12:27
memberkelzi20-Apr-14 12:27 
AnswerRe: invalid MBF array Pin
Antonino Porcino20-Apr-14 23:00
memberAntonino Porcino20-Apr-14 23:00 
QuestionRead Intraday Pin
kerlos25-Feb-14 21:50
memberkerlos25-Feb-14 21:50 
AnswerRe: Read Intraday Pin
Antonino Porcino25-Feb-14 22:45
memberAntonino Porcino25-Feb-14 22:45 
GeneralRe: Read Intraday Pin
kerlos25-Feb-14 23:44
memberkerlos25-Feb-14 23:44 
AnswerRe: Read Intraday Pin
kerlos25-Feb-14 22:50
memberkerlos25-Feb-14 22:50 
QuestionMetastockReader Namespace error Pin
hussain54829-Jan-14 17:52
memberhussain54829-Jan-14 17:52 
AnswerRe: MetastockReader Namespace error Pin
Antonino Porcino30-Jan-14 4:34
memberAntonino Porcino30-Jan-14 4:34 
QuestionMetaStockTable object Pin
exfake1-Dec-13 2:56
memberexfake1-Dec-13 2:56 
AnswerRe: MetaStockTable object Pin
Antonino Porcino1-Dec-13 6:25
memberAntonino Porcino1-Dec-13 6:25 
Questionhow to write/Save Pin
rahulsalokhe29-Sep-13 5:27
memberrahulsalokhe29-Sep-13 5:27 
AnswerRe: how to write/Save Pin
Antonino Porcino29-Sep-13 6:59
memberAntonino Porcino29-Sep-13 6:59 
GeneralMy vote of 5 Pin
suddeepto24-Apr-13 7:37
membersuddeepto24-Apr-13 7:37 
QuestionMASTER file Pin
exfake18-Mar-13 3:20
memberexfake18-Mar-13 3:20 
AnswerRe: MASTER file Pin
Antonino Porcino18-Mar-13 4:20
memberAntonino Porcino18-Mar-13 4:20 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.1802120.2 | Last Updated 28 Dec 2012
Article Copyright 2012 by Antonino Porcino
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid