Category Other  Published on 22/10/2019

Ctrl timeframes

Description

View bars from other timeframes as transparent rectangles.

Press "Ctrl" to temporary hide (or show - when parameter Show By Default disabled) bars.


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

namespace cAlgo
{
    [Indicator(IsOverlay = true, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class CtrlTimeframes : Indicator
    {
        private static TimeFrame[] TimeFrames;
        private int _tfIndex = 0;

        static CtrlTimeframes()
        {
            TimeFrames = new[] 
            {
                TimeFrame.Minute,
                //TimeFrame.Minute2,
                //TimeFrame.Minute3,
                //TimeFrame.Minute4,
                TimeFrame.Minute5,
                //TimeFrame.Minute6,
                //TimeFrame.Minute7,
                //TimeFrame.Minute8,
                ///TimeFrame.Minute9,
                TimeFrame.Minute10,
                //TimeFrame.Minute15,
                //TimeFrame.Minute20,
                //TimeFrame.Minute30,
                //TimeFrame.Minute45,
                TimeFrame.Hour,
                //TimeFrame.Hour2,
                //TimeFrame.Hour3,
                TimeFrame.Hour4,
                //TimeFrame.Hour6,
                TimeFrame.Hour8,
                //TimeFrame.Hour12,
                TimeFrame.Daily,
                //TimeFrame.Day2,
                //TimeFrame.Day3,
                //TimeFrame.Weekly
                //TimeFrame.Monthly
            };
        }

        [Parameter("Show by default", DefaultValue = true)]
        public bool ShowByDefault { get; set; }

        public override void Calculate(int index)
        {
            // do nothing
        }

        protected override void Initialize()
        {
            if (!TimeFrames.Any(t => t == TimeFrame))
            {
                return;
            }

            _tfIndex = Array.IndexOf(TimeFrames, TimeFrame);

            if (_tfIndex < TimeFrames.Length - 1)
            {
                for (int i = _tfIndex + 1; i < TimeFrames.Length; i++)
                {
                    MarketData.GetSeries(TimeFrames[i]);
                }

                Chart.MouseMove += OnChartMouseMove;
            }
        }

        private void OnChartMouseMove(ChartMouseEventArgs args)
        {
            for (int i = _tfIndex + 1; i < TimeFrames.Length; i++)
            {
                var show = ShowByDefault ? !args.CtrlKey : args.CtrlKey;

                var targetSeries = MarketData.GetSeries(TimeFrames[i]);

                var barTime = MarketSeries.OpenTime[(int)Math.Floor(args.BarIndex)];
                var targetBarIndex = targetSeries.OpenTime.GetIndexByTime(barTime);

                if (!show || targetBarIndex <= 0 || targetBarIndex >= targetSeries.OpenTime.Count - 1)
                {
                    Chart.RemoveObject("bar" + targetSeries.TimeFrame.ToString());
                    continue;
                }

                var openTime = targetSeries.OpenTime[targetBarIndex];
                var closeTime = targetSeries.OpenTime[targetBarIndex + 1];

                double open = targetSeries.Open[targetBarIndex];
                double close = targetSeries.Close[targetBarIndex];

                var targetBar = Chart.DrawRectangle(
                    "bar" + targetSeries.TimeFrame.ToString(),
                    TimeOnChart(openTime),
                    open,
                    TimeOnChart(closeTime),
                    close,
                    Color.FromArgb(50, open > close ? Chart.ColorSettings.BearFillColor : Chart.ColorSettings.BullFillColor));

                targetBar.IsFilled = true;
                targetBar.Thickness = 0 ;
                targetBar.IsInteractive = false;
            }
        }

        private DateTime TimeOnChart(DateTime time)
        {
            var timeIndex = MarketSeries.OpenTime.GetIndexByTime(time);

            var t1 = MarketSeries.OpenTime[timeIndex - 1];
            var t2 = MarketSeries.OpenTime[timeIndex];

            return t1.AddMilliseconds((t2 - t1).TotalMilliseconds / 2);
        }
    }
}


devman's avatar
devman

Joined on 22.10.2019

  • Distribution: Free
  • Language: C#
  • Trading platform: cTrader Automate
  • File name: CtrlTimeframes.algo
  • Rating: 5
  • Installs: 1698
  • Modified: 13/10/2021 09:55
Comments
Log in to add a comment.
No comments found.