Get from Ico-github-logo

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video

Highlights from
okomarov/metastockread

  • metastockread(fullpath)
    METASTOCKREAD Read metastock files (symbols index files: master, emaster, xmaster; data files: .dat and .mwd)
  • View all files

Join the 15-year community celebration.

Play games and win prizes!

» Learn more

4.4
4.4 | 5 ratings Rate this file 5 Downloads (last 30 days) File Size: 1.29 MB File ID: #28664 Version: 1.7
image thumbnail

okomarov/metastockr‚Äčead

by

Oleg Komarov (view profile)

 

08 Sep 2010 (Updated )

Read metastock files (master, emaster, xmaster and .dat/.mwd)

| Watch this File

File Information
Description

Read metastock files (symbols index files: master, emaster, xmaster; data files: .dat and .mwd; no .dop support)
SYNTAXES:
(1) METASTOCKREAD Read the symbols index file selected with uigetfile and import the data
(2) METASTOCKREAD(FULLPATH) Read the file specified by FULLPATH

OUT = metastockread(...)
OUT is a "m by 1" NON-scalar structure, where "m" is the # of .dat/.mwd files in the same directory of the symbol index files.

The (sub)scalar structure has the following fields:
- datNum : # of the .dat/.mwd file with the data
- symbol : security symbol
- name : security name
- inDate : initial date as 'yyyy-mm-dd'
- fiDate : final date as 'yyyy-mm-dd'
- freq : time freq. of the data I (intraday), D (daily), W, M, Q or Y
- idFreq : intraday time frequency in minutes
- data : time series data with variable number of fields (columns)

The data field columns follow the schema:

| Date/Time | Open | High | Low | Close | Volume | OpenInterest |

For details see help
For examples see the attached image.

MATLAB release MATLAB 7.9 (R2009b)
Tags for This File   Please login to tag files.

No tags are associated with this file.

Please login to add a comment or rating.
Comments and Ratings (20)
11 Jul 2016 Oleg Komarov

Oleg Komarov (view profile)

@Joseph thanks for reporting on Github. As mentioned there you need to read the Xmaster.

Comment only
07 Jul 2016 Oleg Komarov

Oleg Komarov (view profile)

Well, I can't help without your other data. Also, if you could submit a bug report on github with all the details, it will be easier to tackle than here.

Comment only
06 Jul 2016 Joseph

Joseph (view profile)

Hi Oleg, When I run it on the sample data or other data struct size is always 255 ? Please advise.

Comment only
11 Feb 2016 Oleg Komarov

Oleg Komarov (view profile)

I updated the submission with:
- cross-OS path handling
- got rid of dataread dependency
- API change: missing data files now load as empty field struct rather than scalar NaNs, this allows fast concatenation.

Comment only
09 Feb 2016 Oleg Komarov

Oleg Komarov (view profile)

My email has the format name.surname@hotmail.it, where you have to sub name and surname all lowercase

Comment only
09 Feb 2016 David Snyder

David Snyder (view profile)

@Oleg: My pleasure. I'm afraid I'm not familiar with git, though I can email you the corrected file if there is a way to do that without violating fileexchange protocol. I discovered one problem with my fix in the case that a folder name includes white space. The 'EndOfLine' parameter in the call to textscan should be 'Delimiter'.

05 Feb 2016 Oleg Komarov

Oleg Komarov (view profile)

@David: thanks a lot for the fixes. If you are familiar with git, you can submit a pull request in Github, else I'll see sometime next week when I can patch it up.

Comment only
04 Feb 2016 David Snyder

David Snyder (view profile)

Great file exchange contribution. A few changes were necessary to make metastockread compatible with Mac OS X (different path delimiters):

replace:
if regexpi(fullpath,'\w*\\master')
Out = readMaster(fid);
elseif regexpi(fullpath,'\w*\\emaster')
Out = readEmaster(fid);
elseif regexpi(fullpath,'\w*\\xmaster')
Out = readXmaster(fid);
else
error('metastockread:namefile','The name of the file should be either MASTER, EMASTER or XMASTER.')
end

with:
if ismac
regexpPrefix = '\w*/';
elseif ispc
regexpPrefix = '\w*\\';
else
error('metastockread:platform', 'The platform must be Mac or PC.');
end
if regexpi(fullpath,[regexpPrefix, 'master'])
Out = readMaster(fid);
elseif regexpi(fullpath,[regexpPrefix, 'emaster'])
Out = readEmaster(fid);
elseif regexpi(fullpath,[regexpPrefix, 'xmaster'])
Out = readXmaster(fid);
else
error('metastockread:namefile','The name of the file should be either MASTER, EMASTER or XMASTER.')
end

replace:
pathname = fullpath(1:find(fullpath == '\',1,'last'));
pathname = strrep(pathname, '\','\\');

with:
if ismac
pathname = fullpath(1:find(fullpath == '/',1,'last'));
elseif ispc
pathname = fullpath(1:find(fullpath == '\',1,'last'));
pathname = strrep(pathname, '\','\\');
end

replace call to deprecated dataread function:
if Out(1).datNum < 256; fmt = 'F%d.DAT';
else fmt = 'F%d.MWD'; end
names = dataread('string',sprintf([pathname fmt],cat(1,Out.datNum)),'%s','delimiter','\n');

with:
if Out(1).datNum < 256; fmt = 'F%d.DAT\n';
else fmt = 'F%d.MWD\n'; end
names = textscan(sprintf([pathname fmt],cat(1,Out.datNum)),'%s','EndOfLine','\n');
names = names{1};

Comment only
27 Mar 2015 Oleg Komarov

Oleg Komarov (view profile)

Thanks for reporting Richard and Goryn. I'll see when I can port it on github and fix those problems.

Comment only
10 Mar 2015 Richard

Made the following change to lines 118-119 to get the code to work in R2015a:

if Out(1).datNum < 256; fmt = 'F%d.DAT\n';
else fmt = 'F%d.MWD\n'; end

changed to:

if Out(1).datNum < 256; fmt = 'F%d.DAT';
else fmt = 'F%d.MWD'; end

Not sure what the compatibility issue is here.
Maybe someone else can explain?

Comment only
06 Jan 2014 Goryn

Goryn (view profile)

Very useful, thank you! I've found an error on line 156 and 158. Variable 'm' should be changed for 'mn' (minutes) or it overwrites variable 'm'(months) and causes error with intraday data.

Comment only
06 Jan 2014 Goryn

Goryn (view profile)

 
04 Mar 2012 James Kunce

Thank you!! Exactly what I was looking for and works great.

09 Dec 2010 cheng

cheng (view profile)

just i want;so thanks

03 Nov 2010 Oleg Komarov

Oleg Komarov (view profile)

I will look into the problem after the 12th of nov.
I hope I'll be able to write a metastockwrite function, in the meantime you can use the solution suggested by Tony.

Comment only
03 Nov 2010 Tony Tran

For whose who need to export to Metastock format
Step 1: output to text format, see the example

% format AAM,20100913,24.40,24.90,23.70,24.00,13600,0
fid = fopen('scindex.txt','w');
for i=1:size(SCIndex)
sdate=datestr(TDate(i),'yyyymmdd');
fprintf(fid, 'SCINDEX,%s,%.2f,%.2f,%.2f,%.2f,0\n',sdate,SCIndex(i),SCIndex(i),SCIndex(i),SCIndex(i));
end
fclose(fid);

Then call external program to convert the text file to MS. asc2ms can be found easily by google.

% execute
dos('asc2ms -r recordReplace -f "scindex.txt" -o "D:\MetaStock Data\VNINDEX"');

Comment only
29 Oct 2010 Han Oostdijk

nice well structured example of reading binary data.

27 Oct 2010 Shailesh Joshi

I will be happy to have program which will write data in metastock format. Mostly I convert it after downloading manually.

Comment only
09 Oct 2010 Oleg Komarov

Oleg Komarov (view profile)

I may if there's request.

Comment only
07 Oct 2010 Shailesh Joshi

By any chance do you have program to write to metastock format?

Comment only
Updates
09 Sep 2010 1.1

Added link to FEX page, edited example and edited description.

03 Aug 2011 1.2

Added fix for buggy *.dat files as pointed out by Davide Dalmasso

03 Aug 2011 1.3

Added fix for buggy *.dat files as pointed out by Davide Dalmasso

19 Feb 2012 1.4

Fixed erroneous date conversion for intraday series. As pointed out by Luca LPr the month had the same value as minutes due to same name of the variable.

19 Feb 2012 1.5

Uploaded wrong file.

17 Oct 2013 1.6

 - Cross platform compatible filepath handling (thanks to Thiago Tavares for reporting)
- got rid of dataread for forward compatibility

19 Sep 2016 1.7

Fixes suggeste dby Richard and Goryn, ported to github

Contact us