Trying to code a Bot that should Display the max Drawdown of the last 24Hours

Created at 27 Jan 2023, 14: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!
MI

milsenace

Joined 27.01.2023

Trying to code a Bot that should Display the max Drawdown of the last 24Hours
27 Jan 2023, 14:00


Hello,

 

Iam trying to code a Bot that only should print on the chart the max drawdown of the last 24 hours resetting itself every 24 hours at 00:00 UTC.

 

This is what I did come up with so far:

 

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

namespace cAlgo
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class DrawdownRecorder : Robot
    {
        [Parameter(DefaultValue = 24)]
        public int RecordingInterval { get; set; }

        private double _previousHighWaterMark;
        private DateTime _lastRecordTime;
        private string _drawdownLabel;

        protected override void OnStart()
        {
            _previousHighWaterMark = Account.Equity;
            _lastRecordTime = Server.Time;
            _drawdownLabel = "Drawdown";
        }

        protected override void OnTick()
        {
            if (Server.Time.Hour == 0 && Server.Time.Minute == 0 && Server.Time.Second == 0)
            {
                _lastRecordTime = Server.Time;
                _previousHighWaterMark = Account.Equity;
            }
            else if ((Server.Time - _lastRecordTime).TotalHours >= RecordingInterval)
            {
                double currentEquity = Account.Equity;
                double drawdown = _previousHighWaterMark - currentEquity;
                _previousHighWaterMark = Math.Max(_previousHighWaterMark, currentEquity);

                ChartObjects.RemoveObject(_drawdownLabel);
                ChartObjects.DrawText(_drawdownLabel, "24 hour Drawdown : " + drawdown, StaticPosition.TopLeft, Colors.Red);
                _lastRecordTime = Server.Time;
            }
        }
    }
}

Sadly when I build it it gives me some warnings, but when I try to run it I see nothing on the chart displayed.

 

Can someone maybe guide me in the right direction?


@milsenace
Replies

milsenace
27 Jan 2023, 14:56 ( Updated at: 27 Jan 2023, 14:57 )

I could build it now without any warnings or Failures with this version:

 

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

namespace cAlgo
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class DrawdownRecorder : Robot
    {
        [Parameter(DefaultValue = 24)]
        public int RecordingInterval { get; set; }

        private double _previousHighWaterMark;
        private DateTime _lastRecordTime;
        private string _drawdownLabel;

        protected override void OnStart()
        {
            _previousHighWaterMark = Account.Equity;
            _lastRecordTime = Server.Time;
            _drawdownLabel = "Drawdown";
        }

        protected override void OnTick()
        {
            if (Server.Time.Hour == 0 && Server.Time.Minute == 0 && Server.Time.Second == 0)
            {
                _lastRecordTime = Server.Time;
                _previousHighWaterMark = Account.Equity;
            }
            else if ((Server.Time - _lastRecordTime).TotalHours >= RecordingInterval)
            {
                double currentEquity = Account.Equity;
                double drawdown = _previousHighWaterMark - currentEquity;
                _previousHighWaterMark = Math.Max(_previousHighWaterMark, currentEquity);

                Chart.RemoveObject(_drawdownLabel);
                Chart.DrawStaticText(_drawdownLabel, "24 hour Drawdown : " + drawdown, VerticalAlignment.Top, HorizontalAlignment.Right, Color.Yellow);
                _lastRecordTime = Server.Time;
            }
        }
    }
}

But when I start it nothing is displayed on the Chart :(


@milsenace