RSI to only use data from closed candles

Created at 05 Jun 2021, 20:29
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!
TH

thelightingfast9

Joined 01.03.2021

RSI to only use data from closed candles
05 Jun 2021, 20:29


Hello

Could somebody transfer the code, so the bot would only take RSI data from previously closed candles instead of the ones that are still open? 

It leads to too many incorrectly opened positions resulting in a loss.

 

// -------------------------------------------------------------------------------------------------
//
//    This code is a cAlgo API sample.
//
//    This robot is intended to be used as a sample and does not guarantee any particular outcome or
//    profit of any kind. Use it at your own risk.
//
//    All changes to this file will be lost on next application start.
//    If you are going to modify this file please make a copy using the "Duplicate" command.
//
//    The "Sample RSI Range Robot" will create a buy order when the Relative Strength Index indicator crosses the  level 30, 
//    and a Sell order when the RSI indicator crosses the level 70. The order is closed be either a Stop Loss, defined in 
//    the "Stop Loss" parameter, or by the opposite RSI crossing signal (buy orders close when RSI crosses the 70 level 
//    and sell orders are closed when RSI crosses the 30 level). 
//
//    The robot can generate only one Buy or Sell order at any given time.
//
// -------------------------------------------------------------------------------------------------

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

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

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

        [Parameter("Volume", DefaultValue = 10000, MinValue = 100)]
        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 < 55)
            {
                Open(TradeType.Buy);
            }
            else if (rsi.Result.LastValue > 70)
            {
                Close(TradeType.Buy);
            }


            if (rsi.Result.LastValue < 45)
            {
                Open(TradeType.Sell);
            }
            else if (rsi.Result.LastValue > 30)
            {
                Close(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");
        }
    }
}


@thelightingfast9
Replies

PanagiotisCharalampous
07 Jun 2021, 08:53

Hi thelightingfast9,

You just need to change LastValue to Last(1).

Best Regards,

Panagiotis 

Join us on Telegram and Facebook


@PanagiotisCharalampous

thelightingfast9
07 Jun 2021, 19:00 ( Updated at: 07 Jun 2021, 19:02 )

Hello.

Thanks for your help.

I have another issue, where bot does not close positions as instructed. 

It opens position when RSI goes above set number, but it doesn't close the position when RSI falls below the set level. for an example, on closing BUY position it should not allow RSI fall below 55 without closing the position, and it does.. at some instances, RSI drops on the floor and bot does not close the positions, it's rather random closing.

I've tried looking over forum and internet but no help.

Could somebody help with this, and possibly let me know how to inser "hascrossedabove" and "hascrossedbelow" into this code, in order for RSI to be following it and working?

Thanks!

 

// -------------------------------------------------------------------------------------------------
//
//    This code is a cAlgo API sample.
//
//    This robot is intended to be used as a sample and does not guarantee any particular outcome or
//    profit of any kind. Use it at your own risk.
//
//    All changes to this file will be lost on next application start.
//    If you are going to modify this file please make a copy using the "Duplicate" command.
//
//    The "Sample RSI Range Robot" will create a buy order when the Relative Strength Index indicator crosses the  level 30, 
//    and a Sell order when the RSI indicator crosses the level 70. The order is closed be either a Stop Loss, defined in 
//    the "Stop Loss" parameter, or by the opposite RSI crossing signal (buy orders close when RSI crosses the 70 level 
//    and sell orders are closed when RSI crosses the 30 level). 
//
//    The robot can generate only one Buy or Sell order at any given time.
//
// -------------------------------------------------------------------------------------------------

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

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

        [Parameter("Take_Profit", DefaultValue = 180, MinValue = 1)]
        public int TakeProfit { get; set; }

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

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

        private RelativeStrengthIndex rsi;

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


        protected override void OnTick()
        {
            if (rsi.Result.Last(1) <= 60)
            {
                Open(TradeType.Buy);
            }
            else if (rsi.Result.Last(0) >= 55)
            {
                Close(TradeType.Buy);
            }


            if (rsi.Result.Last(1) <= 40)
            {
                Open(TradeType.Sell);
            }
            else if (rsi.Result.Last(0) >= 45)
            {
                Close(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");
        }
    }
}


@thelightingfast9

amusleh
08 Jun 2021, 11:54

Hi,

Try to put your entry code inside OnBar method, not on OnTick, right now your bot opens a new position on each tick if conditions were ok.

And put your closing code inside OnTick method, as you want to close as soon as possible when the conditions were met.


@amusleh