示例#1
0
        public ATRTrade(bool sim, int _lotDigits, string _logFileName, double _newHHLL, double _ATR, int _lengthIn1MBarsOfWaitingPeriod, double _percentageOfATRForMaxRisk, double _percentageOfATRForMaxVolatility,
            double _minProfitTarget, int _rangeBufferInMicroPips, double _rangeRestriction, double _tenDayRange, Session referenceSession, double _maxBalanceRisk, MqlApi mql4) : base(sim, _lotDigits, _logFileName, mql4)
        {
            this.newHHLL = _newHHLL;
            this.atr = _ATR;
            this.lengthIn1MBarsOfWaitingPeriod = _lengthIn1MBarsOfWaitingPeriod;
            this.percentageOfATRForMaxRisk = _percentageOfATRForMaxRisk;
            this.percentageOfATRForMaxVolatility = _percentageOfATRForMaxVolatility;
            this.minProfitTarget = _minProfitTarget;
            this.rangeBufferInMicroPips = _rangeBufferInMicroPips;
            this.rangeRestriction = _rangeRestriction;

            this.rangeHigh = 0;
            this.rangeLow = 0;
            this.rangePips = 0;
            this.newHHLL = _newHHLL;
            this.tenDayRange = _tenDayRange;
            this.referenceSession = referenceSession;
            this.currentDailyRange = mql4.iHigh(null, MqlApi.PERIOD_D1, 0) - mql4.iLow(null, MqlApi.PERIOD_D1, 0);
            this.maxBalanceRisk = _maxBalanceRisk;
        }
        //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;
    }