How to use the file_read/write with array

Posted By: dusktrader

How to use the file_read/write with array - 04/28/14 00:48

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
Posted By: DdlV

Re: How to use the file_read/write with array - 04/28/14 02:39

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?
Posted By: dusktrader

Re: How to use the file_read/write with array - 04/28/14 10:00

Thanks DdlV. I edited the script above now but I still get a crash trying to read it back in.
Posted By: DdlV

Re: How to use the file_read/write with array - 04/28/14 13:00

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...
Posted By: dusktrader

Re: How to use the file_read/write with array - 04/28/14 14:08

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

Posted By: jcl

Re: How to use the file_read/write with array - 04/28/14 14:24

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.
Posted By: DdlV

Re: How to use the file_read/write with array - 04/28/14 14:30

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

Posted By: DdlV

Re: How to use the file_read/write with array - 04/28/14 14:36

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?
Posted By: dusktrader

Re: How to use the file_read/write with array - 04/28/14 15:16

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 picture equitycurve-write.png
Attached picture equitycurve-read.png
Posted By: DdlV

Re: How to use the file_read/write with array - 04/28/14 15:58

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...)
Posted By: dusktrader

Re: How to use the file_read/write with array - 04/28/14 16:12

Ok, I see that it does produce "something" when I use EXITRUN. But it is not right -- the EquityCurve produced in the write mode should look relatively identical to the one in the read mode if correct.

I may try to simplify a little. Perhaps I'll try writing a simple array to a file first, and verify I can read that back intact.
Posted By: DdlV

Re: How to use the file_read/write with array - 04/28/14 16:38

Hi dusktrader. Not sure what you mean by "relatively identical". Since the series is only 80 bars long, all the file holds is the last 80 bars of the equity curve. Shading aside, those 80 bars seemed to match the end of the write equity curve...
© 2024 lite-C Forums