Minimum number of bars since moving average crossover

Created at 21 Jul 2022, 16:55
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!
WA

waym77

Joined 22.07.2021

Minimum number of bars since moving average crossover
21 Jul 2022, 16:55


Hi,

I created an indicator that counts the bars since a triple EMA crossover, it needs to reach the minimum count before signaling entries.

The count stays stuck at 1 though. Any advice?

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 NewIndicator : Indicator
    {
        ExponentialMovingAverage _fast, _medium, _slow;

        double _trend_min_length;

        protected override void Initialize()
        {
            _slow = Indicators.ExponentialMovingAverage(Bars.ClosePrices, 12);
            _medium = Indicators.ExponentialMovingAverage(Bars.ClosePrices, 26);
            _fast = Indicators.ExponentialMovingAverage(Bars.ClosePrices, 50);

            _trend_min_length = 5;
        }

        public override void Calculate(int index)
        {
            int _bull_count = 0;
            int _bear_count = 0;

            if (_fast.Result.LastValue > _medium.Result.LastValue && _medium.Result.LastValue > _slow.Result.LastValue)
            {
                _bear_count = 0;
                _bull_count++;
                Print("Bull Count #{0}: {1}", index, _bull_count);
            }

            if (_fast.Result.LastValue < _medium.Result.LastValue && _medium.Result.LastValue < _slow.Result.LastValue)
            {
                _bull_count = 0;
                _bear_count++;
                Print("Bear Count #{0}: {1}", index, _bear_count);
            }

            if (_bull_count >= _trend_min_length)
            {
                Print("Is Bullish, Can Look For Buys");
            }

            if (_bear_count >= _trend_min_length)
            {
                Print("Is Bearish, Can Look For Sells");
            }
        }
    }
}

 


@waym77
Replies

PanagiotisCharalampous
25 Jul 2022, 12:12

Hi waym77,

It's because you set the counters to 0 every time the Calculate method is called.

Best Regards,

Panagiotis 

Join us on Telegram and Facebook


@PanagiotisCharalampous