How to calculate daily and monthly drawdown in cAlgo.

Created at 03 Sep 2016, 17:51
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!
UC

ucpva_reg

Joined 03.09.2016

How to calculate daily and monthly drawdown in cAlgo.
03 Sep 2016, 17:51


Hello,

How to calculate daily and monthly drawdown in cAlgo.

Thanks.


@ucpva_reg
Replies

fxctrader
13 Apr 2022, 04:36

Can ADMIN help on this?

Also finding a way to determine daily DD? Any sample would be highly appreciated.

Thanks.


amusleh
13 Apr 2022, 11:53

Hi,

This might help you:

using cAlgo.API;
using System;
using System.Globalization;
using System.Linq;

namespace NewcBot
{
    [Robot(AccessRights = AccessRights.None)]
    public class NewcBot : Robot
    {
        private const string DATE_FORMAT = "dd/MM/yyyy HH:mm:ss";

        [Parameter("Initial Balance", DefaultValue = 10000)]
        public double InitialBalance { get; set; }

        [Parameter("Start Time", DefaultValue = DATE_FORMAT)]
        public string StartTime { get; set; }

        [Parameter("End Time", DefaultValue = DATE_FORMAT)]
        public string EndTime { get; set; }

        protected override void OnStart()
        {
            DateTime startTime, endTime;

            if (!DateTime.TryParseExact(StartTime, DATE_FORMAT, CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal, out startTime))
            {
                Print("Invalid start time");

                Stop();
            }

            if (!DateTime.TryParseExact(EndTime, DATE_FORMAT, CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal, out endTime))
            {
                Print("Invalid end time");

                Stop();
            }

            var history = from trade in History
                          where trade.EntryTime >= startTime && trade.EntryTime <= endTime
                          where trade.ClosingTime >= startTime && trade.ClosingTime <= endTime
                          orderby trade.ClosingTime
                          select trade;

            var troughValue = InitialBalance;
            var peakValue = InitialBalance;
            var currentBalance = InitialBalance;
            var tradeCount = 0;
            var maxDrawdown = 0.0;
            DateTime? peakTime = null;
            DateTime? troughTime = null;

            foreach (var trade in history)
            {
                // As we don't have account registration date we use the first trade
                // closing time as initial value for peak and trough times
                if (peakTime.HasValue == false) peakTime = trade.EntryTime;
                if (troughTime.HasValue == false) troughTime = peakTime;

                tradeCount++;

                currentBalance += trade.NetProfit;

                var isPeakOrTroughChanged = false;

                if (currentBalance > peakValue)
                {
                    peakValue = currentBalance;

                    isPeakOrTroughChanged = true;

                    peakTime = trade.ClosingTime;
                }

                if (currentBalance < troughValue)
                {
                    troughValue = currentBalance;

                    isPeakOrTroughChanged = true;

                    troughTime = trade.ClosingTime;
                }

                if (isPeakOrTroughChanged && peakTime < troughTime)
                {
                    var currentDrawdown = (troughValue - peakValue) / peakValue * 100;

                    if (currentDrawdown < maxDrawdown)
                    {
                        maxDrawdown = currentDrawdown;
                    }
                }
            }

            Print("Peak Value: {0} | Trough Value: {1} | Max DD %: {2} | # Of Trades: {3}", peakValue, troughValue, maxDrawdown, tradeCount);
        }
    }
}

You have to pass the initial account balance and start/end time for max drawdown period.


@amusleh