Extreme RSI news algo

Created at 15 Sep 2017, 14:02
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!
CH

chris.wandel00

Joined 06.09.2017

Extreme RSI news algo
15 Sep 2017, 14:02


Hello there,

I'm trying to make a very simple algo which opens orders on RSI values with an adjustable trailing stop.

My plan is to switch it on at 10tick chart at news times and catch upwards or downwards momentum.

It certainly catches the momentum, but would anybody care to help adding the adjustable trailing stop and a take profit parameter please?

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


        private RelativeStrengthIndex rsi;

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

        protected override void OnTick()
        {
            if (rsi.Result.LastValue < 20)
            {
                Close(TradeType.Buy);

            }
            else if (rsi.Result.LastValue > 79)
            {
                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);
            var volumeInUnits = Symbol.QuantityToVolume(Quantity);

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

@chris.wandel00
Replies

Spotware
15 Sep 2017, 17:11

Dear Trader,

Thanks for posting in the forum. You can find a sample of how to program a trailing stop loss here. It might be helpful in your effort to incorporate trailing stop loss logic inside your cBot.

Best Regards,

cTrader Team

 


@Spotware

chris.wandel00
15 Sep 2017, 17:19

RE:

Spotware said:

Dear Trader,

Thanks for posting in the forum. You can find a sample of how to program a trailing stop loss here. It might be helpful in your effort to incorporate trailing stop loss logic inside your cBot.

Best Regards,

cTrader Team

 

Thanks, I have been trying all afternoon but I keep getting errors.

 


@chris.wandel00

chris.wandel00
15 Sep 2017, 18:26

This is where I am and it works apart from this : Error occured during parsing project file: 'Syntax for an XML declaration is invalid. Line 1, position 7.'

 

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 = 14)]
        public int Periods { get; set; }

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

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

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

        [Parameter("Trigger (pips)", DefaultValue = 20)]
        public int Trigger { get; set; }

        [Parameter("Trailing Stop (pips)", DefaultValue = 10)]
        public int TrailingStop { 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);
            }
            var position = Positions.Find("SampleBuyTrailing");

            if (position == null)
            {
                Stop();
                return;
            }

            double distance = position.EntryPrice - Symbol.Ask;

            if (distance >= Trigger * Symbol.PipSize)
            {
                double newStopLossPrice = Symbol.Ask + TrailingStop * Symbol.PipSize;
                if (position.StopLoss == null || newStopLossPrice < position.StopLoss)
                {
                    ModifyPosition(position, newStopLossPrice, position.TakeProfit);
                }
            }
        }

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

 


@chris.wandel00

Spotware
18 Sep 2017, 10:11

Dear chris.wandel00,

We tried to ran your cBot but we could not reproduce the error reported. In fact, the cBot seems to stop immediately since there are no positions open. If you can provide more detailed steps on how to reproduce your problem, we can have another look.

Best Regards,

cTrader Team


@Spotware