RSI Cbot

Created at 19 Jan 2021, 21:42
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!
AM

amirrezaik

Joined 19.01.2021

RSI Cbot
19 Jan 2021, 21:42


Hi everyone,

In this Cbot when the RSI last value pass 70 or drop below 30 it open or close the the trade, I need to change it to when the candle stick is closed and then if the RSI value is over 70 or under 30 it open or close the trade.

Also the TP and SL is not working , I would appreciate if someone advice me 

Thanks



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 SampleRSIRobot : Robot
    {
        [Parameter("Source")]
        public DataSeries Source { get; set; }

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

        [Parameter("Stop Loss (pips)", DefaultValue = 100, MinValue = 1)]
        public int StopLoss { get; set; }

        [Parameter("Take Profit (pips)", DefaultValue = 100, MinValue = 1)]
        public int TakeProfit { get; set; }

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

        private RelativeStrengthIndex rsi;

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

        protected override void OnTick()
        {
            if (rsi.Result.LastValue < 30)
            {

                Close(TradeType.Sell);
                Open(TradeType.Buy);
            }
            else if (rsi.Result.LastValue > 70)
            {

                Close(TradeType.Buy);
                Open(TradeType.Sell);

            }
        }

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

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

            if (position == null)
                ExecuteMarketOrder(tradeType, Symbol, Volume, "SampleRSI");
        }
    }
}


@amirrezaik
Replies

PanagiotisCharalampous
20 Jan 2021, 08:09

Hi amirrezaik,

Try this

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 SampleRSIRobot : Robot
    {
        [Parameter("Source")]
        public DataSeries Source { get; set; }

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

        [Parameter("Stop Loss (pips)", DefaultValue = 100, MinValue = 1)]
        public int StopLoss { get; set; }

        [Parameter("Take Profit (pips)", DefaultValue = 100, MinValue = 1)]
        public int TakeProfit { get; set; }

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

        private RelativeStrengthIndex rsi;

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

        protected override void OnBar()
        {
            if (rsi.Result.LastValue < 30)
            {

                Close(TradeType.Sell);
                Open(TradeType.Buy);
            }
            else if (rsi.Result.LastValue > 70)
            {

                Close(TradeType.Buy);
                Open(TradeType.Sell);

            }
        }

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

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

            if (position == null)
                ExecuteMarketOrder(tradeType, Symbol, Volume, "SampleRSI", StopLoss, TakeProfit);
        }
    }
}

Best Regards,

Panagiotis 

Join us on Telegram


@PanagiotisCharalampous