Can anyone fix this clock indicator? Thanks.

Created at 21 Aug 2019, 16:16
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!
YouFX65's avatar

YouFX65

Joined 26.07.2017

Can anyone fix this clock indicator? Thanks.
21 Aug 2019, 16:16


When I try to build it, I receive the message:

Error CS0246: The namespace name or type 'ElapsedEventArgs' could not be found. Need a using directive or an assembly reference?

Link: https://ctrader.com/algos/indicators/show/217

SOURCE CODE:

using System;
using cAlgo.API;
//using System.Timers;

namespace cAlgo.Indicators
{
    [Indicator(IsOverlay = true, AccessRights = AccessRights.None)]
    public class Clock : Indicator
    {
        private readonly Timer _timer = new Timer();
        private TimeSpan _timeFrame;
        private TimeSpan _nextOpenTime;
        private int _serverHourDiff;
        private DateTime _serverTime;
        private int _index;

        protected override void Initialize()
        {
            _timer.Elapsed += OnTimedEvent;
            //  OnTimedEvent is called at each timer tick
            _timer.Interval = 1000;
            // Timer will tick every Interval (milliseconds)
            _timer.Enabled = true;
            // Enable the timer
            _timer.Start();
            // Start the timer
        }


        private void OnTimedEvent(object sender, ElapsedEventArgs e)
        {

            _index = MarketSeries.Close.Count - 1;
            _timeFrame = MarketSeries.OpenTime[_index] - MarketSeries.OpenTime[_index - 1];

            if (_timeFrame <= TimeSpan.FromHours(24.0))
            {
                _nextOpenTime = MarketSeries.OpenTime[_index].TimeOfDay + _timeFrame;
                _serverHourDiff = MarketSeries.OpenTime[_index].TimeOfDay.Hours - DateTime.Now.TimeOfDay.Hours;
                _serverTime = DateTime.Now.AddHours(_serverHourDiff);

                ChartObjects.DrawText("CountDown", (_nextOpenTime - _serverTime.TimeOfDay).ToString().Substring(0, 6), StaticPosition.TopRight, Colors.Orange);
            }
        }

        public override void Calculate(int index)
        {
            // Calculate value at specified index
            // Result[index] = ...
        }
    }
}

 


@YouFX65
Replies

PanagiotisCharalampous
26 Aug 2019, 11:19

Hi youforex,

See below

using System;
using cAlgo.API;
using System.Timers;

namespace cAlgo.Indicators
{
    [Indicator(IsOverlay = true, AccessRights = AccessRights.None)]
    public class Clock : Indicator
    {
        private readonly System.Timers.Timer _timer = new System.Timers.Timer();
        private TimeSpan _timeFrame;
        private TimeSpan _nextOpenTime;
        private int _serverHourDiff;
        private DateTime _serverTime;
        private int _index;

        protected override void Initialize()
        {
            _timer.Elapsed += OnTimedEvent;
            //  OnTimedEvent is called at each timer tick
            _timer.Interval = 1000;
            // Timer will tick every Interval (milliseconds)
            _timer.Enabled = true;
            // Enable the timer
            _timer.Start();
            // Start the timer
        }


        private void OnTimedEvent(object sender, ElapsedEventArgs e)
        {

            _index = MarketSeries.Close.Count - 1;
            _timeFrame = MarketSeries.OpenTime[_index] - MarketSeries.OpenTime[_index - 1];

            if (_timeFrame <= TimeSpan.FromHours(24.0))
            {
                _nextOpenTime = MarketSeries.OpenTime[_index].TimeOfDay + _timeFrame;
                _serverHourDiff = MarketSeries.OpenTime[_index].TimeOfDay.Hours - DateTime.Now.TimeOfDay.Hours;
                _serverTime = DateTime.Now.AddHours(_serverHourDiff);

                ChartObjects.DrawText("CountDown", (_nextOpenTime - _serverTime.TimeOfDay).ToString().Substring(0, 6), StaticPosition.TopRight, Colors.Orange);
            }
        }

        public override void Calculate(int index)
        {
            // Calculate value at specified index
            // Result[index] = ...
        }
    }
}

Best Regards,

Panagiotis


@PanagiotisCharalampous