Description
This bot is a simple RSI bot that buys when oversold and sells when overbought.
Moving average is added in so only long trades are made when above the moving average and only short trades when below it.
RSI overbought, oversold, and MA variables are all changeable.
using cAlgo.API;
using cAlgo.API.Indicators;
namespace cAlgo.Robots
{
[Robot(AccessRights = AccessRights.None)]
public class RsiMa : Robot
{
private Position _position;
private MovingAverage ma;
private RelativeStrengthIndex rsi;
[Parameter("Source")]
public DataSeries Source { get; set; }
[Parameter("RSI Periods", DefaultValue = 14)]
public int Periods { get; set; }
[Parameter("RSI Overbought", DefaultValue = 70)]
public int Overbought { get; set; }
[Parameter("RSI Oversold", DefaultValue = 30)]
public int Oversold { get; set; }
[Parameter("MA Period", DefaultValue = 200)]
public int MAPeriod { get; set; }
[Parameter("Volume", DefaultValue = 1000, MinValue = 0)]
public int Volume { get; set; }
[Parameter("MA Type")]
public MovingAverageType MAType { get; set; }
protected override void OnStart()
{
rsi = Indicators.RelativeStrengthIndex(Source, Periods);
ma = Indicators.MovingAverage(Source, MAPeriod, MAType);
}
protected override void OnBar()
{
if (Trade.IsExecuting)
return;
if (rsi.Result.LastValue < Oversold && (ma.Result.LastValue < MarketSeries.Close.LastValue) && _position == null)
{
OpenPosition(TradeType.Buy);
}
if (rsi.Result.LastValue > Overbought && (ma.Result.LastValue > MarketSeries.Close.LastValue) && _position == null)
{
OpenPosition(TradeType.Sell);
}
if (_position != null)
{
if (_position.TradeType == TradeType.Buy && rsi.Result.LastValue > Overbought)
{
Trade.Close(_position);
}
if (_position.TradeType == TradeType.Sell && rsi.Result.LastValue < Oversold)
{
Trade.Close(_position);
}
}
}
private void OpenPosition(TradeType command)
{
Trade.CreateMarketOrder(command, Symbol, Volume);
}
protected override void OnPositionOpened(Position openedPosition)
{
_position = openedPosition;
}
protected override void OnPositionClosed(Position closedPosition)
{
_position = null;
}
}
}
Mr4x
Joined on 12.04.2019
- Distribution: Free
- Language: C#
- Trading platform: cTrader Automate
- File name: RSI Bot with MA.algo
- Rating: 0
- Installs: 3277
- Modified: 30/12/2021 13:17
Comments
Hello! I need to be able to open 0.1 lots if possible, can anyone help please? Minimum volume is 1 lot.
hi to everyone
i have a problem, with that bot
there are very much warning, do you hsve same problem? and
how resolve that? thanks
dario
Hello Mr4x
Anyway of adding a code for max spreads
Can this bot be run on many symbol instances at same time?
If you add a LABEL parameter, that will allow the bot run on same symbol with different timeframe.
Thanks
Anyone found good parameters, trading instruments, and timeframe for this bot? Thanks
The added moving average filter helps in increasing the overall profitability of the indicator. The ability to customize the values of indicators is also great. Thank you for sharing the code.
Hello Mr4x,
First thanks for your sharing. I have good backtests with it.
Can you add an SL when the price crosses and closes after the MA?
For example:
The robot opens a buy position, but the price crosses the MA in the opposite direction. In this case the robot places a custom SL of (x) pips chosen by the user.
In this way it is possible to avoid very large losses.
Thanks
Thanks you
Feel free to share successful parameters and trading instruments if anybody manages to make this work for them ;)
Here is the code with SL and TP. Just be aware that if SL or TP is hit whilst RSI value is still under "oversold" or over "overbought" then it will add a new position when the old one is closed out. To disable SL and TP just set a very high SL and TP like 10000.
using cAlgo.API;
using cAlgo.API.Indicators;
namespace cAlgo.Robots
{
[Robot(AccessRights = AccessRights.None)]
public class RSIBotwithMA : Robot
{
private Position _position;
private MovingAverage ma;
private RelativeStrengthIndex rsi;
[Parameter("Source")]
public DataSeries Source { get; set; }
[Parameter("RSI Periods", DefaultValue = 14)]
public int Periods { get; set; }
[Parameter("RSI Overbought", DefaultValue = 70)]
public int Overbought { get; set; }
[Parameter("RSI Oversold", DefaultValue = 30)]
public int Oversold { get; set; }
[Parameter("MA Period", DefaultValue = 200)]
public int MAPeriod { get; set; }
[Parameter("Volume", DefaultValue = 1000, MinValue = 0)]
public int Volume { get; set; }
[Parameter("Take Profit", DefaultValue = 100, MinValue = 1)]
public int TP { get; set; }
[Parameter("Stop Loss", DefaultValue = 100, MinValue = 1)]
public int SL { get; set; }
[Parameter("MA Type")]
public MovingAverageType MAType { get; set; }
protected override void OnStart()
{
rsi = Indicators.RelativeStrengthIndex(Source, Periods);
ma = Indicators.MovingAverage(Source, MAPeriod, MAType);
}
protected override void OnBar()
{
if (Trade.IsExecuting)
return;
if (rsi.Result.LastValue < Oversold && (ma.Result.LastValue < MarketSeries.Close.LastValue) && _position == null)
{
OpenPosition(TradeType.Buy);
}
if (rsi.Result.LastValue > Overbought && (ma.Result.LastValue > MarketSeries.Close.LastValue) && _position == null)
{
OpenPosition(TradeType.Sell);
}
if (_position != null)
{
if (_position.TradeType == TradeType.Buy && rsi.Result.LastValue > Overbought)
{
Trade.Close(_position);
}
if (_position.TradeType == TradeType.Sell && rsi.Result.LastValue < Oversold)
{
Trade.Close(_position);
}
}
}
private void OpenPosition(TradeType command)
{
ExecuteMarketOrder(TradeType.Buy, SymbolName, Volume, null, SL, TP);
}
protected override void OnPositionOpened(Position openedPosition)
{
_position = openedPosition;
}
protected override void OnPositionClosed(Position closedPosition)
{
_position = null;
}
}
}
Any way of adding SL and TP pips parameters?
Thank you
I am getting so many great results with Brent Crude Oil.
TP and SL in pips would be a great option!