Topics

Forum Topics not found

Replies

zofoyobis
11 Nov 2014, 07:04

I am trying to use this feature to create Heikin-Ashi indicator with custom timeframe as below. The value of haClose in the HeikinAshiClose series looks correct but I am always getting NaN for haOpen. I am not sure if something like below is supported?

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

namespace cAlgo
{
    [Indicator(IsOverlay = true, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class TFHeikinAshiIndicator : Indicator
    {
        [Parameter("Heikin-Ashi Timeframe")]
        public TimeFrame HeikinAshiTimeframe { get; set; }

        [Output("Open", Color = Colors.DarkSlateBlue)]
        public IndicatorDataSeries HeikinAshiOpen { get; set; }

        [Output("Close", Color = Colors.SlateBlue)]
        public IndicatorDataSeries HeikinAshiClose { get; set; }

        private MarketSeries heikinAshiSeries;

        protected override void Initialize()
        {
            heikinAshiSeries = MarketData.GetSeries(HeikinAshiTimeframe);
        }

        public override void Calculate(int index)
        {
            int heikinAshiSeriesIndex = GetIndexByDate(heikinAshiSeries, MarketSeries.OpenTime[index]);

            double open = heikinAshiSeries.Open[heikinAshiSeriesIndex];
            double high = heikinAshiSeries.High[heikinAshiSeriesIndex];
            double low = heikinAshiSeries.Low[heikinAshiSeriesIndex];
            double close = heikinAshiSeries.Close[heikinAshiSeriesIndex];

            double haClose = (open + high + low + close) / 4;
            double haOpen;
            if (heikinAshiSeriesIndex != -1)
                haOpen = (HeikinAshiOpen[heikinAshiSeriesIndex - 1] + HeikinAshiClose[heikinAshiSeriesIndex - 1]) / 2;
            else
                haOpen = (open + close) / 2;

            ChartObjects.DrawText("ha", string.Format("heikinAshiSeriesIndex={0} haOpen={1}, haClose={2}", heikinAshiSeriesIndex, haOpen, haClose), StaticPosition.TopLeft, Colors.Red);

            HeikinAshiOpen[heikinAshiSeriesIndex] = haOpen;
            HeikinAshiClose[heikinAshiSeriesIndex] = haClose;
        }

        private int GetIndexByDate(MarketSeries series, DateTime time)
        {
            for (int i = series.Close.Count - 1; i > 0; i--)
            {
                if (time == series.OpenTime[i])
                    return i;
            }
            return -1;
        }
    }
}

 


@zofoyobis