Category Other  Published on 20/10/2019

Syncronized Crosshair

Description

Follow my cTrader Telegram group at https://t.me/cTraderCommunity; everyone can talk about cTrader indicators and algorithm without restrictions, though it is not allowed to spam commercial indicators to sell them. There's also a Discord Server now @ https://discord.gg/5GAPMtp and an Instagram page https://www.instagram.com/ctrader_community/

This indicator syncronizes crosshairs between different charts, just add an instance of this indi on whichever chart you want syncronized.

Obviously, if you point a H1 chart and look at a syncronized m5 accuracy will be less than what you'd get the other way around.

Special thanks to Bart A for having uploaded the TimeframeSync indicator, which gave me the idea

Please report any kind of bugs in the comments or in my telegram group


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

namespace cAlgo
{
    [Indicator(IsOverlay = true, TimeZone = TimeZones.UTC, AccessRights = AccessRights.FullAccess)]
    public class CrossHairSync : Indicator
    {
        [Parameter("LineStyle", Group = "Style for Current Chart", DefaultValue = LineStyle.Solid)]
        public LineStyle LS { get; set; }
        [Parameter("Thickness", Group = "Style for Current Chart", DefaultValue = 1)]
        public int Thc { get; set; }
        [Parameter("Color", Group = "Style for Current Chart", DefaultValue = "White")]
        public string Col { get; set; }
        [Parameter("Opacity", Group = "Style for Current Chart", DefaultValue = 60)]
        public int Opc { get; set; }

        private Color Colour;

        protected override void Initialize()
        {
            Opc = (int)(255 * 0.01 * Opc);
            Colour = Color.FromArgb(Opc, Color.FromName(Col).R, Color.FromName(Col).G, Color.FromName(Col).B);
            Crosshair.AddChart(this);
            Chart.MouseMove += OnChartMouseMove;
            Chart.MouseLeave += OnChartMouseLeave;
        }

        void OnChartMouseLeave(ChartMouseEventArgs obj)
        {
            Crosshair.DeleteCrosshair();
        }

        void OnChartMouseMove(ChartMouseEventArgs obj)
        {
            Crosshair.DrawCrosshair(MarketSeries.OpenTime[(int)obj.BarIndex], obj.YValue, LS, Thc, Colour);
        }

        public override void Calculate(int index)
        {
        }
    }

    public static class Crosshair
    {
        private static List<Indicator> Indicators = new List<Indicator>();
        private static object _lock = new object();

        public static void DrawCrosshair(DateTime x, double y, LineStyle ls, int thc, Color colour)
        {
            try
            {
                lock (_lock)
                {
                    foreach (Indicator i in Indicators)
                    {
                        Thread t = new Thread(() =>
                        {
                            i.BeginInvokeOnMainThread(() => { i.Chart.DrawHorizontalLine("HorizontalCrosshairLine", y, colour, thc, ls); });
                            i.BeginInvokeOnMainThread(() => { i.Chart.DrawVerticalLine("VerticalCrosshairLine", x, colour, thc, ls); });
                        });
                        t.Start();
                    }
                }
            } catch (Exception)
            {
            }
        }

        public static void DeleteCrosshair()
        {
            try
            {
                lock (_lock)
                {
                    foreach (Indicator i in Indicators)
                    {
                        Thread t = new Thread(() =>
                        {
                            i.BeginInvokeOnMainThread(() => { i.Chart.RemoveObject("HorizontalCrosshairLine"); });
                            i.BeginInvokeOnMainThread(() => { i.Chart.RemoveObject("VerticalCrosshairLine"); });
                        });
                        t.Start();
                    }
                }
            } catch (Exception)
            {
            }
        }

        public static void AddChart(Indicator indicator)
        {
            try
            {
                lock (_lock)
                    Indicators.Add(indicator);
            } catch (Exception e)
            {
                indicator.Print(e);
            }
        }
    }
}


CY
cysecsbin.01

Joined on 10.11.2018 Blocked

  • Distribution: Free
  • Language: C#
  • Trading platform: cTrader Automate
  • File name: CrossHairSync.algo
  • Rating: 5
  • Installs: 2453
  • Modified: 13/10/2021 09:54
Comments
Log in to add a comment.
OK
okshuvro · 1 year ago

this thing lags in 6-8 pairs

ZA
zarkos12 · 2 years ago

Great work @bru34! Much faster! 

OK
okshuvro · 3 years ago

This thing is too much laggy brother, kindly update this. :(

MO
moksha · 4 years ago

However two addition would be great :
- Ability to turn off crosshair like the original one with the middle clic of the mouse
- Take all charts to the date when I clic somewhere. Ex: I clic on daily a few days ago and all charts goes to this date.

Kudos cysecsbin.01

MO
moksha · 4 years ago

Hello,
I just downloaded Syncronized Crosshair and it works great so THANK YOU

BR
bru34 · 4 years ago

Hey Brother, i really love this indicator, but its was laggy with 6 charts open on my PC, so i optimised it. You could update if you wish, it's my way to say thanks you for your work !

 

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

namespace cAlgo
{
    [Indicator(IsOverlay = true, TimeZone = TimeZones.UTC, AccessRights = AccessRights.FullAccess)]
    public class CrossHairSync : Indicator
    {
        [Parameter("LineStyle", Group = "Style for Current Chart", DefaultValue = LineStyle.Solid)]
        public LineStyle LS { get; set; }
        [Parameter("Thickness", Group = "Style for Current Chart", DefaultValue = 1)]
        public int Thc { get; set; }
        [Parameter("Color", Group = "Style for Current Chart", DefaultValue = "White")]
        public string Col { get; set; }
        [Parameter("Opacity", Group = "Style for Current Chart", DefaultValue = 60)]
        public int Opc { get; set; }

        private Color Colour;

        protected override void Initialize()
        {
            Opc = (int)(255 * 0.01 * Opc);
            Colour = Color.FromArgb(Opc, Color.FromName(Col).R, Color.FromName(Col).G, Color.FromName(Col).B);
            Crosshair.AddChart(this);
            Chart.MouseMove += OnChartMouseMove;
            Chart.MouseLeave += OnChartMouseLeave;
        }

        void OnChartMouseLeave(ChartMouseEventArgs obj)
        {
            Crosshair.DeleteCrosshair();
        }

        void OnChartMouseMove(ChartMouseEventArgs obj)
        {
            Crosshair.DrawCrosshair(Bars.OpenTimes[(int)obj.BarIndex], obj.YValue, LS, Thc, Colour);
        }

        public override void Calculate(int index)
        {
        }
    }

    public static class Crosshair
    {
        private static List<Indicator> Indicators = new List<Indicator>();
        private static object _lock = new object();

        public static void DrawCrosshair(DateTime x, double y, LineStyle ls, int thc, Color colour)
        {
            try
            {
                lock (_lock)
                {
                    foreach (Indicator i in Indicators)
                    {
                        Thread t = new Thread(() =>
                        {
                            if (i.Chart.TopY > y && i.Chart.BottomY < y)
                            {
                                i.BeginInvokeOnMainThread(() => { i.Chart.DrawHorizontalLine("HCL", y, colour, thc, ls); });
                            }
                            System.DateTime xRight = i.Chart.Bars.LastBar.OpenTime;
                            if (xRight.CompareTo(x) > 0)
                            {
                                i.BeginInvokeOnMainThread(() => { i.Chart.DrawVerticalLine("VCL", x, colour, thc, ls); });
                            }

                        });
                        t.Start();
                    }
                }
            } catch (Exception)
            {
            }
        }

        public static void DeleteCrosshair()
        {
            try
            {
                lock (_lock)
                {
                    foreach (Indicator i in Indicators)
                    {
                        Thread t = new Thread(() =>
                        {
                            i.BeginInvokeOnMainThread(() => { i.Chart.RemoveObject("HCL"); });
                            i.BeginInvokeOnMainThread(() => { i.Chart.RemoveObject("VCL"); });
                        });
                        t.Start();
                    }
                }
            } catch (Exception)
            {
            }
        }

        public static void AddChart(Indicator indicator)
        {
            try
            {
                lock (_lock)
                    Indicators.Add(indicator);
            } catch (Exception e)
            {
                indicator.Print(e);
            }
        }
    }
}