Gamestudio Links
Zorro Links
Newest Posts
Help with plotting multiple ZigZag
by degenerate_762. 04/30/24 23:23
M1 Oversampling
by 11honza11. 04/30/24 08:16
Trading Journey
by howardR. 04/28/24 09:55
Zorro Trader GPT
by TipmyPip. 04/27/24 13:50
Data from CSV not parsed correctly
by jcl. 04/26/24 11:18
Why Zorro supports up to 72 cores?
by jcl. 04/26/24 11:09
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
4 registered members (AndrewAMD, 7th_zorro, VoroneTZ, Quad), 912 guests, and 0 spiders.
Key: Admin, Global Mod, Mod
Newest Members
firatv, wandaluciaia, Mega_Rod, EternallyCurious, howardR
19050 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Page 1 of 3 1 2 3
My attempt to create a .bar file #435147
01/03/14 14:10
01/03/14 14:10
Joined: Jun 2013
Posts: 41
Ohio, USA
P
Pork Offline OP
Newbie
Pork  Offline OP
Newbie
P

Joined: Jun 2013
Posts: 41
Ohio, USA
Hello all,
after Dusktrader's excellent trading script, I wa eager to create some other strategy's. Come to find out, I don't have access to the history that I need from my trading account with Finfx. I don't have a fxcm account so I went to histdata and downloaded a sample of their data.
the data is formatted as follows:
Row Fields:
DateTime Stamp,Bid Quote,Ask Quote,Volume

DateTime Stamp Format:
YYYYMMDD HHMMSSNNN

Legend:
YYYY – Year
MM – Month (01 to 12)
DD – Day of the Month
HH – Hour of the day (in 24h format)
MM – Minute
SS – Second

I've attached my script which doesn't seem to be able to move the open,close,high and low to the TICK structure and doesn't seem to create the .bar file. It also aborts after the third time thru.
I'm out of ideas. Would appreciate any help any one can bring.
Thanks
P

//convert History downloaded from www.histdata.com
//download each year, change in excel to use a comma delimited file, if not already
//sorted so the latest entry in first as required by zorro.
//This script will read the file defined in the script, convert the records to Zorro's
//format and then write them to a binary file name export.bar to be used by Zorro.
//youo can then rename the file to the Zorro .bar format.
//the format of the histdata.com file is DateTime,Open,High,Low,Close,Volume
//the format for zorron is the tick stru included in include\trading.h
//including OLE Date.counting days since midnight 30 December 1899
//typedef struct TICK{ float fOpen, fClose; float fHigh, fLow; DATE time; // time of the tick in UTC time, OLE date/time format} TICK;
//
//the timezone for the histdata.com files is EST without daylight savings time
//so the conversion must take that into account
#include <default.c>
//#include <litec.h>
#include <stdio.h>
#include <trading.h>

TICK mytick;
int count=0;

typedef struct _SYSTEMTIME {
WORD wYear;
WORD wMonth;
WORD wDayOfWeek;
WORD wDay;
WORD wHour;
WORD wMinute;
WORD wSecond;
WORD wMilliseconds;
} SYSTEMTIME;

API(SystemTimeToVariantTime,oleaut32)
int _stdcall SystemTimeToVariantTime(SYSTEMTIME* lpSystemTime, double* pvtime);

DATE ConvertTime(int Year,int Month,int Day,int Hour,int Minute)
{
SYSTEMTIME Time;
memset(&Time,0,sizeof(SYSTEMTIME));
Time.wYear = Year;
Time.wMonth = Month;
Time.wDay = Day;
Time.wHour = Hour;
Time.wMinute = Minute;
DATE vTime;
SystemTimeToVariantTime(&Time,&vTime);
return vTime;
}

function convertdata(string csv)
{
string nextline = strstr(csv,"\n");//this is the file without the first line
string separator = ",";

int Year,Month,Day,Hour,Minute;
int mysec;
//use the csv string to seperate the date time
//format of datetime is "YYYYMMDD HHMMSS" SS is always zero and is not used
sscanf(strtok(csv,separator),"%4i%2i%2i %2i%2i%2i",&Year,&Month,&Day,&Hour,&Minute,&mysec);
mytick.time = ConvertTime(Year,Month,Day,Hour,Minute);

//separate the other vars last column is vol it is always 0 and is ignored
sscanf(strtok(0,separator),"%f",&mytick.fOpen);
sscanf(strtok(0,separator),"%f",&mytick.fHigh);
sscanf(strtok(0,separator),"%f",&mytick.fLow);
sscanf(strtok(0,separator),"%f",&mytick.fClose);
printf("\n %f %f %f %f %f",mytick.time,mytick.fOpen,mytick.fHigh,mytick.fLow,mytick.fClose);

return nextline;
}

function main()
{
string Name = "History\\AUDCAD_M1_2012.csv"; // name of the CSV file containing M1 data
if(!file_date(Name)){
quit("File not found!");
}
//open and create export.bar
FILE* pfile;
pfile=fopen("export.bar","w+b");
if(pfile==NULL){
quit("\n unable to open bar file");
}

printf("\n%s found export file opened",Name);
string mycontent = file_content(Name);
printf("\n firstline %s",mycontent);
while(mycontent) {
mycontent=convertdata(mycontent);
printf("\n nextline %s", mycontent);
fwrite(&mytick,sizeof(mytick),sizeof(mytick),pfile);
if(ferror(pfile)){
printf("\nfile write error");
}
x++;
}//endwhile
printf("\n before close file");
fclose(pfile);
}

Last edited by Pork; 01/03/14 14:17.
Re: My attempt to create a .bar file [Re: Pork] #435174
01/03/14 17:28
01/03/14 17:28
Joined: Jul 2013
Posts: 522
D
dusktrader Offline
User
dusktrader  Offline
User
D

Joined: Jul 2013
Posts: 522
Rather than go through all that, it would be easier to just open an FXCM demo account and download the history that way. Have you tried this yet? FXCM has several pairs and using the Download.c script it's pretty easy to get the ones you want.

Re: My attempt to create a .bar file [Re: dusktrader] #435217
01/03/14 22:20
01/03/14 22:20
Joined: Dec 2013
Posts: 1
North Carolina
P
POGLife Offline
Guest
POGLife  Offline
Guest
P

Joined: Dec 2013
Posts: 1
North Carolina
Thanks for this. Been really helpful dusktrader. And thanks for asking this Pork.

Re: My attempt to create a .bar file [Re: POGLife] #435220
01/03/14 23:17
01/03/14 23:17

A
acidburn
Unregistered
acidburn
Unregistered
A



Still, this script is quite interesting because Pork did some important homework there, especially with dateformat munging, parsing etc... Previously when I needed to convert date formats, I was too lazy to discover how to do it properly, so I cheated. wink

I will take a peek Pork, and see if the script can be finished. Thanks for your work so far.

Re: My attempt to create a .bar file [Re: ] #435221
01/03/14 23:36
01/03/14 23:36

A
acidburn
Unregistered
acidburn
Unregistered
A



Pork, you're describing their tick data in the beginning of your post, yet it seems that you eventually downloaded their M1 data. I will assume that you wanted to import the latter, that should be much easier to accomplish. But later I will build upon that, to create bar synthesizer, so that we can also import tick data and convert it to M1, and some other interesting formats. At least, that's the idea, currently all those windows calls and data structures look very spooky to me. tongue

Here's what my sample import data looks like:

Code:
20131201 170000;1.358680;1.358740;1.358680;1.358680;0
20131201 170100;1.358700;1.358750;1.358650;1.358650;0
20131201 170200;1.358680;1.358750;1.358640;1.358740;0
20131201 170300;1.358850;1.359180;1.358840;1.359070;0



Downloaded from http://www.histdata.com/download-free-fo.../eurusd/2013/12

Re: My attempt to create a .bar file [Re: Pork] #435222
01/04/14 00:14
01/04/14 00:14
Joined: Jun 2013
Posts: 41
Ohio, USA
P
Pork Offline OP
Newbie
Pork  Offline OP
Newbie
P

Joined: Jun 2013
Posts: 41
Ohio, USA
Thanks Dusk trader, I did not know about the FXCM demos. will give that a try.
@acidburn.. I beleive I loaded the M1 data. Here's a sample:
20120102 020200,1.04143,1.04152,1.04143,1.04152,0
20120102 023700,1.04072,1.04075,1.0395,1.04075,0
20120102 023800,1.0412,1.0412,1.04069,1.04088,0
20120102 023900,1.04085,1.04095,1.04059,1.04095,0
20120102 024000,1.04093,1.04097,1.04073,1.04094,0
20120102 024100,1.04092,1.04103,1.0409,1.041,0
20120102 024200,1.04087,1.04108,1.04087,1.04108,0
20120102 024300,1.04105,1.04108,1.04094,1.04097,0
20120102 024400,1.04098,1.04108,1.04098,1.04102,0

Hopefully you have better luck than I did finding the issue or issues. I've tried a number of things over the last few days without effect. I appreciate your efforts.
P

Re: My attempt to create a .bar file [Re: Pork] #435223
01/04/14 00:46
01/04/14 00:46

A
acidburn
Unregistered
acidburn
Unregistered
A



Lots of stuff is confusing, yes. Zorro crashes left and right. It crashes so often that I had to move the icon closer, so I can restart it faster. grin

Definitely hard working without the debugger...

The data I downloaded uses ';' as a separator, but otherwise looks the same. A different separator should not be a big issue.

Re: My attempt to create a .bar file [Re: ] #435235
01/04/14 11:13
01/04/14 11:13

A
acidburn
Unregistered
acidburn
Unregistered
A



And here's the first version for the public. It's small, simple, beautiful and it actually works. Unfortunately, it's still not doing what Zorro expects, because bar files are backwards in time. &^%^$$#@#&*!

I'm afraid coding that last requirement to make bar file fully Zorro compliant will make it much uglier, thus I decided to post this intermediate version. I still think that bar files are poorly designed, CPU cycles are cheap, human time is precious.

While I'm adding the final piece of magic, if anybody can quickly explain how to add a new instrument to Zorro would be helpful. Or point to an existing forum post, I'm sure it popped up before and I might have it bookmarked, but I'll never find it in dozens of bookmarks I collected. This will come handy to not overwrite existing history files, but test with new instrument names.

Code:
#include <default.c>
#include <stdio.h>
#include <windows.h>

typedef struct SYSTEMTIME {
	WORD wYear;
	WORD wMonth;
	WORD wDayOfWeek;
	WORD wDay;
	WORD wHour;
	WORD wMinute;
	WORD wSecond;
	WORD wMilliseconds;
} SYSTEMTIME;

int _stdcall SystemTimeToVariantTime(SYSTEMTIME *lpSystemTime, DATE *pvtime);

DATE ConvertTime(int year, int month, int day, int hour, int minute, int second)
{
	SYSTEMTIME Time;
	DATE vTime;

	memset(&Time, 0, sizeof(SYSTEMTIME));
	Time.wYear = year;
	Time.wMonth = month;
	Time.wDay = day;
	Time.wHour = hour;
	Time.wMinute = minute;
	Time.wSecond = second;

	SystemTimeToVariantTime(&Time, &vTime);
	return vTime;
}

function main()
{
	char line[64];
	int year, month, day, hour, minute, second, bars = 0, errors = 0;
	float open, high, low, close;
	FILE *in, *out;
	TICK tick;

	if (!(out = fopen("export.bar", "wb"))) {
		printf("\ncan't open output file");
		return;
	}

	if (!(in = fopen("EURUSD_M1_201312.csv", "r"))) {
		printf("\ncan't open input file");
		fclose(out);
		return;
	}

	while (fgets(line, 64, in)) {
		bars++;
		if (sscanf(line, "%4d%2d%2d %2d%2d%2d;%f;%f;%f;%f;",
			&year, &month, &day, &hour, &minute, &second,
			&tick.fOpen, &tick.fHigh, &tick.fLow, &tick.fClose) == 10) {
			tick.time = ConvertTime(year, month, day, hour, minute, second);
			if (!fwrite(&tick, sizeof(tick), 1, out)) {
				printf("\nwrite error");
				fclose(in);
				fclose(out);
				return;
			}
		} else {
			errors++;
		}
	}

	fclose(in);
	fclose(out);
	printf("\nConverted: %d bars, %d errors", bars, errors);
}


Re: My attempt to create a .bar file [Re: ] #435237
01/04/14 13:41
01/04/14 13:41
Joined: Jun 2013
Posts: 41
Ohio, USA
P
Pork Offline OP
Newbie
Pork  Offline OP
Newbie
P

Joined: Jun 2013
Posts: 41
Ohio, USA
WOW! Truly amazing!
My idea for the zorro requirement of reverse order was to sort the csv file in excel before running the script. Otherwise a bubble sort should work. tho not very effecient.
as far as the bar file goes I was just going to rename.
Thanks acidburn.
Adding a new instrument is explained on the data import page. There is also a slightly confusing sript to create the needed entries.
P

Re: My attempt to create a .bar file [Re: ] #435238
01/04/14 13:56
01/04/14 13:56

A
acidburn
Unregistered
acidburn
Unregistered
A



Done! The generated export.bar file now has bars in reverse order, as required by Zorro. Instead of slowly parsing csv backwards, or stuffing all bars in the memory to write them out in reverse at the very end, I decided to go with a temporary file. It should be the most efficient approach of the three (having in mind that Zorro is an ancient 32bit architecture). Will pay off later when I choke Zorro with gigabytes of tick data. grin

The script has not been tested, I leave that to you. If you find any bugs, do report!

Code:
#include <default.c>
#include <stdio.h>
#include <windows.h>

typedef struct SYSTEMTIME {
	WORD wYear;
	WORD wMonth;
	WORD wDayOfWeek;
	WORD wDay;
	WORD wHour;
	WORD wMinute;
	WORD wSecond;
	WORD wMilliseconds;
} SYSTEMTIME;

int _stdcall SystemTimeToVariantTime(SYSTEMTIME *lpSystemTime, DATE *pvtime);

DATE ConvertTime(int year, int month, int day, int hour, int minute, int second)
{
	SYSTEMTIME Time;
	DATE vTime;

	memset(&Time, 0, sizeof(SYSTEMTIME));
	Time.wYear = year;
	Time.wMonth = month;
	Time.wDay = day;
	Time.wHour = hour;
	Time.wMinute = minute;
	Time.wSecond = second;

	SystemTimeToVariantTime(&Time, &vTime);
	return vTime;
}

function main()
{
	FILE *in, *tmp, *out;

	if (!(in = fopen("EURUSD_M1_201312.csv", "r"))) {
		printf("\ncan't open input file");
		return;
	}

	if (!(tmp = tmpfile())) {
		printf("\ncan't open temporary file");
		return;
	}

	if (!(out = fopen("export.bar", "wb"))) {
		printf("\ncan't open output file");
		return;
	}

	char line[64];
	TICK tick;

	int lines = 0, errors = 0;
	while (fgets(line, 64, in)) {
		int year, month, day, hour, minute, second;
		if (sscanf(line, "%4d%2d%2d %2d%2d%2d;%f;%f;%f;%f;",
			&year, &month, &day, &hour, &minute, &second,
			&tick.fOpen, &tick.fHigh, &tick.fLow, &tick.fClose) == 10) {
			tick.time = ConvertTime(year, month, day, hour, minute, second);
			if (!fwrite(&tick, sizeof(TICK), 1, tmp)) {
				printf("\nwrite error");
				return;
			}
		} else {
			errors++;
		}
		lines++;
	}

	printf("\nStep 1/2: %d lines parsed (%d errors)", lines, errors);	

	int offset, bars = 0;
	for (offset = ftell(tmp) - sizeof(TICK); offset >= 0; offset -= sizeof(TICK)) {
		fseek(tmp, offset, SEEK_SET);
		if (fread(&tick, sizeof(TICK), 1, tmp)) {
			if (!fwrite(&tick, sizeof(TICK), 1, out)) {
				printf("\nwrite error");
				return;
			}
			bars++;
		}			
	}

	fclose(out);
	printf("\nStep 2/2: %d bars saved", bars);	
}


Page 1 of 3 1 2 3

Moderated by  Petra 

Gamestudio download | chip programmers | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1