How to Use Heiken Ashi data calculated EMA cross-over and cross-under for cRobot scripts

Created at 11 May 2023, 00:59
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!
AS

ashish.sah.np

Joined 19.12.2022

How to Use Heiken Ashi data calculated EMA cross-over and cross-under for cRobot scripts
11 May 2023, 00:59


I have tried the following but it didn't worked.

Can anyone help me out in this? It will be a great help. Thank You!!

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

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class HeikenAshiEmaCrossOverBot : Robot
    {
        [Parameter("EMA Period", DefaultValue = 50)]
        public int EmaPeriod { get; set; }

        [Parameter("Volume", DefaultValue = 10000, MinValue = 0)]
        public int Volume { get; set; }

        private ExponentialMovingAverage _ema;
        private IndicatorDataSeries _haClose;
        private IndicatorDataSeries _haOpen;

        protected override void OnStart()
        {
            _haClose = CreateDataSeries();
            _haOpen = CreateDataSeries();
        }

        protected override void OnBar()
        {
            if (Bars.ClosePrices.Count < EmaPeriod)
            {
                return;
            }

            var index = Bars.ClosePrices.Count - 1;

            _haClose[index] = (MarketSeries.Open[index] + MarketSeries.High[index] + MarketSeries.Low[index] + MarketSeries.Close[index]) / 4;
            _haOpen[index] = index > 0 ? (_haOpen[index - 1] + _haClose[index - 1]) / 2 : (MarketSeries.Open[index] + MarketSeries.Close[index]) / 2;

            _ema = Indicators.ExponentialMovingAverage(_haClose, EmaPeriod);

            if (_haClose[index] > _ema.Result[index] && _haOpen[index - 1] < _ema.Result[index - 1])
            {
                ClosePreviousPositions();
                ExecuteMarketOrder(TradeType.Buy, SymbolName, Volume, "HeikenAshiEmaCrossOverBot");
            }
            else if (_haClose[index] < _ema.Result[index] && _haOpen[index - 1] > _ema.Result[index - 1])
            {
                ClosePreviousPositions();
                ExecuteMarketOrder(TradeType.Sell, SymbolName, Volume, "HeikenAshiEmaCrossOverBot");
            }
        }

        private void ClosePreviousPositions()
        {
            foreach (var position in Positions)
            {
                if (position.Label == "HeikenAshiEmaCrossOverBot")
                {
                    ClosePosition(position);
                }
            }
        }
    }
}

@ashish.sah.np
Replies

firemyst
11 May 2023, 05:05

What's the problem? Eg, why do you think it doesn't work?

Also, are you aware cTrader has HA candles built in, so you don't have to calculate them yourself -- just pass in the market data by choosing the appropriate timeframe. That is, Timeframe.HeinkinDay, etc.


@firemyst

ashish.sah.np
11 May 2023, 08:49

No trade is taking place from above trade.
 

And also When i coded a simple crossover and crossunder with Bars.ClosePrices and tried to choose the Heiken Ashi chart during the backtest with my desired timeframe but it seem that it didn’t use Heiken Ashi Candle data for its calculation during backtest.


@ashish.sah.np