Multiple Indicators Robot

Created at 25 May 2022, 16:46
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!
CT

ctid5083541

Joined 25.05.2022

Multiple Indicators Robot
25 May 2022, 16:46


I would like to at least two robots to execute at the same time because I want both to be valid at the same time. Below I have the two different robots that work fine as stand alone cBots, but I can't get them to work together.

First the RSI code:

using cAlgo.API;
using cAlgo.API.Indicators;

namespace cAlgo.Robots
{
    // This sample cBot shows how to use the Relative Strength Index indicator
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class RelativeStrengthIndexSample : Robot
    {
        private double _volumeInUnits;

        private RelativeStrengthIndex _relativeStrengthIndex;

        [Parameter("Volume (Lots)", DefaultValue = 1.0)]
        public double VolumeInLots { get; set; }

        [Parameter("Stop Loss (Pips)", DefaultValue = 10)]
        public double StopLossInPips { get; set; }

        [Parameter("Take Profit (Pips)", DefaultValue = 10)]
        public double TakeProfitInPips { get; set; }

        [Parameter("Label", DefaultValue = "Sample")]
        public string Label { get; set; }

        public Position[] BotPositions
        {
            get
            {
                return Positions.FindAll(Label);
            }
        }

        protected override void OnStart()
        {
            _volumeInUnits = Symbol.QuantityToVolumeInUnits(VolumeInLots);
            _relativeStrengthIndex = Indicators.RelativeStrengthIndex(Bars.ClosePrices, 20);
        }

        protected override void OnBar()
        {
            if (_relativeStrengthIndex.Result.Last(1) < 30 && _relativeStrengthIndex.Result.Last(2) > 30)
            {
                //ClosePositions(TradeType.Buy);

                ExecuteMarketOrder(TradeType.Sell, SymbolName, _volumeInUnits, Label, StopLossInPips, TakeProfitInPips);
            }
            else if (_relativeStrengthIndex.Result.Last(1) > 70 && _relativeStrengthIndex.Result.Last(2) < 70)
            {
                //ClosePositions(TradeType.Sell);

                ExecuteMarketOrder(TradeType.Buy, SymbolName, _volumeInUnits, Label, StopLossInPips, TakeProfitInPips);
            }
        }

        private void ClosePositions(TradeType tradeType)
        {
            foreach (var position in BotPositions)
            {
                if (position.TradeType != tradeType) continue;

                ClosePosition(position);
            }
        }
    }
}

and below the MACD code:

 

using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;

namespace cAlgo.Robots
{
    // This sample cBot shows how to use the MACD Cross Over indicator
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class MacdCrossOverSample : Robot
    {
        private double _volumeInUnits;

        private MacdCrossOver _macdCrossOver;

        [Parameter("Volume (Lots)", DefaultValue = 1.0)]
        public double VolumeInLots { get; set; }

        [Parameter("Stop Loss (Pips)", DefaultValue = 2)]
        public double StopLossInPips { get; set; }

        [Parameter("Take Profit (Pips)", DefaultValue = 3)]
        public double TakeProfitInPips { get; set; }

        [Parameter("Label", DefaultValue = "Sample")]
        public string Label { get; set; }

        public Position[] BotPositions
        {
            get
            {
                return Positions.FindAll(Label);
            }
        }

        protected override void OnStart()
        {
            _volumeInUnits = Symbol.QuantityToVolumeInUnits(VolumeInLots);

            _macdCrossOver = Indicators.MacdCrossOver(Bars.ClosePrices, 26, 12, 9);
        }

        protected override void OnBar()
        {
            if (_macdCrossOver.MACD.Last(1) > _macdCrossOver.Signal.Last(1) && _macdCrossOver.MACD.Last(2) <= _macdCrossOver.Signal.Last(2) && _macdCrossOver.MACD.Last(1) >=0)
            {
                ClosePositions(TradeType.Sell);

                ExecuteMarketOrder(TradeType.Buy, SymbolName, _volumeInUnits, Label, StopLossInPips, TakeProfitInPips);
            }
            else if (_macdCrossOver.MACD.Last(1) < _macdCrossOver.Signal.Last(1) && _macdCrossOver.MACD.Last(2) >= _macdCrossOver.Signal.Last(2) && _macdCrossOver.MACD.Last(1) <=0)
            {
                ClosePositions(TradeType.Buy);

                ExecuteMarketOrder(TradeType.Sell, SymbolName, _volumeInUnits, Label, StopLossInPips, TakeProfitInPips);
            }
        }

        private void ClosePositions(TradeType tradeType)
        {
            foreach (var position in BotPositions)
            {
                if (position.TradeType != tradeType) continue;

                ClosePosition(position);
            }
        }
    }
}

 

 

Many thanks for any advice.


@ctid5083541
Replies

amusleh
26 May 2022, 10:02

Hi,

What's the issue? are you using different labels?


@amusleh

ctid5083541
26 May 2022, 15:09

RE:

amusleh said:

Hi,

What's the issue? are you using different labels?

I'm not a programmer although I know a little bit of Visual Basic. I think I now have it working correctly, at least I hope so.

 


@ctid5083541