Category Other  Published on 15/07/2022

AutoZoom

Description

AutoZoom Tool for cTrader 4.2+ (net6.0).  

This tool automatically changes the chart time frame on zoom change.

How to use: just add it as a regular indicator.

Demo:

Parameters:

Feel free to make your suggestions to improve this indicator!


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

namespace cAlgo
{
    [Indicator(AccessRights = AccessRights.None, IsOverlay = true)]
    public class AutoZoom : Indicator
    {
        private record TimeFrameTransition(TimeFrame Less, TimeFrame Greater);

        private const int MinZoomLevel = 5;
        private const int MaxZoomLevel = 500;

        private readonly Dictionary<TimeFrame, TimeFrameTransition> Transitions = new();

        [Parameter(DefaultValue = "m1,m5,m15,m30,h1,h4,h12,D1")]
        public string TimeOrder { get; set; }

        [Parameter(DefaultValue = "t1,t5,t20,t100,t200,t500,t1000")]
        public string TickOrder { get; set; }

        [Parameter(DefaultValue = "re1,re2,re5,re20,re100,re200,re500,re1000")]
        public string RenkoOrder { get; set; }

        [Parameter(DefaultValue = "ra1,ra2,ra5,ra20,ra100,ra200,ra500,ra1000")]
        public string RangeOrder { get; set; }

        [Parameter(MinValue = 160, MaxValue = MaxZoomLevel, Step = 5, DefaultValue = 320)]
        public int MaxZoom { get; set; }

        [Parameter(MinValue = MinZoomLevel, MaxValue = 160, Step = 5, DefaultValue = 80)]
        public int MinZoom { get; set; }

        protected override void Initialize()
        {
            ApplyTransitions(TimeOrder);
            ApplyTransitions(TickOrder);
            ApplyTransitions(RenkoOrder);
            ApplyTransitions(RangeOrder);

            Chart.ZoomChanged += Chart_ZoomChanged;
        }

        public override void Calculate(int index)
        {
            // DO NOTHING
        }

        private void Chart_ZoomChanged(ChartZoomEventArgs args)
        {
            TimeFrame nextTimeFrame = null;
            int nextZoomLevel = 0;

            if (Chart.ZoomLevel >= MaxZoom && TryGetLessTimeFrame(Chart.TimeFrame, out nextTimeFrame))
                nextZoomLevel = MinZoom;

            if (Chart.ZoomLevel <= MinZoom && TryGetGreaterTimeFrame(Chart.TimeFrame, out nextTimeFrame))
                nextZoomLevel = MaxZoom;

            if (nextTimeFrame != null)
            {
                Chart.ZoomLevel = nextZoomLevel;
                Chart.TryChangeTimeFrame(nextTimeFrame);
            }
        }
        
        private bool TryGetLessTimeFrame(TimeFrame timeFrame, out TimeFrame lessTimeFrame) {
            if (Transitions.TryGetValue(timeFrame, out var transition) && transition.Less != null)
            {
                lessTimeFrame = transition.Less;
                return true;
            }

            lessTimeFrame = null;
            return false;
        }
        
        private bool TryGetGreaterTimeFrame(TimeFrame timeFrame, out TimeFrame greaterTimeFrame) {
            if (Transitions.TryGetValue(timeFrame, out var transition) && transition.Greater != null)
            {
                greaterTimeFrame = transition.Greater;
                return true;
            }

            greaterTimeFrame = null;
            return false;
        }
        
        private void ApplyTransitions(string order)
        {
            var tfNames = order.Split(',');

            if (tfNames.Length == 0)
                return;

            var i = 0;

            TimeFrame less;
            TimeFrame current = null;
            TimeFrame greater = TimeFrame.Parse(tfNames[i]);            

            while (++i <= tfNames.Length) {
                less = current;
                current = greater;
                greater = i < tfNames.Length ? TimeFrame.Parse(tfNames[i]) : null;

                Transitions.Add(current, new TimeFrameTransition(less, greater));
            }
        }
    }
}

devman's avatar
devman

Joined on 22.10.2019

  • Distribution: Free
  • Language: C#
  • Trading platform: cTrader Automate
  • File name: AutoZoom.algo
  • Rating: 0
  • Installs: 925
Comments
Log in to add a comment.
Tspring's avatar
Tspring · 1 year ago

Hi, this indicator is a really good idea but it doesn't work well, it would need an update in my opinion because when zooming in and out, the time frames do not change in a linear, sequential way but jump chaotically from one to another.

Also it would be good if while zooming, it could keep the same position in the chart that one is looking at, but unfortunately the indicator cannot keep it.

Thank you, I hope you decide to update it.