ModifyStopLossPrice consistantly throwing exception.

Created at 24 Apr 2023, 03:13
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

ModifyStopLossPrice consistantly throwing exception.
24 Apr 2023, 03:13


I've run into an issue in my indicator which I believe may be a platform issue (big call, I know... but I think I've eliminated all the other possibilities...)

Simply, in my indicator, i use a position object and call ModifyStopLossPrice and parse in the new stop loss value. I have simplified my code during testing this issue to even hard code the stop loss value directly it to a current open position.  I have tried refactoring and accessing this method in different ways, even down to placing it directly in the calculate method. Everything I have tried has yielded the same results.  This is the code I'm running. As I have said, this has been accessed in various means, even from within calculate method:

foreach(Position position in Positions)
{
    if (position.SymbolName == "CADCHF" && position.StopLoss != 0.65770)
    {
        try
        {
            position.ModifyStopLossPrice(0.65770);
        }
        catch (Exception ex)
        {
            Print(ex.ToString());
        }
    }
}

The resulting error message is as follows:

24/04/2023 06:58:40.086 | 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 cAlgo.ZipLineControlCenter.Calculate(Int32 index) in D:\Documents\cAlgo\Sources\Indicators\ZipLine Control Center\ZipLine Control Center\ZipLine Control Center.cs:line 155

Within the automate log, the trade message is sent, which shows the take profit of the position. (I removed the position ID)

24/04/2023 06:58:40.086 | Modifying position PIDXXXXXXXX (SL: 0.6577, TP: 0.66382)

This indicates to me that the position object is not null and the stop loss value has been parsed into the method correctly. After this message is shown, the exception above is thrown.

Is this a case of this method being accessible through the Indicator class when it actually shouldn't be? Is there potentially a platform issue? Or am I just an idiot and have missed something obvious?

Thanks in advance for reading this far and any assistance/info/guidance you provide.


@Jexodus
Replies

PanagiotisChar
24 Apr 2023, 09:44

Hi Jexodus,

You cannot modify positions using an indicator. You need to use a cBot.

Aieden Technologies

Need help? Join us on Telegram

Need premium support? Trade with us

 


@PanagiotisChar

Jexodus
24 Apr 2023, 09:49

RE:

PanagiotisChar said:

Hi Jexodus,

You cannot modify positions using an indicator. You need to use a cBot.

Aieden Technologies

Need help? Join us on Telegram

Need premium support? Trade with us

 

Thanks for the reply PanagiotisChar. I thought that might be the case. Is it not weird that the method is accessible from the indicator class though? Noting that it can't be used....


@Jexodus