Description
Heikin Ashi Candles
High performance and low memory usage, useful when scrolling backwards, works smoothly even with Tick Charts and 1m charts
*** IMPORTANT ***
Set Chart Type back to "Candlesticks" after REMOVING indicator.
Can, also turn off Heikin Ashi Candles without removing the indicator.
Press "HA" button or press "h" to toggle HA Candles on/off.
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 HeikinAshiCandles : Indicator
{
[Parameter("Candle width", DefaultValue = 7)]
public int CandleWidth { get; set; }
[Parameter("Transparency", DefaultValue = 180)]
public int Alpha { get; set; }
[Parameter("Up color", DefaultValue = "#FF00BF00")]
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 = 1, MaxValue = 6)]
public int BtnPosition { 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]);
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);
}
}
}
}
}
AK
aksbenz
Joined on 26.09.2020
- Distribution: Free
- Language: C#
- Trading platform: cTrader Automate
- File name: Heikin Ashi Candles.algo
- Rating: 5
- Installs: 3656
- Modified: 08/01/2022 03:10
Note that publishing copyrighted material is strictly prohibited. If you believe there is copyrighted material in this section, please use the Copyright Infringement Notification form to submit a claim.
Comments
Log in to add a comment.
I work on HTML but this time I am using c+. However, I need help in the Capstone Project writing services website. I need some assistance regarding this language.