3 registered members (Ayumi, Akow, AndrewAMD),
1,505
guests, and 9
spiders. |
Key:
Admin,
Global Mod,
Mod
|
|
|
Different result in main() and run()
#487350
03/22/23 02:05
03/22/23 02:05
|
Joined: Mar 2023
Posts: 11
DisplayName
OP
Newbie
|
OP
Newbie
Joined: Mar 2023
Posts: 11
|
Hi, can anyone explain why I get different output, if I call the function main() or run()?
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.
Last edited by DisplayName; 03/22/23 12:40.
|
|
|
Re: Different result in main() and run()
[Re: DisplayName]
#487351
03/22/23 14:06
03/22/23 14:06
|
Joined: Feb 2017
Posts: 1,731 Chicago
AndrewAMD
Serious User
|
Serious User
Joined: Feb 2017
Posts: 1,731
Chicago
|
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.htmSince 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.
|
|
|
Re: Different result in main() and run()
[Re: DisplayName]
#487356
03/22/23 18:09
03/22/23 18:09
|
Joined: Mar 2023
Posts: 11
DisplayName
OP
Newbie
|
OP
Newbie
Joined: Mar 2023
Posts: 11
|
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 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 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 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 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 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;
}
}
Last edited by DisplayName; 03/22/23 18:19.
|
|
|
Re: Different result in main() and run()
[Re: DisplayName]
#487357
03/22/23 19:26
03/22/23 19:26
|
Joined: Feb 2017
Posts: 1,731 Chicago
AndrewAMD
Serious User
|
Serious User
Joined: Feb 2017
Posts: 1,731
Chicago
|
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): 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!");
}
}
|
|
|
Re: Different result in main() and run()
[Re: AndrewAMD]
#487358
03/23/23 01:41
03/23/23 01:41
|
Joined: Mar 2023
Posts: 11
DisplayName
OP
Newbie
|
OP
Newbie
Joined: Mar 2023
Posts: 11
|
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?
Last edited by DisplayName; 03/23/23 01:44.
|
|
|
|