Adding built-in indicators to a collection.

Created at 24 May 2021, 03: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!
PA

patrock333

Joined 05.01.2020

Adding built-in indicators to a collection.
24 May 2021, 03:38


I am having problems adding built-in indicators to a collection. Is it possible?

Here is the code so far.

        List<IIndicator> indicatorList;
        Bars prices;
        MovingAverage ma5;
        BollingerBands bb20;
        AverageTrueRange atr20;

        protected override void OnStart()
        {
            // Put your initialization logic here
            prices = MarketData.GetBars(TimeFrame);
            ma5 = Indicators.MovingAverage(prices.ClosePrices, 5, MovingAverageType.Triangular);
            bb20 = Indicators.BollingerBands(prices.ClosePrices, 20, 2, MovingAverageType.Simple);
            atr20 = Indicators.AverageTrueRange(20, MovingAverageType.Exponential);
            indicatorList.Add(ma5); // seems to be adding to the list since there are no errors.
            indicatorList.Add(bb20);
            indicatorList.Add(atr20);
        }

The errors:

Error CS1502: The best overloaded method match for 'System.Collections.Generic.List<cAlgo.API.Internals.IIndicator>.Add(cAlgo.API.Internals.IIndicator)' has some invalid arguments

Error CS1503: Argument 1: cannot convert from 'cAlgo.API.Indicators.BollingerBands' to 'cAlgo.API.Internals.IIndicator'

Error CS1502: The best overloaded method match for 'System.Collections.Generic.List<cAlgo.API.Internals.IIndicator>.Add(cAlgo.API.Internals.IIndicator)' has some invalid arguments

Error CS1503: Argument 1: cannot convert from 'cAlgo.API.Indicators.AverageTrueRange' to 'cAlgo.API.Internals.IIndicator'


@patrock333
Replies

amusleh
24 May 2021, 07:44

Hi,

Not all built-in indicators implement the IIndicator interface, or Indicator and Algo base classes, some do like Moving Averages.

The solution for you is to use Object as your List generic type, and then cast back from Object to the indicator type when you needed by checking the type of list Item.

Or use a dictionary with an Enum key type, the Enum will have each indicator type name and you can easily cast the object to that indicator type instead of checking each item, it will increase the performance.

To check the bases of built-in indicator types you can use Visual Studio.


@amusleh