Indicator lines update

Created at 30 Jan 2024, 20:50
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!
DA

danerius

Joined 07.01.2024

Indicator lines update
30 Jan 2024, 20:50


Hello

Id like this indicator to update on every tick. As it is now, its updating for every new candle. How can I fix that?

Thanks /d

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 DoubleRenko : Indicator
    {
        [Parameter("ATR Periods (default 12)", DefaultValue = 12, MinValue = 2)]
        public int AtrPeriods { get; set; }

        [Parameter("Lookback Period (default 1000)", DefaultValue = 500, MinValue = 1)]
        public int LookbackPeriod { get; set; }

        [Parameter("ATR Smoothing Type", DefaultValue = MovingAverageType.Exponential)]
        public MovingAverageType AtrSmoothingType { get; set; }

        [Output("Renko Top", LineColor = "Gray")]
        public IndicatorDataSeries RenkoTop { get; set; }

        [Output("Renko Bottom", LineColor = "Gray")]
        public IndicatorDataSeries RenkoBottom { get; set; }

        [Output("Current Brick", LineColor = "White", Thickness = 1)]
        public IndicatorDataSeries CurrentBrick { get; set; }
        
        [Output("Direction Change", PlotType = PlotType.Points, LineColor = "Red")]
        public IndicatorDataSeries DirectionChange { get; set; }

        public double _brickSize;
        public AverageTrueRange _atr;
        public bool _isAscending = true;
        
        private bool _previousAscending;
 
        protected override void Initialize()
        {
            _atr = Indicators.AverageTrueRange(AtrPeriods, AtrSmoothingType);
            _previousAscending = true; // Initialize with a default value
        }

        public override void Calculate(int index)
{
    if (index < LookbackPeriod)
    {
        RenkoTop[index] = double.NaN;
        RenkoBottom[index] = double.NaN;
        CurrentBrick[index] = double.NaN;
        return;
    }
    
    double atrValue = _atr.Result[index];
    _brickSize = atrValue;

    if (index == LookbackPeriod)
    {
        RenkoTop[index] = Bars.ClosePrices[index];
        RenkoBottom[index] = Bars.ClosePrices[index] - _brickSize;
        _isAscending = Bars.ClosePrices[index] > Bars.ClosePrices[index - 1];
    }
    else
    {
        RenkoTop[index] = RenkoTop[index - 1];
        RenkoBottom[index] = RenkoBottom[index - 1];

        if (Bars.ClosePrices[index] >= RenkoTop[index] + _brickSize)
        {
            _isAscending = true;
            RenkoTop[index] = RenkoTop[index - 1] + _brickSize;
            RenkoBottom[index] = RenkoTop[index] - _brickSize;
        }
        else if (Bars.ClosePrices[index] <= RenkoBottom[index] - _brickSize)
        {
            _isAscending = false;
            RenkoBottom[index] = RenkoBottom[index - 1] - _brickSize;
            RenkoTop[index] = RenkoBottom[index] + _brickSize;
        }
    }

    CurrentBrick[index] = _isAscending ? RenkoTop[index] : RenkoBottom[index];
    
            // Detect direction change
            if (_isAscending != _previousAscending)
            {
                DirectionChange[index] = _isAscending ? RenkoTop[index] : RenkoBottom[index];
                _previousAscending = _isAscending;
            }
            else
            {
                DirectionChange[index] = double.NaN; // No direction change
            }
        }
    }
}


@danerius
Replies

danerius
05 Feb 2024, 09:56

Bumping…


@danerius

firemyst
13 Feb 2024, 00:36

What time frame are you using?

You don't say, and that's what's causing your issue. 

 

Obviously that will make a difference. Your default lookback period is 1000. So if you're on a weekly, daily, 4-hourly, 200 Renko, 100-range, 2/3/4 hour HA, etc etc, you won't have more bars than your lookback period, so your “else” logic never gets executed.

 if (index < LookbackPeriod)
    {
        RenkoTop[index] = double.NaN;
        RenkoBottom[index] = double.NaN;
        CurrentBrick[index] = double.NaN;
        return;
    }
    
    			// HOW DO YOU EXPECT THIS LOGIC TO EVER BE EXECUTED
    			// with a 1000 lookback period when you
    			//have less than 1000 (default lookback period) bars on the chart?
    			//Unless you get more than 1000 bars/candles on a chart, index
    			//will always be less than the lookback period except in 1 case.
    			
     double atrValue = _atr.Result[index];
    _brickSize = atrValue;

if (index == LookbackPeriod)
    {
        RenkoTop[index] = Bars.ClosePrices[index];
        RenkoBottom[index] = Bars.ClosePrices[index] - _brickSize;
        _isAscending = Bars.ClosePrices[index] > Bars.ClosePrices[index - 1];
    }
    else
    {

    }

 

You should debug your code by putting in lots of print statements to see where it's getting to, and what parts of your logic are or are not being executed.

 


@firemyst

danerius
21 Feb 2024, 20:16

RE: Indicator lines update

firemyst said: 

What time frame are you using?

Thanks. That makes sense. Havent decided on a Timeframe yet, still in the works /danerius


@danerius