Gamestudio Links
Zorro Links
Newest Posts
Help with plotting multiple ZigZag
by degenerate_762. 04/30/24 23:23
M1 Oversampling
by 11honza11. 04/30/24 08:16
Trading Journey
by howardR. 04/28/24 09:55
Zorro Trader GPT
by TipmyPip. 04/27/24 13:50
Data from CSV not parsed correctly
by jcl. 04/26/24 11:18
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
3 registered members (AndrewAMD, dr_panther, Quad), 935 guests, and 3 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
Exact time of previous bar of higher timeframe #487576
06/08/23 15:12
06/08/23 15:12
Joined: Jan 2022
Posts: 58
N
NorbertSz Offline OP
Junior Member
NorbertSz  Offline OP
Junior Member
N

Joined: Jan 2022
Posts: 58
Dear all,

I have a simple problem:
I have a script with BarPeriod = 1, and I want to get the exact closetime of another timeframe's previous bar.
For example I want to get the time of the 5th bar of the TimeFrame 60 (in Unix timestamp format).

In the documentation of Time and Calendar Functions page:
Quote

The offset parameter is affected by TimeFrame, f.i. when TimeFrame is 6 and offset is 3, the time of 3*6 = 18 bars ago is returned.


So I have this script:
Code
	...
	BarPeriod = 1;

	int currentTimeStamp;

	TimeFrame = 1;
	currentTimeStamp = utm(wdate(0));
	printf("\n TIME1: %d", currentTimeStamp);

	TimeFrame = 60;
	currentTimeStamp = utm(wdate(0));
	printf("\n TIME2: %d", currentTimeStamp);

	printf("\n-----");
	...


And I have this result:

Code
...
 TIME1: 1505149200
 TIME2: 1505149200
-----
 TIME1: 1505149260
 TIME2: 1505149260
-----
 TIME1: 1505149320
 TIME2: 1505149320
-----


I expected that the two amounts will not be the same, the second one should change in every 60th run. So it seems the wdate is not affected by TimeFrame.

Of course I could do the math for the 5th bar of TimeFrame 60 like:
Code
utm(wdate(60*5 - minute(0)))

or something, but I guess the TimeFrame should do the trick.

Is it a bug, or am I missing something?
Is there a standard way to do that?

Thank you!

Last edited by NorbertSz; 06/08/23 15:14.
Re: Exact time of previous bar of higher timeframe [Re: NorbertSz] #487577
06/08/23 15:27
06/08/23 15:27
Joined: Jan 2022
Posts: 58
N
NorbertSz Offline OP
Junior Member
NorbertSz  Offline OP
Junior Member
N

Joined: Jan 2022
Posts: 58
Ouch, now I see the math; because I asked for the 0th bar with wdate:
Code
0*60 = 0

So if I ask wdate(0) it doesn't matter what is the current TimeFrame.

But if I do this...
Code
	int currentTimeStamp;

	TimeFrame = 1;
	currentTimeStamp = utm(wdate(1));
	printf("\n TIME1: %d", currentTimeStamp);

	TimeFrame = 60;
	currentTimeStamp = utm(wdate(1));
	printf("\n TIME2: %d", currentTimeStamp);

	printf("\n-----");


...I get the result...

Code
 TIME1: 1505147340
 TIME2: 1505143800
-----
 TIME1: 1505147400
 TIME2: 1505143860
-----
 TIME1: 1505147460
 TIME2: 1505143920
-----

...which is fine by definition.

However, I want a method where I can get the closetime of the Xth bar of any given TimeFrame (it could be even 0). So the above example the TIME2 should change in every 60th run (because BarPeriod = 1).

Re: Exact time of previous bar of higher timeframe [Re: NorbertSz] #487578
06/08/23 15:30
06/08/23 15:30
Joined: Feb 2017
Posts: 1,726
Chicago
AndrewAMD Online
Serious User
AndrewAMD  Online
Serious User

Joined: Feb 2017
Posts: 1,726
Chicago
Use a static variable and change it when frame() returns true.
https://zorro-project.com/manual/en/frame.htm

Re: Exact time of previous bar of higher timeframe [Re: AndrewAMD] #487582
06/09/23 12:48
06/09/23 12:48
Joined: Jan 2022
Posts: 58
N
NorbertSz Offline OP
Junior Member
NorbertSz  Offline OP
Junior Member
N

Joined: Jan 2022
Posts: 58
Do you mean something like this?
(I did not test the code below, it could have edge cases, it's just an outline for now.)

Code
int getTimestamp(int tf, int bar){

	//when I call this function I don't know which TimeFrame I will use right that moment
	//so store it to restore later
	int pre_tf = TimeFrame;       
	
	TimeFrame = tf;
	int result = 0;
	for(int i = (bar-1)*tf; i <= bar*tf && result == 0; i++){
		if (frame(i)){
			result = i;
		}
	}
	
	int resultTimestamp = utm(wdate(result));
	
	TimeFrame = pre_tf; //restore the original TimeFrame
	return resultTimestamp;
}

It could work, but there is another problem.
From the documentation:
Quote
When a value > 30000 is used for offset, it is assumed a day number in DATE format, and the corresponding year, month, or day is returned.

I am working with multiple timeframes and long lookback periods: I store the H4 bar prices, and it's size is more than 200.
So there could be a point where I want to check something like this: "which was the exact time of the 200th H4 bar"?

In that case my function call would look like this:
Code
getTimestamp(240, 200);

This case the variable inside my function called "result" will be between 47760 and 48000, therefore the wdate wil return a wrong date, because the input value > 30000.

So how should I make this int getTimestamp(int tf, int bar) function?

Re: Exact time of previous bar of higher timeframe [Re: NorbertSz] #487583
06/09/23 15:50
06/09/23 15:50
Joined: Feb 2017
Posts: 1,726
Chicago
AndrewAMD Online
Serious User
AndrewAMD  Online
Serious User

Joined: Feb 2017
Posts: 1,726
Chicago
Better idea: make a timeseries of timestamps:
Code
TimeFrame = 1;
vars TimeStamps1 = series(utm(wdate(0)));
TimeFrame = 4;
vars TimeStamps4 = series(utm(wdate(0)));

Re: Exact time of previous bar of higher timeframe [Re: AndrewAMD] #487589
06/12/23 07:28
06/12/23 07:28
Joined: Jan 2022
Posts: 58
N
NorbertSz Offline OP
Junior Member
NorbertSz  Offline OP
Junior Member
N

Joined: Jan 2022
Posts: 58
You're right; thank you!


Moderated by  Petra 

Powered by UBB.threads™ PHP Forum Software 7.7.1