Running robot in multiple pairs

Created at 09 Nov 2016, 14:28
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!
RI

richard_alcaide

Joined 02.11.2015

Running robot in multiple pairs
09 Nov 2016, 14:28


Hi All,

I am running this robot in multiple pairs but the problem is everytime there is a position that has been opened in just one specific pair it also created pending order to the other pairs where the robot is also running, I already used different Label for different instance.

Please help...

 

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 AlcaideSR : Robot
    {
        [Parameter(DefaultValue = 1000)]
        public int Volume { get; set; }

        [Parameter("Pips away", DefaultValue = 20)]
        public int PipsAway { get; set; }

        [Parameter("TP", DefaultValue = 60)]
        public int tp { get; set; }

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

        [Parameter("Label", DefaultValue = "Rich")]
        //Use different Label for different instance
        public string Label { get; set; }

        [Parameter("Buy ?", DefaultValue = true)]
        public bool buy { get; set; }

        private bool _ordersCreated;
        private PendingOrder _buyOrder;
        private PendingOrder _sellOrder;


        protected override void OnStart()
        {

            Positions.Opened += OnPositionsOpened;

            if (Positions.Count == 0 && buy)
            {
                var buyOrderTargetPrice = Symbol.Ask - PipsAway * Symbol.PipSize;
                PlaceLimitOrder(TradeType.Buy, Symbol, Volume, buyOrderTargetPrice, Label, sl, tp);
            }
            if (Positions.Count == 0 && !buy)
            {
                var sellOrderTargetPrice = Symbol.Bid + PipsAway * Symbol.PipSize;
                PlaceLimitOrder(TradeType.Sell, Symbol, Volume, sellOrderTargetPrice, Label, sl, tp);
            }
        }

        void OnPositionsOpened(PositionOpenedEventArgs args)
        {

            var originalPosition = args.Position;

            if (originalPosition.TradeType == TradeType.Buy)
            {
                var sellOrderTargetPrice = Symbol.Bid - PipsAway * Symbol.PipSize;
                PlaceStopOrder(TradeType.Sell, Symbol, Volume, sellOrderTargetPrice, Label, sl, tp);
                //break; // optional break to hedge only one position
            }
            else
            {
                var buyOrderTargetPrice = Symbol.Ask + PipsAway * Symbol.PipSize;
                PlaceStopOrder(TradeType.Buy, Symbol, Volume, buyOrderTargetPrice, Label, sl, tp);
                //break; // optional break to hedge only one position
            }

        }



    }
}


@richard_alcaide
Replies

Jiri
09 Nov 2016, 14:37

Positions is collection of all opened positions of the trading account. You might be using different labels for each instance but you are still working with the collection containing all trades. Add condition that checks if opened position label is equal to your instance label or make new collection inside cBot memory and handle positions from there.


@Jiri

... Deleted by UFO ...

richard_alcaide
09 Nov 2016, 17:58 ( Updated at: 21 Dec 2023, 09:20 )

RE:

lucian said:

change:

  void OnPositionsOpened(PositionOpenedEventArgs args)
        {

            var originalPosition = args.Position;

            if (originalPosition.TradeType == TradeType.Buy)

with:

  void OnPositionsOpened(PositionOpenedEventArgs args)
        {

            var originalPosition = args.Position;

            if (originalPosition.TradeType == TradeType.Buy && originalPosition.Label==Label)

 

Lucian there was an error


@richard_alcaide

... Deleted by UFO ...

richard_alcaide
09 Nov 2016, 19:38

RE:

lucian said:

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 AlcaideSR : Robot
    {
        [Parameter(DefaultValue = 1000)]
        public int Volume { get; set; }

        [Parameter("Pips away", DefaultValue = 20)]
        public int PipsAway { get; set; }

        [Parameter("TP", DefaultValue = 60)]
        public int tp { get; set; }

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

        [Parameter("Label", DefaultValue = "Rich")]
        //Use different Label for different instance
        public string Label { get; set; }

        [Parameter("Buy ?", DefaultValue = true)]
        public bool buy { get; set; }




        protected override void OnStart()
        {

            Positions.Opened += OnPositionsOpened;

            if (Positions.Count == 0 && buy)
            {
                var buyOrderTargetPrice = Symbol.Ask - PipsAway * Symbol.PipSize;
                PlaceLimitOrder(TradeType.Buy, Symbol, Volume, buyOrderTargetPrice, Label, sl, tp);
            }
            if (Positions.Count == 0 && !buy)
            {
                var sellOrderTargetPrice = Symbol.Bid + PipsAway * Symbol.PipSize;
                PlaceLimitOrder(TradeType.Sell, Symbol, Volume, sellOrderTargetPrice, Label, sl, tp);
            }
        }

        void OnPositionsOpened(PositionOpenedEventArgs args)
        {

            var originalPosition = args.Position;

            if (originalPosition.TradeType == TradeType.Buy && originalPosition.Label == Label)
            {
                var sellOrderTargetPrice = Symbol.Bid - PipsAway * Symbol.PipSize;
                PlaceStopOrder(TradeType.Sell, Symbol, Volume, sellOrderTargetPrice, Label, sl, tp);
                //break; // optional break to hedge only one position
            }
            else
            {
                var buyOrderTargetPrice = Symbol.Ask + PipsAway * Symbol.PipSize;
                PlaceStopOrder(TradeType.Buy, Symbol, Volume, buyOrderTargetPrice, Label, sl, tp);
                //break; // optional break to hedge only one position
            }

        }



    }
}

 

Thanks Lucian no error now but still it doesn't fix the issue :(


@richard_alcaide

... Deleted by UFO ...

richard_alcaide
09 Nov 2016, 20:59

RE:

lucian said:

Try to remove any instance from cAlgo or cTrader, and close cAlgo and/or cTrader.

Reload cTrader/cAlgo and add again the instances with carefully setting of the Label.

I just did it but still doesn't work


@richard_alcaide

Jiri
09 Nov 2016, 21:12

Try following code. It's checking symbol code instead of label so you don't have to set different label for each instance to make them work seperatly.

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

namespace cAlgo
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class AlcaideSR : Robot
    {
        [Parameter("Volume", DefaultValue = 1000)]
        public int Volume { get; set; }

        [Parameter("Pips away", DefaultValue = 20)]
        public int PipsAway { get; set; }

        [Parameter("TP", DefaultValue = 60)]
        public int TakeProfitPips { get; set; }

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

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

        [Parameter("Buy ?", DefaultValue = true)]
        public bool IsBuy { get; set; }

        protected override void OnStart()
        {
            if (Positions.Where(pos => pos.SymbolCode == Symbol.Code).Count() == 0)
            {
                if (IsBuy)
                {
                    double targetPrice = Symbol.Ask - PipsAway * Symbol.PipSize;
                    PlaceLimitOrder(TradeType.Buy, Symbol, Volume, targetPrice, Label, StopLossPips, TakeProfitPips);
                }
                else
                {
                    double targetPrice = Symbol.Bid + PipsAway * Symbol.PipSize;
                    PlaceLimitOrder(TradeType.Sell, Symbol, Volume, targetPrice, Label, StopLossPips, TakeProfitPips);
                }
            }

            Positions.Opened += OnPositionOpened;
        }

        private void OnPositionOpened(PositionOpenedEventArgs obj)
        {
            if (obj.Position.SymbolCode == Symbol.Code)
            {
                if (obj.Position.TradeType == TradeType.Buy)
                {
                    double targetPrice = Symbol.Bid - PipsAway * Symbol.PipSize;
                    PlaceStopOrder(TradeType.Sell, Symbol, Volume, targetPrice, Label, StopLossPips, TakeProfitPips);
                }
                else
                {
                    double targetPrice = Symbol.Ask + PipsAway * Symbol.PipSize;
                    PlaceStopOrder(TradeType.Buy, Symbol, Volume, targetPrice, Label, StopLossPips, TakeProfitPips);
                }
            }
        }
    }
}

 


@Jiri

richard_alcaide
09 Nov 2016, 21:24

RE:

tmc. said:

Try following code. It's checking symbol code instead of label so you don't have to set different label for each instance to make them work seperatly.

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

namespace cAlgo
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class AlcaideSR : Robot
    {
        [Parameter("Volume", DefaultValue = 1000)]
        public int Volume { get; set; }

        [Parameter("Pips away", DefaultValue = 20)]
        public int PipsAway { get; set; }

        [Parameter("TP", DefaultValue = 60)]
        public int TakeProfitPips { get; set; }

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

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

        [Parameter("Buy ?", DefaultValue = true)]
        public bool IsBuy { get; set; }

        protected override void OnStart()
        {
            if (Positions.Where(pos => pos.SymbolCode == Symbol.Code).Count() == 0)
            {
                if (IsBuy)
                {
                    double targetPrice = Symbol.Ask - PipsAway * Symbol.PipSize;
                    PlaceLimitOrder(TradeType.Buy, Symbol, Volume, targetPrice, Label, StopLossPips, TakeProfitPips);
                }
                else
                {
                    double targetPrice = Symbol.Bid + PipsAway * Symbol.PipSize;
                    PlaceLimitOrder(TradeType.Sell, Symbol, Volume, targetPrice, Label, StopLossPips, TakeProfitPips);
                }
            }

            Positions.Opened += OnPositionOpened;
        }

        private void OnPositionOpened(PositionOpenedEventArgs obj)
        {
            if (obj.Position.SymbolCode == Symbol.Code)
            {
                if (obj.Position.TradeType == TradeType.Buy)
                {
                    double targetPrice = Symbol.Bid - PipsAway * Symbol.PipSize;
                    PlaceStopOrder(TradeType.Sell, Symbol, Volume, targetPrice, Label, StopLossPips, TakeProfitPips);
                }
                else
                {
                    double targetPrice = Symbol.Ask + PipsAway * Symbol.PipSize;
                    PlaceStopOrder(TradeType.Buy, Symbol, Volume, targetPrice, Label, StopLossPips, TakeProfitPips);
                }
            }
        }
    }
}

 

Excellent tmc.! its working perfect now Thanks to both of you for being helpful!


@richard_alcaide

richard_alcaide
10 Nov 2016, 13:19

RE: RE:

richard_alcaide said:

tmc. said:

Try following code. It's checking symbol code instead of label so you don't have to set different label for each instance to make them work seperatly.

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

namespace cAlgo
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class AlcaideSR : Robot
    {
        [Parameter("Volume", DefaultValue = 1000)]
        public int Volume { get; set; }

        [Parameter("Pips away", DefaultValue = 20)]
        public int PipsAway { get; set; }

        [Parameter("TP", DefaultValue = 60)]
        public int TakeProfitPips { get; set; }

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

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

        [Parameter("Buy ?", DefaultValue = true)]
        public bool IsBuy { get; set; }

        protected override void OnStart()
        {
            if (Positions.Where(pos => pos.SymbolCode == Symbol.Code).Count() == 0)
            {
                if (IsBuy)
                {
                    double targetPrice = Symbol.Ask - PipsAway * Symbol.PipSize;
                    PlaceLimitOrder(TradeType.Buy, Symbol, Volume, targetPrice, Label, StopLossPips, TakeProfitPips);
                }
                else
                {
                    double targetPrice = Symbol.Bid + PipsAway * Symbol.PipSize;
                    PlaceLimitOrder(TradeType.Sell, Symbol, Volume, targetPrice, Label, StopLossPips, TakeProfitPips);
                }
            }

            Positions.Opened += OnPositionOpened;
        }

        private void OnPositionOpened(PositionOpenedEventArgs obj)
        {
            if (obj.Position.SymbolCode == Symbol.Code)
            {
                if (obj.Position.TradeType == TradeType.Buy)
                {
                    double targetPrice = Symbol.Bid - PipsAway * Symbol.PipSize;
                    PlaceStopOrder(TradeType.Sell, Symbol, Volume, targetPrice, Label, StopLossPips, TakeProfitPips);
                }
                else
                {
                    double targetPrice = Symbol.Ask + PipsAway * Symbol.PipSize;
                    PlaceStopOrder(TradeType.Buy, Symbol, Volume, targetPrice, Label, StopLossPips, TakeProfitPips);
                }
            }
        }
    }
}

 

Excellent tmc.! its working perfect now Thanks to both of you for being helpful!

One last thing guys I also need  to automatically cancel the Pending order of the pair where TP made. TIA


@richard_alcaide