New to CTrader and and CAlgo - need a small assist on delaying entry by 1 full bar

Created at 28 Nov 2020, 22:58
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!
DI

dinodelides

Joined 19.10.2020

New to CTrader and and CAlgo - need a small assist on delaying entry by 1 full bar
28 Nov 2020, 22:58


Hi Guys,

I spent some time with MT5 and MQL but I am not a coder. I do love the UI for Ctrader and made the jump and I am enjoying it so far. So I was messing about with the Automate function and often, I like to get my trigger, then wait for the bar in which the trigger occurred to close, then allow one more bar to form and on close of that extra bar, enter the trade. 

Using the sample RSI bot as an example, what snippet would I add to achieve this? I appreciate any responses.

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 SampleRSIcBot : 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; }

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

 


@dinodelides
Replies

prosteel1
29 Nov 2020, 03:12

RE:

This is a modified RSI bot.

series.Count - 3 gives 2 full bars back, each new bar increases the count by 1.

You can check the print of the High to make sure it is working properly.

 

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 SampleRSIcBot : 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("Partial Close offset", DefaultValue = 10, MinValue = 10, Step = 10)]
        public int Offset { get; set; }

        [Parameter("Timeframe", DefaultValue = "Hour")]
        public TimeFrame tf { get; set; }

        private RelativeStrengthIndex rsi;

        Bars series;
        int BarLast;

        protected override void OnStart()
        {
            rsi = Indicators.RelativeStrengthIndex(Source, Periods);
            series = MarketData.GetBars(tf);
            BarLast = series.Count - 3;
        }
        protected override void OnStop()
        {
            foreach (var position in Positions)
            {
                ClosePosition(position);
            }
        }

        protected override void OnTick()
        {
            if (series.Count - 3 > BarLast)
            {
                Print("series.Count - 3 = " + (series.Count - 3) + " has a High of = " + series.HighPrices[series.Count - 3]);

                if (rsi.Result.LastValue < 30)
                {
                    Close(TradeType.Sell);
                    Open(TradeType.Buy);
                }
                else if (rsi.Result.LastValue > 70)
                {
                    Close(TradeType.Buy);
                    Open(TradeType.Sell);
                }
                if (rsi.Result.LastValue < 30 + Offset)
                {
                    PartialClose(TradeType.Sell);
                }
                if (rsi.Result.LastValue > 70 - Offset)
                {
                    PartialClose(TradeType.Buy);
                }
                BarLast = series.Count - 3;
            }
        }

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

        private void PartialClose(TradeType tradeType)
        {

            foreach (var position in Positions.FindAll("SampleRSI", SymbolName, tradeType))
            {
                double newVol = Symbol.NormalizeVolumeInUnits((position.VolumeInUnits / 2), RoundingMode.Up);
                if (position.Quantity == Quantity)
                {
                    ClosePosition(position, newVol);
                    Print("Partial closing " + newVol + " of " + position + " due to RSI = " + rsi.Result.LastValue);
                }
            }
        }

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

 


@prosteel1

dinodelides
29 Nov 2020, 09:41

RE: RE:

prosteel1 said:

This is a modified RSI bot.

series.Count - 3 gives 2 full bars back, each new bar increases the count by 1.

You can check the print of the High to make sure it is working properly.

 

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 SampleRSIcBot : 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("Partial Close offset", DefaultValue = 10, MinValue = 10, Step = 10)]
        public int Offset { get; set; }

        [Parameter("Timeframe", DefaultValue = "Hour")]
        public TimeFrame tf { get; set; }

        private RelativeStrengthIndex rsi;

        Bars series;
        int BarLast;

        protected override void OnStart()
        {
            rsi = Indicators.RelativeStrengthIndex(Source, Periods);
            series = MarketData.GetBars(tf);
            BarLast = series.Count - 3;
        }
        protected override void OnStop()
        {
            foreach (var position in Positions)
            {
                ClosePosition(position);
            }
        }

        protected override void OnTick()
        {
            if (series.Count - 3 > BarLast)
            {
                Print("series.Count - 3 = " + (series.Count - 3) + " has a High of = " + series.HighPrices[series.Count - 3]);

                if (rsi.Result.LastValue < 30)
                {
                    Close(TradeType.Sell);
                    Open(TradeType.Buy);
                }
                else if (rsi.Result.LastValue > 70)
                {
                    Close(TradeType.Buy);
                    Open(TradeType.Sell);
                }
                if (rsi.Result.LastValue < 30 + Offset)
                {
                    PartialClose(TradeType.Sell);
                }
                if (rsi.Result.LastValue > 70 - Offset)
                {
                    PartialClose(TradeType.Buy);
                }
                BarLast = series.Count - 3;
            }
        }

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

        private void PartialClose(TradeType tradeType)
        {

            foreach (var position in Positions.FindAll("SampleRSI", SymbolName, tradeType))
            {
                double newVol = Symbol.NormalizeVolumeInUnits((position.VolumeInUnits / 2), RoundingMode.Up);
                if (position.Quantity == Quantity)
                {
                    ClosePosition(position, newVol);
                    Print("Partial closing " + newVol + " of " + position + " due to RSI = " + rsi.Result.LastValue);
                }
            }
        }

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

Thank you very much for the quick response Prosteel1 - I will work through it to make sure I understand what it is doing and then give it a test.  Have a great day :)

 


@dinodelides

PanagiotisCharalampous
30 Nov 2020, 09:25

Hi dinodelides,

You can change the value which the cBot checks to enter the trade. Instead of the LastValue, you can check Last(1), Last(2) etc. where the number represent a backwards index of the relevant series.

Best Regards,

Panagiotis 

Join us on Telegram


@PanagiotisCharalampous

dinodelides
30 Nov 2020, 17:36

RE:
Ευχαριστώ πολύ Panagiotis! I have added this to my bots where necessary.

Best regards

Dino

 

PanagiotisCharalampous said:

Hi dinodelides,

You can change the value which the cBot checks to enter the trade. Instead of the LastValue, you can check Last(1), Last(2) etc. where the number represent a backwards index of the relevant series.

Best Regards,

Panagiotis 

Join us on Telegram

 


@dinodelides