OnBar never executed in custom indicator

Created at 21 Dec 2016, 18:38
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!
PI

pipsniper123

Joined 21.12.2016

OnBar never executed in custom indicator
21 Dec 2016, 18:38


Hello, 

 

Please help me with the following problem.

I want to make an idicator which updates its value every time a new bar is opened. As an example, I wrote this very simple code which adds 1 to the variable 'result' everytime a new bar is opened. 

HOWEVER, the OnBar() part somehow never gets executed. So the message "A new bar is opened!" is never showed and the variable result is never updated. 

Please tell me what the problem is.

 

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

namespace cAlgo
{
    [Indicator(IsOverlay = false, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class NewIndicator : Indicator
    {
        [Parameter(DefaultValue = 0.0)]
        public double Parameter { get; set; }

        [Output("Main")]
        public IndicatorDataSeries Result { get; set; }

        MovingAverage SMA;

        protected override void Initialize()
        {
            SMA = Indicators.MovingAverage(MarketSeries.Close, 20, MovingAverageType.Simple);
        }


        public override void Calculate(int index)
        {

        }

        public void OnBar()
        {
            Print("A new bar is opened!");
        }

    }
}


@pipsniper123
Replies

andi21
21 Dec 2016, 18:45

Hello pipsniper123, the solution is the correct method defintion: public override void OnBar() { } // notice the override keyword Best Regards, Andi21
@andi21

pipsniper123
21 Dec 2016, 22:05

RE:

andi21 said:

Hello pipsniper123, the solution is the correct method defintion: public override void OnBar() { } // notice the override keyword Best Regards, Andi21

Hi Andi, thanks for your response.

But when I try this, I get the error: Error CS0115: 'cAlgo.NewIndicator.OnBar()': no suitable method found to override.

When exactly are you allowed to use override? I got this message a lot of times before when I tried implementing override somewhere. 

 

Kind regards

 


@pipsniper123

andi21
21 Dec 2016, 23:05

RE: RE:

pipsniper123 said:

andi21 said:

Hello pipsniper123, the solution is the correct method defintion: public override void OnBar() { } // notice the override keyword Best Regards, Andi21

Hi Andi, thanks for your response.

But when I try this, I get the error: Error CS0115: 'cAlgo.NewIndicator.OnBar()': no suitable method found to override.

When exactly are you allowed to use override? I got this message a lot of times before when I tried implementing override somewhere. 

 

Kind regards

Sorry, my mistake - the OnBar Method is only available in a Robot. In an indicator there is only the Calculate Method, which is called for each historic bar starting from the beginning of the series up to the current bar and then on each incoming tick.

I haven't used an indicator for just on any new bar, but perhaps you can use a variable MyRememberedLastOpenIndex etc. and save the last .Open.Count and if MyRememberedLastOpenIndex != Open.Count, then it is a new bar and remember the new open Count. That is just a simple thought about this and could be a solution.

Override is a keyword which is used, when you derive from a class and this class has abstract or virtual methods which can be overidden.


@andi21

andi21
21 Dec 2016, 23:21

Or more easy: just remember the incoming index parameter in the calculate method and if this index changed then it is a new bar (it is late so this easier solution came a little later than my previous answer ;) ).
@andi21

trading.kay27
27 Jan 2017, 09:02

To late to reply but, if you still having this trouble, then use OpenTime for the current bar and save it in a variable. On every tick compare open time, if its changed, its your new bar.


@trading.kay27

itmfar
08 Nov 2017, 09:29

RE:

andi21 said:

Or more easy: just remember the incoming index parameter in the calculate method and if this index changed then it is a new bar (it is late so this easier solution came a little later than my previous answer ;) ).

How we can save old index and comparing with new one? because as result of each tick both parameters in Calculate would be update and reffer to same index


@itmfar

itmfar
08 Nov 2017, 09:46

RE:

hook_1000 said:

To late to reply but, if you still having this trouble, then use OpenTime for the current bar and save it in a variable. On every tick compare open time, if its changed, its your new bar.

Would you please explain how to save OpenTime in calculate() and save it? it is always last openTime for each tick

           public int currentIndex;  

public override void Calculate(int index)
        {
            currentIndex = MarketSeries.OpenTime.LastValue;
            Print( " last " + MarketSeries.Open.LastValue);

        }


@itmfar

andi21
08 Nov 2017, 10:02

RE: RE:

itmfar said:

andi21 said:

Or more easy: just remember the incoming index parameter in the calculate method and if this index changed then it is a new bar (it is late so this easier solution came a little later than my previous answer ;) ).

How we can save old index and comparing with new one? because as result of each tick both parameters in Calculate would be update and reffer to same index

You could do something like this:

public int lastCalculatedIdx = -1;  

public override void Calculate(int index)
        {
            bool gotANewIdxSoANewBar = index > lastCalculatedIdx;
            if (gotANewIdxSoANewBar)
            {
                  lastCalculatedIdx = index;
                  // Do some calculations
            }
        }

 


@andi21

itmfar
08 Nov 2017, 11:56

RE: RE: RE:

andi21 said:

itmfar said:

andi21 said:

Or more easy: just remember the incoming index parameter in the calculate method and if this index changed then it is a new bar (it is late so this easier solution came a little later than my previous answer ;) ).

How we can save old index and comparing with new one? because as result of each tick both parameters in Calculate would be update and reffer to same index

You could do something like this:

public int lastCalculatedIdx = -1;  

public override void Calculate(int index)
        {
            bool gotANewIdxSoANewBar = index > lastCalculatedIdx;
            if (gotANewIdxSoANewBar)
            {
                  lastCalculatedIdx = index;
                  // Do some calculations
            }
        }

thanks alot. it works

 


@itmfar