daily Heikeinashi

Created at 24 Jan 2019, 16:36
How’s your experience with the cTrader Platform?
Your feedback is crucial to cTrader's development. Please take a few seconds to share your opinion and help us improve your trading experience. Thanks!
PO

pozhy

Joined 03.04.2018

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

    }
}

 


@pozhy
Replies

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? 

 

 

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

namespace cAlgo.Indicators
{
    [Indicator(IsOverlay = true, TimeZone = TimeZones.EEuropeStandardTime, AccessRights = AccessRights.None)]
    public class AHeikenashi3 : Indicator
    {
        public List<double> dailyIndexesOpensList = new List<double>();
        private MarketSeries dailySeries;
        public IndicatorDataSeries MyPreviousClose;
        public IndicatorDataSeries CurrentOpens;
        public IndicatorDataSeries xClose;
        public IndicatorDataSeries xHigh;
        public IndicatorDataSeries xLow;
        public IndicatorDataSeries xOpen;

        private IndicatorDataSeries _haOpen;
        private IndicatorDataSeries _haClose;
        private IndicatorDataSeries _haOpenD;
        private IndicatorDataSeries _haCloseD;

        public double prviousClose;
        protected override void Initialize()
        {
            MyPreviousClose = CreateDataSeries();
            CurrentOpens = CreateDataSeries();

            xClose = CreateDataSeries();
            xHigh = CreateDataSeries();
            xLow = CreateDataSeries();
            xOpen = CreateDataSeries();
            dailySeries = MarketData.GetSeries(Symbol, TimeFrame.Daily);

            _haOpen = CreateDataSeries();
            _haClose = CreateDataSeries();
            _haOpenD = CreateDataSeries();
            _haCloseD = CreateDataSeries();

        }

        public override void Calculate(int index)
        {

            int dailyIndex = GetIndexByDate(dailySeries, MarketSeries.OpenTime[index]);
            if (dailyIndex < 0)
            {
                dailyIndex = GetClosestIndexByDate(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;

            double open = dailySeries.Open[idx1];
            double high = dailySeries.High[idx1];
            double low = dailySeries.Low[idx1];
            double close = dailySeries.Close[idx1];

            var haClose = (open + high + low + close) / 4;
            double haOpen;
            if (index > 0)
            {
                //haOpen = (MarketData.GetSeries(TimeFrame.Daily).Open[idx1 - 1] + MarketData.GetSeries(TimeFrame.Daily).Close[idx1 - 1]) / 2;
                haOpen = (_haOpen[index - 1] + _haClose[index - 1]) / 2;
                //haOpen = (dailySeries.Open[idx1 - 1] + dailySeries.Close[idx1 - 1]) / 2;
            }
            else
                haOpen = (open + close) / 2;
            var haCloseD = (open + high + low + close) / 4;

            double haOpenD;
            if (idx1 > 0)
            {
                haOpenD = (_haOpenD[idx1 - 1] + _haCloseD[idx1 - 1]) / 2;
            }
            else
            {
                haOpenD = (open + close) / 2;
            }

            _haOpen[index] = haOpen;
            _haClose[index] = haClose;
            _haOpenD[idx1] = haOpen;
            _haCloseD[idx1] = haClose;

            var haHigh = Math.Max(Math.Max(high, haOpenD), haCloseD);
            var haLow = Math.Min(Math.Min(low, haOpenD), haCloseD);


            dailyIndexesOpensList.Add((haCloseD + haOpenD) / 2);
            MyPreviousClose[idx1] = (haCloseD + haOpenD) / 2;

            prviousClose = ((dailySeries.Open[idx1 - 1] + dailySeries.High[idx1 - 1] + dailySeries.Low[idx1 - 1] + dailySeries.Close[idx1 - 1]) / 4);

            var mDcolor = haOpenD > haCloseD ? Color.Red : Color.Blue;

            Chart.DrawRectangle("recHeiken " + idx2, idx2, (MyPreviousClose[idx1 - 2] + prviousClose) / 2, index, haCloseD, mDcolor, 7);
            Chart.DrawTrendLine("lineHUP " + idx2, idx3, haHigh, idx3, haLow, mDcolor, 3);
        }

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

        private int GetClosestIndexByDate(MarketSeries series, DateTime time)
        {
            var lastIndex = series.Close.Count - 1;

            if (time >= series.OpenTime[lastIndex])
                return lastIndex;

            var timeDifference = time.Subtract(series.OpenTime[0]);

            int index = 0;

            for (int i = 0; i < lastIndex - 1; i++)
            {
                if (time < series.OpenTime[i])
                    break;
                var currDiff = time.Subtract(series.OpenTime[i]);

                if (currDiff < timeDifference)
                {
                    timeDifference = currDiff;
                    index = i;
                }
            }

            return index;
        }
    }
}

 


@pozhy

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

afhacker
26 Jan 2019, 21:48 ( Updated at: 21 Dec 2023, 09:21 )

It's working fine for me, H1 chart:

Daily chart:


@afhacker