Help on RSI with BreakEven

Created at 03 Feb 2018, 16:35
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!
alexander.n.fedorov's avatar

alexander.n.fedorov

Joined 02.01.2018

Help on RSI with BreakEven
03 Feb 2018, 16:35


I am trying to write a bot on RSI. I also want to include a Break Even stop

When BE is not enabled backtesting is working fine. There is no problem with build

But when I try to enable BE in the log I receive the following:

"

" 01/09/2017 03:00:00.000 | Backtesting started
 01/09/2017 03:01:00.000 | Crashed in OnTick with NullReferenceException: Object reference not set to an instance of an object.
01/09/2017 03:01:00.000 | Backtesting was stopped"

 

Somebody, please tell me what do I do wrong!!!

Here is the complete code

"

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

namespace cAlgo
{
    [Robot(TimeZone = TimeZones.RussianStandardTime, AccessRights = AccessRights.FullAccess)]
    public class SampleRSIcBot : Robot
    {

        [Parameter("Envelopes - RSI", DefaultValue = "Envelopes - RSI")]
        public string InstanceName { get; set; }

        [Parameter("Source")]
        public DataSeries Source { get; set; }

        [Parameter("Stop Loss", DefaultValue = 20.0)]
        public double SL { get; set; }

        [Parameter("Take Profit", DefaultValue = 40.0)]
        public double TP { get; set; }

        [Parameter("BO", DefaultValue = 70)]
        public double High { get; set; }

        [Parameter("SO", DefaultValue = 30)]
        public double Low { get; set; }

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

        [Parameter("Quantity (Lots)", DefaultValue = 1, MinValue = 0.01, Step = 0.01)]
        public double Quantity { get; set; }

        [Parameter("Include Break-Even", DefaultValue = false)]
        public bool IncludeBreakEven { get; set; }

        [Parameter("Break-Even Trigger (pips)", DefaultValue = 10.0, MinValue = 1.0)]
        public double Trigger { get; set; }

        [Parameter("Break-Even Extra (pips)", DefaultValue = 2.0, MinValue = 0.0)]
        public double ExtraPips { get; set; }

        private RelativeStrengthIndex rsi;

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

        protected override void OnTick()
        {
            {
                if (rsi.Result.LastValue < Low)
                {
                    Open(TradeType.Buy);
                }
                else if (rsi.Result.LastValue > High)
                {
                    Open(TradeType.Sell);
                }
            }
            if (IncludeBreakEven == true)
                GoToBreakEven();
        }

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

        private void Open(TradeType tradeType)
        {
            var position = Positions.Find(InstanceName, Symbol, tradeType);
            var volumeInUnits = Symbol.QuantityToVolume(Quantity);

            if (position == null)
                ExecuteMarketOrder(tradeType, Symbol, volumeInUnits, InstanceName, SL, TP);
        }
        #region Break Even
        private void GoToBreakEven()
        {
            var position = Positions.Find(InstanceName, Symbol);
            var entryPrice = position.EntryPrice;
            var distance = 0.0;
            var adjEntryPrice = position.TradeType == TradeType.Buy ? entryPrice + ExtraPips * Symbol.PipSize : entryPrice - ExtraPips * Symbol.PipSize;
            if (position.TradeType == TradeType.Buy)
                distance = Symbol.Bid - entryPrice;
            else
                distance = entryPrice - Symbol.Ask;

            if (distance >= Trigger * Symbol.PipSize && position.StopLoss != adjEntryPrice)

                ModifyPosition(position, adjEntryPrice, TP);
        }
        #endregion
    }
}

"

 

 


@alexander.n.fedorov
Replies

PanagiotisCharalampous
05 Feb 2018, 11:28

Hi Alex,

The problem is this line of code

var position = Positions.Find(InstanceName, Symbol);

Noone assures you that a position will be found. Change your break even function to the following

        private void GoToBreakEven()
        {
            var position = Positions.Find(InstanceName, Symbol);
            if (position != null)
            {
                var entryPrice = position.EntryPrice;
                var distance = 0.0;
                var adjEntryPrice = position.TradeType == TradeType.Buy ? entryPrice + ExtraPips * Symbol.PipSize : entryPrice - ExtraPips * Symbol.PipSize;
                if (position.TradeType == TradeType.Buy)
                    distance = Symbol.Bid - entryPrice;
                else
                    distance = entryPrice - Symbol.Ask;

                if (distance >= Trigger * Symbol.PipSize && position.StopLoss != adjEntryPrice)

                    ModifyPosition(position, adjEntryPrice, TP);
            }
        }

Best Regards,

Panagiotis


@PanagiotisCharalampous

alexander.n.fedorov
05 Feb 2018, 12:11

What a beautiful solution and explanation! Thank you

Everything is working fine now.

Alexander


@alexander.n.fedorov