Earlier versions of Zorro used to ship with a script for converting market data in Zorro binary format to CSV. That script seems to have disappeared with the recent versions of Zorro, so I thought I’d post it here.
When you run this script by selecting it and pressing [Test] on the Zorro interface, you are asked to select a Zorro market data file to convert to CSV format. Zorro then does the conversion for you and writes the data to Zorro/History/Export.csv.
// convert a .t6 or .t1 file to .csv //#define ASCENDING #define MAX_RECORDS 10000 string Target = "History\\Export.csv"; int point(int Counter) { if(0 == Counter % 10000) { if(!wait(0)) return 0; printf("."); } return 1; } function main() { file_delete(Target); string Source = file_select("History","T1,T6\0*.t1;*.t6\0\0"); if(strstr(Source,".t6")) { T6 *Ticks = file_content(Source); int Records = file_length(Source)/sizeof(T6); printf("\n%d records..",Records); #ifdef MAX_RECORDS Records = min(Records,MAX_RECORDS); #endif #ifdef ASCENDING int nTicks = Records; while(--nTicks) #else int nTicks = -1; while(++nTicks < Records) #endif { T6 *t = Ticks+nTicks; file_append(Target,strf("%s,%.5f,%.5f,%.5f,%.5f\n", strdate("%Y-%m-%d %H:%M:%S",t->time), (var)t->fOpen,(var)t->fHigh,(var)t->fLow,(var)t->fClose)); if(!point(nTicks)) return; } } else if(strstr(Source,".t1")) { T1 *Ticks = file_content(Source); int Records = file_length(Source)/sizeof(T1); printf("\n%d records..",Records); #ifdef MAX_RECORDS Records = min(Records,MAX_RECORDS); #endif #ifdef ASCENDING int nTicks = Records; while(--nTicks) #else int nTicks = -1; while(++nTicks < Records) #endif { T1 *t = Ticks+nTicks; file_append(Target,strf("%s,%.5f\n", strdate("%Y-%m-%d %H:%M:%S",t->time),(var)t->fVal)); if(!point(nTicks)) return; } } printf("\nDone!"); }
By default, the data is written in descending order (newest data first). If you want ascending order instead, uncomment the line #define ASCENDING
.
As you can see, this script works with Zorro version 2.30:
This script is useful if you want to convert a single market data file. But it’s a little cumbersome if you want to convert the entire market data history of a ticker since Zorro splits that data into separate files by year (except for end-of-day data – that all goes into a single file).
Here’s a script for converting the entire history of a ticker from Zorro format to CSV:
//Export selected asset history to CSV function run() { StartDate = 20060101; LookBack = 0; BarPeriod = 1; string Format = ifelse(assetType(Asset) == FOREX, "\n%04i-%02i-%02i %02i:%02i, %.5f, %.5f, %.5f, %.5f", "\n%04i-%02i-%02i %02i:%02i, %.1f, %.1f, %.1f, %.1f"); char FileName[40]; sprintf(FileName,"History\\%s.csv",strx(Asset,"/","")); // remove slash from forex pairs if(is(INITRUN)) file_write(FileName,"Date,Open,High,Low,Close",0); else file_append(FileName,strf(Format, year(),month(),day(),hour(),minute(), round(priceOpen(),0.1*PIP), round(priceHigh(),0.1*PIP), round(priceLow(),0.1*PIP), round(priceClose(),0.1*PIP))); }
This one takes the ticker selected in Zorro’s asset dropdown box and writes its entire history to Zorro/History/ticker.csv. Again, you can see it works with Zorro 2.30:
If you want to import that data into R as an xts
object, the following snippet will do the trick:
Data <- xts(read.zoo("ticker.csv", tz="UTC", format="%Y-%m-%d %H:%M", sep=",", header=TRUE))
1 thought on “Exporting Zorro Data to CSV”