RSI bot amendment

Created at 10 Feb 2021, 17:37
How’s your experience with the cTrader Platform?
Your feedback is crucial to cTrader's development. Please take a few seconds to share your opinion and help us improve your trading experience. Thanks!
C1

C123dale

Joined 28.01.2021

RSI bot amendment
10 Feb 2021, 17:37


HI, I'm having trouble working out how to tweak an RSI bot i've built - I have it set to take some last quick pips once it crosses the Upper / Lower RSI, but I only want this to action once on every crossing I.E. One position opened when specified RSI is crossed and let it run for the specified TP / SL, then only open a new instance once the RSI has left the level and re-entered. At present my Bot is opening back to back positions after TP/SL is filled if still within the Lower or Upper RSI specified... Does anyone know the code to add to stop this happening? This is what I have so far, thanks - 

using System;
using System.Linq;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;

namespace cAlgo
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class RSIwithStopLossandTakeProfit : Robot
    {
        [Parameter("Quantity (Lots)", Group = "Volume", DefaultValue = 1, MinValue = 0.01, Step = 0.01)]
        public double Quantity { get; set; }

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

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

        [Parameter("Lower RSI Level", Group = "RSI", DefaultValue = 30, MinValue = 0, MaxValue = 50)]
        public int LRL { get; set; }

        [Parameter("Upper RSI Level", Group = "RSI", DefaultValue = 70, MinValue = 50, MaxValue = 100)]
        public int URL { get; set; }

        [Parameter("Take Profit in pips", Group = "TP SL", DefaultValue = 100)]
        public int TP { get; set; }

        [Parameter("Stop Loss in pips", Group = "TP SL", DefaultValue = 100)]
        public int SL { get; set; }

        private RelativeStrengthIndex rsi;

        protected override void OnStart()
        {
            rsi = Indicators.RelativeStrengthIndex(Source, Periods);
        }

        protected override void OnTick()
        {
            if (rsi.Result.LastValue < LRL)
            {
                Close(TradeType.Buy);
                Open(TradeType.Sell);
            }
            else if (rsi.Result.LastValue > URL)
            {
                Close(TradeType.Sell);
                Open(TradeType.Buy);
            }
        }

        private void Close(TradeType tradeType)
        {
            foreach (var position in Positions.FindAll("SampleRSI", SymbolName, tradeType))
                ClosePosition(position);
        }

        private void Open(TradeType tradeType)
        {
            var position = Positions.Find("SampleRSI", SymbolName, tradeType);
            var volumeInUnits = Symbol.QuantityToVolumeInUnits(Quantity);

            if (position == null)
                ExecuteMarketOrder(tradeType, SymbolName, volumeInUnits, "SampleRSI", SL, TP);
        }
    }
}  


@C123dale
Replies

PanagiotisCharalampous
11 Feb 2021, 15:15

Hi C123dale,

You should try using a flag when the RSI crosses on the other direction and place trades only when the flag is changing. See below an idea

            if (rsi.Result.LastValue < LRL && _isBullish)
            {
                Close(TradeType.Buy);
                Open(TradeType.Sell);
                _isBullish = false;
            }
            else if (rsi.Result.LastValue > URL && !_isBullish)
            {
                Close(TradeType.Sell);
                Open(TradeType.Buy);
                _isBullish = true;
            }

Best Regards,

Panagiotis 

Join us on Telegram


@PanagiotisCharalampous