示例#1
0
        public Session(int aSessionID, string aSessionName, DateTime aSessionStartDateTime, DateTime aSessionEndDateTime, DateTime aHHLL_ReferenceDateTime, bool tradingFlag, int aHHLLThreshold, ATR_Type atrType, MqlApi mql4) : base(mql4)
        {
            this.sessionID = aSessionID;
            this.sessionName = aSessionName;
            this.sessionStartDateTime = aSessionStartDateTime;
            this.sessionEndDateTime = aSessionEndDateTime;
            this.HHLL_ReferenceDateTime = aHHLL_ReferenceDateTime;
            this.isTradingAllowed = tradingFlag;
            this.HHLL_Threshold = aHHLLThreshold;
            this.highestHigh = -1;
            this.lowestLow = 9999999999;
            this.atrType = atrType;

            initialize();
        }
        //This will only work for FXCM properly. Or any broker with exactly 5 trading days. 
        public static Session getCurrentSession(int aLengthOfSundaySession, int aHHLL_Threshold, int lookBackSessions, ATR_Type atrType, MqlApi mql4)
        {
            //TODO Change Session length determination logic
            int aLengthOfSundaySessionInHours = TimeSpan.FromSeconds(aLengthOfSundaySession).Hours;
            System.DateTime weekStartTime = mql4.iTime(mql4.Symbol(), MqlApi.PERIOD_H1, detectWeekStartShift(mql4));
            System.DateTime currentTime = mql4.TimeCurrent();

            //System.DateTime startOfCurrentSession = mql4.iTime(mql4.Symbol(), MqlApi.PERIOD_D1, 0);
            System.DayOfWeek weekday = currentTime.DayOfWeek;
            int weekEndDelay;
            if (weekendOverlap(weekday, lookBackSessions)) weekEndDelay = 2;
            else weekEndDelay = 0;

            //if daily bar is still in the old session - return. Waii until it updates.
            if ((currentSession != null) && (currentSession.getID() != 1) && (currentTime.DayOfWeek == mql4.iTime(mql4.Symbol(), MqlApi.PERIOD_D1, 0).DayOfWeek))
            {
                return currentSession;
            }

            switch (currentTime.DayOfWeek)
            {
                case DayOfWeek.Monday:
                    {
                        if ((currentSession == null) || (currentSession.getID() != 1))
                        {
                            System.DateTime dailyBarStart = mql4.iTime(mql4.Symbol(), MqlApi.PERIOD_D1, 0); 
                            ///Take out session end time. It's hard to calculate and not used currently. 
                            currentSession = new Session(1, "MONDAY", mql4.iTime(mql4.Symbol(), MqlApi.PERIOD_D1, 0), new DateTime(), mql4.iTime(mql4.Symbol(), MqlApi.PERIOD_D1, 0) - TimeSpan.FromDays(lookBackSessions+weekEndDelay), true, aHHLL_Threshold, atrType, mql4);
                        }
                        break;
                    }
                case DayOfWeek.Tuesday:
                    {
                        if ((currentSession == null) || (currentSession.getID() != 2))
                        {
                            ///Take out session end time. It's hard to calculate and not used currently. 
                            currentSession = new Session(2, "TUESDAY", mql4.iTime(mql4.Symbol(), MqlApi.PERIOD_D1, 0), new DateTime(), mql4.iTime(mql4.Symbol(), MqlApi.PERIOD_D1, 1) - TimeSpan.FromDays(lookBackSessions + weekEndDelay-1), true, aHHLL_Threshold, atrType, mql4);
                        }
                        break;

                    }
                case DayOfWeek.Wednesday:
                    {
                        if ((currentSession == null) || (currentSession.getID() != 3))
                        {
                            ///Take out session end time. It's hard to calculate and not used currently. 
                            currentSession = new Session(3, "WEDNESDAY", mql4.iTime(mql4.Symbol(), MqlApi.PERIOD_D1, 0), new DateTime(), mql4.iTime(mql4.Symbol(), MqlApi.PERIOD_D1, 1) - TimeSpan.FromDays(lookBackSessions + weekEndDelay-1), true, aHHLL_Threshold, atrType, mql4);
                        }
                        break;

                    }
                case DayOfWeek.Thursday:
                    {
                        if ((currentSession == null) || (currentSession.getID() != 4))
                        {
                            ///Take out session end time. It's hard to calculate and not used currently. 
                            currentSession = new Session(4, "THURSDAY", mql4.iTime(mql4.Symbol(), MqlApi.PERIOD_D1, 0), new DateTime(), mql4.iTime(mql4.Symbol(), MqlApi.PERIOD_D1, 1) - TimeSpan.FromDays(lookBackSessions + weekEndDelay-1), true, aHHLL_Threshold, atrType, mql4);
                        }
                        break;

                    }
                case DayOfWeek.Friday:
                    {
                        if ((currentSession == null) || (currentSession.getID() != 5))
                        {
                            ///Take out session end time. It's hard to calculate and not used currently. 
                            currentSession = new Session(5, "FRIDAY", mql4.iTime(mql4.Symbol(), MqlApi.PERIOD_D1, 0), new DateTime(), mql4.iTime(mql4.Symbol(), MqlApi.PERIOD_D1, 1) - TimeSpan.FromDays(lookBackSessions + weekEndDelay-1), true, aHHLL_Threshold, atrType, mql4);
                        }
                            break;
                    }

                default:
                    {
                        if ((currentSession == null) || (currentSession.getID() != -1))
                        {
                            currentSession = new Session(-1, "UNKNOWN", new DateTime(), new DateTime(), new DateTime(), false, 0, atrType, mql4);
                        }
                            break;
                    }
            }
            return currentSession;
    }