How to run one bot in multiple symbol without conflict

Created at 24 Jun 2022, 12:15
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!
MongolTrader's avatar

MongolTrader

Joined 12.02.2015

How to run one bot in multiple symbol without conflict
24 Jun 2022, 12:15


I have bot use label and i want to use it many pairs in one account. How to easy customize my bot for able run in multiple symbol in one account. My bot use label mainly so it cant run multiple symbol at once. 


@MongolTrader
Replies

PanagiotisCharalampous
24 Jun 2022, 13:28

Hi MongolTrader,

You need to be a little bit more specific. What exactly is your problem? Did you try something and it did not work?

Best Regards,

Panagiotis 

Join us on Telegram and Facebook


@PanagiotisCharalampous

MongolTrader
26 Jun 2022, 10:42

RE:

PanagiotisCharalampous said:

Hi MongolTrader,

You need to be a little bit more specific. What exactly is your problem? Did you try something and it did not work?

Best Regards,

Panagiotis 

Join us on Telegram and Facebook

My problem is my bot only use label and it can't identify which symbol work with. So main problem is if i  use it such as eurusd and xauusd so my bot cant identify whether it eurusd symbol position or xauusd position. So i need easy way to customize my bot or i look is there any new instance feature on ctrader. 


@MongolTrader

PanagiotisCharalampous
27 Jun 2022, 12:29

Hi MongolTrader,

My problem is my bot only use label and it can't identify which symbol work with. 

Why not? You can use SymbolName property to check the symbol the cBot is running on.

Best Regards,

Panagiotis 

Join us on Telegram and Facebook


@PanagiotisCharalampous

MongolTrader
29 Jun 2022, 09:17

RE:

PanagiotisCharalampous said:

Hi MongolTrader,

My problem is my bot only use label and it can't identify which symbol work with. 

Why not? You can use SymbolName property to check the symbol the cBot is running on.

Best Regards,

Panagiotis 

Join us on Telegram and Facebook

Tell me with sample please 


@MongolTrader

PanagiotisCharalampous
29 Jun 2022, 09:28

Hi MongolTrader,

What exact sample do you need?

Best Regards,

Panagiotis 

Join us on Telegram and Facebook


@PanagiotisCharalampous

MongolTrader
29 Jun 2022, 09:35

RE:

PanagiotisCharalampous said:

Hi MongolTrader,

What exact sample do you need?

Best Regards,

Panagiotis 

Join us on Telegram and Facebook

My bot work only find and identify positions by only name. So when i run it on two pair it will conflict each other such as close funtion will close all position whether is one pair or other pair so how can i use my bot without edit and use one account. 


@MongolTrader

PanagiotisCharalampous
30 Jun 2022, 08:13

Hi MongolTrader,

Here is an example on how to get only the positions of the symbol the cBot is currently working on

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using cAlgo.API;
using cAlgo.API.Collections;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;

namespace cAlgo.Robots
{
    [Robot(AccessRights = AccessRights.None)]
    public class NewcBot : Robot
    {
        [Parameter(DefaultValue = "Hello world!")]
        public string Message { get; set; }

        protected override void OnStart()
        {
            // To learn more about cTrader Automate visit our Help Center:
            // https://help.ctrader.com/ctrader-automate

            Print(Message);
        }

        protected override void OnTick()
        {
            // Handle price updates here
            foreach (var position in Positions.Where(p => p.SymbolName == SymbolName))
            {
                
            }
        }

        protected override void OnStop()
        {
            // Handle cBot stop here
        }
    }
}

Best Regards,

Panagiotis 

Join us on Telegram and Facebook


@PanagiotisCharalampous

newbee
05 Apr 2023, 07:49

RE: Multi Symbol cbot & Paramaters

Hello Panagiotis,

Attached below is a sample of a multi symbol cbot I found which I believe was at (downhttps://ctrader.com/forum/calgo-support/22633) however, this link is no longer available.

It is excellent and I have been adding to this code however I am stuck when adding a Parameters as I can't get it to read the parameter. For example:

Say I wanted to add a Parameter for the "Periods" that will be used for the RSI indicator (which is sitting in the "public void init(Robot bot, string symbol)")

[Parameter("RSI Period", DefaultValue = 14)]

public int period { get; set; }

Or another example would be a parameter to set a "Max Number" of trades & pass this through to "void OnBarOpened(BarOpenedEventArgs obj)" section

[Parameter("Max Open Trades", DefaultValue = 10)]

 public double MaxPositionsAllowedPerSymbol { get; set; }

When I do, for some reason they simply aren't available to utilize or pass through to the RSI indicator sitting in the "public void init(Robot bot, string symbol)" etc.

I know I'm missing something, but I just can't seem to find what that is. I've been trying for weeks. Any assistance sample would be greatly appreciated.

Thank you!

 

Code sample below

 

using System;

using System.Linq;

using System.Collections.Generic;

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 TestMulti : Robot

    {

        // Multi symbol bot test

        string[] symbols =

        {

           "AUDCAD",

            "AUDCHF",

            "EURUSD",

            "CHFJPY"

            "EURUSD",

        };

        List<RSI_Strategy> strategies = new List<RSI_Strategy>();

        protected override void OnStart()

        {

            foreach (var sym in symbols)

            {

                var rsiStrategy = new RSI_Strategy();

                rsiStrategy.init(this, sym);

                strategies.Add(rsiStrategy);

            }

        }

    }

    // Sell when RSI crosses above 70

    public class RSI_Strategy

    {

        RelativeStrengthIndex rsi;

        Robot bot;

        string symbol;

        public void init(Robot bot, string symbol)

        {

            this.symbol = symbol;

            this.bot = bot;

            Bars bars = bot.MarketData.GetBars(bot.TimeFrame, symbol);

            rsi = bot.Indicators.RelativeStrengthIndex(bars.ClosePrices, 14);

            bars.BarOpened += OnBarOpened;

        }

        void OnBarOpened(BarOpenedEventArgs obj)

        {

            if (rsi.Result.Last(1) > 70 && rsi.Result.Last(2) <= 70)

            {

                bot.ExecuteMarketOrder(TradeType.Sell, symbol, 10000, symbol, 10.0, 10.0);

            }

            if (rsi.Result.Last(1) < 30 && rsi.Result.Last(2) >= 30)

            {

                bot.ExecuteMarketOrder(TradeType.Buy, symbol, 10000, symbol, 10.0, 10.0);

            }

        }

    }

}


@newbee

PanagiotisCharalampous
05 Apr 2023, 10:09

Hi MongolTrader,

Instead of passing a Robot object to the Init() method, try passing a TestMulti object.

Aieden Technologies

Need help? Join us on Telegram

Need premium support? Trade with us

 


@PanagiotisCharalampous

newbee
05 Apr 2023, 13:15

RE:

PanagiotisCharalampous said:

Hi MongolTrader,

Instead of passing a Robot object to the Init() method, try passing a TestMulti object.

Aieden Technologies

Need help? Join us on Telegram

Need premium support? Trade with us

 

Hello Panagiotis,

Thank you for this. It is newbee here.

I've been trying to do what you suggested since you sent me this answer but unfortunately to no avail. I simply don't know how to do that. Can you please give me a sample of how to pass a TestMulti object.

Thank you again.


@newbee

PanagiotisChar
06 Apr 2023, 08:05

Hi newbee,

See below

using System;

using System.Linq;

using System.Collections.Generic;

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 TestMulti : Robot

    {

        [Parameter("RSI Period", DefaultValue = 14)]

        public int Period { get; set; }

        // Multi symbol bot test

        string[] symbols =

        {

           "AUDCAD",

            "AUDCHF",

            "EURUSD",

            "CHFJPY",

            "EURUSD",

        };

        List<RSI_Strategy> strategies = new List<RSI_Strategy>();

        protected override void OnStart()

        {

            foreach (var sym in symbols)

            {

                var rsiStrategy = new RSI_Strategy();

                rsiStrategy.init(this, sym);

                strategies.Add(rsiStrategy);

            }

        }

    }

    // Sell when RSI crosses above 70

    public class RSI_Strategy

    {

        RelativeStrengthIndex rsi;

        Robot bot;

        string symbol;

        public void init(TestMulti bot, string symbol)

        {

            this.symbol = symbol;

            this.bot = bot;

            Bars bars = bot.MarketData.GetBars(bot.TimeFrame, symbol);

            rsi = bot.Indicators.RelativeStrengthIndex(bars.ClosePrices, bot.Period);

            bars.BarOpened += OnBarOpened;

        }

        void OnBarOpened(BarOpenedEventArgs obj)

        {

            if (rsi.Result.Last(1) > 70 && rsi.Result.Last(2) <= 70)

            {

                bot.ExecuteMarketOrder(TradeType.Sell, symbol, 10000, symbol, 10.0, 10.0);

            }

            if (rsi.Result.Last(1) < 30 && rsi.Result.Last(2) >= 30)

            {

                bot.ExecuteMarketOrder(TradeType.Buy, symbol, 10000, symbol, 10.0, 10.0);

            }

        }

    }

}

Aieden Technologies

Need help? Join us on Telegram

Need premium support? Trade with us


@PanagiotisChar

newbee
06 Apr 2023, 17:08

RE:

Hello Panagiotis,

Perfect, thank you very much.

 


@newbee

Obiriec
29 Jul 2023, 07:59

Help for use Timer

RE: use multiple timers and events OnTimer ()

PanagiotisCharalampous said: 

Hi tradermatrix,

It depends on what are you trying to achieve. In principle, you can set your timer to a fast interval e.g. one second, and then implement time checks on custom intervals. See an example below

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 NewcBot : Robot    {        [Parameter(DefaultValue = 30)]        public int Interval1 { get; set; }        [Parameter(DefaultValue = 50)]        public int Interval2 { get; set; }        DateTime _interval1;        DateTime _interval2;        protected override void OnStart()        {            _interval1 = Server.Time;            _interval2 = Server.Time;            Timer.TimerTick += Timer_TimerTick;            Timer.Start(1);        }        private void Timer_TimerTick()        {            if (_interval1.AddSeconds(Interval1) < Server.Time)            {                Print("Interval 1 Triggered");                _interval1 = Server.Time;            }            if (_interval2.AddSeconds(Interval2) < Server.Time)            {                Print("Interval 2 Triggered");                _interval2 = Server.Time;            }        }        protected override void OnTick()        {            // Put your core logic here        }        protected override void OnStop()        {            // Put your deinitialization logic here        }    }}

Best Regards,

Panagiotis 

Join us on Telegram

Good evening, I have a request similar to this user's post:
I programmed a cBot that works on a single instance of cTrader simultaneously on all 28 Forex pairs.
The cBot is able to recognize if there are "correlated" pairs to avoid having to open multiple operations, for example: if a EURUSD operation is already open, it will no longer open operations on all pairs that contain EUR or USD.
This logic works well but only if there is already an open trade.
When there is no operation open, it happens that, if the opening conditions occur simultaneously, the various instances of the cBot open the first operation and very often the presence of pairs that should not have been opened is seen.
I tried inserting a timer inside the code that sets the seconds of delay depending on the pair it's running on taking the value from a list but unfortunately I can't get the result I want because the timer doesn't make the requested pause but it loops in just one tick.
This must be done "OnBar" every time.
I may have some suggestions or correction of the code that I report in the attachment.

This is a part of code:

…..
 var Loop = 0;

           string AUDCAD = "AUDCAD";
           string AUDCHF = "AUDCHF";
           string AUDJPY = "AUDJPY";
           string AUDNZD = "AUDNZD";
           string AUDUSD = "AUDUSD";
           string CADCHF = "CADCHF";
           string CADJPY = "CADJPY";
           string CHFJPY = "CHFJPY";
           string EURAUD = "EURAUD";
           string EURCAD = "EURCAD";
           string EURCHF = "EURCHF";
           string EURGBP = "EURGBP";
           string EURJPY = "EURJPY";
           string EURNZD = "EURNZD";
           string EURUSD = "EURUSD";
           string GBPAUD = "GBPAUD";
           string GBPCAD = "GBPCAD";
           string GBPCHF = "GBPCHF";
           string GBPJPY = "GBPJPY";
           string GBPNZD = "GBPNZD";
           string GBPUSD = "GBPUSD";
           string NZDCAD = "NZDCAD";
           string NZDCHF = "NZDCHF";
           string NZDJPY = "NZDJPY";
           string NZDUSD = "NZDUSD";
           string USDCAD = "USDCAD";
           string USDCHF = "USDCHF";
           string USDJPY = "USDJPY";

           if (Symbol.Name.Contains(AUDCAD))
           {
               Loop = 7;
           }
           if (Symbol.Name.Contains(AUDCHF))
           {
               Loop = 11;
           }
           if (Symbol.Name.Contains(AUDJPY))
           {
               Loop = 13;
           }
           if (Symbol.Name.Contains(AUDNZD))
           {
               Loop = 17;
           }
           if (Symbol.Name.Contains(AUDUSD))
           {
               Loop = 19;
           }
           if (Symbol.Name.Contains(CADCHF))
           {
               Loop = 23;
           }
           if (Symbol.Name.Contains(CADJPY))
           {
               Loop = 29;
           }
           if (Symbol.Name.Contains(CHFJPY))
           {
               Loop = 31;
           }
           if (Symbol.Name.Contains(EURAUD))
           {
               Loop = 37;
           }
           if (Symbol.Name.Contains(EURCAD))
           {
               Loop = 41;
           }
           if (Symbol.Name.Contains(EURCHF))
           {
               Loop = 43;
           }
           if (Symbol.Name.Contains(EURGBP))
           {
               Loop = 47;
           }
           if (Symbol.Name.Contains(EURJPY))
           {
               Loop = 53;
           }
           if (Symbol.Name.Contains(EURNZD))
           {
               Loop = 59;
           }
           if (Symbol.Name.Contains(EURUSD))
           {
               Loop = 61;
           }
           if (Symbol.Name.Contains(GBPAUD))
           {
               Loop = 67;
           }
           if (Symbol.Name.Contains(GBPCAD))
           {
               Loop = 71;
           }
           if (Symbol.Name.Contains(GBPCHF))
           {
               Loop = 73;
           }
           if (Symbol.Name.Contains(GBPJPY))
           {
               Loop = 79;
           }
           if (Symbol.Name.Contains(GBPNZD))
           {
               Loop = 83;
           }
           if (Symbol.Name.Contains(GBPUSD))
           {
               Loop = 89;
           }
           if (Symbol.Name.Contains(NZDCAD))
           {
               Loop = 97;
           }
           if (Symbol.Name.Contains(NZDCHF))
           {
               Loop = 101;
           }
           if (Symbol.Name.Contains(NZDJPY))
           {
               Loop = 103;
           }
           if (Symbol.Name.Contains(NZDUSD))
           {
               Loop = 107;
           }
           if (Symbol.Name.Contains(USDCAD))
           {
               Loop = 109;
           }
           if (Symbol.Name.Contains(USDCHF))
           {
               Loop = 113;
           }
           if (Symbol.Name.Contains(USDJPY))
           {
               Loop = 127;
           }

           var Account_Positions = Positions.Count;

           Timer.Start(Loop);

           for (int i = 0; i < Loop; i++)
           {
               Print("Loop n.: " + Loop + " >> " + i + "");
           }
           Timer.Stop();

…….

 

Once the cBot has "entered" the Loop, it should remain still for the set time but instead immediately exits and executes the rest of the code.

Surely I made a mistake in setting this part of the code but despite trying to write it in another way, I still can't find the solution!


@Obiriec