Withdrawal and deposit detection.

Created at 03 Mar 2023, 17:10


Joined 14.10.2018

Withdrawal and deposit detection.
03 Mar 2023, 17:10

In order to do proper Riskmanagement tehre is no way of detecting a withdrawl or deposit on a bot running on several symbol instances.
In order to enable this we need 2 events on the account class.

OnWithdrawal(WithdrawalEventArgs args){}

OnDeposit(DepositEventArgs args){}

Best rgds,

cTrader Automate

03 Mar 2023, 20:33


It would be great indeed, to have these events available.
Unfortunately, not every useful tool is available on cAlgo and sometimes we need to work around and be a bit "archaic" to get to something similar.

In fact, I have suggested for something similar a while ago:


For the time being you can try the following code. Let me know if it helps!

using cAlgo.API;
using System.Linq;

namespace cAlgo.Robots
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class DepositsWithdrawals_Check : Robot
        #region Private Variables

        private double myDeposits;
        private double myWithdrawals;
        private double realisedNetProfit;


        #region Methods

        protected override void OnStart()
            Positions.Closed += Positions_Closed; // Subscribes to the Positions_Closed event

            var cBotClosedTrades = History.ToArray();

            foreach (var trade in cBotClosedTrades)
                realisedNetProfit += trade.NetProfit;

            myWithdrawals = 0; // Abs value ( > 0 ) !
            myDeposits = Account.Equity - realisedNetProfit + myWithdrawals; // myDeposits = (deposits - withdrawals) balance when cBot starts

        // Will update realisedNetProfit and myDeposits everytime a position is closed
        private void Positions_Closed(PositionClosedEventArgs obj)
            realisedNetProfit += obj.Position.NetProfit;
            myDeposits = Account.Equity - realisedNetProfit + myWithdrawals;

        // Selected the desired timeframe for deposits/withdrawals to be checked ( I used OnTick() here. You can use OnBar, etc.)
        protected override void OnTick()
            if (Account.Equity - realisedNetProfit + myWithdrawals > myDeposits) // A deposit has been made
                myDeposits = Account.Equity - realisedNetProfit + myWithdrawals; // Updates myDeposits
                // Do something

            if (Account.Equity - realisedNetProfit + myWithdrawals < myDeposits) // A withdrawal has been made
                myWithdrawals = myDeposits + realisedNetProfit - Account.Equity; // Updates myWithdrawals
                // Do something

                // No deposit or withdral has been made

        protected override void OnStop()





23 Apr 2023, 20:40


great workaround, had something similar. but i'd like Spotware to provide the event it they are able to.
It's a pitty they are totally silent and do not answer these suggestions with a 'is possible or is not possible' so we can write our own libraries for things like this if it will never come from spotware.