Calculate indicator value on close of bar (and not per tick)

Created at 28 Nov 2016, 11:52
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!
CT

ctid220249

Joined 08.06.2016

Calculate indicator value on close of bar (and not per tick)
28 Nov 2016, 11:52


Hi guys

I am working on an indicator that draws points to indicate highs and lows.

When I add the indicator to a chart, all the historic points are drawn perfectly.

The newly drawn points, however, are a mess, as the indicator does not wait for the new bar to close before adding it.

Does anybody know hoe to get around this.

 

Regards

 


@ctid220249
Replies

Jiri
28 Nov 2016, 12:18

Hi, you need to write a method that's called on each new bar. See sample below.

using cAlgo.API;

namespace cAlgo
{
    [Indicator(IsOverlay = false, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class NewIndicator : Indicator
    {
        private int lastIndex = 0;

        public override void Calculate(int index)
        {
            if (index > lastIndex)
            {
                OnBarClosed(lastIndex);
                lastIndex = index;
            }
        }

        private void OnBarClosed(int index)
        {
            // Put your logic here
        }
    }
}

 


@Jiri

ctid220249
28 Nov 2016, 12:37

Thank you for the assistance.

You're a STAR!

 

Regards

 


@ctid220249

tazotodua
08 Aug 2019, 13:43

I use shorter approach:

 

.....
        [Parameter(DefaultValue = true)]
        public bool CalculateOnBarClose { get; set; }

        private int lastIndex = 0;
 
        public override void Calculate(int index)
        {
            if (CalculateOnBarClose) if (index == lastIndex) return;  lastIndex = index;
            
            // Put your logic here
        }
.....

 


@tazotodua