• Home
  • Forums
  • Trades
  • News
  • Calendar
  • Market
  • Brokers
  • Login
  • Join
  • User/Email: Password:
  • 4:02am
Menu
  • Forums
  • Trades
  • News
  • Calendar
  • Market
  • Brokers
  • Login
  • Join
  • 4:02am
Sister Sites
  • Metals Mine
  • Energy EXCH
  • Crypto Craft

Options

Bookmark Thread

First Page First Unread Last Page Last Post

Print Thread

Similar Threads

Simple Help with iBarShift Please 4 replies

How do I iBarShift in reverse? 8 replies

iBarShift 3 replies

What does this do? iBarShift 2 replies

Bug w/ iBarShift function? 0 replies

  • Platform Tech
  • /
  • Reply to Thread
  • Subscribe
  • 3
Attachments: Need Help: iBarShift: Smaller TF Shift from Bigger TF
Exit Attachments

Need Help: iBarShift: Smaller TF Shift from Bigger TF

  • Post #1
  • Quote
  • First Post: Edited at 10:07pm Jun 20, 2022 9:39pm | Edited at 10:07pm
  •  chimoong
  • | Joined May 2016 | Status: Member | 204 Posts
Hi coders, kindly advise and help.

Inserted Code
   int tTF=5;
 
   int b0M1Sf=iBarShift(Symbol(),1,iTime(Symbol(),tTF,0),true);
   int b1M1Sf=iBarShift(Symbol(),1,iTime(Symbol(),tTF,1),true);
   int b2M1Sf=iBarShift(Symbol(),1,iTime(Symbol(),tTF,2),true);
 
   {Alert(Symbol()+" M"+IntegerToString(tTF)+" Bar0/Bar1/Bar2 : "+IntegerToString(b0M1Sf)+" / "+IntegerToString(b1M1Sf)+" / "+IntegerToString(b2M1Sf))}

I would like to get the shift of smaller TF starting from bigger TF, but the output of different pairs are different, could you please advise :-

1) What is the solution to get the correct output results ?

2) Is this the bug of mq4 / MT4 and can never be resolved ?

Thanks
Attached Image (click to enlarge)
Click to Enlarge

Name: iBarShift.png
Size: 59 KB
  • Post #2
  • Quote
  • Jun 21, 2022 3:33pm Jun 21, 2022 3:33pm
  •  Oliver.Twist
  • | Joined Nov 2021 | Status: Member | 43 Posts
When you access another instrument via code (i.e., not the chart on which the indi is attached to), or even if it's the same instrument but a different timeframe, you can easily get missing data.
When it happens, you can catch the error 4066 which is basically that the history hasn't updated. This is quite common for fetching info on other instruments/TFs, and there's no easy workaround.
Depending on how your test script is, for example if the results are erroneous on the first launch, most of the time if you launch it a second time it should work. That's because between the first call and the second call, the history has been downloaded.

One way to prevent this, is simply to compare the iTime(M5, bar 0), it should at least be the same value for your batch of symbols. Same thing for iTime(M1, bar 0) If you have a different value for a particular symbol, it means the isn't complete.
 
 
  • Post #3
  • Quote
  • Jun 21, 2022 3:43pm Jun 21, 2022 3:43pm
  •  chimoong
  • | Joined May 2016 | Status: Member | 204 Posts
Quoting Oliver.Twist
Disliked
When you access another instrument via code (i.e., not the chart on which the indi is attached to), or even if it's the same instrument but a different timeframe, you can easily get missing data. When it happens, you can catch the error 4066 which is basically that the history hasn't updated. This is quite common for fetching info on other instruments/TFs, and there's no easy workaround. Depending on how your test script is, for example if the results are erroneous on the first launch, most of the time if you launch it a second time it should work....
Ignored
Thanks Oliver for your reply. Is there any solution to get the correct output ? If yes, could you please write down your codes so that I can test your codes. FYI, I have no programming background and I'm learning now, thanks again.

For my codes above, it is pretty simple, no error at all. The only issue I don't know how to get the correct output. I suggest you to test my codes in your MT4, then you can see the incorrect / inconsistent output.
 
 
  • Post #4
  • Quote
  • Jun 22, 2022 10:32am Jun 22, 2022 10:32am
  •  Asw
  • Joined Jan 2008 | Status: Member | 246 Posts
Quoting chimoong
Disliked
Hi coders, kindly advise and help. int tTF=5; int b0M1Sf=iBarShift(Symbol(),1,iTime(Symbol(),tTF,0),true); int b1M1Sf=iBarShift(Symbol(),1,iTime(Symbol(),tTF,1),true); int b2M1Sf=iBarShift(Symbol(),1,iTime(Symbol(),tTF,2),true); {Alert(Symbol()+" M"+IntegerToString(tTF)+" Bar0/Bar1/Bar2 : "+IntegerToString(b0M1Sf)+" / "+IntegerToString(b1M1Sf)+" / "+IntegerToString(b2M1Sf))} I would like to get the shift of smaller TF starting from bigger TF, but the output of different pairs are different, could you please advise :- 1) What is the solution to get...
Ignored

Looking at alert time in your example: 09:03:30, M5 Bar 0, would read 2 , 0, 1 ,2, counting strats at 0.
 
 
  • Post #5
  • Quote
  • Jun 22, 2022 10:42am Jun 22, 2022 10:42am
  •  chimoong
  • | Joined May 2016 | Status: Member | 204 Posts
Quoting Asw
Disliked
{quote} Looking at alert time in your example: 09:03:30, M5 Bar 0, would read 2 , 0, 1 ,2, counting strats at 0.
Ignored
Hi ASW, I suggest you to test my codes yourself as my intention is to get Smaller TF Shift (M1) from Bigger TF (M5). At 09:03:30, the output should read 0,5,10, but in actual fact that not all the pairs are same output ...

My Pic link : https://www.forexfactory.com/attachm...3?d=1655775620
 
 
  • Post #6
  • Quote
  • Jun 22, 2022 11:24am Jun 22, 2022 11:24am
  •  Asw
  • Joined Jan 2008 | Status: Member | 246 Posts
Quoting chimoong
Disliked
{quote} Hi ASW, I suggest you to test my codes yourself as my intention is to get Smaller TF Shift (M1) from Bigger TF (M5). At 09:03:30, the output should read 0,5,10, but in actual fact that not all the pairs are same output ... My Pic link : https://www.forexfactory.com/attachm...3?d=1655775620
Ignored
your code looks at the M1 Timeframe and checks the start of the current M5 bar, last M5 bar and previous M5 bar
 
 
  • Post #7
  • Quote
  • Jun 22, 2022 11:28am Jun 22, 2022 11:28am
  •  chimoong
  • | Joined May 2016 | Status: Member | 204 Posts
Quoting Asw
Disliked
{quote} your code looks at the M1 Timeframe and checks the start of the current M5 bar, last M5 bar and previous M5 bar
Ignored
Yes, correct, but why the output are varies ? Please advise. If you have the solution to get the correct output for all the pairs, kindly provide your codes, appreciated.
 
1
  • Post #8
  • Quote
  • Jun 22, 2022 11:40am Jun 22, 2022 11:40am
  •  Asw
  • Joined Jan 2008 | Status: Member | 246 Posts
Quoting chimoong
Disliked
{quote} Yes, correct, but why the output are varies ? Please advise. If you have the solution to get the correct output for all the pairs, kindly provide your codes, appreciated.
Ignored
You need to make sure the chats data is upto date, bring up all the charts you are scanning for data and use the arrow key to load all data, then try.

You are looking at M1 data and getting the M5 bar, not looking at M5 data, looking for M1 bar.
 
 
  • Post #9
  • Quote
  • Jun 22, 2022 12:36pm Jun 22, 2022 12:36pm
  •  chimoong
  • | Joined May 2016 | Status: Member | 204 Posts
Quoting Asw
Disliked
{quote} You need to make sure the chats data is upto date, bring up all the charts you are scanning for data and use the arrow key to load all data, then try. You are looking at M1 data and getting the M5 bar, not looking at M5 data, looking for M1 bar.
Ignored
Hi ASW, I already updated all the historical data many times for all the charts and all the pairs, yet cannot get the standardised correct output for all the pairs.

Have you tested yourself ? Are you getting the correct answers ? If yes, please provide your codes for me to test. Please advise, thanks
 
 
  • Post #10
  • Quote
  • Jun 22, 2022 3:31pm Jun 22, 2022 3:31pm
  •  Asw
  • Joined Jan 2008 | Status: Member | 246 Posts
Quoting chimoong
Disliked
{quote} Hi ASW, I already updated all the historical data many times for all the charts and all the pairs, yet cannot get the standardised correct output for all the pairs. Have you tested yourself ? Are you getting the correct answers ? If yes, please provide your codes for me to test. Please advise, thanks
Ignored

What are you trying to achieve ?
 
 
  • Post #11
  • Quote
  • Jun 22, 2022 4:34pm Jun 22, 2022 4:34pm
  •  jeanlouie
  • Joined Dec 2010 | Status: Member | 1,240 Posts
You're getting the correct results, they're just not what you expect.

- put a 5m mtf candle on the 1m chart, and see for yourself where the 1m shifts are for every 5m candle as the minutes go by
- dont assume that the market watch time is the time that every symbol's last quote is
 
 
  • Post #12
  • Quote
  • Edited at 11:02pm Jun 22, 2022 9:41pm | Edited at 11:02pm
  •  chimoong
  • | Joined May 2016 | Status: Member | 204 Posts
Quoting Asw
Disliked
{quote} What are you trying to achieve ?
Ignored
Hi ASW, what I want to achieve is my pic link https://www.forexfactory.com/attachm...3?d=1655775620 and stated clearly in red text and with the Alert to tell you the output ...

Sadly to say that I suggested you two times to test the codes yourself but you reluctant to do so and ended up with only your imaginations that kept on telling wrong answers. I really do not know how to explain further to you. Imagination with false assumptions don't work my dear friend ...
 
 
  • Post #13
  • Quote
  • Jun 22, 2022 9:43pm Jun 22, 2022 9:43pm
  •  chimoong
  • | Joined May 2016 | Status: Member | 204 Posts
Quoting jeanlouie
Disliked
You're getting the correct results, they're just not what you expect. - put a 5m mtf candle on the 1m chart, and see for yourself where the 1m shifts are for every 5m candle as the minutes go by - dont assume that the market watch time is the time that every symbol's last quote is
Ignored
Hi Jean, thanks for your clarification. I would assume that this would be the inefficiency of mq4 and there is no solution at all to get the correct output ....
 
 
  • Post #14
  • Quote
  • Jun 23, 2022 12:51am Jun 23, 2022 12:51am
  •  eess
  • Joined Feb 2021 | Status: Member | 451 Posts
Quoting chimoong
Disliked
{quote} Hi ASW, what I want to achieve is my pic link https://www.forexfactory.com/attachm...3?d=1655775620 and stated clearly in red text and with the Alert to tell you the output ... Sadly to say that I suggested you two times to test the codes yourself but you reluctant to do so and ended up with only your imaginations that kept on telling wrong answers. I really do not know how to explain further to you. Imagination with false assumptions don't work my dear friend ...
Ignored

You are misinterpreting ASW's question to you, it's completely legitimate why he asked you this question because he wants to know what you are trying to achieve for the end product (note: I said end product, not your intermediate objective you posted in your screenshot), so that he can advise if there is a better way to code this, rather than the code you published here. Your approach is likely to be completely wrong in the first place, which is why it is not worth people's time to even test them in the first place. In other words, people need to know the BIG PICTURE before they can advise correctly. It's just common sense.
 
 
  • Post #15
  • Quote
  • Jun 23, 2022 2:21am Jun 23, 2022 2:21am
  •  chimoong
  • | Joined May 2016 | Status: Member | 204 Posts
Quoting eess
Disliked
{quote} You are misinterpreting ASW's question to you, it's completely legitimate why he asked you this question because he wants to know what you are trying to achieve for the end product (note: I said end product, not your intermediate objective you posted in your screenshot), so that he can advise if there is a better way to code this, rather than the code you published here. Your approach is likely to be completely wrong in the first place, which is why it is not worth people's time to even test them in the first place. In other words, people...
Ignored
In my 1st post "1) What is the solution to get the correct output results ?" ... this is what I want to achieve, thanks
 
 
  • Post #16
  • Quote
  • Jun 23, 2022 4:25am Jun 23, 2022 4:25am
  •  eess
  • Joined Feb 2021 | Status: Member | 451 Posts
Quoting chimoong
Disliked
{quote} In my 1st post "1) What is the solution to get the correct output results ?" ... this is what I want to achieve, thanks
Ignored

Either you don't understand English or you are trying not to give more information, anyway no one can help you like that, you are on your own. Good luck. Anyone who wants to help you is just wasting their time playing guessing game with incomplete information.
 
 
  • Post #17
  • Quote
  • Jun 23, 2022 4:38am Jun 23, 2022 4:38am
  •  chimoong
  • | Joined May 2016 | Status: Member | 204 Posts
Quoting eess
Disliked
{quote} Either you don't understand English or you are trying not to give more information, anyway no one can help you like that, you are on your own. Good luck. Anyone who wants to help you is just wasting their time playing guessing game with incomplete information.
Ignored
Hi Eess, kindly look at the pic : https://www.forexfactory.com/attachm...3?d=1655775620

1) At Market Watch time XX.03.XX, the correct output should be 3 / 8 / 13 but the codes gave the wrong output for some of the pairs as :-
a) 2 / 7 / 12

2) At Market Watch time XX.05.XX, the correct output should be 0 / 5 / 10 but the codes gave the wrong output for some of the pairs as :-
a) 4 / 9 / 14
b) 5 / 10 / 15

3) At Market Watch time XX.06.XX, the correct output should be 1 / 6 / 11 but the codes gave the wrong output for some of the pairs as :-
a) 4 / 9 / 14
b) 6 / 11 / 16

4) At Market Watch time XX.07.XX, the correct output should be 2 / 7 / 12 but the codes gave the wrong output for some of the pairs as :-
a) 1 / 6 / 11

Hope the above simple explanation can enlighten you, thanks ...
 
 
  • Post #18
  • Quote
  • Edited Jun 24, 2022 1:18am Jun 23, 2022 6:22am | Edited Jun 24, 2022 1:18am
  •  emmzett
  • Joined Apr 2008 | Status: Member | 572 Posts
Quoting chimoong
Disliked
...I would like to get the shift of smaller TF starting from bigger TF, but the output of different pairs are different...
Ignored
Quoting jeanlouie
Disliked
...You're getting the correct results, they're just not what you expect.
Ignored
JeanLouie is right. In general it doesn't matter whether one timeframe is larger or smaller. Key here is that you access two timeframes which both may be different from the current one. A timeseries different from the current chart timeframe needs to be preloaded and synchronized before one can expect correct results. If you don't do this (like you do in your code) you will get different results for every call until data is synchronized.

Quoting chimoong
Disliked
2) Is this a bug of mq4 / MT4 and can never be resolved?
Ignored
No, this is not an MQL bug. It's expected behavior and documented in the language reference. The bug is in front of the computer and needs to be fixed there.

Quoting chimoong
Disliked
1) What is the solution to get the correct output results?
Ignored
The best way to forcefully preload and synchronize a timeseries different from the current chart timeframe is to use ArrayCopyRates(MqlRates &array[], ...), and copy the timeseries to a local array. Calling iBarShift() or iTime() also loads/synchronizes the series but with a big catch. Data accessed this way only stays synchronized as long as you constantly access it. If for example you call your code once and say - the next time after 20 minutes, at the second call data will be most probably out of sync again. That's because the terminal monitors access to all timeseries and if you stop accessing one it automatically stops updating them and resources are released. This happens "after some time" which from my observations is about after 15 minutes.

To make sure data stays up-to-date call ArrayCopyRates() on every tick. It will not allocate new resources on each call as it only manages a pointer to the terminal's internal data structures.

After loading the timeseries the first time you have to wait until data synchronization finished. Begin of synchronization is signaled by ERR_HISTORY_WILL_UPDATED, there is no documented way to detect the end of it (although there are ways). So I recommend to call ArrayCopyRates() on every tick and in your main code you wait for about 10 seconds until calling iBarShift()/iTime() the first time. After those 10 seconds your timeframes will be synced and your code example will work flawless.

As you access two timeframes which may be different from the current one you have to manage two local arrays (one for each timeframe) and call ArrayCopyRates() for each of them.
 
 
  • Post #19
  • Quote
  • Last Post: Edited at 3:27pm Jun 23, 2022 2:58pm | Edited at 3:27pm
  •  emmzett
  • Joined Apr 2008 | Status: Member | 572 Posts
Quoting chimoong
Disliked
1) At Market Watch time XX.03.XX, the correct output should be 3 / 8 / 13
2) At Market Watch time XX.05.XX, the correct output should be 0 / 5 / 10
3) At Market Watch time XX.06.XX, the correct output should be 1 / 6 / 11
4) At Market Watch time XX.07.XX, the correct output should be 2 / 7 / 12
Ignored
If your real and true intent is to produce above output depending on the time displayed in the title bar of the "MarketWatch" window then I suggest to not involve timeseries access at all. That's just not the right tool to achieve what you want. Instead use a snippet like the following. It will work for all standard timeframes except for PERIOD_W1 as the small one.

Inserted Code
int largeTimeframe = PERIOD_M5;
int smallTimeframe = PERIOD_M1;
 
int minutes = TimeCurrent()/60;
 
int offset0 = (minutes % largeTimeframe) / smallTimeframe;
int offset1 = offset0 + largeTimeframe/smallTimeframe;    
int offset2 = offset1 + largeTimeframe/smallTimeframe;    
  
Alert(Symbol() +" M"+ IntegerToString(largeTimeframe) +" Bar0/Bar1/Bar2 : "+ IntegerToString(offset0) +" / "+ IntegerToString(offset1) +" / "+ IntegerToString(offset2));
Attached Images (click to enlarge)
Click to Enlarge

Name: Calculation of timeframe offsets.png
Size: 7 KB Click to Enlarge

Name: Results of offset calculation.png
Size: 11 KB
 
 
  • Platform Tech
  • /
  • Need Help: iBarShift: Smaller TF Shift from Bigger TF
  • Reply to Thread
1 trader viewing now
Top of Page
  • Facebook
  • Twitter
About FF
  • Mission
  • Products
  • User Guide
  • Media Kit
  • Blog
  • Contact
FF Products
  • Forums
  • Trades
  • Calendar
  • News
  • Market
  • Brokers
  • Trade Explorer
FF Website
  • Homepage
  • Search
  • Members
  • Report a Bug
Follow FF
  • Facebook
  • Twitter

FF Sister Sites:

  • Metals Mine
  • Energy EXCH
  • Crypto Craft

Forex Factory® is a brand of Fair Economy, Inc.

Terms of Service / ©2022