Different result in main() and run()

Posted By: DisplayName

Different result in main() and run() - 03/22/23 02:05

Hi,
can anyone explain why I get different output, if I call the function main() or run()?
Code
int count;
function main() //run()
{
	vars MySeriesArrayX = series(); 
	vars MySeriesArrayY = series(); 
	if (count!=1){
		int x;
		for(x=0; x<5; x++) {
			MySeriesArrayX[x] = 20220101+x;
			MySeriesArrayY[x] = x+0.1;
		}
		
		int date;
		for(x=0; x<5; x++){		
			date = MySeriesArrayX[x];
			printf("\n date: %d",date);
			printf("\n number: %f",MySeriesArrayY[x]);
			
		}
		count=1;
	}
}


If it is called main() the output is correct:

date: 20220101
number: 0.100000
date: 20220102
number: 1.100000
date: 20220103
number: 2.100000
date: 20220104
number: 3.100000
date: 20220105
number: 4.100000


If it is called run() date returns the int of number:

date: 0
number: 0.100000
date: 1
number: 1.100000
date: 2
number: 2.100000
date: 3
number: 3.100000
date: 4
number: 4.100000

I edited the code to the core problem.
If I comment out the count and the if-case, then it is wrong in the first 'run' of run() and after that it shows also correct results.

Thanks for any help.


Posted By: AndrewAMD

Re: Different result in main() and run() - 03/22/23 14:06

First of all, series() was designed to be used in run(), not main().

Second, during INITRUN, series() only returns a dummy buffer. So you're writing to the same buffer for both arrays.

Read the documentation for series():
https://zorro-project.com/manual/en/series.htm
Quote
Since the LookBack value is normally only known after the INITRUN, series are allocated in the FIRSTRUN. During the INITRUN they are set to a temporary pointer and filled with the initial value. This temporary content is overwritten by the series allocation in the FIRSTRUN. Series are only valid during the session and released after the EXITRUN.

An internal counter determines the pointer to be returned by a series() call. For keeping the counter in sync with the series calls, they are restricted to the run function or a function that is called from the run function, and must be always in the same order. Therefore series calls cannot be skipped by if or other conditions that change from bar to bar (see example below). If the content of a series shall depend on if conditions, set the [0] element dependent on if. These restrictions also apply to all functions that internally create series, such as some indicator or signal processing functions. Zorro detects wrong series calls and will display an error message in this case.

For accessing the same series from several functions, declare a global vars, and set it with a series call in the run function.
Posted By: DisplayName

Re: Different result in main() and run() - 03/22/23 15:26

Thanks for your reply!
I have already read the manual about series and also tried it with global variables.

In this case I always get Error 111: Crash in run: run()

Mybe I did not understand what exactly is meant with 'set it with a series call' and what the correct syntax should look like.
Can you show me an example?
Posted By: AndrewAMD

Re: Different result in main() and run() - 03/22/23 17:01

Show me the code with the error.
Posted By: DisplayName

Re: Different result in main() and run() - 03/22/23 17:18

Belive me I work since 4 nights on this little piece of code.
I can show you tons of not working examples. Do you want to see them all? I tried nearly everything.
Now I don't know what else I can do. So I'm looking for some help.
Is it a tremendously huge problem to say look this 3 lines of code are an example for a correct series call?
I would then adapt my code and post it, if it's not working.
Posted By: AndrewAMD

Re: Different result in main() and run() - 03/22/23 17:28

Post the code that confuses you the most, and I will comment.
Posted By: DisplayName

Re: Different result in main() and run() - 03/22/23 18:09

Oh, it seems to be a tremendously huge problem.
But no problem, for me all of them are looking good. Have a look on it.
Please tell me in detail for each example what 's the problem. And if you like you can also give marks.
Just tell me if you want to see more examples.

Eg1
Code
int count;
vars MyGlobalArray[1]; 

function load()
{	
	MyGlobalArray[0] = series();
	MyGlobalArray[1] = series();
	int x;	
	for(x=0; x<5; x++) {
		(MyGlobalArray[0])[x] = 20220101+x;
		(MyGlobalArray[1])[x] = x+0.1;
	}
}

function run()
{
	vars MySeriesArray[1];
	MySeriesArray[0] = series(); 	
	MySeriesArray[1] = series(); 
	if (count!=1){
		load();
		int x;
		int date;
		for(x=0; x<5; x++) {
			(MySeriesArray[0])[x] = (MyGlobalArray[0])[x];
			(MySeriesArray[1])[x] = (MyGlobalArray[1])[x];
		}

		for(x=0; x<5; x++){		
			date = (MySeriesArray[0])[x];
			printf("\n date: %d",date);
			printf("\n number: %.4f",(MySeriesArray[1])[x]);
			
		}
		count=1;
	}
}


Eg2
Code
int count;
vars MyGlobalArray[1]; 

function load()
{	
	MyGlobalArray[0] = series();
	MyGlobalArray[1] = series();
	int x;	
	for(x=0; x<5; x++) {
		(MyGlobalArray[0])[x] = 20220101+x;
		(MyGlobalArray[1])[x] = x+0.1;
	}
}

function run()
{
	vars MySeriesArray[1]; 
	if (count!=1){
		load();
		int x;
		int date;
		for(x=0; x<5; x++) {
			(MySeriesArray[0])[x] = (MyGlobalArray[0])[x];
			(MySeriesArray[1])[x] = (MyGlobalArray[1])[x];
		}

		for(x=0; x<5; x++){		
			date = (MySeriesArray[0])[x];
			printf("\n date: %d",date);
			printf("\n number: %.4f",(MySeriesArray[1])[x]);
			
		}
		count=1;
	}
}


Eg3
Code
int count;
vars MyGlobalArray[1]; 

function load()
{	
	MyGlobalArray[0] = series();
	MyGlobalArray[1] = series();
	int x;	
	for(x=0; x<5; x++) {
		(MyGlobalArray[0])[x] = 20220101+x;
		(MyGlobalArray[1])[x] = x+0.1;
	}
}

function run()
{
	vars MySeriesArray[1]; 
	vars MySeriesArray[0] = series();
	vars MySeriesArray[1] = series();
	if (count!=1){
		load();
		int x;
		int date;
		
		MySeriesArray[0] = (MyGlobalArray[0];
		MySeriesArray[1] = (MyGlobalArray[1];

		for(x=0; x<5; x++){		
			date = (MySeriesArray[0])[x];
			printf("\n date: %d",date);
			printf("\n number: %.4f",(MySeriesArray[1])[x]);
			
		}
		count=1;
	}
}


Eg4
Code
int count;
vars MyGlobalArray[1]; 

function load()
{	
	MyGlobalArray[0] = series();
	MyGlobalArray[1] = series();
	int x;	
	for(x=0; x<5; x++) {
		(MyGlobalArray[0])[x] = 20220101+x;
		(MyGlobalArray[1])[x] = x+0.1;
	}
}

function run()
{
	vars MySeriesArray[1]; 
	if (count!=1){
		load();
		int x;
		int date;
		
		MySeriesArray[0] = (MyGlobalArray[0];
		MySeriesArray[1] = (MyGlobalArray[1];

		for(x=0; x<5; x++){		
			date = (MySeriesArray[0])[x];
			printf("\n date: %d",date);
			printf("\n number: %.4f",(MySeriesArray[1])[x]);
			
		}
		count=1;
	}
}


Eg5
Code
int count;
vars MySeriesArray[1]; 

vars load()
{	
	
	MySeriesArray[0] = series();
	MySeriesArray[1] = series();
	int x;	
	for(x=0; x<5; x++) {
			(MySeriesArray[0])[x] = 20220101+x;
		}
	for(x=0; x<5; x++) {
			(MySeriesArray[1])[x] = x+0.1;
		}
	return MySeriesArray; 	
}

function run() 
{
	vars MySeriesArrayX[0] = series(); 
	vars MySeriesArrayX[1] = series(); 
	if (count!=1){
		MySeriesArrayX = load();
		int x;	
		int date;
		for(x=0; x<5; x++){		
			date = (MySeriesArrayX[0])[x];
			printf("\n date: %d",date);
			printf("\n number: %f",(MySeriesArrayX[1])[x]);
			
		}
		count=1;
	}
}
Posted By: AndrewAMD

Re: Different result in main() and run() - 03/22/23 19:26

OK, there are so many problems here.

1) vars is a pointer to a buffer.
2) series() returns a pointer to an array created by Zorro.
3) vars MyGlobalArray[1]; creates an array of one pointer (which defeats the purpose of it being an array).
4) the manual was hinting at using an array of var (that is, doubles), not vars (pointers to doubles). And once you do, you have absolutely no reason to call series because you supplied an array.

OK, now try this. (Not tested, I don't have time):
Code
function run(){
	if(!Test){return quit("!Click [Test] to begin");}
	assetList("AssetsFix");
	asset("EUR/USD");
	vars MySeriesArrayX = series(0,-5); 
	vars MySeriesArrayY = series(0,-5); 
	if (!Init){
		int x;
		for(x=0; x<5; x++) {
			MySeriesArrayX[x] = 20220101+x;
			MySeriesArrayY[x] = x+0.1;
		}
		
		int date;
		for(x=0; x<5; x++){		
			date = MySeriesArrayX[x];
			printf("\n date: %d",date);
			printf("\n number: %f",MySeriesArrayY[x]);
			
		}
		return quit("!Done!");
	}
}
Posted By: DisplayName

Re: Different result in main() and run() - 03/23/23 01:41

Thank you and if one wants to keep it simple, the short answer was: use if(!Int) instead of if(count!=1).

I nearly do not dare to ask but the reason why I do this is because I want to compare the time stamp of the time series with my created dates.

So, how can I access my array during each run?
Posted By: AndrewAMD

Re: Different result in main() and run() - 03/23/23 02:02

remove the line:
return quit("!Done!");

And it will print it for every line except for during INITRUN. You skip INITRUN because series() returns a dummy buffer.

You dereference item x from MySeriesArrayX when you call MySeriesArrayX[x], as shown in the code.
Posted By: DisplayName

Re: Different result in main() and run() - 03/23/23 02:51

In the end I want to have a call function which loads the external data and this should called only once and not in every run. That's why the (count!=1) part.
Here's my simple plan:
1) load the external data
2) Store it in an array
3) run the time series and compare the dates
4) build an indicator for the whole time period
5) plot it

So, ist there a way to get no.3 done?

Can anyone explain, why I am losing one array entry per run?

Code
int count;
int cnt;
vars MySeriesArrayX; 
vars MySeriesArrayY;
function run()
{
	MySeriesArrayX=series(); 
	MySeriesArrayY=series(); 
	int date;
	int x;
	if (cnt<7){
		if (count!=1){
			if (!Init){
				
				for(x=0; x<5; x++) {
					MySeriesArrayX[x] = 20220101+x;
					MySeriesArrayY[x] = x+0.1;
				}	
				count=1;
			}
		}
		if(count==1){
			int i;
			for(i=0; i<5; i++){				
				date = MySeriesArrayX[i];
				printf("\n date: %d",date);
				printf("\n number: %f",MySeriesArrayY[i]);
			}
		}
		cnt++;
	}
}

Posted By: AndrewAMD

Re: Different result in main() and run() - 03/23/23 12:48

What kind
Originally Posted by DisplayName
In the end I want to have a call function which loads the external data and this should called only once and not in every run.

What exactly is this data? Is it price data? Is it just an indicator? Is it saved to disk? Is it a timeseries? Do you simply need the latest value in the timeseries relative to the timestamp of the current bar?

The answers to these questions will affect my recommendation. There's a 90% chance your approach is sub-optimal in the first place.
Posted By: DisplayName

Re: Different result in main() and run() - 03/23/23 13:32

I collect montly data from the web.
This already works. I have the date and the published data. I don't want to make a new web request with every run.
For example for 2022 I have an array with 12 * date information and an array with 12 * published data.

Then I have the daily stock time series.

My indicator should start with 0 and when the daily time stamp equals the montly time stamp the indicator gets the value of the published data from this day until the next dates match.
Posted By: AndrewAMD

Re: Different result in main() and run() - 03/23/23 15:01

The Zorro way:

1) In a separate script, download and convert the historical data to a dataset, and save it to t1 or t6 file. This goes in your History folder.
2) Then make an asset list with the stock data you downloaded and the asset you plan on trading.
3) In your trading script: load your asset list and call the stock asset to get its data. Then when you're ready to trade, change to the trading asset.
Posted By: DisplayName

Re: Different result in main() and run() - 03/23/23 15:37

Ok, thank you this is worth a try. But, sorry I don't get it.

For example if I want to trade daily SPY and download montly unemployed data.
Then I store this data in a mud.t6 file.

My asset list contains "SPY" and "MUD"??

Asset("SPY");

I do not understand how to call the 'MUD'-Asset or the mud.t6 file.
Posted By: DisplayName

Re: Different result in main() and run() - 03/24/23 16:07

Now I am at this specific point.

First try, I wrote an extra mud.t6 file which I wish to import additionally with the asset file.
Is this possible?

Second try, I created an new asset with my data in the 'Vol'-column.
Is this what you meant?
Unfortunately for this zorro s is required.

So is there a way to import additional data or only as Val or Vol with zorro s subscription?
Posted By: Petra

Re: Different result in main() and run() - 03/24/23 17:19

Store the data in a dataset. Its file name is not relevant. You need no Zorro S for handling data.

For unemployment data, I think you need a dataset with only 2 fields, one for the date and one for the data.

https://zorro-project.com/manual/en/data.htm
Posted By: DisplayName

Re: Different result in main() and run() - 03/24/23 20:16

This was exactly the part of the manual I used for downloading, but for some reason I missed dataSave and dataLoad, these are the commands I needed.
I think it's going to work now.

1000 times thank you AndrewAMD and Petra.
© 2024 lite-C Forums