Help needed to return [true] once a new session is found. Current code returns multiple [true]

 

Wishing all a happy weekend.

I have coded a IsNewBarSession, which is supposed to return true at first cross of timeStamp with cATS.checkTimeCross(vHour,vMinute,vCurrTime,vPrevTime).

I could not apply a proper logic/condition to return [false] if timeStamp have been crossed.

For Asian Session start multiple [true] are marked with "IsNewSession[true] ... Should not detect this".

The next [true] should be when LON session starts and next when [NYC] session starts.

//+-----------------------------------------------------------------------------------------------------------------------------+
//| METHOD:             IsNewSession()
//|                                                     Used TimeCross() function from https://www.mql5.com/en/articles/599#mql5time
//| A simple check for equality to the specified time will not work properly as ticks do not occur at regular intervals and
//| there may be delays of several seconds to several minutes. It is very likely that there will simply be no tick with
//| the specified time in the market. We need to check for the intersection of a given timestamp.
//+-----------------------------------------------------------------------------------------------------------------------------+
bool CVPSBase::IsNewSession() {

                string vMethod = "[" + mSymbol + "," + TimeToString(TimeCurrent()) + "] " + __FUNCTION__;
        //+---------------------------------------------------------------------------------------------------------------------------+
        //| Volume Profile is calculated on if Current Day's New Session is formed
        //+---------------------------------------------------------------------------------------------------------------------------+
                bool timeASA = (mSVRCurr >= tForexOpen[2] && mSVRCurr < tForexOpen[1])                           ? true : false;                // Time between ASA Session
                bool timeLON = (mSVRCurr >= tForexOpen[1] && mSVRCurr < tForexOpen[0])                           ? true : false;                // Time between LON Session
                bool timeNYC = (mSVRCurr >= tForexOpen[0] && mSVRCurr < tSessions[0].NYCClose) ? true : false;          // Time between NYC Session

                // If all Boolean conditions false, no New Session found, Exit
                if(!timeASA && !timeLON && !timeNYC)    {
                        PrintFormat("%s: Error: timeASA[%s] && timeLON[%s] && timeNYC[%s]",vMethod,(string)timeASA,(string)timeLON,(string)timeNYC);
                        return(false);
                }
        //+---------------------------------------------------------------------------------------------------------------------------+
        //| If Current Day's New Session we calculate and draw mMaxSessionToCalculate
        //+---------------------------------------------------------------------------------------------------------------------------+
                /* Error returning [true] on multiple bars
                2024.02.01 01:01:59   [XAUUSD,2024.02.01 01:01] CVPSBase::IsNewSession: mPrevCross[] != vCurrCross[2024.02.01 01:01:59]
                2024.02.01 01:01:59   [XAUUSD,2024.02.01 01:01] CVPSBase::IsNewSession: vPrevTime [2024.01.31 23:58] (HoD[23] MoH[58]) = vHour[1]vMinute[1]
                2024.02.01 01:02:00   [XAUUSD,2024.02.01 01:02] CVPSBase::IsNewSession: mPrevCross[] != vCurrCross[2024.02.01 01:02:00]
                2024.02.01 01:02:00   [XAUUSD,2024.02.01 01:02] CVPSBase::IsNewSession: vPrevTime [2024.01.31 23:58] (HoD[23] MoH[58]) = vHour[1]vMinute[2]
                2024.02.01 01:02:00   [XAUUSD,2024.02.01 01:02] CVPSBase::IsNewSession: mPrevCross[] != vCurrCross[2024.02.01 01:02:00]
                2024.02.01 01:02:00   [XAUUSD,2024.02.01 01:02] CVPSBase::IsNewSession: vPrevTime [2024.02.01 01:01] (HoD[1] MoH[1]) = vHour[1]vMinute[2]
                2024.02.01 01:02:00   [XAUUSD PERIOD_M15] CVPSBase::OnTick: IsNewSession[true] ... IsNewSession[true] on First Time Detection
                2024.02.01 01:02:20   [XAUUSD,2024.02.01 01:02] CVPSBase::IsNewSession: mPrevCross[2024.02.01 01:02:00] != vCurrCross[2024.02.01 01:02:20]
                2024.02.01 01:02:20   [XAUUSD,2024.02.01 01:02] CVPSBase::IsNewSession: vPrevTime [2024.01.31 23:58] (HoD[23] MoH[58]) = vHour[1]vMinute[2]
                2024.02.01 01:02:20   [XAUUSD,2024.02.01 01:02] CVPSBase::IsNewSession: mPrevCross[2024.02.01 01:02:00] != vCurrCross[2024.02.01 01:02:20]
                2024.02.01 01:02:20   [XAUUSD,2024.02.01 01:02] CVPSBase::IsNewSession: vPrevTime [2024.02.01 01:01] (HoD[1] MoH[1]) = vHour[1]vMinute[2]
                2024.02.01 01:02:20   [XAUUSD PERIOD_M15] CVPSBase::OnTick: IsNewSession[true] ... Should not detect this 
                2024.02.01 01:02:40   [XAUUSD,2024.02.01 01:02] CVPSBase::IsNewSession: mPrevCross[2024.02.01 01:02:00] != vCurrCross[2024.02.01 01:02:40]
                2024.02.01 01:02:40   [XAUUSD,2024.02.01 01:02] CVPSBase::IsNewSession: vPrevTime [2024.01.31 23:58] (HoD[23] MoH[58]) = vHour[1]vMinute[2]
                2024.02.01 01:02:40   [XAUUSD,2024.02.01 01:02] CVPSBase::IsNewSession: mPrevCross[2024.02.01 01:02:00] != vCurrCross[2024.02.01 01:02:40]
                2024.02.01 01:02:40   [XAUUSD,2024.02.01 01:02] CVPSBase::IsNewSession: vPrevTime [2024.02.01 01:01] (HoD[1] MoH[1]) = vHour[1]vMinute[2]
                2024.02.01 01:02:40   [XAUUSD PERIOD_M15] CVPSBase::OnTick: IsNewSession[true] ... Should not detect this 
                2024.02.01 01:02:59   [XAUUSD,2024.02.01 01:02] CVPSBase::IsNewSession: mPrevCross[2024.02.01 01:02:00] != vCurrCross[2024.02.01 01:02:59]
                2024.02.01 01:02:59   [XAUUSD,2024.02.01 01:02] CVPSBase::IsNewSession: vPrevTime [2024.01.31 23:58] (HoD[23] MoH[58]) = vHour[1]vMinute[2]
                2024.02.01 01:02:59   [XAUUSD,2024.02.01 01:02] CVPSBase::IsNewSession: mPrevCross[2024.02.01 01:02:00] != vCurrCross[2024.02.01 01:02:59]
                2024.02.01 01:02:59   [XAUUSD,2024.02.01 01:02] CVPSBase::IsNewSession: vPrevTime [2024.02.01 01:01] (HoD[1] MoH[1]) = vHour[1]vMinute[2]
                2024.02.01 01:02:59   [XAUUSD PERIOD_M15] CVPSBase::OnTick: IsNewSession[true] ... Should not detect this 
                2024.02.01 01:03:00   [XAUUSD,2024.02.01 01:03] CVPSBase::IsNewSession: mPrevCross[2024.02.01 01:02:00] != vCurrCross[2024.02.01 01:03:00]
                2024.02.01 01:03:00   [XAUUSD,2024.02.01 01:03] CVPSBase::IsNewSession: vPrevTime [2024.01.31 23:58] (HoD[23] MoH[58]) = vHour[1]vMinute[3]
                2024.02.01 01:03:00   [XAUUSD,2024.02.01 01:03] CVPSBase::IsNewSession: mPrevCross[2024.02.01 01:02:00] != vCurrCross[2024.02.01 01:03:00]
                2024.02.01 01:03:00   [XAUUSD,2024.02.01 01:03] CVPSBase::IsNewSession: vPrevTime [2024.02.01 01:02] (HoD[1] MoH[2]) = vHour[1]vMinute[3]
                2024.02.01 01:03:00   [XAUUSD PERIOD_M15] CVPSBase::OnTick: IsNewSession[true] ... Should not detect this 
                2024.02.01 01:03:20   [XAUUSD,2024.02.01 01:03] CVPSBase::IsNewSession: mPrevCross[2024.02.01 01:03:00] != vCurrCross[2024.02.01 01:03:20]
                2024.02.01 01:03:20   [XAUUSD,2024.02.01 01:03] CVPSBase::IsNewSession: vPrevTime [2024.01.31 23:58] (HoD[23] MoH[58]) = vHour[1]vMinute[3]
                2024.02.01 01:03:20   [XAUUSD,2024.02.01 01:03] CVPSBase::IsNewSession: mPrevCross[2024.02.01 01:03:00] != vCurrCross[2024.02.01 01:03:20]
                2024.02.01 01:03:20   [XAUUSD,2024.02.01 01:03] CVPSBase::IsNewSession: vPrevTime [2024.02.01 01:02] (HoD[1] MoH[2]) = vHour[1]vMinute[3]
                2024.02.01 01:03:20   [XAUUSD PERIOD_M15] CVPSBase::OnTick: IsNewSession[true] ... Should not detect this 
                2024.02.01 01:03:40   [XAUUSD,2024.02.01 01:03] CVPSBase::IsNewSession: mPrevCross[2024.02.01 01:03:00] != vCurrCross[2024.02.01 01:03:40]
                2024.02.01 01:03:40   [XAUUSD,2024.02.01 01:03] CVPSBase::IsNewSession: vPrevTime [2024.01.31 23:58] (HoD[23] MoH[58]) = vHour[1]vMinute[3]
                2024.02.01 01:03:40   [XAUUSD,2024.02.01 01:03] CVPSBase::IsNewSession: mPrevCross[2024.02.01 01:03:00] != vCurrCross[2024.02.01 01:03:40]
                2024.02.01 01:03:40   [XAUUSD,2024.02.01 01:03] CVPSBase::IsNewSession: vPrevTime [2024.02.01 01:02] (HoD[1] MoH[2]) = vHour[1]vMinute[3]
                2024.02.01 01:03:40   [XAUUSD PERIOD_M15] CVPSBase::OnTick: IsNewSession[true] ... Should not detect this 
                2024.02.01 01:03:59   [XAUUSD,2024.02.01 01:03] CVPSBase::IsNewSession: mPrevCross[2024.02.01 01:03:00] != vCurrCross[2024.02.01 01:03:59]
                2024.02.01 01:03:59   [XAUUSD,2024.02.01 01:03] CVPSBase::IsNewSession: vPrevTime [2024.01.31 23:58] (HoD[23] MoH[58]) = vHour[1]vMinute[3]
                2024.02.01 01:03:59   [XAUUSD,2024.02.01 01:03] CVPSBase::IsNewSession: mPrevCross[2024.02.01 01:03:00] != vCurrCross[2024.02.01 01:03:59]
                2024.02.01 01:03:59   [XAUUSD,2024.02.01 01:03] CVPSBase::IsNewSession: vPrevTime [2024.02.01 01:02] (HoD[1] MoH[2]) = vHour[1]vMinute[3]
                2024.02.01 01:03:59   [XAUUSD PERIOD_M15] CVPSBase::OnTick: IsNewSession[true] ... Should not detect this 
                2024.02.01 01:04:00   [XAUUSD,2024.02.01 01:04] CVPSBase::IsNewSession: mPrevCross[2024.02.01 01:03:00] != vCurrCross[2024.02.01 01:04:00]
                2024.02.01 01:04:00   [XAUUSD,2024.02.01 01:04] CVPSBase::IsNewSession: vPrevTime [2024.01.31 23:58] (HoD[23] MoH[58]) = vHour[1]vMinute[4]
                2024.02.01 01:04:00   [XAUUSD,2024.02.01 01:04] CVPSBase::IsNewSession: mPrevCross[2024.02.01 01:03:00] != vCurrCross[2024.02.01 01:04:00]
                2024.02.01 01:04:00   [XAUUSD,2024.02.01 01:04] CVPSBase::IsNewSession: vPrevTime [2024.02.01 01:03] (HoD[1] MoH[3]) = vHour[1]vMinute[4]
                2024.02.01 01:04:00   [XAUUSD PERIOD_M15] CVPSBase::OnTick: IsNewSession[true] ... Should not detect this 
                2024.02.01 01:04:20   [XAUUSD,2024.02.01 01:04] CVPSBase::IsNewSession: mPrevCross[2024.02.01 01:04:00] != vCurrCross[2024.02.01 01:04:20]
                2024.02.01 01:04:20   [XAUUSD,2024.02.01 01:04] CVPSBase::IsNewSession: vPrevTime [2024.01.31 23:58] (HoD[23] MoH[58]) = vHour[1]vMinute[4]
                2024.02.01 01:04:20   [XAUUSD,2024.02.01 01:04] CVPSBase::IsNewSession: mPrevCross[2024.02.01 01:04:00] != vCurrCross[2024.02.01 01:04:20]
                2024.02.01 01:04:20   [XAUUSD,2024.02.01 01:04] CVPSBase::IsNewSession: vPrevTime [2024.02.01 01:03] (HoD[1] MoH[3]) = vHour[1]vMinute[4]
                2024.02.01 01:04:20   [XAUUSD PERIOD_M15] CVPSBase::OnTick: IsNewSession[true] ... Should not detect this 
                2024.02.01 01:04:40   [XAUUSD,2024.02.01 01:04] CVPSBase::IsNewSession: mPrevCross[2024.02.01 01:04:00] != vCurrCross[2024.02.01 01:04:40]
                2024.02.01 01:04:40   [XAUUSD,2024.02.01 01:04] CVPSBase::IsNewSession: vPrevTime [2024.01.31 23:58] (HoD[23] MoH[58]) = vHour[1]vMinute[4]
                2024.02.01 01:04:40   [XAUUSD,2024.02.01 01:04] CVPSBase::IsNewSession: mPrevCross[2024.02.01 01:04:00] != vCurrCross[2024.02.01 01:04:40]
                2024.02.01 01:04:40   [XAUUSD,2024.02.01 01:04] CVPSBase::IsNewSession: vPrevTime [2024.02.01 01:03] (HoD[1] MoH[3]) = vHour[1]vMinute[4]
                2024.02.01 01:04:40   [XAUUSD PERIOD_M15] CVPSBase::OnTick: IsNewSession[true] ... Should not detect this 
                */
                datetime vCurrCross = TimeCurrent();
                for(int i = 2; i >= 0; i--) {
                        // We need first PERIOD_M1 bar for tForexOpen[i] which is on PERIOD_H1. For Asian session/tSVR starts at [XAUSUD]01:01 Hrs
                        int              idx                     = iBarShift(mSymbol,PERIOD_M1,tForexOpen[i]);
                        datetime vPrevTime = iTime(mSymbol,PERIOD_M1,idx+1);
                        int                      vHour           = HoD(TimeCurrent());                                                                                                          // Hour   value of CurrentTime
                        int                      vMinute         = MoH(TimeCurrent());                                                                                                          // Minute value of CurrentTime
                        datetime vCurrTime = StringToTime(""+(string)YoY(tForexOpen[i])+"."+(string)MoY(tForexOpen[i])+"."+(string)DoM(tForexOpen[i])+" "+(string)vHour+":"+(string)vMinute);

                        if(cATS.checkTimeCross(vHour,vMinute,vCurrTime,vPrevTime)) {
                                //PrintFormat("%s: vPrevTime[%s] vCurrTime[%s]",vMethod,TimeToString(vPrevTime),TimeToString(vCurrTime));
                                if(mPrevCross == WRONG_VALUE || mPrevCross != vCurrCross) {
                                        PrintFormat("%s: mPrevCross[%s] != vCurrCross[%s]",vMethod,TimeToString(mPrevCross),TimeToString(vCurrCross));
                                        PrintFormat("%s: vPrevTime [%s] (HoD[%i] MoH[%i]) = vHour[%i]vMinute[%i]",vMethod,TimeToString(vPrevTime),HoD(vPrevTime),MoH(vPrevTime),vHour,vMinute);
                                        if(HoD(vPrevTime) == vHour) {
                                                vCurrCross = vCurrTime;
                                                mPrevCross = vCurrCross;
                                        return(true);
                                }
                                }
                        }
                }       
        //+---------------------------------------------------------------------------------------------------------------------------+
          return(false);
//+-----------------------------------------------------------------------------------------------------------------------------+

 Thanks in advance and looking for solution from Gurus :)

Regards.

 
So what is your question exactly 
 
amrali #:
So what is your question exactly 

Hi

IsNewSession() should return [true] value only once when 

if(cATS.checkTimeCross(vHour,vMinute,vCurrTime,vPrevTime)) 

returns true for a given timeStamp for example Asian Session started at [2024.02.01 01:01:00] and it was crossed at vCurrCross[2024.02.01 01:02:00]  should return true.

Next cPrevCross would be now set at [2024.02.01 01:02:00] and when London session open [2024.02.01 10:00:00] and is crossed at [2024.02.01 10:01:00] should return true.

Next cPrevCross would be now set at [2024.02.01 10:01:00] and when New York session open [2024.02.01 15:00:00] and is crossed at [2024.02.01 15:01:00] should return true.

Hope this make my requirements clearer.

 

UPDATE ...

Hi I am having some success with OnTimer() functions to get NewSession detection.

Please ignore this thread for time being. If I fail with OnTimer() too, I will restart the thread again.

Thanks.

 

I use bool. 

Assign Asian = 0.

if (Asian Flag ==0 && New Asian Session) then Asian = 1.

Then after a few hours , or when vHour=0, you can reset the Asian Flag back to 0.

 
Chijioke Chukwunonso Iloabachie #:

I use bool. 

Assign Asian = 0.

if (Asian Flag ==0 && New Asian Session) then Asian = 1.

Then after a few hours , or when vHour=0, you can reset the Asian Flag back to 0.

Thanks Chijioke

Its seems good workaround way :) Will give it a try if OnTimer() does not work well.