Problem with Pending Orders and ProtectionType

Created at 28 Feb 2025, 19:00
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!
FA

Falcorest

Joined 01.10.2024

Problem with Pending Orders and ProtectionType
28 Feb 2025, 19:00


Hello everyone,
I hope someone can help me solve an issue with my cBot.

First of all, I want to mention that I’m trying to learn how to create cBots, so for now, I’ve used a very simple code (not mine) and tried to add a logic for creating pending orders:

When the conditions for buy and sell are met, the cBot should first ensure that there are no other open positions. If there are none, it should proceed to place pending orders at a fixed distance, configurable from the control panel.

Once a Buy order is triggered, the code should then cancel all pending Buy orders. Same thing for Sell orders. This option is also configurable from the panel.

Unfortunately, when I run the cBot on the cloud, the pending orders are not placed when the openBuy or openSell conditions are met.

Additionally, I receive this error message in the log, which I don’t understand:
Error | Crashed in OnBar with TypeLoadException: Could not load type 'cAlgo.API.ProtectionType' from assembly 'cAlgo.API, Version=1.0.0.0, Culture=neutral, PublicKeyToken=3499da3018340880'.

I also want to specify that ProtectionType is a parameter I am forced to configure; otherwise, the system tells me that the code for pending orders is obsolete.

I’ve tried searching online for a possible solution but haven’t been able to find anything.

I hope someone can help me resolve this issue.

Thank you in advance, and I’m including the code of my cBot below:

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

namespace cAlgo
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class SampleRSIcBot : Robot
    {
        // POSITION PARAMETERS
        private double _volumeInUnits;
        
        [Parameter("Volume (Lots)", DefaultValue = 0.1, Group = "Trade", Step = 0.1)]
        public double VolumeInLots { get; set; }
        
        [Parameter("Distance in pips for Limit", DefaultValue = 4, Group = "Trade", Step = 1)]
        public double PipsDistance { get; set; }
        
        [Parameter("Take Profit (Pips)", DefaultValue = 20, Group = "Trade", Step = 1)]
        public double TakeProfitInPips { get; set; }

        [Parameter("Stop Loss (Pips)", DefaultValue = 9, Group = "Trade", Step = 1)]
        public double StopLossInPips { get; set; }
        
        [Parameter("Enable Cancel Pending Orders", DefaultValue = true, Group = "Trade")]
        public bool EnableCancelPendingOrders { get; set; }

        // RSI Parameters
        [Parameter("Source", Group = "RSI")]
        public DataSeries Source { get; set; }

        [Parameter("Periods", Group = "RSI", DefaultValue = 14)]
        public int Periods { get; set; }
        
        [Parameter("OB", Group = "RSI", DefaultValue = 70, MinValue = 0, MaxValue = 100)]
        public int OBLevel { get; set; }
        
        [Parameter("OS", Group = "RSI", DefaultValue = 30, MinValue = 0, MaxValue = 100)]
        public int OSLevel { get; set; }

        private RelativeStrengthIndex rsi;

        protected override void OnStart()
        {
            // Convert volume from lots to units
            _volumeInUnits = Symbol.QuantityToVolumeInUnits(VolumeInLots);
            rsi = Indicators.RelativeStrengthIndex(Source, Periods);
        }

        protected override void OnBar()
        {
            bool openBuy = rsi.Result.LastValue < OSLevel;
            Print("OpenBuy: " + openBuy); 
            
            bool openSell = rsi.Result.LastValue > OBLevel;
            Print("OpenSell: " + openSell);
            
            string _buyLabel = "WiCh-Buy";
            string _sellLabel = "WiCh-Sell";
        
            // Check if there are open Buy positions
            bool _hasOpenBuyPosition = Positions.Any(p => p.SymbolName == SymbolName && p.TradeType == TradeType.Buy && p.Label == _buyLabel);
        
            // Check if there are open Sell positions
            bool _hasOpenSellPosition = Positions.Any(p => p.SymbolName == SymbolName && p.TradeType == TradeType.Sell && p.Label == _sellLabel);
        
            // Open pending Buy order
            if (openBuy && !_hasOpenBuyPosition)
            {
                try
                {
                    PlaceLimitOrder(TradeType.Buy, SymbolName, _volumeInUnits, Symbol.Ask - PipsDistance * Symbol.PipSize, _buyLabel, StopLossInPips, TakeProfitInPips, ProtectionType.Relative);
                    Print("Buy Limit Order Placed");
                }
                catch (Exception ex)
                {
                    Print("Error placing Buy order: " + ex.Message);
                }
            }
            
            // Open pending Sell order
            if (openSell && !_hasOpenSellPosition)
            {
                try
                {
                    PlaceLimitOrder(TradeType.Sell, SymbolName, _volumeInUnits, Symbol.Bid + PipsDistance * Symbol.PipSize, _sellLabel, StopLossInPips, TakeProfitInPips, ProtectionType.Relative);
                    Print("Sell Limit Order Placed");
                }
                catch (Exception ex)
                {
                    Print("Error placing Sell order: " + ex.Message);
                }
            }
        
            // Cancel pending orders when one is executed
            if (EnableCancelPendingOrders)
            {
                var _openBuyPositions = Positions.FindAll(_buyLabel);
                var _openSellPositions = Positions.FindAll(_sellLabel);
        
                // If there is at least one open Buy position, cancel all pending Buy orders
                if (_openBuyPositions.Length > 0)
                {
                    var _buyPendingOrders = PendingOrders.Where(o => o.Label == _buyLabel && o.TradeType == TradeType.Buy).ToList();
                    foreach (var order in _buyPendingOrders)
                    {
                        CancelPendingOrder(order);
                        Print("Pending Buy order canceled: {0}", order.Id);
                    }
                }
        
                // If there is at least one open Sell position, cancel all pending Sell orders
                if (_openSellPositions.Length > 0)
                {
                    var _sellPendingOrders = PendingOrders.Where(o => o.Label == _sellLabel && o.TradeType == TradeType.Sell).ToList();
                    foreach (var order in _sellPendingOrders)
                    {
                        CancelPendingOrder(order);
                        Print("Pending Sell order canceled: {0}", order.Id);
                    }
                }
            }
        }
    }
}
 


@Falcorest
Replies

firemyst
01 Mar 2025, 02:51

Two suggestions from me:

 

  1. Don't run the bot in the cloud. There's too many people having too many issues. Run it locally, especially if you're just testing and playing around.
  2. The obsolete issue. Ignore it. Compile it anyway without the protection type parameter. It'll still run. That's what I've done when editing code in Visual Studio. Spotware has yet to post documentation or respond to threads asking what the protection type parameter is, or does.

@firemyst

Falcorest
03 Mar 2025, 08:39 ( Updated at: 03 Mar 2025, 08:54 )

RE: Problem with Pending Orders and ProtectionType

firemyst said: 

Two suggestions from me:

 

  1. Don't run the bot in the cloud. There's too many people having too many issues. Run it locally, especially if you're just testing and playing around.
  2. The obsolete issue. Ignore it. Compile it anyway without the protection type parameter. It'll still run. That's what I've done when editing code in Visual Studio. Spotware has yet to post documentation or respond to threads asking what the protection type parameter is, or does.

hi, firemyst,
Thank you for your help!
This morning I tested the cBot on the latest version of cTrader, and the error message is still the same. However, if I run the cBot locally, everything works fine.

I can't understand the reason.

It's a shame not to be able to use the "On Cloud" setting, which allows the cBot to stay active all day and be managed from any device.


@Falcorest

firemyst
03 Mar 2025, 10:47

RE: RE: Problem with Pending Orders and ProtectionType

Falcorest said: 

 

hi, firemyst,
Thank you for your help!
This morning I tested the cBot on the latest version of cTrader, and the error message is still the same. However, if I run the cBot locally, everything works fine.

I can't understand the reason.

It's a shame not to be able to use the "On Cloud" setting, which allows the cBot to stay active all day and be managed from any device.

You're welcome.

And I get your frustration.

It's even more frustrating that Spotware doesn't even seem to acknowledge these issues any more, and don't give us any updates on fixes. 

Right now, I don't even think they're working on or investigating the issue because they've said absolutely nothing and people have been reporting Cloud issues since Nov/Dec.

 


@firemyst

Falcorest
03 Mar 2025, 16:20

RE: RE: RE: Problem with Pending Orders and ProtectionType

firemyst said: 

 

Right now, I don't even think they're working on or investigating the issue because they've said absolutely nothing and people have been reporting Cloud issues since Nov/Dec.

 

O_O

So, at this point, I think a good solution might be to get a VPS.

Do you know if cTrader can run on a server with Windows Server 2022?


@Falcorest

firemyst
03 Mar 2025, 23:33

RE: RE: RE: RE: Problem with Pending Orders and ProtectionType

Falcorest said: 

firemyst said: 

 

Right now, I don't even think they're working on or investigating the issue because they've said absolutely nothing and people have been reporting Cloud issues since Nov/Dec.

 

O_O

So, at this point, I think a good solution might be to get a VPS.

Do you know if cTrader can run on a server with Windows Server 2022?

I've been using VPS for ages. Currently have one with 4GB memory running server 2019. It's doing fine. Here's who I have mine with:

newyorkcityservers . com/billing/aff.php?aff=74

They have awesome customer service and I've never experienced any down time with them except when I had my VPS reimaged from 2012 → 2019, which took me less than an hour to have done and reconfigure one Saturday afternoon.

 


@firemyst

CNsofia
05 Mar 2025, 23:30 ( Updated at: 10 Mar 2025, 09:04 )

I have exactly the same problem. “Error | Crashed in OnBar with TypeLoadException: Could not load type 'cAlgo.API.ProtectionType' from assembly 'cAlgo.API, Version=1.0.0.0, Culture=neutral, PublicKeyToken=3499da3018340880'.”

 


@CNsofia

Falcorest
10 Mar 2025, 15:34

RE: Problem with Pending Orders and ProtectionType

CNsofia said: 

I have exactly the same problem. “Error | Crashed in OnBar with TypeLoadException: Could not load type 'cAlgo.API.ProtectionType' from assembly 'cAlgo.API, Version=1.0.0.0, Culture=neutral, PublicKeyToken=3499da3018340880'.”

 

i just deleted the protection.type parameter and now the bot works, even if the console get the “obsolete” error message


@Falcorest