Log in to see Cloud of Tags

Wealth-Lab Wiki

Page History: Data | Exporting data out of WL6 to ASCII and binary files

Compare Page Revisions



« Older Revision - Back to Page History - Current Revision


Page Revision: 2014/02/18 13:37


Note:
Version 1.2.5 of the ASCII data provider contains an architectural change that brings serious performance improvements. From now on, it makes possible to work with ASCII files at the speed of binary files! If you used to export ASCII data to binary formats to speed up processing, there's no more need to do so.

Exporting to ASCII comma-separated files (CSV)

With WL6 you can export data effortlessly when required. The example below dumps security data of each symbol in the DataSet to ASCII comma-separated (CSV) files. (Modified routine 2/18/2014 to use simply use File.WriteAllLines instead of StreamWriter.)

Configure target directory:

using System;
using System.Collections.Generic;
using System.Text;
using WealthLab;
using System.IO;

public class Export2ASCII : WealthScript { protected override void Execute() { const string sep = ","; const string fmt = "0.00########"; string dte = ""; string path = @"C:\Data\ASCII\"; if (!Directory.Exists(path)) throw new Exception("You must create the directory " + path); PrintStatusBar("Exporting: " + Bars.Symbol); string file = path + Bars.Symbol + ".csv"; List<string> datalist = new List<string>(); for(int bar = 0; bar < Bars.Count; bar++) { if( Bars.BarInterval == 0 ) dte = Date[bar].ToString("yyyyMMdd"); else dte = Date[bar].ToString("yyyyMMdd HHmm"); string csv = dte + sep + Open[bar].ToString(fmt) + sep + High[bar].ToString(fmt) + sep + Low[bar].ToString(fmt) + sep + Close[bar].ToString(fmt) + sep + Volume[bar].ToString("0"); datalist.Add(csv); } File.WriteAllLines(file, datalist); RestoreContext(); PrintStatusBar("Complete!"); } }

Getting dates formatted in a custom way

To create the Date/Time in one of the standard formats (or in a custom one), modify the code as shown below:

As you see, instead of using the .ToShortDateString that takes the format specified in Windows Control Panel's Regional settings applet, we're getting the desired output by using .ToString() and passing a format string:


...
csv.WriteLine( Date[bar].ToString("yyyyMMdd") +
...

This instructs the program to output the date formatted as yyyymmdd. More format examples can be found on Microsoft's site:


Standard DateTime Format Strings
Custom DateTime Format Strings


Exporting as WLD 3/4 native binaries (*.WL)

Illustrating the usage of BinaryWriter class mostly, this simple code exports data contained in ASCII (or any other) DataSet to WLP/WLD 4.x native binary format.

By default, files will be placed into C:\Data\WL4\; configure target directory as necessary:

string path = @"C:\Data\WL4\";

Run it in Single Symbol Mode or on the entire DataSet (as MSB).


using System;
using WealthLab;
using System.IO;

namespace WealthLab.Strategies { public class ExportToWL : WealthScript { protected override void Execute() { double dt; float o,h,l,c,v; string path = @"C:\Data\WL4\"; if (!Directory.Exists(path)) throw new Exception("You must create the directory " + path); // Open a binary writer for a file FileInfo f = new FileInfo( path + Bars.Symbol + ".wl" ); BinaryWriter bw = new BinaryWriter( f.OpenWrite() ); // The first 4 bytes of each file hold BarCount (integer): bw.Write(Bars.Count); // The bar loop for(int bar = 0; bar < Bars.Count; bar++) { /* Write data (one block of data for each bar) double Datetime as "OLE Automation Date" float: Open, High, Low, Close, Volume */ { dt = (double)Date[bar].ToOADate(); o = (float)Open[bar]; h = (float)High[bar]; l = (float)Low[bar]; c = (float)Close[bar]; v = (float)Volume[bar]; bw.Write(dt); bw.Write(o); bw.Write(h); bw.Write(l); bw.Write(c); bw.Write(v); } } } } }

Important Disclaimer: The information provided by Wealth-Lab is strictly for informational purposes and is not to be construed as advice or solicitation to buy or sell any security.  The owner of Wealth-Lab.com assumes no liability resulting from the use of the material contained herein for investment purposes. By using this web site, you agree to the terms of this disclaimer and our Terms of Use.


ScrewTurn Wiki. Some of the icons created by FamFamFam.