Information

Username: dpetkov89
Member since: 28 Mar 2023
Last login: 28 Mar 2023
Status: Active

Activity

Where Created Comments
Algorithms 0 1
Forum Topics 0 0
Jobs 0 0

Last Algorithm Comments

DP
dpetkov89 · 1 year ago

run this code on ctrader, will work with the latest version :

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

namespace cAlgo
{
    [Indicator(IsOverlay = true, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class TimeframeSync : Indicator
    {
        private static Dictionary<string, List<Indicator>> _groups = new Dictionary<string, List<Indicator>>();
        private static object _lock = new object();

        [Parameter(DefaultValue = "Sync")]
        public string Group { get; set; }

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

        protected override void Initialize()
        {
            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
        {
            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);
                }
            }
        }
    }
}