Can someone help me customize this free cBot to work with multiple instances?

Created at 27 May 2022, 17:09
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!
GL

glmxvs

Joined 27.05.2022

Can someone help me customize this free cBot to work with multiple instances?
27 May 2022, 17:09


Hello everyone,

I am all new with the whole cAlgo thing, I am trying to modify this simple cBot I got from the free codebase here, to be able to work with multiple instances but without success.

It seems that the bot was written to only work with one instance i.e. one pair at a time and what I would like is to have it to be able to run multiple instances (multiple pairs) at the same time independently. Right now, if I add another instance it will open an initial position but then it will stop working as it shouldbe since it seems it "confuses" the orders from the new instance (new pair) as they were the orders added to the initial instance (pair) that it got started with.

So, simply, can anyone tell me which part of the code to update so this works independently with multiple pairs at the same time.

I've also tried to clone the bot and give it another name to see if it would work with another pair alongside the original bot running a different pair and nothing changed - it still does the same as in case I was trying to use it with multiple pairs at once.

I did look if some had a similar issue with other bots here, found this topic https://ctrader.com/forum/cbot-support/11843?page=1 that was discussing the same issue with another bot but I couldn't figure out how to apply the logic here.

Here's the code of the bot, thanks:

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 AdvancedHedgingandScalpingcBot : Robot
    {

        [Parameter("Volume", DefaultValue = 1000, MinValue = 1, Step = 1)]
        public int Volume { get; set; }
        private double _equity;
        private int noOfPositions = 4;
        private int pips = 2;
        protected override void OnStart()
        {
            _equity = Account.Balance;
            ExecuteMarketOrder(TradeType.Buy, Symbol, Volume, "BUY");
            ExecuteMarketOrder(TradeType.Sell, Symbol, Volume, "SELL");
        }
        protected override void OnBar()
        {
            foreach (var position in Positions)
            {
                if (position.Pips > pips)
                {
                    ClosePosition(position);
                }
            }
            if (Positions.Count < noOfPositions)
            {
                ExecuteMarketOrder(TradeType.Buy, Symbol, Volume, "BUY");
                ExecuteMarketOrder(TradeType.Sell, Symbol, Volume, "SELL");
            }


            int buyCount = 0;
            int sellCount = 0;
            foreach (var position in Positions)
            {
                if (position.TradeType == TradeType.Buy)
                    buyCount++;
                if (position.TradeType == TradeType.Sell)
                    sellCount++;
            }

            if (buyCount == 0 || sellCount == 0)
            {
                Volume += 1000;
                noOfPositions += 2;
                pips++;
                ExecuteMarketOrder(TradeType.Buy, Symbol, Volume, "BUY");
                ExecuteMarketOrder(TradeType.Sell, Symbol, Volume, "SELL");
            }

            if (Account.Equity > _equity + Account.Equity / 100)
            {
                foreach (var position in Positions)
                {
                    ClosePosition(position);
                }
                _equity = Account.Equity;
                Volume = 1000;
                noOfPositions = 4;
                pips = 2;
                ExecuteMarketOrder(TradeType.Buy, Symbol, Volume, "BUY");
                ExecuteMarketOrder(TradeType.Sell, Symbol, Volume, "SELL");
            }

        }

    }


}

 


@glmxvs
Replies

amusleh
30 May 2022, 09:05

Hi,

Try this:

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 AdvancedHedgingandScalpingcBot : Robot
    {
        [Parameter("Volume", DefaultValue = 1000, MinValue = 1, Step = 1)]
        public int Volume { get; set; }

        private double _equity;
        private int noOfPositions = 4;
        private int pips = 2;
        private string label;

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

        protected override void OnStart()
        {
            label = string.Format("AdvancedHedgingandScalpingcBot-{0}-{1}", SymbolName, TimeFrame.ToString());

            _equity = Account.Balance;
            ExecuteMarketOrder(TradeType.Buy, Symbol, Volume, label);
            ExecuteMarketOrder(TradeType.Sell, Symbol, Volume, label);
        }

        protected override void OnBar()
        {
            foreach (var position in BotPositions)
            {
                if (position.Pips > pips)
                {
                    ClosePosition(position);
                }
            }
            if (BotPositions.Length < noOfPositions)
            {
                ExecuteMarketOrder(TradeType.Buy, Symbol, Volume, label);
                ExecuteMarketOrder(TradeType.Sell, Symbol, Volume, label);
            }

            int buyCount = 0;
            int sellCount = 0;
            foreach (var position in BotPositions)
            {
                if (position.TradeType == TradeType.Buy)
                    buyCount++;
                if (position.TradeType == TradeType.Sell)
                    sellCount++;
            }

            if (buyCount == 0 || sellCount == 0)
            {
                Volume += 1000;
                noOfPositions += 2;
                pips++;
                ExecuteMarketOrder(TradeType.Buy, Symbol, Volume, label);
                ExecuteMarketOrder(TradeType.Sell, Symbol, Volume, label);
            }

            if (Account.Equity > _equity + Account.Equity / 100)
            {
                foreach (var position in BotPositions)
                {
                    ClosePosition(position);
                }
                _equity = Account.Equity;
                Volume = 1000;
                noOfPositions = 4;
                pips = 2;
                ExecuteMarketOrder(TradeType.Buy, Symbol, Volume, label);
                ExecuteMarketOrder(TradeType.Sell, Symbol, Volume, label);
            }
        }
    }
}

 


@amusleh

m4trader4
30 May 2022, 17:40

Check the thread, you need change some of the trade operations parameters in bot, it should work

 


@m4trader4

glmxvs
30 May 2022, 18:12

RE:

amusleh said:

Hi,

Try this:

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 AdvancedHedgingandScalpingcBot : Robot
    {
        [Parameter("Volume", DefaultValue = 1000, MinValue = 1, Step = 1)]
        public int Volume { get; set; }

        private double _equity;
        private int noOfPositions = 4;
        private int pips = 2;
        private string label;

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

        protected override void OnStart()
        {
            label = string.Format("AdvancedHedgingandScalpingcBot-{0}-{1}", SymbolName, TimeFrame.ToString());

            _equity = Account.Balance;
            ExecuteMarketOrder(TradeType.Buy, Symbol, Volume, label);
            ExecuteMarketOrder(TradeType.Sell, Symbol, Volume, label);
        }

        protected override void OnBar()
        {
            foreach (var position in BotPositions)
            {
                if (position.Pips > pips)
                {
                    ClosePosition(position);
                }
            }
            if (BotPositions.Length < noOfPositions)
            {
                ExecuteMarketOrder(TradeType.Buy, Symbol, Volume, label);
                ExecuteMarketOrder(TradeType.Sell, Symbol, Volume, label);
            }

            int buyCount = 0;
            int sellCount = 0;
            foreach (var position in BotPositions)
            {
                if (position.TradeType == TradeType.Buy)
                    buyCount++;
                if (position.TradeType == TradeType.Sell)
                    sellCount++;
            }

            if (buyCount == 0 || sellCount == 0)
            {
                Volume += 1000;
                noOfPositions += 2;
                pips++;
                ExecuteMarketOrder(TradeType.Buy, Symbol, Volume, label);
                ExecuteMarketOrder(TradeType.Sell, Symbol, Volume, label);
            }

            if (Account.Equity > _equity + Account.Equity / 100)
            {
                foreach (var position in BotPositions)
                {
                    ClosePosition(position);
                }
                _equity = Account.Equity;
                Volume = 1000;
                noOfPositions = 4;
                pips = 2;
                ExecuteMarketOrder(TradeType.Buy, Symbol, Volume, label);
                ExecuteMarketOrder(TradeType.Sell, Symbol, Volume, label);
            }
        }
    }
}

 

@amusleh thank you, it seems it works perfectly! This makes me want to learn even more! 

@m4trader4 thank you for the link as well, I can see that that link was a way better resource than the two I had. 


@glmxvs