How to get Bear & Bull signal from HeikenAshi (smoothed) indicator?

Created at 04 Sep 2016, 17:26
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!
GD

GDPR-24_203122

Joined 02.10.2015 Blocked

How to get Bear & Bull signal from HeikenAshi (smoothed) indicator?
04 Sep 2016, 17:26


Heya fellow coders, traders and humans!

I have this HeikenAshi indicator over here below which has smoothing in it. It seem very good for my purposes, with Period: 3, and Type: WilderSmoothing. I'd like to get into my bot a signal which tells if the last bar is up or down. (Blue or red). I did a bot, and referenced the indicator right, but it seems to get wrong values... The bot has these (below) lines of code concerning the indicator (The indicator is at the bottom of post):

 

(From beginning of the bot:)

 

        [Parameter("Heiken Periods", DefaultValue = 3)]
        public int Periods { get; set; }

        [Parameter("Heiken MA Type", DefaultValue = MovingAverageType.WilderSmoothing)]
        public MovingAverageType MAType { get; set; }

        private HeikenAshiSmoothed _heikenAshi;

 

  protected override void OnStart()

      _heikenAshi = Indicators.GetIndicator<HeikenAshiSmoothed>(Periods, MAType);

}

   protected override void OnBar()

{

     Print("LatestHAClose:", _heikenAshi.haClose[1]);
     Print("LatestHAOpen:", _heikenAshi.haOpen[1]);

}

(I was going to use those to determine if it's an up or down bar, by using <> -symbols in a "if" sentence, but in backtest they don't give the real values that the indicator shows in the chart, even though the timeframe is 1H, both in chart and backtest.) Below is the full code of the indicator used, which I have made small changes concerning private/public, and set the "haClose" and "haOpen" to "Outputs".


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

namespace cAlgo.Indicators
{
    [Indicator(IsOverlay = true, AccessRights = AccessRights.None)]
    public class HeikenAshiSmoothed : Indicator
    {
        [Parameter(DefaultValue = 3, MinValue = 1)]
        public int Periods { get; set; }

        [Parameter("MA Type", DefaultValue = MovingAverageType.WilderSmoothing)]
        public MovingAverageType MAType { get; set; }

        public MovingAverage maOpen;
        public MovingAverage maClose;
        public MovingAverage maHigh;
        public MovingAverage maLow;

        [Output("haClose")]
        public IndicatorDataSeries haClose { get; set; }

        [Output("haOpen")]
        public IndicatorDataSeries haOpen { get; set; }



        protected override void Initialize()
        {
            maOpen = Indicators.MovingAverage(MarketSeries.Open, Periods, MAType);
            maClose = Indicators.MovingAverage(MarketSeries.Close, Periods, MAType);
            maHigh = Indicators.MovingAverage(MarketSeries.High, Periods, MAType);
            maLow = Indicators.MovingAverage(MarketSeries.Low, Periods, MAType);
        }


        public override void Calculate(int index)
        {
            double haHigh;
            double haLow;
            Colors Color;

            if (index > 0 && !double.IsNaN(maOpen.Result[index - 1]))
            {
                haOpen[index] = (haOpen[index - 1] + haClose[index - 1]) / 2;
                haClose[index] = (maOpen.Result[index] + maClose.Result[index] + maHigh.Result[index] + maLow.Result[index]) / 4;
                haHigh = Math.Max(maHigh.Result[index], Math.Max(haOpen[index], haClose[index]));
                haLow = Math.Min(maLow.Result[index], Math.Min(haOpen[index], haClose[index]));
                Color = (haOpen[index] > haClose[index]) ? Colors.Red : Colors.Blue;
                ChartObjects.DrawLine("BarHA" + index, index, haOpen[index], index, haClose[index], Color, 5, LineStyle.Solid);
                ChartObjects.DrawLine("LineHA" + index, index, haHigh, index, haLow, Color, 1, LineStyle.Solid);
            }
            else if (!double.IsNaN(maOpen.Result[index]))
            {
                haOpen[index] = (maOpen.Result[index] + maClose.Result[index]) / 2;
                haClose[index] = (maOpen.Result[index] + maClose.Result[index] + maHigh.Result[index] + maLow.Result[index]) / 4;
                haHigh = maHigh.Result[index];
                haLow = maLow.Result[index];
            }
        }
    }
}


 

BIG THANKS FOR ANYONE WHO HAS THE TAIM


Replies

croucrou
04 Sep 2016, 22:23

Shouldn't it be:

Print("LatestHAClose:", _heikenAshi.haClose.Last(1));
Print("LatestHAOpen:", _heikenAshi.haOpen.Last(1));

instead of:

Print("LatestHAClose:", _heikenAshi.haClose[1]);
Print("LatestHAOpen:", _heikenAshi.haOpen[1]);

@croucrou

GDPR-24_203122
05 Sep 2016, 00:13

RE:

croucrou said:

Shouldn't it be:

Print("LatestHAClose:", _heikenAshi.haClose.Last(1));
Print("LatestHAOpen:", _heikenAshi.haOpen.Last(1));

instead of:

Print("LatestHAClose:", _heikenAshi.haClose[1]);
Print("LatestHAOpen:", _heikenAshi.haOpen[1]);

Hmm. No, it doesn't work that way, but it doesn't matter anymore. I noticed I can probably get approx. the same results using the (standard issue) Accumulative Swing Index -indicator, and watching the correlating levels, concerning HeikenAshiSmooth-indicator turning either red or blue. About 

There were other posts about the problem I had (finding the right output) but they did not get them working. Other similar was with Super Trend indicator.

The indicator's drawn line is also not the same in real time, as it is looking backwards in the chart, because mathematical functions are applied later on, so it's tricky. It "lies", so to speak.

 

PS: Keep pushing on!

 

 


croucrou
05 Sep 2016, 01:07

You could verify the heiken up/down condition within the indicator and output it as bool.

Honestly, I would use int, where 1 would be for up, -1 for down and 0 is for doji candle.


@croucrou

croucrou
11 Sep 2016, 20:42

Okay, it looks like only data series can be output. If creating a new one basing on the up/down conditon is not the solution, I would consider it a buggy situation.


@croucrou

GDPR-24_203122
11 Sep 2016, 22:25

RE:

croucrou said:

Okay, it looks like only data series can be output. If creating a new one basing on the up/down conditon is not the solution, I would consider it a buggy situation.

Okay, thanks for informing.

:-)