Information

Username: bru34
Member since: 13 Jun 2020
Last login: 13 Jun 2020
Status: Active

Activity

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

Last Algorithm Comments

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);
            }
        }
    }
}