Run code once per a Bar/Candle in custom indicator

Created at 08 Nov 2017, 10:03
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!
itmfar's avatar

itmfar

Joined 08.11.2017

Run code once per a Bar/Candle in custom indicator
08 Nov 2017, 10:03


I am going to run my code for each candle once. Especially last candle which is current index

        public override void Calculate(int index)
        {
           Print(" print only once for each bar at the moment " + index); 
        }

but it print with each tick and last index repeats a lot

....
08/11/2017 11:27:52.937 |  print only once for each bar at the moment 1996
08/11/2017 11:27:52.937 |  print only once for each bar at the moment 1997
08/11/2017 11:27:52.937 |  print only once for each bar at the moment 1998
08/11/2017 11:27:52.937 |  print only once for each bar at the moment 1999
08/11/2017 11:27:55.093 |  print only once for each bar at the moment 2000
08/11/2017 11:27:55.093 |  print only once for each bar at the moment 2000
08/11/2017 11:27:57.140 |  print only once for each bar at the moment 2000
08/11/2017 11:27:57.781 |  print only once for each bar at the moment 2000
08/11/2017 11:27:58.687 |  print only once for each bar at the moment 2000

How it is possible to run my code once only for each bar without any doublicate of last bar?

 


@itmfar
Replies

PanagiotisCharalampous
08 Nov 2017, 10:35

Hi itmfar,

Calculate function is invoked once for each bar and then for each tick for the last bar. The reason this happens is that for each tick the values for the last bar change therefore the indicator might need to be recalculated. If you want to skip recalculation for the last bar, you might consider using the IsLastBar property of the Indicator.

Best Regards,

Panagiotis


@PanagiotisCharalampous

itmfar
08 Nov 2017, 11:19

RE:

Panagiotis Charalampous said:

Hi itmfar,

Calculate function is invoked once for each bar and then for each tick for the last bar. The reason this happens is that for each tick the values for the last bar change therefore the indicator might need to be recalculated. If you want to skip recalculation for the last bar, you might consider using the IsLastBar property of the Indicator.

Best Regards,

Panagiotis

thanks for your response. i have changed my code:

        public override void Calculate(int index)
        {

            if (IsLastBar)
                Print(" print only once for each bar at the moment" + index);

        }

but it still repeats last result


08/11/2017 12:47:27.789 |  print only once for each bar at the moment2098
08/11/2017 12:47:27.914 |  print only once for each bar at the moment2098
08/11/2017 12:47:33.102 |  print only once for each bar at the moment2098
08/11/2017 12:47:33.399 |  print only once for each bar at the moment2098
08/11/2017 12:47:33.774 |  print only once for each bar at the moment2098
08/11/2017 12:47:34.071 |  print only once for each bar at the moment2098
08/11/2017 12:47:34.321 |  print only once for each bar at the moment2098

 


@itmfar

PanagiotisCharalampous
08 Nov 2017, 11:29

Hi irmfar,

It will repeat the last result since you are instructing the indicator to print only when it is at the last bar. You should change the condition as follows

        public override void Calculate(int index)
        {
            if (!IsLastBar)
                Print(" print only once for each bar at the moment" + index);
        }

Let me know if this helps.

Best Regards,

Panagiotis


@PanagiotisCharalampous

itmfar
08 Nov 2017, 12:11

RE:

Panagiotis Charalampous said:

Hi irmfar,

It will repeat the last result since you are instructing the indicator to print only when it is at the last bar. You should change the condition as follows

        public override void Calculate(int index)
        {
            if (!IsLastBar)
                Print(" print only once for each bar at the moment" + index);
        }

Let me know if this helps.

Best Regards,

Panagiotis

it totally helps. thank you


@itmfar