MQL4 Script - open on new bar question - Page 2
Page 2 of 843 FirstFirst 123 LastLast
Results 11 to 20 of 23

Thread: MQL4 Script - open on new bar question

  1. #11
    Quote Originally Posted by ;
    I have done it using the Bars function, not sure it's bullet proof but I believe it does the trick, as well
    Be advised that this approach isn't reliable, as MT stops updating pubs count after maximum pubs setting is achieved. So, thereal! Reliable method is to check time.

  2. #12
    Quote Originally Posted by ;
    quote Be advised that this method isn't reliable, as MT stops upgrading bars count after maximum bars atmosphere is attained. So, the only! Method that is reliable is to inspect time.
    Yup, agreed, certainly don't use the Bars method. The method to to utilize SeriesInfoInteger and call it for your LASTBAR_DATE and ask again until your function times-out or you get a clean result. Here is an example of a MTF isNewBar function...

    Inserted Code #include lt;Arrays\ArrayObj.mqhgt; class NewBars : public CObject{ ENUM_TIMEFRAMES m_timeframe; datetime m_last_bar_time; people: NewBars(ENUM_TIMEFRAMES tf):m_timeframe(tf),m_last_bar_time(0){} bool isNewBar() uint ms = GetTickCount(); datetime curr; do ResetLastError(); curr = (datetime)SeriesInfoInteger(_Symbol,m_timeframe,SE RIES_LASTBAR_DATE); while(_LastError ! = ERR_NO_ERROR GetTickCount() - ms lt; 500); if(curr gt; 0 _LastError == ERR_NO_ERROR curr ! = m_last_bar_time) bool res = true; if(m_last_bar_time == 0) res = false; m_last_bar_time = curr; return res; return false; bool isMatching(ENUM_TIMEFRAMES tf) return m_timeframe == tf ? True : false; }; bool isNewBar(const ENUM_TIMEFRAMES interval) static CArrayObj tfs; for(int I = tfs.Total()-1; igt;=0; I--) NewBars *tf = dynamic_castlt;NewBars*gt;(tfs.At(I)); if(tf.isMatching(interval)) return tf.isNewBar(); tfs.Add(new NewBars(interval)); return false;

  3. #13
    Quote Originally Posted by ;
    quote Be advised that this method isn't dependable, as MT stops updating bars count after max bars setting is achieved. So, the! Method is to check time.
    It doesn't (stop updating)
    Bars() exceeds max number of bars in scenarios when maximal pubs setting is reached (and exceeded)


  4. #14
    Quote Originally Posted by ;
    quote Yes, consented, definitely don't use the Bars method. The method to to utilize SeriesInfoInteger and call it to the LASTBAR_DATE and then ask again before your work times-out or you get a outcome. Here is an instance of a MTF isNewBar function... #include lt;Arrays\ArrayObj.mqhgt; course NewBars : public CObject{ ENUM_TIMEFRAMES m_timeframe; datetime m_last_bar_time; people: NewBars(ENUM_TIMEFRAMES tf):m_timeframe(tf),m_last_bar_time(0){} bool isNewBar() { uint ms = GetTickCount(); datetime...
    Thank you, you are very kind in providing this examples, here and on my other threads.
    I know what you've done here but the linguistics eludes me as I am yet to learn the OO side of the MQL4.
    I am also not entirely convinced your code will work within the context of a script, however I am not all that edued.

  5. #15
    Quote Originally Posted by ;
    Also, check this code that's acceptable for arbitrary time frames (detected using the first price quote): #property strict void OnStart() datetime seconds = _Period * 60; // state any amount in seconds datetime t_last_recorded = 0; // for demonion interest while(! IsStopped()) RefreshRates(); datetime t = TimeCurrent(); // time of last known quote datetime t_normalized = t / moments * moments; if(t_normalized gt; t_last_recorded) // new bar printf(new bar!) ; t_last_recorded = t_normalized; Sleep(1);
    This is intriguing, thanks, will it work on offline charts, as well? Given the fact that they are based?

  6. #16
    Quote Originally Posted by ;
    quote Be advised that this method is not reliable, as MT stops upgrading bars count after max bars atmosphere is achieved. So, thereal! Method is to check time.
    Thanks, I read contradicting answers in various threads so I wasn't convinced but it had been working for me so I figured what the heck

  7. #17
    Quote Originally Posted by ;
    quote It doesn't (stop upgrading) Bars() exceeds max amount of pubs in scenarios when max bars setting is attained image
    Thank you for responding, so am I right to use this Bars method or not? Feels like it is easier then the Time system and that I do prefer to keep things simple

  8. #18
    Quote Originally Posted by ;
    quote Thank you for replying, so am I right to utilize this Bars method or not? Feels like it is simpler then the Time process and I really do like to keep things easy
    Neither method is complex :
    Inserted Code static datetime _prevTime = 0; datetime _currTime = iTime(NULL,desired time frame,0); bool _isNewBar = (_prevTime ! = _currTime); if (_isNewBar) _prevTime = _currTime;

  9. #19
    Quote Originally Posted by ;
    quote Neither procedure is complex : static datetime _prevTime = 0; datetime _currTime = iTime(NULL,desired time period,0); bool _isNewBar = (_prevTime ! = _currTime); if (_isNewBar) _prevTime = _currTime;
    Thanks, I had something like my script but it did not work (posted earlier in initial article) even when I added the RefreshRates().
    I will try again with your code, with added ResreshRates() inside and outside of the loop so the script gets fresh information.

  10. #20
    Inserted Code // -- Worldwide Variable ------------------------------------------------------------------ datetime ArrayTime#91;#93;, LastTime; void OnTick() if(NewBar(PERIOD_CURRENT)) // your code bool NewBar(int period) bool firstRun = false, newBar = false; ArraySetAsSeries(ArrayTime,true); CopyTime(Symbol(),period,0,2,ArrayTime); if(LastTime == 0) firstRun = true; if(ArrayTime#91;0#93; gt; LastTime) if(firstRun == false) newBar = true; LastTime = ArrayTime#91;0#93;; Yield newBar;

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
This website uses cookies
We use cookies to store session information to facilitate remembering your login information, to allow you to save website preferences, to personalise content and ads, to provide social media features and to analyse our traffic. We also share information about your use of our site with our social media, advertising and analytics partners.