TimeframeSync

by Bart A in category Other at 03/08/2019
Description

Syncronize timeframe between different charts. Support different chart groups.

Instructions:
Add this indicator to all the charts that you want to have the same timeframe. If you want to have more than one group, you should set the Group parameter.
When changing timeframe on one chart, all charts with the same group will be switched to the same timeframe.

Using the existing feature in cTrader to link charts by the symbol, you can setup workspace like this:

Download
58 downloads
How to install
Notification Publishing copyrighted material is strictly prohibited. If you believe there is copyrighted material in this section you may use the Copyright Infringement Notification form to submit a claim.
Formula / Source Code
Language: C#
Trading Platform: cAlgocTrader
using System;
using cAlgo.API;
using cAlgo.API.Internals;
using cAlgo.API.Indicators;
using cAlgo.Indicators;
using System.Collections.Generic;

namespace cAlgo
{
    [Indicator(IsOverlay = true, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class TimeframeSync : Indicator
    {
        [Parameter(DefaultValue = "Sync")]
        public string Group { get; set; }

        [Parameter("Show group name", DefaultValue = true)]
        public bool ShowGroupName { get; set; }

        protected override void Initialize()
        {
            if (Group == null)
                return;

            CommonTimeframeProvider.InitChart(Group, this);

            if (ShowGroupName)
                Chart.DrawStaticText("syncGroupText", Group, VerticalAlignment.Top, HorizontalAlignment.Left, Chart.ColorSettings.ForegroundColor);
        }

        public override void Calculate(int index)
        {
        }
    }

    public static class CommonTimeframeProvider
    {
        private static Dictionary<string, List<Indicator>> _groups = new Dictionary<string, List<Indicator>>();
        private static object _lock = new object();

        public static void InitChart(string @group, Indicator indicator)
        {
            lock (_lock)
            {
                List<Indicator> indicators;

                if (!_groups.TryGetValue(@group, out indicators))
                {
                    indicators = new List<Indicator>();
                    _groups[@group] = indicators;
                }

                var charts = indicators.ToArray();
                var timeframe = indicator.TimeFrame;

                foreach (var anotherIndicator in charts)
                {
                    if (timeframe != anotherIndicator.TimeFrame)
                    {
                        indicators.Remove(anotherIndicator);
                        anotherIndicator.BeginInvokeOnMainThread(() => anotherIndicator.Chart.TryChangeTimeFrame(timeframe));
                    }
                }
                indicators.Add(indicator);
            }
        }
    }
}
Comments
5