Category Trend  Published on 05/04/2024

Smoothed Heikin Ashi Candles

Description

Smoothed Heikin Ashi Candles

Only added a few modifications to: https://ctrader.com/algos/indicators/show/2935 of @aksbenz

1- Smoothed Heikin Ashi candles 

2- Shadow coloring (a different color from the candle indicates that the shadow is larger on that side."
 

Have fun, and for any collaboration, contact me !!!

On telegram : https://t.me/nimi012 (direct messaging)

 


using System;
using cAlgo.API;
using cAlgo.API.Internals;
using cAlgo.API.Indicators;
using cAlgo.Indicators;

namespace cAlgo
{
    [Indicator(IsOverlay = true, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class SmoothedHeikinAshiCandles : Indicator
    {
        [Parameter("Candle width", DefaultValue = 7)]
        public int CandleWidth { get; set; }

        [Parameter("Transparency", DefaultValue = 180)]
        public int Alpha { get; set; }

        [Parameter("Up color", DefaultValue = "DeepSkyBlue")]
        public string UpColor { get; set; }

        [Parameter("Down color", DefaultValue = "Red")]
        public string DownColor { get; set; }

        [Parameter("Button Position (1,2,3,4,5,6)", DefaultValue = 5, MaxValue = 6)]
        public int BtnPosition { get; set; }

        [Parameter("Smoothing Period", DefaultValue = 3)]
        public int SmoothingPeriod { get; set; }

        IndicatorDataSeries HAOpen, HAHigh, HALow, HAClose, IsDrawn;
        Color _upColor, _downColor;
        Button BtnShowHide;
        bool haVisible = true;

        protected override void Initialize()
        {
            HAOpen = CreateDataSeries();
            HAHigh = CreateDataSeries();
            HALow = CreateDataSeries();
            HAClose = CreateDataSeries();
            IsDrawn = CreateDataSeries();

            _upColor = UpColor.StartsWith("#") ? Color.FromHex(UpColor.TrimStart('#')) : Color.FromName(UpColor);
            _downColor = DownColor.StartsWith("#") ? Color.FromHex(DownColor.TrimStart('#')) : Color.FromName(DownColor);

            Chart.ChartType = ChartType.Dots;
            Chart.ScrollChanged += Chart_ScrollChanged;
            Chart.AddHotkey(ToggleHA, "h");

            // Button Settings to HIDE/SHOW HA Candles
            StackPanel mainPanel = new StackPanel();
            BtnShowHide = new Button
            {
                HorizontalAlignment = GetHorAlign(),
                VerticalAlignment = GetVertAlign(),
                Text = "HA",
                Width = 100,
                Height = 20,
                FontSize = 10,
                Margin = new Thickness(0, 5, 5, 5),
                BackgroundColor = Color.FromArgb(80, Color.Green)
            };

            BtnShowHide.Click += BtnShowHide_Click;

            Chart.AddControl(BtnShowHide);
            mainPanel.AddChild(BtnShowHide);
        }

        HorizontalAlignment GetHorAlign()
        {
            switch (BtnPosition)
            {
                case 1:
                case 4:
                default:
                    return HorizontalAlignment.Left;
                case 2:
                case 5:
                    return HorizontalAlignment.Center;
                case 3:
                case 6:
                    return HorizontalAlignment.Right;
            }
        }

        VerticalAlignment GetVertAlign()
        {
            switch (BtnPosition)
            {
                case 1:
                case 2:
                case 3:
                default:
                    return VerticalAlignment.Top;
                case 4:
                case 5:
                case 6:
                    return VerticalAlignment.Bottom;
            }
        }

        void ToggleHA()
        {
            haVisible = !haVisible;
            BtnShowHide.BackgroundColor = haVisible ? Color.FromArgb(80, Color.Green) : Color.FromArgb(80, Color.Red);

            if (!haVisible)
            {
                // Remove all HA Bars and Reset IsDrawn
                for (int i = 0; i < IsDrawn.Count; i++)
                {
                    IsDrawn[i] = 0;
                    Chart.RemoveObject("candle" + i);
                    Chart.RemoveObject("line" + i);
                }
                Chart.ChartType = ChartType.Candlesticks;
            }
            else
            {
                Chart.ChartType = ChartType.Dots;
                DrawHABars();
            }
        }

        void BtnShowHide_Click(ButtonClickEventArgs obj)
        {
            ToggleHA();
        }

        void Chart_ScrollChanged(ChartScrollEventArgs obj)
        {
            if (haVisible)
                DrawHABars();
        }

        public override void Calculate(int index)
        {
            HAClose[index] = (Bars.OpenPrices[index] + Bars.HighPrices[index] + Bars.LowPrices[index] + Bars.ClosePrices[index]) / 4;
            HAOpen[index] = index > 0 ? (HAOpen[index - 1] + HAClose[index - 1]) / 2 : (Bars.OpenPrices[index] + Bars.ClosePrices[index]) / 2;
            HAHigh[index] = Math.Max(Math.Max(Bars.HighPrices[index], HAOpen[index]), HAClose[index]);
            HALow[index] = Math.Min(Math.Min(Bars.LowPrices[index], HAOpen[index]), HAClose[index]);

            // Smoothing the Heikin Ashi data
            if (index >= SmoothingPeriod - 1)
            {
                double smoothedClose = 0;
                double smoothedOpen = 0;
                double smoothedHigh = 0;
                double smoothedLow = 0;

                for (int i = 0; i < SmoothingPeriod; i++)
                {
                    smoothedClose += HAClose[index - i];
                    smoothedOpen += HAOpen[index - i];
                    smoothedHigh += HAHigh[index - i];
                    smoothedLow += HALow[index - i];
                }

                HAClose[index] = smoothedClose / SmoothingPeriod;
                HAOpen[index] = smoothedOpen / SmoothingPeriod;
                HAHigh[index] = smoothedHigh / SmoothingPeriod;
                HALow[index] = smoothedLow / SmoothingPeriod;
            }

            if (IsLastBar && haVisible)
                DrawHABars();



        }

        void DrawHABars()
        {
            for (int i = Chart.FirstVisibleBarIndex; i <= Chart.LastVisibleBarIndex; i++)
            {
                if (IsDrawn[i] != 1 || (i == Chart.LastVisibleBarIndex))
                {
                    IsDrawn[i] = 1;
                    var color = HAOpen[i] > HAClose[i] ? _downColor : _upColor;

                    // Make the Bar a little transparent, if the BAR CLOSE DOT will be hidden
                    color = (Bars[i].Close < HAOpen[i] && Bars[i].Close > HAClose[i]) || (Bars[i].Close > HAOpen[i] && Bars[i].Close < HAClose[i]) ? Color.FromArgb(Alpha, color) : color;

                    Chart.DrawTrendLine("candle" + i, i, HAOpen[i], i, HAClose[i], color, CandleWidth, LineStyle.Solid);
                    Chart.DrawTrendLine("line" + i, i, HAHigh[i], i, HALow[i], color, 1, LineStyle.Solid);

                    if (HAHigh[i] - Math.Max(HAClose[i], HAOpen[i]) < Math.Min(HAClose[i], HAOpen[i]) - HALow[i])
                    {
                        Chart.DrawTrendLine("Point" + i, i, HALow[i], i, Math.Min(HAClose[i], HAOpen[i]), Color.Magenta,1, LineStyle.Solid);

                    }
                    else if (HAHigh[i] - Math.Max(HAClose[i], HAOpen[i]) > Math.Min(HAClose[i], HAOpen[i]) - HALow[i])
                    {

                        Chart.DrawTrendLine("Point2" + i, i, HAHigh[i], i, Math.Max(HAClose[i], HAOpen[i]), Color.Lime, 1, LineStyle.Solid);

                    }
                    else
                    {

                        Chart.DrawTrendLine("Point3" + i, i, HAHigh[i], i, HALow[i], Color.Orange, 1, LineStyle.Solid);

                    }


                }
            }
        }
    }
}


YE
YesOrNot

Joined on 10.10.2022 Blocked

  • Distribution: Free
  • Language: C#
  • Trading platform: cTrader Automate
  • File name: Smoothed Heikin Ashi Candles.algo
  • Rating: 5
  • Installs: 361
  • Modified: 05/04/2024 15:49
Comments
Log in to add a comment.
No comments found.