Can you programmatically Start a cBot

Created at 17 Apr 2023, 06:07
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!
JE

Jexodus

Joined 15.04.2023

Can you programmatically Start a cBot
17 Apr 2023, 06:07


G'day Automaters!

I'm building a project which is essentially broken up in to separate areas that look after individual functionality. I have .cs files and classes that look after these general areas:

  • Chart indicator
  • Display management (button overlays, etc)
  • Database management
  • Messaging management, and
  • Trade management

The issue I have is with the trade management portion of the project. Essentially, what I'd like to do is have a class that inherits Robot that can do all my trade management. The issue I've found is that there does not seem to be a programmatic way to start the created Robot class.  Is that indeed possible?

If not, that leads me to the other side of this issue.  I have developed the Trade Management files and classes (inheriting from indicator) which gives me access to Position.ModifyStopLossPrice().  I can loop through all the position data, calculate the new stop loss, etc. However, when I get to the execution of the ModifyStopLossPrice method, I get a notification that the position is being modified in the Automate log window, and then the following Invalid Operation error (obviously the position is not modified successfully):

17/04/2023 10:07:44.660 | System.InvalidOperationException: Operation is not valid due to the current state of the object.
   at cTrader.Automate.Host.Runtime.Controllers.Trade.SmallTradeCommandChannel.SendTradeMessage(AutomateTradeMessage message)
   at cTrader.Automate.Host.Runtime.Controllers.Trade.SmallTradeApiController.OnBeginModifyPositionProtection(ISmallSymbol symbol, AutomateModifyPositionProtectionCommandDto& cmdDto)
   at cTrader.Automate.Host.Runtime.Controllers.Trade.SmallTradeApiControllerBase.BeginModifyPositionProtection(AutomateModifyPositionProtectionCommandDto& cmdDto)
   at cTrader.Automate.Host.Runtime.Controllers.Trade.SmallTradeApiControllerBase.cTrader.Automate.Host.Runtime.Controllers.Trade.ISmallTradeApiController.BeginModifyPositionProtection(AutomateModifyPositionProtectionCommandDto& cmdDto)
   at cTrader.Automate.Host.Runtime.Api.Trade.SmallTradeMethods.ModifyPositionProtection(AutomateModifyPositionProtectionCommandDto& commandDto)
   at cTrader.Automate.Host.Runtime.Api.Trade.SmallTradeMethods.cTrader.Automate.Host.Runtime.Api.Trade.ISmallTradeMethods.ModifyPositionProtection(AutomateModifyPositionProtectionCommandDto& commandDto)
   at cTrader.Automate.Adapters.TradeApi.TradeMethodsAdapter.ModifyPosition(Position position, Nullable`1 stopLoss, Nullable`1 takeProfit, Boolean hasTrailingStop, Nullable`1 stopLossTriggerMethod)
   at cTrader.Automate.Adapters.OrderApi.PositionAdapter.ModifyStopLossPrice(Nullable`1 stopLoss)
   at ZipLine_Control_Center.TradeManager.ModifyTradeStopLoss(Position position, Double stopLossLevel) in D:\Documents\cAlgo\Sources\Indicators\ZipLine Control Center\ZipLine Control Center\TradeManager.cs:line 161

Is the ModifyStopLossPrice method able to be used from an Indicator?

I would prefer to be able to start a cBot as my trade manager as it gives me more flexibility in design; however, if that is not possible is there a reason for getting an Invalid Operation error that I'm missing?

Thanks in advance for any assistance.


@Jexodus
Replies

firemyst
18 Apr 2023, 09:31

No, you cannot start a bot programmatically (at least as of cTrader version 4.5.x). However, you can stop a cbot manually or programmatically.

 

"Is the ModifyStopLossPrice method able to be used from an Indicator?"

I'm not sure honestly as I've never tried. However, since you don't post your code that does it, are you sure the value you're passing in for "position" isn't null? Or actually has a reference to a valid, open trade?


@firemyst

Jexodus
18 Apr 2023, 11:21

RE:

Thanks for the confirmation about the cBot.  I thought that would be the case.

I am quite certain that the value I', passing in for "position" is not null, as i am referencing the position object and drawing data off it for stop loss calculation. 

        public void AutoTradeManagement()
        {
            double stopLossLevel = double.NaN;

            foreach (Position position in Positions)
            {
                SymbolData currentSymbol = DataManager.GetSymbolrecord(position.SymbolName);

                switch (currentSymbol.AutoStops)
                {
                    // Stop loss is calculated and returned here through calling relevant methods.
                    stopLossLevel = // stop loss calculation
                }

                Print($"Stop loss is: {stopLossLevel}");
                position.ModifyStopLossPrice(stopLossLevel);
            }
        }

The other clue that the position object not null is that the automate log shows the following:

18/04/2023 15:55:06.349 | Modifying position PIDXXXXXXX (SL: 1.09389, TP: 1.09776)

I have remove the PID from the output. The take profit is not calculated by my code, so can only be accessed from the position object. As you can see, it also shows the calculated stop loss value, showing a suitable value has been parsed into the ModifyStopLossPrice method.  This one really has me stumped...


@Jexodus