File Exchange

image thumbnail

okomarov/metastockr​ead

version 1.7 (1.29 MB) by

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

7 Downloads

Updated

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.

Comments and Ratings (20)

Oleg Komarov

Oleg Komarov (view profile)

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

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.

Joseph

Joseph (view profile)

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

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.

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

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'.

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.

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};

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.

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?

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.

Goryn

Goryn (view profile)

James Kunce

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

cheng

cheng (view profile)

just i want;so thanks

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.

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"');

Han Oostdijk

nice well structured example of reading binary data.

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

Oleg Komarov

Oleg Komarov (view profile)

I may if there's request.

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

Updates

1.7

Fixes suggeste dby Richard and Goryn, ported to github

1.6

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

1.5

Uploaded wrong file.

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.

1.3

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

1.2

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

1.1

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

MATLAB Release
MATLAB 7.9 (R2009b)
Tags Add Tags

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

» Watch video