RSI and Bollinger Bands Retracement free

by neokosta1 in category Other at 14/08/2020
Description

Sell when RSI is overbought and price crossed top Bollinger Bands line. Buy when RSI is oversold and price crossed bottom Bollinger Bands line.

 

This is the implementation of the strategy described in this video:

RESULTS ARE NOT AS PROMISED IN THE VIDEO :)

Entry for Sell:

  • Candle closes above Bollinger Bands top line
  • RSI is in overbought area (above 70)
  • Waiting for next candle to go down
  • Sell when that down candle closes

Entry for Buy uses opposite signals.

As there was no description for the exit strategy, added following rules to close positions:

  • Stop Loss level will be set as the difference between Bollinger Bands lines: Top - Main lines for Sell and Main-Bottom line for Buy
  • Position closes when price crosses opposite Bollinger Bands line (Bottom for Sell, Top for Buy)

 

Warning! Executing the following cBot may result in loss of funds. Use it at your own risk.
Notification Publishing copyrighted material is strictly prohibited. If you believe there is copyrighted material in this section you may use the Copyright Infringement Notification form to submit a claim.
Formula / Source Code
Language: C#
Trading Platform: cAlgo
´╗┐using System;
using System.Linq;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class RSIandBollingerBandsRetracement : Robot
    {

        [Parameter("Periods", DefaultValue = 20, Group = "Bollinger Bands")]
        public int BbPeriods { get; set; }

        [Parameter("Standart Dev", DefaultValue = 2, Group = "Bollinger Bands")]
        public double BbStdDev { get; set; }

        [Parameter("MA Type", DefaultValue = 0, Group = "Bollinger Bands")]
        public MovingAverageType BbMaType { get; set; }

        [Parameter("Source", Group = "Bollinger Bands")]
        public DataSeries BbSource { get; set; }

        [Parameter("Periods", DefaultValue = 14, Group = "RSI")]
        public int RsiPeriods { get; set; }

        [Parameter("Source", Group = "RSI")]
        public DataSeries RsiSource { get; set; }

        [Parameter("Overbought level", DefaultValue = 70, Group = "RSI")]
        public int RsiOverboughtLevel { get; set; }

        [Parameter("Oversold level", DefaultValue = 30, Group = "RSI")]
        public int RsiOversoldLevel { get; set; }

        [Parameter("Volume", DefaultValue = 10000, Group = "Trade")]
        public int Volume { get; set; }

        [Parameter("Robot label", DefaultValue = "RSI and BB", Group = "Orders management")]
        public string RobotLabel { get; set; }

        private BollingerBands BollingerBands;
        private RelativeStrengthIndex Rsi;

        protected override void OnStart()
        {
            BollingerBands = Indicators.BollingerBands(Bars.ClosePrices, BbPeriods, BbStdDev, BbMaType);
            Rsi = Indicators.RelativeStrengthIndex(Bars.ClosePrices, RsiPeriods);
        }

        protected override void OnBar()
        {
            var i = Bars.Count - 1;
            var prevBbTop = BollingerBands.Top[i - 2];
            var prevBbBottom = BollingerBands.Bottom[i - 2];
            var prevRsi = Rsi.Result[i - 2];
            if (double.IsNaN(prevBbTop) || double.IsNaN(prevBbBottom) || double.IsNaN(prevRsi))
                return;

            var prevClose = Bars.ClosePrices[i - 2];
            var prevHighOverBbTop = prevClose > prevBbTop;
            var prevLowBelowBbBottom = prevClose < prevBbBottom;

            if (prevHighOverBbTop && prevLowBelowBbBottom)
                return;

            var isClosedCandleUp = Bars.ClosePrices[i - 1] > Bars.OpenPrices[i - 1];

            var bbTop = BollingerBands.Top.LastValue;
            var bbBottom = BollingerBands.Bottom.LastValue;
            var bbMain = BollingerBands.Main.LastValue;

            if (prevHighOverBbTop && prevRsi > RsiOverboughtLevel && isClosedCandleUp == false)
            {
                var stopLossPips = (bbTop - bbMain) / Symbol.PipSize;
                OpenPosition(TradeType.Sell, stopLossPips);
            }
            else if (prevLowBelowBbBottom && prevRsi < RsiOversoldLevel && isClosedCandleUp == true)
            {
                var stopLossPips = (bbMain - bbBottom) / Symbol.PipSize;
                OpenPosition(TradeType.Buy, stopLossPips);
            }
        }

        protected override void OnTick()
        {

            var openedPosition = Positions.FirstOrDefault(p => p.SymbolName == SymbolName && p.Label == RobotLabel);
            if (openedPosition == null)
                return;

            if (openedPosition.TradeType == TradeType.Sell && Bid <= BollingerBands.Bottom.LastValue)
            {
                ClosePosition(openedPosition);
            }
            else if (openedPosition.TradeType == TradeType.Buy && Bid >= BollingerBands.Top.LastValue)
            {
                ClosePosition(openedPosition);
            }
        }

        private void OpenPosition(TradeType side, double stopLossPips)
        {
            if (Positions.Any(p => p.SymbolName == SymbolName && p.Label == RobotLabel))
                return;

            ExecuteMarketOrder(side, SymbolName, Volume, RobotLabel, stopLossPips, null);
        }

        protected override void OnStop()
        {
            if (RunningMode != RunningMode.RealTime)
            {
                foreach (var position in Positions)
                {
                    ClosePosition(position);
                }
            }
        }
    }
}
Comments
0