Max DrawDown

Created at 26 Apr 2013, 11:26
cAlgo_Fanatic's avatar

cAlgo Fanatic

Joined 10.01.2013

Max DrawDown
26 Apr 2013, 11:26


This is the sample martingale robot included in the cAlgo with the calculation of Max Draw Down.

using System;
using System.Collections.Generic;
using cAlgo.API;

namespace cAlgo.Robots
{
    [Robot]
    public class MaxDrawdown:Robot
    {
        [Parameter("Position Label", DefaultValue = "Martingale MaxDrawDown")]
        public string MyLabel { get; set; }

        [Parameter("Initial Volume", DefaultValue = 10000, MinValue = 0)]
        public int InitialVolume { get; set; }

        [Parameter("Stop Loss", DefaultValue = 40)]
        public int StopLoss { get; set; }

        [Parameter("Take Profit", DefaultValue = 40)]
        public int TakeProfit { get; set; }

        private Random random = new Random();

        private double peak;
        private List<double> drawdown = new List<double>();
        private double maxDrawdown;

        protected override void OnStart()
        {
            Positions.Closed += PositionsOnClosed;
            ExecuteOrder(InitialVolume, GetRandomTradeCommand());
        }

        private void PositionsOnClosed(PositionClosedEventArgs args)
        {
            var position = args.Position;
            if (position.Label != MyLabel) return;

            Print("Position labeled {0} has closed", position.Label);

            if (position.NetProfit > 0)
            {
                ExecuteOrder(InitialVolume, GetRandomTradeCommand());
            }
            else
            {                
                ExecuteOrder((int)position.Volume * 2, position.TradeType);
            }

            GetMaxDrawDown();
        }

        private void ExecuteOrder(int volume, TradeType tradeType)
        {
            ExecuteMarketOrder(tradeType, Symbol, volume, MyLabel, StopLoss, TakeProfit);
        }


        private void GetMaxDrawDown()
        {
            peak = Math.Max(peak, Account.Balance);
                        
            drawdown.Add((peak - Account.Balance) / peak * 100);
            drawdown.Sort();

            maxDrawdown = drawdown[drawdown.Count - 1];

            Print("Max DrawDown = {0}", Math.Round(maxDrawdown, 2));
            
        }

        protected override void OnError(Error error)
        {
            if (error.Code == ErrorCode.BadVolume)
                Stop();
        }

        private TradeType GetRandomTradeCommand()
        {
            return random.Next(2) == 0 ? TradeType.Buy : TradeType.Sell;
        }

    }
}

 


@cAlgo_Fanatic
Replies

cogs
17 Jan 2014, 08:35

Error: Using the genetic type 'Systems.Collections.Generic.List<T>' requires 1 type arguements


@cogs

cTrader Team
17 Jan 2014, 09:51

RE:

Thank you, it has been corrected.

cogs said:

Error: Using the genetic type 'Systems.Collections.Generic.List' requires 1 type arguements

 


@Spotware

Bots4Us
04 Jan 2021, 17:05

MaxDrawDown - All - Long - Short

Dear cTrader Experts,
The above code snippet works great for the Max. DrawDown of All Trades, but I struggle to separately determine the Max. DrawDown for Long vs. Short Trades in OnPositionClosed event. However, the Trade Statistics do include separate Max. DD calculations. Can you please explain where and how to do this?
Thank you.
Bots4Us

 

 


@Bots4Us

Bots4Us
08 Jan 2021, 23:34

RE: MaxDrawDown - All - Long - Short

Bots4Us said:

Dear cTrader Experts,
The above code snippet works great for the Max. DrawDown of All Trades, but I struggle to separately determine the Max. DrawDown for Long vs. Short Trades in OnPositionClosed event. However, the Trade Statistics do include separate Max. DD calculations. Can you please explain where and how to do this?
Thank you.
Bots4Us

Please consider this closed. I figured this out myself. Thx.


@Bots4Us