Gamestudio Links
Zorro Links
Newest Posts
Data from CSV not parsed correctly
by dr_panther. 05/06/24 18:50
Help with plotting multiple ZigZag
by degenerate_762. 04/30/24 23:23
M1 Oversampling
by 11honza11. 04/30/24 08:16
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
4 registered members (dr_panther, AndrewAMD, Ayumi, TedMar), 1,033 guests, and 2 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 2 1 2
How to use the file_read/write with array #440555
04/28/14 00:48
04/28/14 00:48
Joined: Jul 2013
Posts: 522
D
dusktrader Offline OP
User
dusktrader  Offline OP
User
D

Joined: Jul 2013
Posts: 522
I'm trying to build a proof-of-concept code that writes an array to a file, then later read it back in. I can see that it writes something to the file, but I'm not sure if I have the length code correct. Also, trying to read it back in (and verify) does not work.

Any help on this would be appreciated. Here is a sample code:
Code:
function run()
{
	StartDate = 20140101;
	EndDate = 20140212;
	BarPeriod = 15;
	int mode = 0; //1=write; 0=read
	string path = "Strategy\\dt-file-series.ini";
	vars EquityCurve;
	
	if (mode) //write mode
	{
		EquityCurve = series(EquityLong+EquityShort);
		
		if (NumOpenTotal < 5)
		{
			if(random()>0) reverseLong(1);
			else reverseShort(1);
		}
	
		if(is(EXITRUN)) file_write(path, EquityCurve, LookBack*sizeof(var));
	}
	else //read mode
	{
		file_read(path, EquityCurve, 1000);
	}
	
	plot("EquityCurve",EquityCurve[0],1,BLUE);
}


THANKS

Last edited by dusktrader; 04/28/14 10:01.
Re: How to use the file_read/write with array [Re: dusktrader] #440556
04/28/14 02:39
04/28/14 02:39
Joined: Jun 2013
Posts: 1,609
D
DdlV Offline
Serious User
DdlV  Offline
Serious User
D

Joined: Jun 2013
Posts: 1,609
Hi dusktrader. I don't think the sizeof is giving you what you want. Since vars is the pointer to an array of var, sizeof will only return the length of the pointer. Instead, if each var is 4 bytes and the length of your series is defaulted to LookBack, shouldn't the length be 4*LookBack?

Re: How to use the file_read/write with array [Re: DdlV] #440561
04/28/14 10:00
04/28/14 10:00
Joined: Jul 2013
Posts: 522
D
dusktrader Offline OP
User
dusktrader  Offline OP
User
D

Joined: Jul 2013
Posts: 522
Thanks DdlV. I edited the script above now but I still get a crash trying to read it back in.

Re: How to use the file_read/write with array [Re: dusktrader] #440582
04/28/14 13:00
04/28/14 13:00
Joined: Jun 2013
Posts: 1,609
D
DdlV Offline
Serious User
DdlV  Offline
Serious User
D

Joined: Jun 2013
Posts: 1,609
Hi dusktrader. OK, I'll guess a little more! laugh vars EquityCurve just defines EquityCurve as a pointer. Before the read I think you need to create the array and assign EquityCurve to the start of the array, which is what series does. So, in the read else add EquityCurve=series(0) before the read.

The read is going to execute on every Bar, just reloading the array every time. Do you want it to be in INITRUN? Or actually EXITRUN for your testing / proof of concept...

In read mode, the plot is going to plot the 1st value in the array at every bar, which will just be a flat line? I think you may be better off using plotGraph in a loop to specify the Bar for each equity value...

Re: How to use the file_read/write with array [Re: DdlV] #440587
04/28/14 14:08
04/28/14 14:08
Joined: Jul 2013
Posts: 522
D
dusktrader Offline OP
User
dusktrader  Offline OP
User
D

Joined: Jul 2013
Posts: 522
Thanks DdlV,
I agree there should be an INITRUN in that logic, so I've added that.

I am still getting a crash when trying to read in the array (note: I'm assuming it was correctly written to the file).

I think the issue could still be the length, which I've arbitrarily set to 1000. I don't think Zorro would know the length when reading in, so I'm not sure how that would work.

plotGraph sounds neat and I might explore that (I haven't used it before) but first I need to figure out the reason for the crash.
Code:
function run()
{
	StartDate = 20140101;
	EndDate = 20140212;
	BarPeriod = 15;
	int mode = 0; //1=write; 0=read
	string path = "Strategy\\dt-file-series.ini";
	vars EquityCurve;
	
	if (mode) //write mode
	{
		EquityCurve = series(EquityLong+EquityShort);
		
		if (NumOpenTotal < 5)
		{
			if(random()>0) reverseLong(1);
			else reverseShort(1);
		}
	
		if(is(EXITRUN)) file_write(path, EquityCurve, LookBack*sizeof(var));
	}
	else if(is(INITRUN)) //read mode
	{
		EquityCurve=series(0);
		file_read(path, EquityCurve, 1000);
	}
	
	plot("EquityCurve",EquityCurve[0],1,BLUE);
}


Re: How to use the file_read/write with array [Re: dusktrader] #440589
04/28/14 14:24
04/28/14 14:24
Joined: Jul 2000
Posts: 27,986
Frankfurt
jcl Offline

Chief Engineer
jcl  Offline

Chief Engineer

Joined: Jul 2000
Posts: 27,986
Frankfurt
At a first glance, the '1000' will overwrite the series and can thus cause a crash - the default LookBack is 80, sizeof(var) is 8, so a series has 80*8 = 640 bytes.

Re: How to use the file_read/write with array [Re: jcl] #440590
04/28/14 14:30
04/28/14 14:30
Joined: Jun 2013
Posts: 1,609
D
DdlV Offline
Serious User
DdlV  Offline
Serious User
D

Joined: Jun 2013
Posts: 1,609
Hi dusktrader. Try this else section:
Code:
else { //read mode
		if(is(EXITRUN)) {
			EquityCurve=series(0);
			file_read(path, EquityCurve, 1000);
			int	i;
			for(i=0;i<80;i++) {
				plotGraph("EquityCurve",i,EquityCurve[i],NEW,BLUE);
			}
		}
	}


Re: How to use the file_read/write with array [Re: DdlV] #440592
04/28/14 14:36
04/28/14 14:36
Joined: Jun 2013
Posts: 1,609
D
DdlV Offline
Serious User
DdlV  Offline
Serious User
D

Joined: Jun 2013
Posts: 1,609
BTW - the crash was being caused by the "1" in the plot statement - change it to "0" and no crash. No idea why... What does "1" mean for the type parameter?

Re: How to use the file_read/write with array [Re: DdlV] #440601
04/28/14 15:16
04/28/14 15:16
Joined: Jul 2013
Posts: 522
D
dusktrader Offline OP
User
dusktrader  Offline OP
User
D

Joined: Jul 2013
Posts: 522
Hmm... ok, but that plotGraph code doesn't seem to produce the EquityCurve values. That looks more like the price() series. See screenshots... the EquityCurve should be a steady downward decline like the image labelled "write".

Also, I noticed that Zorro doesn't seem to care about the size parameter in the file_read() function. I can set it to 0 and it produces the same result. Not sure if that is an issue.

I normally use 0 or 1 in plot to signify the last/main window (0) or a new window (1).

Code:
function run()
{
	StartDate = 20140101;
	EndDate = 20140212;
	BarPeriod = 15;
	int mode = 0; //1=write; 0=read
	string path = "Strategy\\dt-file-series.ini";
	vars EquityCurve;
	
	if (mode) //write mode
	{
		EquityCurve = series(EquityLong+EquityShort);
		
		if (NumOpenTotal < 5)
		{
			if(random()>0) reverseLong(1);
			else reverseShort(1);
		}
	
		if(is(EXITRUN)) file_write(path, EquityCurve, LookBack*sizeof(var));
	}

	else if(is(INITRUN)) //read mode
	{
		EquityCurve=series(0);
		file_read(path, EquityCurve, 0);

		int i;
		for(i=0;i<80;i++)
		{
			plotGraph("EquityCurve",i,EquityCurve[i],NEW,BLUE);
		}
	}
	
	//plot("EquityCurve",EquityCurve[0],NEW,BLUE);
}


Attached Files equitycurve-write.pngequitycurve-read.png
Re: How to use the file_read/write with array [Re: dusktrader] #440607
04/28/14 15:58
04/28/14 15:58
Joined: Jun 2013
Posts: 1,609
D
DdlV Offline
Serious User
DdlV  Offline
Serious User
D

Joined: Jun 2013
Posts: 1,609
Change to EXITRUN. Doing it in INITRUN will try to plot before the chart is even started. (If it even works at all in INITRUN per jcl's comments in another thread...)

Page 1 of 2 1 2

Moderated by  Petra 

Powered by UBB.threads™ PHP Forum Software 7.7.1