Gamestudio Links
Zorro Links
Newest Posts
Trading Journey
by howardR. 04/28/24 09:55
basik85278
by basik85278. 04/28/24 08:56
Zorro Trader GPT
by TipmyPip. 04/27/24 13:50
Help with plotting multiple ZigZag
by M_D. 04/26/24 20:03
Data from CSV not parsed correctly
by jcl. 04/26/24 11:18
M1 Oversampling
by jcl. 04/26/24 11:12
Why Zorro supports up to 72 cores?
by jcl. 04/26/24 11:09
Eigenwerbung
by jcl. 04/26/24 11:08
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
2 registered members (SBGuy, Quad), 768 guests, and 5 spiders.
Key: Admin, Global Mod, Mod
Newest Members
wandaluciaia, Mega_Rod, EternallyCurious, howardR, 11honza11
19049 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Page 1 of 2 1 2
Strange behavior assigning dayHigh / dayLow #462145
09/11/16 21:28
09/11/16 21:28
Joined: Aug 2016
Posts: 61
D
dr_panther Offline OP
Junior Member
dr_panther  Offline OP
Junior Member
D

Joined: Aug 2016
Posts: 61
Post Edit:
The title should be: Strange behavior assigning dayHigh / dayLow


I try to get the daily high/low for a defined open range of the New York session for a stock. When assigning the dayLow(ET,0) to a static variable I get other values then assigning is to a local variable in the same code block. What is the reason for that?


Code:
if( !is_after_or and ltod(ET,0) > EndMarket ) {
	   DH = dayHigh(ET,0); // <----
	   DL = dayLow(ET,0);	
	   
 	  double dh_1 = dayHigh(ET,0);  // <----
	 
 	  is_new_day = 0;
  	  is_after_or = true;
  	  
  	  string msg_text = strf( "\n*** Debug INSIDE market open range: Bar Date %s  \nDH: %f,dh_1 %f, DL: %f, dayLow(ET,0) %f, ,StartMarket %d, EndMarket %d   ", 
  	  datetime(), DH,dh_1 ,DL, dayLow(ET,0), StartMarket, EndMarket );

	  if( debug ) print(TO_LOG,msg_text);
 
  	 
	}



writes into the log: please see that DH and dh_1 are different.
DH is a static variable.

*** Debug INSIDE market open range: Bar Date 21.01.15 15:35:00
DH: 110.050003,dh_1 110.139999, DL: 108.269997, dayLow(ET,0) 108.269997, ,StartMarket 830, EndMarket 1030

When I move the DH variable assignment below the DL, the values for DH and dh_1 are correct but the value for the daily low get incorrect.

Code:
// DH = dayHigh(ET,0); commented
DL = dayLow(ET,0);	
DH = dayHigh(ET,0); // moved here from above




returns: here the daily highs (DH, dh_1) are correct but the
daily lows (DH and dayLow(ET,0) ) are incorrect.
*** Debug INSIDE market open range: Bar Date 21.01.15 15:35:00
DH: 110.139999,dh_1 110.139999, DL: 109.900002, dayLow(ET,0) 108.269997, ,StartMarket 830, EndMarket 1030

So literally by just moving the position of the line of code in the same block changes the value, how is this possible?


Full working script:
Code:
#include <profile.c>
 
int debug  = 1;
 
function run()
{
	 
	set(LOGFILE);
	
	BarPeriod =5;
	LookBack  = 60;//40*24+1;
	UnstablePeriod=10;
	Hedge = 0;
	//Stop = 15 * PIP;
	//Trail = 10 * PIP;
	Spread=0.0;
	Lots=100;
	
	StartDate = 20150101;
	EndDate   = 20150202;
	///////////////////////////////////////////////////////////////////////////////////////////////////////
	int marketstart 	= 930;
	int marketend=1600; 
	
	vars Price = series(priceClose()); 	
	
	StartMarket = 830; // open_range_start
	EndMarket = 1030;    // end	
	
 
	static int cur_day = 0 ,is_new_day = 1;
 	static double DH , DL ,atr_avg; 
	static bool is_after_or;
  
   
 	bool is_week_day=ldow(ET,0) <= FRIDAY;
 	vars crosses = series(0);
 	
 	
 /// EVERY RUN
 //	asset("APPL");
 
 	TimeFrame=24;
	atr_avg = (0.05 *((ATR(10) + ATR(30))/2));
	TimeFrame=1;
 
 	
 	/////////////////// / N E W - D A Y //////////////////////////////////////////////////////
 
	if ( is_week_day && day(0) !=  cur_day){ // new day
 
		cur_day = day(0);
		is_new_day = 1;
		is_after_or = false;
	} 

////////////////////// Market Open range/////////////////////////////////////////////////////////////////////////////////
 
	if( !is_after_or and ltod(ET,0) > EndMarket ) {
	DL = dayLow(ET,0);	
	DL = dayLow(ET,0);
	   DH = dayHigh(ET,0);
 	   
	   
 
 	   
	  double dh_1 = dayHigh(ET,0);
	    
	 
 	  is_new_day = 0;
  	  is_after_or = true;
  	  
  	  string msg_text = strf( "\n*** Debug INSIDE market open range: Bar Date %s  \nDH: %f,dh_1 %f, DL: %f, dayLow(ET,0) %f, ,StartMarket %d, EndMarket %d   ", 
  	  datetime(), DH,dh_1 ,DL, dayLow(ET,0), StartMarket, EndMarket );

	  if( debug ) print(TO_LOG,msg_text);
 
  	 
	}
}


Last edited by dr_panther; 09/12/16 20:03.
Re: Strange behavior with static variables [Re: dr_panther] #462152
09/12/16 08:07
09/12/16 08:07
Joined: Jul 2000
Posts: 27,986
Frankfurt
jcl Offline

Chief Engineer
jcl  Offline

Chief Engineer

Joined: Jul 2000
Posts: 27,986
Frankfurt
I see no difference between static and local variables in your code, or in the log. But your code produces peeking errors and I suspect that your day function will not run at ET 10:30, but at GMT midnight. For detecting a new day in New York, use the ldow() function, not day(). day() is GMT time.

Re: Strange behavior with static variables [Re: jcl] #462153
09/12/16 09:35
09/12/16 09:35
Joined: Aug 2016
Posts: 61
D
dr_panther Offline OP
Junior Member
dr_panther  Offline OP
Junior Member
D

Joined: Aug 2016
Posts: 61
I changed it to ldow(), but get the same values:

static variable : DH: 110.050003,
local variable : dh_1 110.139999,

and the strange thing is, when I move the assignment of DH (daily high) below another static variable the DH value is correct but the value of DL (daily low) will be wrong.
like that:
// DH = dayHigh(ET,0); commented
DL = dayLow(ET,0);
DH = dayHigh(ET,0); // moved here from above

Another thing I figured out is, when I
set TimeFrame= frameSync(24); // instead of TimeFrame= 24;
the values are correct except at 23:00 UTC for my script.

And if I remove the setting of TimeFrame, the values are correct. Please can you help me to understand, which concept I don't understand.

Re: Strange behavior with static variables [Re: dr_panther] #462154
09/12/16 09:52
09/12/16 09:52
Joined: Jul 2000
Posts: 27,986
Frankfurt
jcl Offline

Chief Engineer
jcl  Offline

Chief Engineer

Joined: Jul 2000
Posts: 27,986
Frankfurt
When you changed it to ldow(), you should get different values since they are then sampled over a completely different period.

If static variables would not work in C, someone would probably have noticed this meanwhile. Your problem, as far as I understood it, was wrong return values of dayLow/dayHigh calls. But I could not reproduce that either. I could not even start your script due to the error messages. So obviously something is missing in either your version or mine. But it started ok here after fixing the ldow() issue, and all variables looked fine to me. Let's synchronize our tests:

- use your posted full script with this modification:

if ( is_week_day && ldow(ET,0) != cur_day){ // new day
cur_day = ldow(ET,0);

- use the latest Zorro version 1.50, that's the version I tested it with

- use EUR/USD for the asset.

Then run a test again and check all times and variables. When you still see something suspicious, please upload the complete log here and I'll look into it again.

You can also check the source code of dayHigh() and dayLow() in the indicators.c script. They are simple functions, at least I do not see a code part that would depend on the order of calls. Which does not mean that there is none.

Re: Strange behavior with static variables [Re: jcl] #462161
09/12/16 12:29
09/12/16 12:29
Joined: Aug 2016
Posts: 61
D
dr_panther Offline OP
Junior Member
dr_panther  Offline OP
Junior Member
D

Joined: Aug 2016
Posts: 61
I still get the errors, with EURUSD, Zorro Version 1.5.

Please find the scripts and logs attached here:

High level overview:

When using this code, Daily High Values are different, the Daily Low values are correct.

Code:
DH = dayHigh(ET,0); 
DL = dayLow(ET,0);




When swapping the assignments, Daily High Values are correct, the Daily Low values are wrong.

Code:
DL = dayLow(ET,0);
DH = dayHigh(ET,0);




Thank you for the help.

Scripts and Logs

Re: Strange behavior with static variables [Re: dr_panther] #462165
09/12/16 15:50
09/12/16 15:50
Joined: Aug 2016
Posts: 61
D
dr_panther Offline OP
Junior Member
dr_panther  Offline OP
Junior Member
D

Joined: Aug 2016
Posts: 61
jcl, thanks for your input.
I analyzed the error further and found, that the dayHigh(int zone,int day)
is the problem, in particular the call of:
int morning = timeOffset(zone,day,StartMarket/100,StartMarket%100);

The first time the dayHigh function is called, the timeOffset is wrong, after that it calculated it correct.
Here is the script to demonstrate that:

the result of the script below looks like that (please watch for the morning value).

before DH = mydayHigh(ET,0)evening 0, morning 1, StarMarket 830,timeOffset(zone,day,StartMarket/100,StartMarket mod 100) 1, zone: -5, vHH 1.178200, day 0
before dh_1 = mydayHigh(ET,0)evening 0, morning 24, StarMarket 830,timeOffset(zone,day,StartMarket/100,StartMarket mod 100) 24, zone: -5, vHH 1.179920, day 0
before 2nd time: DH = mydayHigh(ET,0)evening 0, morning 24, StarMarket 830,timeOffset(zone,day,StartMarket/100,StartMarket mod 100) 24, zone: -5, vHH 1.179920, day 0

Code:
#include <profile.c>
int g_count;
int skipWeekend(int day) 
{
	int d = dow(0)-day;
	while(d < 0) d += 7;
	if(d == SATURDAY) day += 1;
	if(d == SUNDAY) day += 2;
	return day;
} 
 
var mydayHigh(int zone,int day)
{
 
	if(is(INITRUN)) return 0;
	day = skipWeekend(day);
 
	int evening = timeOffset(zone,day,EndMarket/100,EndMarket%100);
	int morning = timeOffset(zone,day,StartMarket/100,StartMarket%100);
print(TO_LOG, "evening %d, morning %d, StarMarket %d,timeOffset(zone,day,StartMarket/100,StartMarket mod 100)  %d,    zone: %d", evening, morning, StartMarket,timeOffset(zone,day,StartMarket/100,StartMarket%100), zone);
	if(evening >= morning) { 
print(TO_LOG,", evening >= morning %d",evening >= morning);
	return priceHigh(evening);}
	var vHH = 0;
	for(g_count = evening; g_count <= morning; g_count++)
		vHH = max(vHH,priceHigh(g_count));
print(TO_LOG, ", vHH %f, day %d", vHH, day);
	return vHH;
}

var mydayLow(int zone,int day)
{
	if(is(INITRUN)) return 0;
	day = skipWeekend(day);
	int evening = timeOffset(zone,day,EndMarket/100,EndMarket%100);
	int morning = timeOffset(zone,day,StartMarket/100,StartMarket%100);
	if(evening >= morning) return priceLow(evening);
	var vLL = 999999;
	for(g_count = evening; g_count <= morning; g_count++)
		vLL = min(vLL,priceLow(g_count));
	return vLL;
}
 
int debug  = 1;
 double DH , DL, atr_avg;
function run()
{
	 
	set(LOGFILE);
	
	BarPeriod =5;
	LookBack  = 100;//40*24+1;
	UnstablePeriod=10;
	Hedge = 0;
	//Stop = 15 * PIP;
	//Trail = 10 * PIP;
	Spread=0.0;
	Lots=100;
	
	StartDate = 20150101;
	EndDate   = 20150202;
	///////////////////////////////////////////////////////////////////////////////////////////////////////
	int marketstart 	= 930;
	int marketend=1600; 
	
	vars Price = series(priceClose()); 	
	
	StartMarket = 830; // open_range_start
	EndMarket = 1030;    // end	
	
 
	static int cur_day = 0 ,is_new_day = 1;
 	 
	static bool is_after_or;
  
   
 	bool is_week_day=ldow(ET,0) <= FRIDAY;
 	vars crosses = series(0);
 	
 	
 /// EVERY RUN
 //	asset("APPL");
 
 	TimeFrame=24;
	atr_avg = ATR(1);//(0.05 *((ATR(1) + ATR(2))/2));
	TimeFrame=1;
 
 	
 	/////////////////// / N E W - D A Y //////////////////////////////////////////////////////
 
	if ( is_week_day && ldow(ET,0) != cur_day){ // new day
	cur_day = ldow(ET,0);
		is_new_day = 1;
		is_after_or = false;
	} 

////////////////////// Market Open range/////////////////////////////////////////////////////////////////////////////////
 
	if( !is_after_or and ltod(ET,0) > EndMarket and Bar > LookBack ) {
		
		
		
		print (TO_LOG, "\nbefore DH = mydayHigh(ET,0)");
		
		DH = mydayHigh(ET,0); 
		DL = mydayLow(ET,0);
		print (TO_LOG, "\nbefore dh_1 = mydayHigh(ET,0)");
	    double dh_1 = mydayHigh(ET,0);
	
	    
	    print (TO_LOG, "\nbefore 2nd time: DH = mydayHigh(ET,0)");
		
		DH = mydayHigh(ET,0); 
		
 	  is_new_day = 0;
  	  is_after_or = true;
  	  
  	  string msg_text = strf( "\n*** Debug INSIDE market open range: Bar Date %s  \nDH: %f,dh_1 %f, DL: %f, dayLow(ET,0) %f, ,StartMarket %d, EndMarket %d   ", 
  	  datetime(), DH,dh_1 ,DL, dayLow(ET,0), StartMarket, EndMarket );

	  if( debug ) print(TO_LOG,msg_text);
 
  	 
	}
}


Re: Strange behavior with static variables [Re: dr_panther] #462168
09/12/16 17:35
09/12/16 17:35
Joined: Jul 2000
Posts: 27,986
Frankfurt
jcl Offline

Chief Engineer
jcl  Offline

Chief Engineer

Joined: Jul 2000
Posts: 27,986
Frankfurt
Yes, I can confirm the problem. It did not happen with the full script that you posted, but it happened with the script from the zip, and is indeed caused by timeOffset. Since I can not see the problem in the source code, I've forwarded this to the developers for checking. Thank you for finding this bug!

Update: The bug was supposedly fixed in version 1.50.4. Please check if there's still a problem with this version. This bug was also responsible for the wrong day numbers of ldow() with time zones.

Re: Strange behavior with static variables [Re: jcl] #469167
11/06/17 21:38
11/06/17 21:38
Joined: Oct 2017
Posts: 9
D
debs Offline
Newbie
debs  Offline
Newbie
D

Joined: Oct 2017
Posts: 9
Hi jcl, I am facing also strange behaviour with dayHigh/DayLow.
In manual there parameter definition :
Quote:
day Working day offset, i.e. 0 = today (see remarks), 1 = yesterday, 2 = day before yesterday, and so on. Weekends are skipped, f.i. if today is Monday, 1 refers to last Friday.


I am trying use this functionality with simple script with yesterday High/Low H1close. But unsuccessfully - script takes Sunday value and skip Friday ones frown
How to solve this issue? What I am missing? I tried set weekend start, but without success...


Trying on AUD/USD pair:
Code:
#include <profile.c>
#define H24 (1440/BarPeriod)
#define H4 (240/BarPeriod)
#define H1 (60/BarPeriod)
function run()
{
   BarPeriod = 60;	
	LookBack = 200;	
	StartDate = 20170620;
	EndDate = 20170706; 	
	//StartWeek=10000;
	//EndWeek=52350;
	static bool LongOp,LongSignal,ShortOp,ShortSignal;
   static int counter;	
	if(is(INITRUN))
	 { 
	 ShortOp=false;
	 LongOp=false;
	 ShortSignal=false;
	 LongSignal=false;
	 counter=0;
	 }

	vars Price=series(price());
	vars D1Close = series(priceClose());
	var Lowx,Highx;
	Lowx=dayLow(UTC,1);
	Highx=dayHigh(UTC,1);

   print(TO_LOG,"D1High= %f ,D1Low= %f",Highx,Lowx);
  
	if(crossOver(Price,Highx) and LongOp==false)
	{LongOp=true;ShortOp=false;counter=0;  plot("Dotted",1.01*price(),DOT,GREEN);}
	
	if(crossUnder(Price,Lowx) and ShortOp==false)
	{ShortOp=true;LongOp=false;counter=0;  plot("shorted",1.01*price(),DOT,RED);}
	
	plot("D1High",Highx,MAIN,BLUE);
	plot("D1Low",Lowx,MAIN,RED);
	set(PLOTNOW+TESTNOW+LOGFILE);
  }



Thanks

Last edited by debs; 11/08/17 14:26.
Re: Strange behavior with static variables [Re: debs] #469170
11/07/17 09:11
11/07/17 09:11
Joined: Jul 2000
Posts: 27,986
Frankfurt
jcl Offline

Chief Engineer
jcl  Offline

Chief Engineer

Joined: Jul 2000
Posts: 27,986
Frankfurt
The outcommented line in your acript, StartWeek=10000, should exclude Sunday. Otherwise I suppose that Sunday, not Friday, is the day with offset 1, since the trading week begins on Sunday 22:00 UTC.

Do you get Friday when you include that line?

Re: Strange behavior with static variables [Re: jcl] #469197
11/08/17 06:12
11/08/17 06:12
Joined: Oct 2017
Posts: 9
D
debs Offline
Newbie
debs  Offline
Newbie
D

Joined: Oct 2017
Posts: 9
When I include that line, zorro returns price chart with sunday excluded, but dayLow/High is still calculated from sunday.. smirk What to do?

Page 1 of 2 1 2

Moderated by  Petra 

Powered by UBB.threads™ PHP Forum Software 7.7.1