First cBot - Error CS0266

Created at 11 Jan 2020, 08:48
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!
HG

hgrams

Joined 22.02.2018

First cBot - Error CS0266
11 Jan 2020, 08:48


 Hi Guys!

I'm trying to create my own bot and have some difficulties.

my code is based on the 'Sample Trend cBot'

I'm getting the following error:

Error CS0266: Cannot implicitly convert type 'cAlgo.EmaOffset' to 'cAlgo.API.Indicators.MovingAverage'. An explicit conversion exists (are you missing a cast?)

I found another similar question HERE but could not understand how to solve.

My cBot Code:

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

namespace cAlgo
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class SampleTrendcBot : Robot
    {
        [Parameter("MA Type")]
        public MovingAverageType MAType { get; set; }

        [Parameter()]
        public DataSeries SourceSeries { get; set; }

        [Parameter("Slow Periods", DefaultValue = 10)]
        public int SlowPeriods { get; set; }

        [Parameter("Slow Periods Offset", DefaultValue = 1)]
        public int SPOffset { get; set; }

        [Parameter("Fast Periods", DefaultValue = 0)]
        public int FastPeriods { get; set; }

        [Parameter("Fast Periods Offset", DefaultValue = 1)]
        public int FPOffset { get; set; }

        [Parameter("Quantity (Lots)", DefaultValue = 1, MinValue = 0.1, Step = 0.1)]
        public double Quantity { get; set; }

        private MovingAverage slowMa;
        private MovingAverage fastMa;
        private const string label = "Sample Trend cBot EMA Cross";

        protected override void OnStart()
        {
            fastMa = Indicators.GetIndicator<EmaOffset>(SourceSeries, FastPeriods, FPOffset);
            slowMa = Indicators.GetIndicator<EmaOffset>(SourceSeries, SlowPeriods, SPOffset);
        }

        protected override void OnTick()
        {
            var longPosition = Positions.Find(label, Symbol, TradeType.Buy);
            var shortPosition = Positions.Find(label, Symbol, TradeType.Sell);

            var currentSlowMa = slowMa.Result.Last(0);
            var currentFastMa = fastMa.Result.Last(0);
            var previousSlowMa = slowMa.Result.Last(1);
            var previousFastMa = fastMa.Result.Last(1);

            if (previousSlowMa > previousFastMa && currentSlowMa <= currentFastMa && longPosition == null)
            {
                if (shortPosition != null)
                    ClosePosition(shortPosition);
                ExecuteMarketOrder(TradeType.Buy, Symbol, VolumeInUnits, label);
            }
            else if (previousSlowMa < previousFastMa && currentSlowMa >= currentFastMa && shortPosition == null)
            {
                if (longPosition != null)
                    ClosePosition(longPosition);
                ExecuteMarketOrder(TradeType.Sell, Symbol, VolumeInUnits, label);
            }
        }

        private long VolumeInUnits
        {
            get { return Symbol.QuantityToVolume(Quantity); }
        }
    }
}

 

EmaOffset code:

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 EmaOffset : Indicator
    {
        [Parameter(DefaultValue = 0)]
        public int Offset { get; set; }

        [Parameter(DefaultValue = 34)]
        public int Periods { get; set; }

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

        ExponentialMovingAverage _ema;
        protected override void Initialize()
        {
            _ema = Indicators.ExponentialMovingAverage(MarketSeries.Close, Periods);
        }

        public override void Calculate(int index)
        {
            // Calculate value at specified index
            Result[index + Offset] = _ema.Result[index];
        }
    }
}

 


@hgrams
Replies

PanagiotisCharalampous
13 Jan 2020, 08:36

Hi,

You are declaring your indicators as MovingAverage type but then you are initializing them as EmaOffset type

        private MovingAverage slowMa;
        private MovingAverage fastMa;

 

        fastMa = Indicators.GetIndicator<EmaOffset>(SourceSeries, FastPeriods, FPOffset);
        slowMa = Indicators.GetIndicator<EmaOffset>(SourceSeries, SlowPeriods, SPOffset);

You should be declaring them as follows

​
        private EmaOffset slowMa;
        private EmaOffset fastMa;

​

Best Regards,

Panagiotis 

Join us on Telegram

 


@PanagiotisCharalampous

hgrams
14 Jan 2020, 03:22

RE:

That's a silly mistake.

Tks!


@hgrams