Esempio n. 1
0
        protected void Init(
            bool takeLongsParameter,
            bool takeShortsParameter,
            string initialStopLossRule,
            string trailingStopLossRule,
            string lotSizingRule,
            int initialStopLossInPips = 0,
            int takeProfitInPips      = 0)
        {
            _takeLongsParameter    = takeLongsParameter;
            _takeShortsParameter   = takeShortsParameter;
            _initialStopLossRule   = (StopLossRule)Enum.Parse(typeof(StopLossRule), initialStopLossRule);
            _trailingStopLossRule  = (StopLossRule)Enum.Parse(typeof(StopLossRule), trailingStopLossRule);
            _lotSizingRule         = (LotSizingRule)Enum.Parse(typeof(LotSizingRule), lotSizingRule);
            _initialStopLossInPips = initialStopLossInPips;
            _takeProfitInPips      = takeProfitInPips;

            _canOpenPosition = true;

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

            Print("Symbol.TickSize: {0}, Symbol.Digits: {1}, Symbol.PipSize: {2}",
                  Symbol.TickSize, Symbol.Digits, Symbol.PipSize);
        }
Esempio n. 2
0
        public override StopValue GetStopValue(StopLossRule rule, double profitPercentage)
        {
            if (profitPercentage > rule.UpperProfitPercentage)
            {
                // Use a very tight stop
                return(new StopValue
                {
                    TrailingPercent = 1
                });
            }

            // Use an equation that produces a parabola curve
            // to move the stop closer the more profit we make
            var height  = rule.UpperProfitPercentage - rule.LowerProfitPercentage;
            var divisor = height / 3;  // 3 works for the lower/upper of 10/28

            var scale = rule.Percentage.Value;
            var x     = (profitPercentage - scale) / divisor;

            x *= x;
            var y = scale - x;

            return(new StopValue
            {
                TrailingPercent = y
            });
        }
Esempio n. 3
0
 public override StopValue GetStopValue(StopLossRule rule, double percentage)
 {
     return(new StopValue
     {
         TrailingPercent = rule.Percentage.Value
     });
 }
Esempio n. 4
0
        public void IsSatisfied()
        {
            decimal tradedAmount = Decimals.ONE;

            // 5% stop-loss
            StopLossRule rule = new StopLossRule(closePrice, 5);

            Assert.IsFalse(rule.IsSatisfied(0, null));
            Assert.IsFalse(rule.IsSatisfied(1, tradingRecord));

            // Enter at 114
            tradingRecord.Enter(2, 114, tradedAmount);
            Assert.IsFalse(rule.IsSatisfied(2, tradingRecord));
            Assert.IsFalse(rule.IsSatisfied(3, tradingRecord));
            Assert.IsTrue(rule.IsSatisfied(4, tradingRecord));
            // Exit
            tradingRecord.Exit(5);

            // Enter at 128
            tradingRecord.Enter(5, 128, tradedAmount);
            Assert.IsFalse(rule.IsSatisfied(5, tradingRecord));
            Assert.IsTrue(rule.IsSatisfied(6, tradingRecord));
            Assert.IsTrue(rule.IsSatisfied(7, tradingRecord));
        }
Esempio n. 5
0
        public virtual void IsSatisfied()
        {
            Decimal tradedAmount = Decimal.One;

            // 5% stop-loss
            _rule = new StopLossRule(_closePrice, Decimal.ValueOf("5"));

            Assert.IsFalse(_rule.IsSatisfied(0, null));
            Assert.IsFalse(_rule.IsSatisfied(1, _tradingRecord));

            // Enter at 114
            _tradingRecord.Enter(2, Decimal.ValueOf("114"), tradedAmount);
            Assert.IsFalse(_rule.IsSatisfied(2, _tradingRecord));
            Assert.IsFalse(_rule.IsSatisfied(3, _tradingRecord));
            Assert.IsTrue(_rule.IsSatisfied(4, _tradingRecord));
            // Exit
            _tradingRecord.Exit(5);

            // Enter at 128
            _tradingRecord.Enter(5, Decimal.ValueOf("128"), tradedAmount);
            Assert.IsFalse(_rule.IsSatisfied(5, _tradingRecord));
            Assert.IsTrue(_rule.IsSatisfied(6, _tradingRecord));
            Assert.IsTrue(_rule.IsSatisfied(7, _tradingRecord));
        }
Esempio n. 6
0
 public abstract StopValue GetStopValue(StopLossRule rule, double percentage);
Esempio n. 7
0
 public override StopValue GetStopValue(StopLossRule rule, double percentage)
 {
     return(null);
 }