Esempio n. 1
0
        protected override void OnBar()
        {
            // This will keep track of a single long and short position by this robots
            Position longPosition  = Positions.Find(label, Symbol, TradeType.Buy);
            Position shortPosition = Positions.Find(label, Symbol, TradeType.Sell);

            double stopLoss   = Symbol.Ask - 11 * Symbol.PipSize;
            double takeProfit = Symbol.Ask + 100 * Symbol.PipSize;

            if (longPosition != null)
            {
                if (longPosition.Pips > 30.0)
                {
                    ModifyPosition(longPosition, stopLoss, takeProfit);
                }
            }
            if (shortPosition != null)
            {
                if (shortPosition.Pips < 30.0)
                {
                    ModifyPosition(shortPosition, stopLoss, takeProfit);
                }
            }

            //
            double lastShortMA    = shortMA.Result.Last(1);
            double lastLongMA     = longMA.Result.Last(1);
            double twoLastShortMA = shortMA.Result.Last(3);
            double twoLastLongMA  = longMA.Result.Last(3);

            //&&  previousSlowMa > previousFastMa

            if (lastShortMA > lastLongMA && twoLastShortMA < twoLastLongMA)
            {
                if (shortPosition != null)
                {
                    ClosePosition(shortPosition);
                }
                if (longPosition == null && SourceSeries.Last(1) > lastShortMA)
                {
                    ExecuteMarketOrder(TradeType.Buy, Symbol, 2000, label, 17, 200, 2, "Nothing");
                }
            }
            //&& previousSlowMa < previousFastMa
            else if (lastShortMA < lastLongMA && twoLastShortMA > twoLastLongMA)
            {
                if (longPosition != null)
                {
                    ClosePosition(longPosition);
                }
                if (shortPosition == null && SourceSeries.Last(1) < lastShortMA)
                {
                    ExecuteMarketOrder(TradeType.Sell, Symbol, 2000, label, 17, 200, 2, "Nothing");
                }
            }
        }
Esempio n. 2
0
        protected override bool HasBullishSignal()
        {
            var close = SourceSeries.Last(1);

            // Price must be > slow EMA
            if (close <= _slowMA.Result.Last(1))
            {
                return(false);
            }

            // The close must have crossed above the upper Bollinger band
            if (!SourceSeries.HasCrossedAbove(_bollingerBands.Top, 1))
            {
                return(false);
            }

            // Ensure price has been between the 2 bands
            for (var index = 2; index < 10; index++)
            {
                var price = SourceSeries.Last(index);
                if (price > _bollingerBands.Top.Last(index) || price < _bollingerBands.Bottom.Last(index))
                {
                    return(false);
                }
            }

            // Measure Band volatility - we want it to have contracted
            if (!HaveBollingerBandsContracted())
            {
                return(false);
            }

            // The close must be > open and the close must be near the high and the open near the low
            if (!ApplyCandleFilter(true))
            {
                return(false);
            }

            ApplyWeighting(true);

            return(true);
        }
Esempio n. 3
0
        private bool ApplyCandleFilter(bool isLong)
        {
            var currentLow    = Bars.LowPrices.Last(1);
            var currentHigh   = Bars.HighPrices.Last(1);
            var currentOpen   = Bars.OpenPrices.Last(1);
            var closeFromHigh = currentHigh - SourceSeries.Last(1);
            var openFromLow   = currentOpen - currentLow;
            var range         = currentHigh - currentLow;

            if (isLong)
            {
                // The close must be > open and the close must be near the high and the open near the low
                return((closeFromHigh / range <= 0.2) && (openFromLow / range <= 0.2));
            }
            else
            {
                // The close must be < open and the close must be near the low and the open near the high
                return((closeFromHigh / range >= 0.8) && (openFromLow / range >= 0.8));
            }
        }
Esempio n. 4
0
        protected override bool HasBearishSignal()
        {
            var close = SourceSeries.Last(1);

            // Price must be < slow EMA
            if (close >= _slowMA.Result.Last(1))
            {
                return(false);
            }

            // The close must have crossed below the lower Bollinger band
            if (!SourceSeries.HasCrossedBelow(_bollingerBands.Bottom, 1))
            {
                return(false);
            }

            // Have we had any other crosses recently?
            for (var index = 2; index < 10; index++)
            {
                var price = SourceSeries.Last(index);
                if (price > _bollingerBands.Top.Last(index) || price < _bollingerBands.Bottom.Last(index))
                {
                    return(false);
                }
            }

            // Measure Band volatility - we want it to have contracted
            if (!HaveBollingerBandsContracted())
            {
                return(false);
            }

            if (!ApplyCandleFilter(false))
            {
                return(false);
            }

            ApplyWeighting(true);

            return(true);
        }