Help requested on a for..loop to get IsNewSession() value

 

Dear Fellows

Please help me to rectify the below code to get correct IsNewSession value when a new session has started.

I failed to resolve it.

//+-----------------------------------------------------------------------------------------------------------------------------+
//| 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 CiVPBase::IsNewSession() {

	#define _HoD(t) ((int)(((t)%86400)/3600))                // Hour of Day 2018.02.03 17:55:56 => (int) 17
        define  _MoH(t) (int(((t)%3600)/60))                     // Minute of Current Hour 
        string 	vMethod = "[" + mSymbol + "," + TimeToString(TimeCurrent()) + "] " + __FUNCTION__;

        datetime vCurrCross = TimeCurrent();
        for(int i = ArraySize(TimeFxOpen)-1; i >= 0; i--) {
        	int	vHour           = _HoD(TimeCurrent());                                                                                                         // Hour   value of CurrentTime
                int     vMinute         = _MoH(TimeCurrent());                                                                                                         // Minute value of CurrentTime
                // We need first PERIOD_M1 bar for TimeFxOpen[i] which is on PERIOD_H1. For Asian session and tSVR it starts at 01:01 Hrs
                int     idx             = iBarShift(mSymbol,PERIOD_M1,TimeFxOpen[i]);
                datetime vPrevTime = iTime(mSymbol,PERIOD_M1,idx-1);
                datetime vCurrTime = StringToTime(""+(string)YoY(TimeFxOpen[i])+"."+(string)MoY(TimeFxOpen[i])+"."+(string)DoM(TimeFxOpen[i])+" "+(string)vHour+":"+(string)vMinute);

                /* Note: Session Time values in TimeFxOpen[] array ... representing new session start time
                CiVPBase::IsNewBarD01: FxSession idxSession[9] timeStart[2024.02.06 13:00]
                CiVPBase::IsNewBarD01: FxSession idxSession[8] timeStart[2024.02.07 00:01]
                CiVPBase::IsNewBarD01: FxSession idxSession[7] timeStart[2024.02.07 08:00]
                CiVPBase::IsNewBarD01: FxSession idxSession[6] timeStart[2024.02.07 13:00]
                CiVPBase::IsNewBarD01: FxSession idxSession[5] timeStart[2024.02.08 00:01]
                CiVPBase::IsNewBarD01: FxSession idxSession[4] timeStart[2024.02.08 08:00]
                CiVPBase::IsNewBarD01: FxSession idxSession[3] timeStart[2024.02.08 13:00]
                CiVPBase::IsNewBarD01: FxSession idxSession[2] timeStart[2024.02.09 00:01]
                CiVPBase::IsNewBarD01: FxSession idxSession[1] timeStart[2024.02.09 08:00]
                CiVPBase::IsNewBarD01: FxSession idxSession[0] timeStart[2024.02.09 13:00]
                */

                /*
                CiVPBase::IsNewSession: vPrevTime[2023.09.21 01:01] vCurrTime[2023.09.21 01:01] ... Should return NewSession[true] at [2023.09.21 01:02] timeCross
                CiVPBase::IsNewSession: vPrevTime[2023.09.21 08:01] vCurrTime[2023.09.21 01:01] ... Should return NewSession[true] at [2023.09.21 08:02] timeCross
                CiVPBase::IsNewSession: mPrevCross[] != vCurrCross[2024.02.09 01:01]
                CiVPBase::IsNewSession: HoD[8] MoD[1] of vPrevTime[2023.09.21 08:01] = vHour[1]vMinute[1]
                CiVPBase::IsNewSession: vPrevTime[2023.09.21 13:01] vCurrTime[2023.09.21 01:01]
                CiVPBase::IsNewSession: mPrevCross[] != vCurrCross[2024.02.09 01:01]
                CiVPBase::IsNewSession: HoD[13] MoD[1] of vPrevTime[2023.09.21 13:01] = vHour[1]vMinute[1]
                */
                PrintFormat("%s: vPrevTime[%s] vCurrTime[%s]",vMethod,TimeToString(vPrevTime),TimeToString(vCurrTime));
                if(cATS.checkTimeCross(vHour,vMinute,vCurrTime,vPrevTime)) {
                	PrintFormat("%s: mPrevCross[%s] != vCurrCross[%s]",vMethod,TimeToString(mPrevCross),TimeToString(vCurrCross));
                        if(mPrevCross != vCurrCross) {
                        	PrintFormat("%s: HoD[%i] MoD[%i] of vPrevTime[%s] = vHour[%i]vMinute[%i]",vMethod,_HoD(vPrevTime),_MoH(vPrevTime),TimeToString(vPrevTime),vHour,vMinute);
                                if(HoD(vPrevTime) == vHour) {
                                	vCurrCross = vCurrTime;
                                        mPrevCross = vCurrCross;
                                        PrintFormat("%s: vHour[%i] vMinute[%i] vPrevTime[%s] mPrevCross[%s] vCurrTime[%s] vCurrCross[%s]",vMethod,vHour,vMinute,TimeToString(vPrevTime),TimeToString(mPrevCross),TimeToString(vCurrTime),TimeToString(vCurrCross));
                                        return(true);
                                }
                        }
                }
	}       

        return(false);

} // End of method IsNewSession()

Thanks in advance and happy weekend.

 
Anil Varma:

Dear Fellows

Please help me to rectify the below code to get correct IsNewSession value when a new session has started.

I failed to resolve it.

Thanks in advance and happy weekend.

I am not sure what you are trying to achieve.

If the code were copy/paste to an mq5 file, I would maybe check it.

But this way, I have issues to go through and would like to point you to this thread:


Is that what you have been looking for??
 
Dominik Egert #:
I am not sure what you are trying to achieve.

Hi Dominik

I personally felt that, there was requirement of another for..loop inside the main loop to check TimeCross at [ith] index of main loop in say 120 counts, and return true if found AND continue to look for i-1th index in main loop.

I tried this, and current problem is RESOLVED. The revised code is as below, may be someone finds it useful.

bool CiVPBase::IsNewSession() {

        #define  _HoD(t) ((int)(((t)%86400)/3600))                // Hour of Day 2018.02.03 17:55:56 => (int) 17
        #define  _MoH(t) (int(((t)%3600)/60))                     // Minute of Current Hour 
        string vMethod = "[" + mSymbol + "," + TimeToString(TimeCurrent()) + "] " + __FUNCTION__;

        datetime vCurrCross = TimeCurrent();
        for(int i = ArraySize(TimeFxOpen)-1; i >= 0; i--) {
                // We need first PERIOD_M1 bar for TimeFxOpen[i] which is on PERIOD_H1. For Asian session and tSVR it starts at 01:01 Hrs
                int              idx                     = iBarShift(mSymbol,PERIOD_M1,TimeFxOpen[i]);
                datetime vPrevTime = iTime(mSymbol,PERIOD_M1,idx-1);

                for(int j = 0; j <= 120; j++) {

                        int                      vHour           = _HoD(TimeCurrent());                                                                                                         // Hour   value of CurrentTime
                        int                      vMinute         = _MoH(TimeCurrent());                                                                                                         // Minute value of CurrentTime
                        datetime vCurrTime = StringToTime(""+(string)YoY(TimeFxOpen[i])+"."+(string)MoY(TimeFxOpen[i])+"."+(string)DoM(TimeFxOpen[i])+" "+(string)vHour+":"+(string)vMinute);

                        if(cATS.checkTimeCross(vHour,vMinute,vCurrTime,vPrevTime)) {
                                if(mPrevCross != vCurrCross) {
                                        if(HoD(vPrevTime) == vHour) {
                                                vCurrCross = vCurrTime;
                                                mPrevCross = vCurrCross;
                                                return(true);
                                        }       
                                }
                        }
                }
        }       

        return(false);

} // End of method IsNewSession()

Thanks for your support and time. Regards.

Reason: