iTime returns wrong time from iBarshift - page 2

 

Your welcome. It is always a pleasure to help those that help themselves and reply to posts.

 

After much messing around with the code, I am still experiencing issues with the times of the last yesterday high and low, which is odd as the start and end times for yesterday are correct (varified by coding arrows on start and end).


Here is the code as it stands at the moment:


datetime yesterdayopen=iTime(NULL,PERIOD_D1,1);

datetime yesterdayclose=iTime(NULL,PERIOD_D1,0);

int yesterdaystart=iBarShift(NULL,PERIOD_M1,yesterdayopen);

int yesterdayend=iBarShift(NULL,PERIOD_M1,yesterdayclose);

int yesterdayhighbar=iHighest(NULL,PERIOD_M1,MODE_HIGH,yesterdaystart,yesterdayend);

datetime yesterdayhightime=iTime(NULL,PERIOD_M1,yesterdayhighbar);

int yesterdaylowbar=iLowest(NULL,PERIOD_M1,MODE_LOW,yesterdaystart,yesterdayend);

datetime yesterdaylowtime=iTime(NULL,PERIOD_M1,yesterdaylowbar);

double yesterdayhighprice=iHigh(NULL,PERIOD_M1,yesterdayhighbar);

double yesterdaylowprice=iLow(NULL,PERIOD_M1,yesterdaylowbar);


ObjectSet ("MN2WKBottom",OBJPROP_PRICE2,lastweeklowprice);

ObjectSet ("MN2WKBottom",OBJPROP_TIME2,lastweeklowtime);}


I have attached a chart to explain what I mean visually. The yellow arrows represent the start and end times of yesterday. The red arrow represents the time of the highest point of yesterday. However, the blue arrow is supposed to represent the lowest point of yesterday, however as you can see, its painted on the week previous. The arrows were painted on the chart programmatically from the code.


Any ideas why this error is occuring?


 
Limstylz:

After much messing around with the code, I am still experiencing issues with the times of the last yesterday high and low, which is odd as the start and end times for yesterday are correct (varified by coding arrows on start and end).

[...]

datetime yesterdayopen=iTime(NULL,PERIOD_D1,1);

datetime yesterdayclose=iTime(NULL,PERIOD_D1,0);

[...]

Apologies for butting in late to a discussion which is now on its second page, particularly if I've completely misunderstood the issue, but isn't the problem that your value for yesterdayclose falls into today's bar? (i.e. yesterdayclose = todayopen).


I haven't actually tried this, but what happens if you do "datetime yesterdayclose=iTime(NULL,PERIOD_D1,0) - 1"? This should simply subtract 1 second from today's open, making the value of yesterdayclose something like 02/08/09 23:59:59 rather than 03/08/09 00:00:00. (Strictly speaking, you could potentially still run into problems if a day's bar doesn't start at 00:00:00. You might need to do something like rounding yesterdayopen down to the nearest day and then adding 3600 * 24 - 1 = 86399 seconds to it to get the day end.)


EDIT: code such as "int yesterdayhighbar=iHighest(NULL,PERIOD_M1,MODE_HIGH,yesterdaystart,yesterdayend);" also doesn't look quite right. According to the documentation, this is going to count yesterdaystart bars back from the bar identified by yesterdayend. If you're 60 minutes into the current day, then yesterdayend is 60 and yesterdaystart is something like 1440 + 60 = 1500. Therefore, you're going to be scanning 1500 bars back from the start of today, which takes you into the day before. You want to be specifying something like "yesterdayend - yesterdaystart" for the Count parameter to iHighest(), and this should always equal 1440 for a complete day's trading.


(But apologies if I'm barking up the wrong tree. Just back from vacation, and brain not working at full power.)

 
jjc:

Apologies for butting in late to a discussion which is now on its second page, particularly if I've completely misunderstood the issue, but isn't the problem that your value for yesterdayclose falls into today's bar? (i.e. yesterdayclose = todayopen).


I haven't actually tried this, but what happens if you do "datetime yesterdayclose=iTime(NULL,PERIOD_D1,0) - 1"? This should simply subtract 1 second from today's open, making the value of yesterdayclose something like 02/08/09 23:59:59 rather than 03/08/09 00:00:00. (Strictly speaking, you could potentially still run into problems if a day's bar doesn't start at 00:00:00. You might need to do something like rounding yesterdayopen down to the nearest day and then adding 3600 * 24 - 1 = 86399 seconds to it to get the day end.)


EDIT: code such as "int yesterdayhighbar=iHighest(NULL,PERIOD_M1,MODE_HIGH,yesterdaystart,yesterdayend);" also doesn't look quite right. According to the documentation, this is going to count yesterdaystart bars back from the bar identified by yesterdayend. If you're 60 minutes into the current day, then yesterdayend is 60 and yesterdaystart is something like 1440 + 60 = 1500. Therefore, you're going to be scanning 1500 bars back from the start of today, which takes you into the day before. You want to be specifying something like "yesterdayend - yesterdaystart" for the Count parameter to iHighest(), and this should always equal 1440 for a complete day's trading.


(But apologies if I'm barking up the wrong tree. Just back from vacation, and brain not working at full power.)

Hi jjc,


You bring up some very good points. I will try them now and come back to you in about an hour.

 

jjc,


Your suggestions have worked, so far so good. I will further test this code over the course of this week. Many thanks for your input. Obviously your vacation has paid dividends... brain working at full strength, unlike mine. Your solution is so obvious, I can't see how I missed it.


I have attached the indicator here for your trading consideration. I use it as a basic breakout indicator. Thanks again, everyone for your input. I have credited Ruptor and jjc in the code. Please let me know if there are any changes you would like to see.


I forgot to mention that you may need to download 1 minute history (incase your M1 chart does not go back as far as 2 months by default).

Files:
 
thaaaaaaaaaankss
 
Reason: