Exemplo n.º 1
0
        protected override void ValidateParameters(
            bool takeLongsParameter,
            bool takeShortsParameter,
            InitialStopLossRuleValues initialStopLossRule,
            int initialStopLossInPips,
            TrailingStopLossRuleValues trailingStopLossRule,
            int trailingStopLossInPips,
            LotSizingRuleValues lotSizingRule,
            TakeProfitRuleValues takeProfitRule,
            int takeProfitInPips,
            int minutesToWaitAfterPositionClosed,
            bool moveToBreakEven,
            bool closeHalfAtBreakEven,
            double dynamicRiskPercentage,
            int barsToAllowTradeToDevelop)
        {
            base.ValidateParameters(
                takeLongsParameter,
                takeShortsParameter,
                initialStopLossRule,
                initialStopLossInPips,
                trailingStopLossRule,
                trailingStopLossInPips,
                lotSizingRule,
                takeProfitRule,
                takeProfitInPips,
                minutesToWaitAfterPositionClosed,
                moveToBreakEven,
                closeHalfAtBreakEven,
                dynamicRiskPercentage,
                barsToAllowTradeToDevelop);

            if (FastPeriodParameter <= 0 || FastPeriodParameter > 999)
            {
                throw new ArgumentException("Invalid 'Fast MA Period' - must be between 1 and 999");
            }

            if (MediumPeriodParameter <= 0 || MediumPeriodParameter > 999)
            {
                throw new ArgumentException("Invalid 'Medium MA Period' - must be between 1 and 999");
            }

            if (SlowPeriodParameter <= 0 || SlowPeriodParameter > 999)
            {
                throw new ArgumentException("Invalid 'Slow MA Period' - must be between 1 and 999");
            }

            if (!(FastPeriodParameter < MediumPeriodParameter && MediumPeriodParameter < SlowPeriodParameter))
            {
                throw new ArgumentException("Invalid 'MA Periods' - fast must be less than medium and medium must be less than slow");
            }

            var initialSLRule  = (InitialStopLossRuleValues)initialStopLossRule;
            var trailingSLRule = (TrailingStopLossRuleValues)trailingStopLossRule;

            if (MaCrossRule == MaCrossRuleValues.None && initialSLRule == InitialStopLossRuleValues.None && trailingSLRule == TrailingStopLossRuleValues.None)
            {
                throw new ArgumentException("The combination of parameters means that a position may incur a massive loss");
            }
        }
Exemplo n.º 2
0
        protected void Init(
            bool takeLongsParameter,
            bool takeShortsParameter,
            MaCrossRuleValues maCrossRule,
            LotSizingRuleValues lotSizingRule,
            double dynamicRiskPercentage)
        {
            _takeLongsParameter    = takeLongsParameter;
            _takeShortsParameter   = takeShortsParameter;
            _maCrossRule           = maCrossRule;
            _lotSizingRule         = lotSizingRule;
            _dynamicRiskPercentage = dynamicRiskPercentage;
            var lotSizing = (LotSizingRuleValues)lotSizingRule;

            if (lotSizing == LotSizingRuleValues.Dynamic && (dynamicRiskPercentage <= 0 || dynamicRiskPercentage >= 10))
            {
                throw new ArgumentOutOfRangeException($"Dynamic Risk value is out of range - it is a percentage (e.g. 2)");
            }

            _canOpenPosition = true;

            Positions.Opened += OnPositionOpened;
            Positions.Closed += OnPositionClosed;

            Print("Symbol.TickSize: {0}, Symbol.Digits: {1}, Symbol.PipSize: {2}",
                  Symbol.TickSize, Symbol.Digits, Symbol.PipSize);
        }
Exemplo n.º 3
0
        protected void Init(
            bool takeLongsParameter,
            bool takeShortsParameter,
            InitialStopLossRuleValues initialStopLossRule,
            int initialStopLossInPips,
            TrailingStopLossRuleValues trailingStopLossRule,
            int trailingStopLossInPips,
            LotSizingRuleValues lotSizingRule,
            TakeProfitRuleValues takeProfitRule,
            int takeProfitInPips = 0,
            int minutesToWaitAfterPositionClosed = 0,
            bool moveToBreakEven          = false,
            bool closeHalfAtBreakEven     = false,
            double dynamicRiskPercentage  = 2,
            int barsToAllowTradeToDevelop = 0)
        {
            ValidateParameters(takeLongsParameter, takeShortsParameter, initialStopLossRule, initialStopLossInPips,
                               trailingStopLossRule, trailingStopLossInPips, lotSizingRule, takeProfitRule, takeProfitInPips,
                               minutesToWaitAfterPositionClosed, moveToBreakEven, closeHalfAtBreakEven, dynamicRiskPercentage, barsToAllowTradeToDevelop);

            _takeLongsParameter               = takeLongsParameter;
            _takeShortsParameter              = takeShortsParameter;
            _initialStopLossRule              = initialStopLossRule;
            _initialStopLossInPips            = initialStopLossInPips;
            _trailingStopLossRule             = trailingStopLossRule;
            _trailingStopLossInPips           = trailingStopLossInPips;
            _lotSizingRule                    = lotSizingRule;
            _takeProfitRule                   = takeProfitRule;
            _takeProfitInPips                 = takeProfitInPips;
            _minutesToWaitAfterPositionClosed = minutesToWaitAfterPositionClosed;
            _moveToBreakEven                  = moveToBreakEven;
            _closeHalfAtBreakEven             = closeHalfAtBreakEven;
            _dynamicRiskPercentage            = dynamicRiskPercentage;
            _barsToAllowTradeToDevelop        = barsToAllowTradeToDevelop;

            _canOpenPosition = true;

            Positions.Opened   += OnPositionOpened;
            Positions.Closed   += OnPositionClosed;
            Positions.Modified += OnPositionModified;

            Print("Symbol.TickSize: {0}, Symbol.Digits: {1}, Symbol.PipSize: {2}",
                  Symbol.TickSize, Symbol.Digits, Symbol.PipSize);

            AttachExistingPosition();
            _onBar = false;
        }
Exemplo n.º 4
0
        protected virtual void ValidateParameters(
            bool takeLongsParameter,
            bool takeShortsParameter,
            InitialStopLossRuleValues initialStopLossRule,
            int initialStopLossInPips,
            TrailingStopLossRuleValues trailingStopLossRule,
            int trailingStopLossInPips,
            LotSizingRuleValues lotSizingRule,
            TakeProfitRuleValues takeProfitRule,
            int takeProfitInPips,
            int minutesToWaitAfterPositionClosed,
            bool moveToBreakEven,
            bool closeHalfAtBreakEven,
            double dynamicRiskPercentage,
            int barsToAllowTradeToDevelop)
        {
            if (!takeLongsParameter && !takeShortsParameter)
            {
                throw new ArgumentException("Must take at least longs or shorts");
            }

            if (!Enum.IsDefined(typeof(InitialStopLossRuleValues), initialStopLossRule))
            {
                throw new ArgumentException("Invalid initial stop loss rule");
            }

            if (!Enum.IsDefined(typeof(TrailingStopLossRuleValues), trailingStopLossRule))
            {
                throw new ArgumentException("Invalid trailing stop loss rule");
            }

            if (initialStopLossInPips < 0 || initialStopLossInPips > 999)
            {
                throw new ArgumentException("Invalid initial stop loss - must be between 0 and 999");
            }

            if (trailingStopLossInPips < 0 || trailingStopLossInPips > 999)
            {
                throw new ArgumentException("Invalid trailing stop loss - must be between 0 and 999");
            }

            if (!Enum.IsDefined(typeof(LotSizingRuleValues), lotSizingRule))
            {
                throw new ArgumentException("Invalid lot sizing rule");
            }

            if (takeProfitInPips < 0 || takeProfitInPips > 999)
            {
                throw new ArgumentException("Invalid take profit - must be between 0 and 999");
            }

            if (!Enum.IsDefined(typeof(TakeProfitRuleValues), takeProfitRule))
            {
                throw new ArgumentException("Invalid take profit rule");
            }

            if (takeProfitRule != TakeProfitRuleValues.StaticPipsValue && takeProfitInPips != 0)
            {
                throw new ArgumentException("Invalid take profit - must be 0 when Take Profit Rule is not Static Pips");
            }

            if (minutesToWaitAfterPositionClosed < 0 || minutesToWaitAfterPositionClosed > 60 * 24)
            {
                throw new ArgumentException(string.Format("Invalid 'Pause after position closed' - must be between 0 and {0}", 60 * 24));
            }

            if (!moveToBreakEven && closeHalfAtBreakEven)
            {
                throw new ArgumentException("'Close half at breakeven?' is only valid when 'Move to breakeven?' is set");
            }

            if (lotSizingRule == LotSizingRuleValues.Dynamic && (dynamicRiskPercentage <= 0 || dynamicRiskPercentage > 5))
            {
                throw new ArgumentOutOfRangeException("Dynamic Risk value is out of range - it is a percentage (e.g. 2) between 0 and 5");
            }

            if (barsToAllowTradeToDevelop < 0 || barsToAllowTradeToDevelop > 99)
            {
                throw new ArgumentOutOfRangeException("BarsToAllowTradeToDevelop is out of range - must be between 0 and 99");
            }
        }