RSI bot with MA free

by Mr4x in category Trend at 30/12/2021
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.

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 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;
        }
    }
}
Comments

evgrinaus - January 06, 2022 @ 08:40

Thank you

I am getting so many great results with Brent Crude Oil.

 

evgrinaus - January 06, 2022 @ 09:58

Any way of adding SL and TP pips parameters?

 

Mr4x - January 06, 2022 @ 13:57

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;
        }
    }
}

Mr4x - January 06, 2022 @ 13:58

Feel free to share successful parameters and trading instruments if anybody manages to make this work for them ;)

evgrinaus - January 07, 2022 @ 10:37

Thanks you

jangdongtwo - January 07, 2022 @ 13:21

You can play Slope Unblocked free here. You’ll need to steer your ball using the arrow keys, while taking care to avoid obstacles and not bump into red walls.

paulomsduarte89 - January 24, 2022 @ 12:49

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

shamsulzoha5 - February 10, 2022 @ 11:47

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.

codey - February 19, 2022 @ 22:15

Anyone found good parameters, trading instruments, and timeframe for this bot?  Thanks

codey - February 19, 2022 @ 22:39

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

bjhlkagnew - April 22, 2022 @ 10:03

Hello Mr4x

Anyway of adding a code for max spreads 

dboffis - May 24, 2022 @ 18:36

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

sado.methodist - June 11, 2022 @ 03:57

Hello! I need to be able to open 0.1 lots if possible, can anyone help please? Minimum volume is 1 lot.

lauralordelaure - September 30, 2022 @ 10:22

Best Binance Trading Bots · Binance Catch wordle the Falling Knife · Binance Golden Cross Trading · Binance Moving Average · Binance Range Trading · Binance RSI · Binance

0