Category Other  Published on 22/11/2023

Tick Timer

Description

Counts the seconds from last price change. Doesn't function properly if there's a gap in the price action, although this occurrence is very rare on higher timeframes.

 

Last update 29.1.2024 (DD.MM.YYYY)

Update log:

v1.12 - Color picker added

v1.1 - Position & color parameters added, warning fixed

v1.0 - Release (300+ installs)


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

namespace cAlgo
{
    [Indicator(IsOverlay = true, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class TickTimer : Indicator
    {
        [Parameter("Public v1.12", DefaultValue = "https://ctrader.com/users/profile/64575", Group = "m0")]
        public string M_ { get; set; }
        
        [Parameter("Centered?", DefaultValue = true, Group = "Settings")]
        public bool CenterPos { get; set; }
        
        [Parameter("Number Color", DefaultValue = "FF808080", Group = "Settings")]
        public Color NumCol { get; set; }
        
        [Parameter("Timer Reset Color", DefaultValue = "FFFE0000", Group = "Settings")]
        public Color ResetCol { get; set; }
        
        private DateTime lastTickTime;
        private int elapsedSeconds;

        protected override void Initialize()
        {
            lastTickTime = Server.Time;
            Timer.Start(1);
            DisplayElapsedTime();
        }

        public override void Calculate(int index)
        {
            if (Bid != Bars.OpenPrices.LastValue)
            {
                lastTickTime = Server.Time;
                Timer.Stop();
                Timer.Start(1);
                //Couldn't get the audio notifications to work
                //Notifications.PlaySound("C:\\Windows\\Media\\tada.wav");
            }
        }

        protected override void OnTimer()
        {
            elapsedSeconds = (int)(Server.Time - lastTickTime).TotalSeconds;
            DisplayElapsedTime();
        }

        private void DisplayElapsedTime()
        {
            Chart.RemoveObject("timerLabel");
            {
                if (CenterPos)
                {
                    if (elapsedSeconds == 1)
                    {
                        Chart.DrawStaticText("timerLabel", " " + elapsedSeconds.ToString(), VerticalAlignment.Center, HorizontalAlignment.Center, ResetCol);
                    }
                    else
                    {
                        Chart.DrawStaticText("timerLabel", " " + elapsedSeconds.ToString(), VerticalAlignment.Center, HorizontalAlignment.Center, NumCol);
                    }
                }
                else
                {
                    if (elapsedSeconds == 1)
                    {
                        Chart.DrawStaticText("timerLabel", " " + elapsedSeconds.ToString(), VerticalAlignment.Bottom, HorizontalAlignment.Right, ResetCol);
                    }
                    else
                    {
                        Chart.DrawStaticText("timerLabel", " " + elapsedSeconds.ToString(), VerticalAlignment.Bottom, HorizontalAlignment.Right, NumCol);
                    }
                }
            }
        }
    }
}

mirk0's avatar
mirk0

Joined on 15.06.2022

  • Distribution: Free
  • Language: C#
  • Trading platform: cTrader Automate
  • File name: Tick Timer.algo
  • Rating: 5
  • Installs: 441
  • Modified: 29/01/2024 20:48
Comments
Log in to add a comment.
mirk0's avatar
mirk0 · 1 year ago

@VEI5S6C4OUNT0 thank you for your suggestion but it didn't work for me.

VE
VEI5S6C4OUNT0 · 1 year ago

         Also try adding this method.

           BeginInvokeOnMainThread(() =>

           {

           Notifications.PlaySound(@"C:\Windows\Media\Windows Information Bar.wav"); } );

VE
VEI5S6C4OUNT0 · 1 year ago

C: \Windows\Media\Windows Information Bar.wav

mirk0's avatar
mirk0 · 1 year ago

@YesOrNot Hi! Thanks for your comment. I actually don't use this on trading. Just made it out of curiosity haha

YE
YesOrNot · 1 year ago

Hi ! 

I just see your work, i have try the same thing but other way, can i ask you how you use it ?

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

namespace cAlgo
{
    [Indicator(IsOverlay = false, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class RenkoDelta : Indicator

    {
        [Parameter("Min", DefaultValue = 1)]
        public double Min { get; set; }
        [Parameter("Max", DefaultValue = 2)]
        public double Max { get; set; }
            
        [Output("Result1", PlotType = PlotType.Histogram)]
        public IndicatorDataSeries Result1 { get; set; }
        [Output("Result11", LineColor = "Lime", PlotType = PlotType.Histogram, Thickness = 5)]
        public IndicatorDataSeries Result11 { get; set; }
        [Output("Result2", LineColor = "Lime", PlotType = PlotType.Points, Thickness = 5)]
        public IndicatorDataSeries Result2 { get; set; }
        [Output("Result3", LineColor = "Red", PlotType = PlotType.Points, Thickness = 5)]
        public IndicatorDataSeries Result3 { get; set; }

        private IndicatorDataSeries Result;
        protected override void Initialize()
        {
            Result = CreateDataSeries();
        }

        public override void Calculate(int index)
        {
            TimeSpan t = Bars.OpenTimes[index] - Bars.OpenTimes[index - 1];
            if (t.TotalMinutes > 2880)
                Result[index] = t.TotalMinutes - 2880;
            else
                Result[index] = t.TotalMinutes;

            Result1[index] = Bars.ClosePrices.Last(0) > Bars.OpenPrices.Last(0) ? Result[index] : double.NaN;
            Result11[index] = Bars.ClosePrices.Last(0) < Bars.OpenPrices.Last(0) ? Result[index] : double.NaN;

            Result2[index] = Result[index] > Min && Result[index] < Max && Bars.OpenPrices.Last(0) < Bars.ClosePrices.Last(0) ? -2 : double.NaN;
            Result3[index] = Result[index] > Min && Result[index] < Max && Bars.OpenPrices.Last(0) > Bars.ClosePrices.Last(0) ? -2 : double.NaN;

        }
    }
}