daily Heikeinashi
daily Heikeinashi
24 Jan 2019, 16:36
Hi, I want to draw Heikeinashi on daily basis ina smaller timeframes. similar this picture below but I have made mistake in my code. does anyone know how to solve it?
using System; using cAlgo.API; using cAlgo.API.Internals; namespace cAlgo.Indicators { [Indicator(IsOverlay = true, TimeZone = TimeZones.EEuropeStandardTime, AccessRights = AccessRights.None)] public class DailyRange : Indicator { private MarketSeries dailySeries; [Output("Main", PlotType = PlotType.DiscontinuousLine)] public IndicatorDataSeries Result { get; set; } public IndicatorDataSeries xClose; public IndicatorDataSeries xHigh; public IndicatorDataSeries xLow; public IndicatorDataSeries xOpen; protected override void Initialize() { xClose = CreateDataSeries(); xHigh = CreateDataSeries(); xLow = CreateDataSeries(); xOpen = CreateDataSeries(); dailySeries = MarketData.GetSeries(Symbol, TimeFrame.Daily); } public override void Calculate(int index) { int dailyIndex = GetIndexByDate(dailySeries, MarketSeries.OpenTime[index]); int idx1 = dailySeries.OpenTime.GetIndexByTime(MarketSeries.OpenTime[index]); int idx2 = MarketSeries.OpenTime.GetIndexByTime(dailySeries.OpenTime[idx1]); int idx3 = idx2 + (index - idx2) / 2; xOpen[dailyIndex] = Math.Round((xOpen[dailyIndex - 1] + xClose[dailyIndex - 1]) / 2, Symbol.Digits); xClose[dailyIndex] = Math.Round((MarketSeries.Open[index] + MarketSeries.Low[dailyIndex] + MarketSeries.High[dailyIndex] + MarketSeries.Close[dailyIndex]) / 4, Symbol.Digits); xHigh[dailyIndex] = Math.Max(MarketSeries.High[dailyIndex], Math.Max(xOpen[dailyIndex], xClose[dailyIndex])); xLow[dailyIndex] = Math.Min(MarketSeries.Low[dailyIndex], Math.Min(xOpen[dailyIndex], xClose[dailyIndex])); ChartObjects.DrawLine("top" + idx1, idx2, xClose[dailyIndex], index, xClose[dailyIndex], Colors.White, 1); ChartObjects.DrawLine("bottom" + idx1, idx2, xOpen[dailyIndex], index, xOpen[dailyIndex], Colors.White, 1); ChartObjects.DrawLine("left" + idx1, idx2, xOpen[dailyIndex], idx2, xClose[dailyIndex], Colors.White, 1); ChartObjects.DrawLine("right" + idx1, index, xOpen[dailyIndex], index, xClose[dailyIndex], Colors.White, 1); ChartObjects.DrawLine("line" + index, idx3, xHigh[dailyIndex], idx3, xLow[dailyIndex], Colors.White, 1, LineStyle.Solid); } private int GetIndexByDate(MarketSeries series, DateTime time) { var lastBar = series.Close.Count - 1; for (int i = lastBar; i > 0; i--) { if (time == series.OpenTime[i]) return i; } return -1; } } }
Replies
afhacker
26 Jan 2019, 12:00
I'm building a free multi time frame Heiken Ashi indicator that will plot another time frame Heiken Ashi bars on top of your chart, it will be available for download on our site in the next few days as an update to our current Heiken Ashi MTF indicator (the indicator current version isn't free but the new version will be free).
Check out our Custom Period Candles indicator, it will look like it with an alerting feature on bar color change.
@afhacker
pozhy
26 Jan 2019, 15:28
RE:
Thank you, Ahmad, but I need this code to go forward, what is the problem? is it from the index or open calculation?
afhacker said:
I'm building a free multi time frame Heiken Ashi indicator that will plot another time frame Heiken Ashi bars on top of your chart, it will be available for download on our site in the next few days as an update to our current Heiken Ashi MTF indicator (the indicator current version isn't free but the new version will be free).
Check out our Custom Period Candles indicator, it will look like it with an alerting feature on bar color change.
@pozhy
afhacker
26 Jan 2019, 16:26
Try this:
private void CalculateHeikenAshi(MarketSeries otherSeries, int periods = 1) { int index = MarketSeries.Close.Count - 1; int otherSeriesIndex = otherSeries.OpenTime.GetIndexByTime(MarketSeries.OpenTime[index]); double barOhlcSum = otherSeries.Open[otherSeriesIndex] + otherSeries.Low[otherSeriesIndex] + otherSeries.High[otherSeriesIndex] + otherSeries.Close[otherSeriesIndex]; _close[index] = Math.Round(barOhlcSum / 4, Symbol.Digits); if (otherSeriesIndex < periods || double.IsNaN(_open[index - 1])) { _open[index] = Math.Round((otherSeries.Open[otherSeriesIndex] + otherSeries.Close[otherSeriesIndex]) / 2, Symbol.Digits); _high[index] = otherSeries.High[otherSeriesIndex]; _low[index] = otherSeries.Low[otherSeriesIndex]; } else { _open[index] = Math.Round((_open[index - 1] + _close[index - 1]) / 2, Symbol.Digits); _high[index] = Math.Max(otherSeries.High[otherSeriesIndex], Math.Max(_open[index], _close[index])); _low[index] = Math.Min(otherSeries.Low[otherSeriesIndex], Math.Min(_open[index], _close[index])); } }
The _open, _high, _low, and _close are IndicatorDataSeries objects and the "otherSeries" is the other time frame market series object.
@afhacker
pozhy
26 Jan 2019, 18:18
RE:
afhacker said:
Try this:
private void CalculateHeikenAshi(MarketSeries otherSeries, int periods = 1) { int index = MarketSeries.Close.Count - 1; int otherSeriesIndex = otherSeries.OpenTime.GetIndexByTime(MarketSeries.OpenTime[index]); double barOhlcSum = otherSeries.Open[otherSeriesIndex] + otherSeries.Low[otherSeriesIndex] + otherSeries.High[otherSeriesIndex] + otherSeries.Close[otherSeriesIndex]; _close[index] = Math.Round(barOhlcSum / 4, Symbol.Digits); if (otherSeriesIndex < periods || double.IsNaN(_open[index - 1])) { _open[index] = Math.Round((otherSeries.Open[otherSeriesIndex] + otherSeries.Close[otherSeriesIndex]) / 2, Symbol.Digits); _high[index] = otherSeries.High[otherSeriesIndex]; _low[index] = otherSeries.Low[otherSeriesIndex]; } else { _open[index] = Math.Round((_open[index - 1] + _close[index - 1]) / 2, Symbol.Digits); _high[index] = Math.Max(otherSeries.High[otherSeriesIndex], Math.Max(_open[index], _close[index])); _low[index] = Math.Min(otherSeries.Low[otherSeriesIndex], Math.Min(_open[index], _close[index])); } }The _open, _high, _low, and _close are IndicatorDataSeries objects and the "otherSeries" is the other time frame market series object.
Thank you, Ahmad , Still no luck except daily chart. I put the stars in the chart to see where is the open and when is the close but when timeframe changes to less than daily the position of stars change
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 AhmadNomanMusleh : Indicator { [Parameter(DefaultValue = 0.0)] public double Parameter { get; set; } [Output("Main")] public IndicatorDataSeries Result { get; set; } public IndicatorDataSeries _close; public IndicatorDataSeries _high; public IndicatorDataSeries _low; public IndicatorDataSeries _open; public IndicatorDataSeries HeikenAshiOpens; public IndicatorDataSeries HeikenAshiCloses; private MarketSeries dailySeries; protected override void Initialize() { HeikenAshiCloses = CreateDataSeries(); HeikenAshiOpens = CreateDataSeries(); _close = CreateDataSeries(); _high = CreateDataSeries(); _low = CreateDataSeries(); _open = CreateDataSeries(); dailySeries = MarketData.GetSeries(Symbol, TimeFrame.Daily); } public override void Calculate(int index) { // Calculate value at specified index // Result[index] = ... CalculateHeikenAshi(dailySeries, 1); } private void CalculateHeikenAshi(MarketSeries otherSeries, int periods = 1) { int index = MarketSeries.Close.Count - 1; int idx1 = dailySeries.OpenTime.GetIndexByTime(MarketSeries.OpenTime[index]); int idx2 = MarketSeries.OpenTime.GetIndexByTime(dailySeries.OpenTime[idx1]); int idx3 = idx2 + (index - idx2) / 2; int otherSeriesIndex = otherSeries.OpenTime.GetIndexByTime(MarketSeries.OpenTime[index]); double barOhlcSum = otherSeries.Open[otherSeriesIndex] + otherSeries.Low[otherSeriesIndex] + otherSeries.High[otherSeriesIndex] + otherSeries.Close[otherSeriesIndex]; _close[index] = Math.Round(barOhlcSum / 4, Symbol.Digits); if (otherSeriesIndex < periods || double.IsNaN(_open[index - 1])) { _open[index] = Math.Round((otherSeries.Open[otherSeriesIndex] + otherSeries.Close[otherSeriesIndex]) / 2, Symbol.Digits); _high[index] = otherSeries.High[otherSeriesIndex]; _low[index] = otherSeries.Low[otherSeriesIndex]; } else { _open[index] = Math.Round((_open[index - 1] + _close[index - 1]) / 2, Symbol.Digits); _high[index] = Math.Max(otherSeries.High[otherSeriesIndex], Math.Max(_open[index], _close[index])); _low[index] = Math.Min(otherSeries.Low[otherSeriesIndex], Math.Min(_open[index], _close[index])); } Chart.DrawIcon("ichac" + index, ChartIconType.Star, index, _close[idx2], Color.White); Chart.DrawIcon("ichao" + index, ChartIconType.Star, index, _open[idx2], Color.Yellow); Chart.DrawIcon("ichah" + index, ChartIconType.Star, index, _high[idx2], Color.DarkBlue); Chart.DrawIcon("ichal" + index, ChartIconType.Star, index, _low[idx2], Color.DarkBlue); Chart.DrawIcon("ichal" + index, ChartIconType.Star, index, _low[idx2], Color.DarkBlue); HeikenAshiCloses[index] = (_close[idx2] + _open[idx2] + _high[idx2] + _low[idx2]) / 4; HeikenAshiOpens[index] = (_close[idx2 - 1] + _open[idx2 - 1]) / 2; Chart.DrawIcon("ichach" + index, ChartIconType.Star, index, HeikenAshiCloses[index], Color.Red); Chart.DrawIcon("ichaoh" + index, ChartIconType.Star, index, HeikenAshiOpens[index], Color.Green); } } }
@pozhy
pozhy
26 Jan 2019, 06:59
I can't make open based on Heiken-Ashi which is Open[index] = (open the last candle + close the last candle )/2
and my chart in daily won't work correctly, is there anyone that has a solution when we want to draw daily Heiken Ashi in a smaller chart like hourly?
@pozhy