Heiken Ashi cBot issue

Created at 24 Jan 2014, 05:31
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!
KY

Kyriakos

Joined 08.01.2014

Heiken Ashi cBot issue
24 Jan 2014, 05:31


Hey guys,

I'm new to cAlgo and i'm preparing a Heiken Ashi robot that buys or sells at the end of each bar depending on the direction. 

Build is successful but it doesn't make any trades.

Please Help

 

//#reference: ..\Indicators\HeikenAshiJohn.algo
using System;
using System.Linq;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC)]
    public class HAK : Robot
    {
        [Parameter("Initial Volume", DefaultValue = 10000, MinValue = 0)]
        public int InitialVolume { get; set; }

        [Parameter("Initial Stop Loss", DefaultValue = 40)]
        public int SL { get; set; }

        [Parameter("Initial Take Profit", DefaultValue = 40)]
        public int TP { get; set; }

        private HeikenAshiJohn HAJ;

        string label = "HAK";


        protected override void OnStart()
        {
            HAJ = Indicators.GetIndicator<HeikenAshiJohn>(1);
        }

        protected override void OnBar()
        {
            int index = MarketSeries.Close.Count - 2;

            Print("Open : " + HAJ.xOpen[index] + " | Close : " + HAJ.xClose[index] + " | Last : " + HAJ.xClose.LastValue);

            if (HAJ.xClose[index] < HAJ.xOpen[index])
            {
                ExecuteMarketOrder(TradeType.Sell, Symbol, InitialVolume, label, SL, TP);
            }

            if (HAJ.xClose[index] > HAJ.xOpen[index])
            {
                ExecuteMarketOrder(TradeType.Buy, Symbol, InitialVolume, label, SL, TP);
            }
        }
    }
}

 


@Kyriakos
Replies

adaled
24 Jan 2014, 14:37

I think it's probably the indicator. Post the code of that.


@adaled

Kyriakos
27 Jan 2014, 08:23

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

namespace cAlgo.Indicators
{
    [Indicator(IsOverlay = true, TimeZone = TimeZones.UTC)]
    public class HeikenAshiJohn : Indicator
    {
        [Parameter("Period", DefaultValue = 1, MinValue = 1, MaxValue = 2147483647)]
        public int ParameterPeriod { 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();
        }

        public override void Calculate(int index)
        {
            if (index <= ParameterPeriod)
            {
                xOpen[index] = Math.Round((MarketSeries.Open[index] + MarketSeries.Close[index]) / 2, Symbol.Digits);
                xClose[index] = Math.Round((MarketSeries.Open[index] + MarketSeries.Low[index] + MarketSeries.High[index] + MarketSeries.Close[index]) / 4, Symbol.Digits);
                xHigh[index] = MarketSeries.High[index];
                xLow[index] = MarketSeries.Low[index];

                return;
            }

            xOpen[index] = Math.Round((xOpen[index - 1] + xClose[index - 1]) / 2, Symbol.Digits);
            xClose[index] = Math.Round((MarketSeries.Open[index] + MarketSeries.Low[index] + MarketSeries.High[index] + MarketSeries.Close[index]) / 4, Symbol.Digits);
            xHigh[index] = Math.Max(MarketSeries.High[index], Math.Max(xOpen[index], xClose[index]));
            xLow[index] = Math.Min(MarketSeries.Low[index], Math.Min(xOpen[index], xClose[index]));

            if (xClose[index] < xOpen[index])
            {
                ChartObjects.DrawLine("OpenClose-" + index.ToString(), index, xOpen[index], index, xClose[index], Colors.Red, 5, LineStyle.Solid);
                ChartObjects.DrawLine("HighLow-" + index.ToString(), index, xHigh[index], index, xLow[index], Colors.Red, 1, LineStyle.Solid);
            }
            else
            {
                ChartObjects.DrawLine("OpenClose-" + index.ToString(), index, xOpen[index], index, xClose[index], Colors.Yellow, 5, LineStyle.Solid);
                ChartObjects.DrawLine("HighLow-" + index.ToString(), index, xHigh[index], index, xLow[index], Colors.Yellow, 1, LineStyle.Solid);
            }
        }
    }
}

 


@Kyriakos

adaled
27 Jan 2014, 09:36

They need to be output:

e.g.

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

etc.


@adaled

Kyriakos
27 Jan 2014, 11:10

RE:

Thank you for your help but it still doesn't trade after changing the indicator:

 

namespace cAlgo.Indicators
{
    [Indicator(IsOverlay = true, TimeZone = TimeZones.UTC)]
    public class HeikenAshiJohn : Indicator
    {
        [Parameter("Period", DefaultValue = 1, MinValue = 1, MaxValue = 2147483647)]
        public int ParameterPeriod { get; set; }

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

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

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

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

 


@Kyriakos

adaled
27 Jan 2014, 12:22

remove these lines also:

xClose = CreateDataSeries();
xHigh = CreateDataSeries();
xLow = CreateDataSeries();
xOpen = CreateDataSeries();

 


@adaled

Kyriakos
27 Jan 2014, 12:47

RE:

You are amazing thank you. :)

adaled said:

remove these lines also:

xClose = CreateDataSeries();
xHigh = CreateDataSeries();
xLow = CreateDataSeries();
xOpen = CreateDataSeries();

 

 


@Kyriakos

karlson
02 Mar 2015, 20:16

RE: RE:

Hi,

How to do the same robot please with Heiken Ashi Smoothed :

Thank you

 

 

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

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

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

        private MovingAverage maOpen;
        private MovingAverage maClose;
        private MovingAverage maHigh;
        private MovingAverage maLow;
        private IndicatorDataSeries haClose;
        private IndicatorDataSeries haOpen;

        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);
            haOpen = CreateDataSeries();
            haClose = CreateDataSeries();
        }

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

 


@karlson

brunoamancio.ti
01 Sep 2016, 01:13

I've added some functions for cBots developers to use HA data in their bots. 

Check out /algos/indicators/show/1452


@brunoamancio.ti

thierry
17 Mar 2018, 16:46

Hi,

Have you find how to change the Heiken Ashi Smoothed indicator in order to get outputs ?

Thanks


@thierry

matteo.cechet
29 Oct 2019, 23:51

RE: RE: full program

Hello, please could you give me the complete HA bot program to install on c-trader?

thanx a lot

Matteo


@matteo.cechet