Running several instances of the bot: ExecuteMarketOrder method does not return anything

Created at 30 May 2022, 10:06
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!
YI

yisrgg

Joined 02.04.2016

Running several instances of the bot: ExecuteMarketOrder method does not return anything
30 May 2022, 10:06


Hello everyone. The following bot (see below) does work correctly when added to only one chart. However, when I add it to more than one chart, the bot does either

  • only enter the first market order for only one of the instruments,
  • or enter the first market order for instruments and only place the pending order for one of the instruments.

I have tried the following without success solving the problem:

  • compiling the bot with Net4.x and Net6.0,
  • using different Robot ID parameters for each instance,
  • testing different instruments and number of instances.

I have seen that there are other people who have a similar problem: https://ctrader.com/forum/cbot-support/38105 I have programmed and tested the bot on Spotware cTrader (Public beta) 4.2.4 and the latest version of ICMarkets cTrader. I hope that you can help me. Find the journal and automate logs below.

Kind regards.

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 MRQuestionExample : Robot
    {
        [Parameter("Robot ID", DefaultValue = "MR Question")]
        public string RobotID { get; set; }

        [Parameter("Position Entry Time in UTC", DefaultValue = "10:00")]
        public string EntryTimeStr { get; set; }

        [Parameter("Trade Type", DefaultValue = TradeType.Buy)]
        public TradeType InitialTradeType { get; set; }

        [Parameter("Initial Lot Size", DefaultValue = 1)]
        public double InitialLotSize { get; set; }

        [Parameter("Lot Multiplier", DefaultValue = 1.1)]
        public double LotMultiplier { get; set; }

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

        [Parameter("Stop-Loss in Pips", DefaultValue = 5)]
        public double SLInPips { get; set; }


        double initialVolume;

        TimeSpan EntryTime;
        DateTime LastPositionEntryDate;

        TimeSpan PreviousTickTimeOfDay;

        protected override void OnStart()
        {
            initialVolume = Symbol.NormalizeVolumeInUnits(InitialLotSize * Symbol.LotSize);

            EntryTime = TimeSpan.Parse(EntryTimeStr);
            LastPositionEntryDate = DateTime.MinValue.Date;

            PreviousTickTimeOfDay = TimeInUtc.TimeOfDay;
        }

        protected override void OnTick()
        {
            if (PreviousTickTimeOfDay < EntryTime
                && TimeInUtc.TimeOfDay >= EntryTime
                && LastPositionEntryDate < TimeInUtc.Date)
            {
                // Initial daily position entry.
                TradeResult result = ExecuteMarketOrder(InitialTradeType, Symbol.Name, initialVolume, RobotID, SLInPips, TPInPips);

                if (result.IsSuccessful)
                {
                    LastPositionEntryDate = TimeInUtc.Date;

                    Print(string.Format("New {0} position with ID {1} opened at {2} with SL at {3} and TP at {4}.",
                                        result.Position.TradeType.ToString().ToLower(),
                                        result.Position.Id,
                                        result.Position.EntryPrice,
                                        result.Position.StopLoss,
                                        result.Position.TakeProfit));

                    TradeType newTradeType = result.Position.TradeType == TradeType.Buy ? TradeType.Sell : TradeType.Buy;
                    double newVolume = Symbol.NormalizeVolumeInUnits(result.Position.VolumeInUnits * LotMultiplier, RoundingMode.Up);
                    double targetPrice = result.Position.EntryPrice;
                    targetPrice -= result.Position.TradeType == TradeType.Buy ? Symbol.PipSize * SLInPips : -Symbol.PipSize * SLInPips;

                    TradeResult result2 = PlaceStopOrder(newTradeType, Symbol.Name, newVolume, targetPrice, RobotID, SLInPips, TPInPips);

                    if (result2.IsSuccessful)
                    {
                        Print(string.Format("{0} stop order placed at {1}.",
                                            newTradeType.ToString(),
                                            result2.PendingOrder.TargetPrice));
                    }

                    else
                    {
                        Print(string.Format("{0} stop order could not be placed at {1}. Error: {2}.",
                                            newTradeType.ToString(),
                                            targetPrice,
                                            result2.Error));
                    }
                }

                else
                {
                    Print(string.Format("New {0} position could not be opened at {1} with SL at {2} and TP at {3}. Error: {4}.",
                                            result.Position.TradeType.ToString().ToLower(),
                                            Bars.ClosePrices.LastValue,
                                            SLInPips,
                                            TPInPips,
                                            result.Error));
                }
            }

            PreviousTickTimeOfDay = TimeInUtc.TimeOfDay;
        }

        protected override void OnStop()
        {

        }
    }
}

Automate log:

30/05/2022 06:46:40.172 | CBot instance [MR Question Example, USDJPY, h1] started.
30/05/2022 06:46:41.828 | CBot instance [MR Question Example, GBPAUD, D1] started.
30/05/2022 06:46:43.219 | CBot instance [MR Question Example, EURUSD, D1] started.
30/05/2022 06:48:00.063 | Executing Market Order to Buy 100000 USDJPY (SL: 5, TP: 10)
30/05/2022 06:48:00.063 | Executing Market Order to Buy 100000 GBPAUD (SL: 5, TP: 10)
30/05/2022 06:48:00.063 | Executing Market Order to Buy 100000 EURUSD (SL: 5, TP: 10)
30/05/2022 06:48:00.547 | → Executing Market Order to Buy 100000 GBPAUD (SL: 5, TP: 10) SUCCEEDED, Position PID37328971
30/05/2022 06:48:00.547 | New buy position with ID 37328971 opened at 1.75939 with SL at 1.75889 and TP at 1.76039.
30/05/2022 06:48:00.547 | Placing Stop Order to Sell 110000 GBPAUD (Price: 1.75889, SL: 5, TP: 10)
30/05/2022 06:48:38.985 | CBot instance [MR Question Example, EURUSD, D1] aborted by timeout.
30/05/2022 06:48:40.235 | CBot instance [MR Question Example, GBPAUD, D1] aborted by timeout.
30/05/2022 06:48:41.297 | CBot instance [MR Question Example, USDJPY, h1] aborted by timeout.

Journal log:

30/05/2022 06:48:00.110 | Request to Buy 1.00 Lots USDJPY (SL: 5, TP: 10) is sent to server
30/05/2022 06:48:00.110 | Request to Buy 1.00 Lots GBPAUD (SL: 5, TP: 10) is sent to server
30/05/2022 06:48:00.110 | Request to Buy 1.00 Lots EURUSD (SL: 5, TP: 10) is sent to server
30/05/2022 06:48:00.172 | → Request to Buy 1.00 Lots USDJPY (SL: 5, TP: 10) is ACCEPTED, order OID66288907 created (30/05/2022 06:48:00.136 UTC+0)
30/05/2022 06:48:00.219 | → Request to Buy 1.00 Lots GBPAUD (SL: 5, TP: 10) is ACCEPTED, order OID66288908 created (30/05/2022 06:48:00.194 UTC+0)
30/05/2022 06:48:00.219 | → Request to Buy 1.00 Lots EURUSD (SL: 5, TP: 10) is ACCEPTED, order OID66288909 created (30/05/2022 06:48:00.194 UTC+0)
30/05/2022 06:48:00.531 | → Order OID66288908 is FILLED at 1.75939, position PID37328971 (30/05/2022 06:48:00.426 UTC+0)
30/05/2022 06:48:00.531 | → Order OID66288907 is FILLED at 127.067, position PID37328970 (30/05/2022 06:48:00.436 UTC+0)
30/05/2022 06:48:00.547 | → Order OID66288909 is FILLED at 1.07585, position PID37328972 (30/05/2022 06:48:00.450 UTC+0)
30/05/2022 06:48:00.547 | Stop order to Sell 1.10 Lots GBPAUD (Price: 1.75889, SL: 5, TP: 10) is sent to server
30/05/2022 06:48:00.625 | → Stop order OID66288913 to Sell 1.10 Lots GBPAUD (Price: 1.75889, SL: 5, TP: 10) is ACCEPTED (30/05/2022 06:48:00.584 UTC+0)
30/05/2022 06:48:30.406 | → Stop order to SELL 1.10 Lots GBPAUD OID66288913 is FILLED at 1.75888, position PID37328973 (30/05/2022 06:48:30.380 UTC+0)
30/05/2022 06:48:30.485 | → Order OID66288910 is FILLED at 1.75889, position PID37328971 closed (30/05/2022 06:48:30.441 UTC+0)

 


@yisrgg
Replies

amusleh
30 May 2022, 10:15

Hi,

I tested your cBot code on cTrader 4.2 .NET 6 compiled and it works fine on multiple symbols, it opens both the position and the stop order on 4 different symbols running simultaneously. 


@amusleh

yisrgg
30 May 2022, 10:21

RE:

amusleh said:

Hi,

I tested your cBot code on cTrader 4.2 .NET 6 compiled and it works fine on multiple symbols, it opens both the position and the stop order on 4 different symbols running simultaneously. 

Thank you for the quick reply. Does that mean that this is a problem restricted to the Spotware cTrader (Public beta) 4.2.4 version only? Could you please confirm that the problem appears in that version? Thank you


@yisrgg

amusleh
30 May 2022, 10:22

RE: RE:

fcomanjoncabeza said:

amusleh said:

Hi,

I tested your cBot code on cTrader 4.2 .NET 6 compiled and it works fine on multiple symbols, it opens both the position and the stop order on 4 different symbols running simultaneously. 

Thank you for the quick reply. Does that mean that this is a problem restricted to the Spotware cTrader (Public beta) 4.2.4 version only? Could you please confirm that the problem appears in that version? Thank you

Hi,

I tested it on Spotware cTrader Beta 4.2.4 and it worked fine.


@amusleh

yisrgg
30 May 2022, 10:24

RE: RE: RE:

amusleh said:

fcomanjoncabeza said:

amusleh said:

Hi,

I tested your cBot code on cTrader 4.2 .NET 6 compiled and it works fine on multiple symbols, it opens both the position and the stop order on 4 different symbols running simultaneously. 

Thank you for the quick reply. Does that mean that this is a problem restricted to the Spotware cTrader (Public beta) 4.2.4 version only? Could you please confirm that the problem appears in that version? Thank you

Hi,

I tested it on Spotware cTrader Beta 4.2.4 and it worked fine.

|nteresting. How do think that the logs attached above can be explained?


@yisrgg

amusleh
31 May 2022, 11:42

RE: RE: RE: RE:

fcomanjoncabeza said:

amusleh said:

fcomanjoncabeza said:

amusleh said:

Hi,

I tested your cBot code on cTrader 4.2 .NET 6 compiled and it works fine on multiple symbols, it opens both the position and the stop order on 4 different symbols running simultaneously. 

Thank you for the quick reply. Does that mean that this is a problem restricted to the Spotware cTrader (Public beta) 4.2.4 version only? Could you please confirm that the problem appears in that version? Thank you

Hi,

I tested it on Spotware cTrader Beta 4.2.4 and it worked fine.

|nteresting. How do think that the logs attached above can be explained?

Hi,

I see "aborted by timeout" errors on your logs, are you sure you used the same posted code? because the aborted by timeout only happens if your cBot hang for more than 5 seconds while platform try to stop it .

 


@amusleh

yisrgg
01 Jun 2022, 08:54

RE: RE: RE: RE: RE:

amusleh said:

fcomanjoncabeza said:

amusleh said:

fcomanjoncabeza said:

amusleh said:

Hi,

I tested your cBot code on cTrader 4.2 .NET 6 compiled and it works fine on multiple symbols, it opens both the position and the stop order on 4 different symbols running simultaneously. 

Thank you for the quick reply. Does that mean that this is a problem restricted to the Spotware cTrader (Public beta) 4.2.4 version only? Could you please confirm that the problem appears in that version? Thank you

Hi,

I tested it on Spotware cTrader Beta 4.2.4 and it worked fine.

|nteresting. How do think that the logs attached above can be explained?

Hi,

I see "aborted by timeout" errors on your logs, are you sure you used the same posted code? because the aborted by timeout only happens if your cBot hang for more than 5 seconds while platform try to stop it .

 

Hello,

 

yes, that is exactly the same code. The "aborted by timeout" error occurs after stopping the bot, and not while it is running and has not yet been stopped.


@yisrgg

amusleh
01 Jun 2022, 10:16

Hi,

I tested again, and it works fine:

 


@amusleh