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)
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);
}
}
}
}
}
Cos2
Joined on 14.08.2020
- Distribution: Free
- Language: C#
- Trading platform: cTrader Automate
- File name: RSI and BollingerBands Retracement.algo
- Rating: 5
- Installs: 3187
- Modified: 13/10/2021 09:54
Comments
The trading volume is 10000. I want to use 0.01 lot but it won't allow it.
Hi, I do like what you have done, i have been working on a similar bot with another code for MT5 which lead to excellent results, i would like to ask you if you would like to consider a discussion to be able to modify the code you have done to provide better results. let me know
bave.rowe28@gmail.com
looking forward to hearing from you
Thanks a lot, well done
this somehow the same strategy that I have in mind and was looking for a coder to develop. but instead of RSI I look at CCI...