CS0311 error building a HMA bot

Created at 10 Mar 2019, 18:10
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!
ER

erikvb

Joined 09.02.2019

CS0311 error building a HMA bot
10 Mar 2019, 18:10


Hi , i get this error on my first bot build.

Severity Code Description Project File Line Suppression State

Error CS0311 The type 'cAlgo.Robots.HMASignals' cannot be used as type parameter 'TIndicator' in the generic type or method 'IIndicatorsAccessor.GetIndicator<TIndicator>(params object[])'. There is no implicit reference conversion from 'cAlgo.Robots.HMASignals' to 'cAlgo.API.Indicator'. HMABOT d:\Users\Erik\Documents\cAlgo\Sources\Robots\HMABOT\HMABOT\HMABOT.cs 45 Active

Can i only use predefined indicators from cAlgo  (RSI , SMA ...) 

Under is the code i started.

But i not yet know how to capture the result from the indicator and transfer to the bot.

----------------------------------------------------------------------------------------------------------------------------------------------------------------------

 

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

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class HMABOT : Robot
    {

        [Parameter(DefaultValue = 0.0)]
        public double Parameter { get; set; }

        [Parameter("Label", DefaultValue = "HMA")]
        public string Label { get; set; }

        [Parameter("Volume", DefaultValue = 1000)]
        public int Volume { get; set; }

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

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

        double i = hmaSignal.hma.LastValue;

        internal HMASignals HmaSignals { get; set; }

        public bool IsBullish = false;
        public bool IsBearish = false;


        private MarketSeries HmaDaySeries;
        // private HMASignals _hmaSignal;

        protected override void OnStart()
        {
            // Put your initialization logic here
            HmaDaySeries = MarketData.GetSeries(TimeFrame.Daily);
            //HmaSignals = Indicators.GetIndicator<HMASignals>(Source, Periods);
        }

        protected override void OnTick()
        {
            // Put your core logic here
            // BEARISH
            if (hmaSignal.IsBearish() && Positions.FindAll(Label, Symbol, TradeType.Buy).Length == 0)
            {
                IsBearish = true;
                IsBullish = false;
                close(TradeType.Sell);
                trade(TradeType.Buy);

            }

            // BULLISH
            if (hmaSignal.IsBullish() && Positions.FindAll(Label, Symbol, TradeType.Buy).Length == 0)
            {
                IsBearish = false;
                IsBullish = true;
                close(TradeType.Buy);
                trade(TradeType.Sell);
            }
        }

        private void close(TradeType tradeType)
        {
            foreach (var position in Positions.FindAll(Label, Symbol, tradeType))
                ClosePosition(position);
        }


        private void trade(TradeType tradetype)
        {
            ExecuteMarketOrder(tradetype, Symbol, Volume, Label);
        }
        protected override void OnStop()
        {
            // Put your deinitialization logic here
        }
    }

    internal class HMASignals
    {
    }
}

 

----------------------------------------------------------------------------------------------------------------------------------------------------------------------

indicator code

 

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

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class HMABOT : Robot
    {

        [Parameter(DefaultValue = 0.0)]
        public double Parameter { get; set; }

        [Parameter("Label", DefaultValue = "HMA")]
        public string Label { get; set; }

        [Parameter("Volume", DefaultValue = 1000)]
        public int Volume { get; set; }

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

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

        double i = hmaSignal.hma.LastValue;

        internal HMASignals HmaSignals { get; set; }

        public bool IsBullish = false;
        public bool IsBearish = false;


        private MarketSeries HmaDaySeries;
        // private HMASignals _hmaSignal;

        protected override void OnStart()
        {
            // Put your initialization logic here
            HmaDaySeries = MarketData.GetSeries(TimeFrame.Daily);
            //HmaSignals = Indicators.GetIndicator<HMASignals>(Source, Periods);
        }

        protected override void OnTick()
        {
            // Put your core logic here
            // BEARISH
            if (hmaSignal.IsBearish() && Positions.FindAll(Label, Symbol, TradeType.Buy).Length == 0)
            {
                IsBearish = true;
                IsBullish = false;
                close(TradeType.Sell);
                trade(TradeType.Buy);

            }

            // BULLISH
            if (hmaSignal.IsBullish() && Positions.FindAll(Label, Symbol, TradeType.Buy).Length == 0)
            {
                IsBearish = false;
                IsBullish = true;
                close(TradeType.Buy);
                trade(TradeType.Sell);
            }
        }

        private void close(TradeType tradeType)
        {
            foreach (var position in Positions.FindAll(Label, Symbol, tradeType))
                ClosePosition(position);
        }


        private void trade(TradeType tradetype)
        {
            ExecuteMarketOrder(tradetype, Symbol, Volume, Label);
        }
        protected override void OnStop()
        {
            // Put your deinitialization logic here
        }
    }

    internal class HMASignals
    {
    }
}


@erikvb
Replies

PanagiotisCharalampous
12 Mar 2019, 12:07

Hi erikvb,

There are several issues with your cBot. I made some changes so that you can build it but you need to check if this is what you want. See the code below

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

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class HMABOT : Robot
    {
        [Parameter(DefaultValue = 0.0)]
        public double Parameter { get; set; }

        [Parameter("Label", DefaultValue = "HMA")]
        public string Label { get; set; }

        [Parameter("Volume", DefaultValue = 1000)]
        public int Volume { get; set; }

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

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

        internal HMASignals HmaSignals { get; set; }

        public bool IsBullish = false;
        public bool IsBearish = false;


        private MarketSeries HmaDaySeries;
        private HMASignals _hmaSignal;

        protected override void OnStart()
        {
            // Put your initialization logic here
            HmaDaySeries = MarketData.GetSeries(TimeFrame.Daily);
            _hmaSignal = Indicators.GetIndicator<HMASignals>(Periods,Source,false,false,1,false,1);
        }

        protected override void OnTick()
        {
            // Put your core logic here
            // BEARISH
            if (_hmaSignal.IsBearish && Positions.FindAll(Label, Symbol, TradeType.Buy).Length == 0)
            {
                IsBearish = true;
                IsBullish = false;
                close(TradeType.Sell);
                trade(TradeType.Buy);

            }

            // BULLISH
            if (_hmaSignal.IsBullish && Positions.FindAll(Label, Symbol, TradeType.Buy).Length == 0)
            {
                IsBearish = false;
                IsBullish = true;
                close(TradeType.Buy);
                trade(TradeType.Sell);
            }
        }

        private void close(TradeType tradeType)
        {
            foreach (var position in Positions.FindAll(Label, Symbol, tradeType))
                ClosePosition(position);
        }

        private void trade(TradeType tradetype)
        {
            ExecuteMarketOrder(tradetype, Symbol, Volume, Label);
        }
        protected override void OnStop()
        {
            // Put your deinitialization logic here
        }
    }
}

Best Regards,

Panagiotis


@PanagiotisCharalampous

erikvb
12 Mar 2019, 12:49

crash on backtest

Thank you for the helping hand  Panagiotis.

 

I still need to learn a lot about programming in C+ :)

At the backtessting a get a crash on start :

 

08/02/2011 09:56:00.000 | Backtesting was stopped
08/02/2011 09:56:00.000 | Crashed in OnStart with ArgumentException: Incorrect parameters count. Parameternaam: parameterValues
08/02/2011 09:56:00.000 | Backtesting started

I see i copy not the indicatorcode in the topic.

i use this indicator :

https://ctrader.com/algos/indicators/show/930

 

I change also the timeframe to hour in the robotcode

 HmaDaySeries = MarketData.GetSeries(TimeFrame.Hour);

thank you all

 

 

 

 

 

 

 


@erikvb

PanagiotisCharalampous
12 Mar 2019, 14:08

Hi erikvb,

It seems we have different indicators and it crashes on parameter initialization.You need to change the initialization of the indicator to the following

_hmaSignal = Indicators.GetIndicator<HMASignals>(Periods,false,false,1,false,1);

Best Regards,

Panagiotis


@PanagiotisCharalampous

erikvb
13 Mar 2019, 13:04

no trades in backtesting

Hi Panagiotis,

thank you for fix the backtesting error.

Did you get any trades in the backtesting ?

I think the bot not pick up the signals.

I will check what is wrong in the code.

 


@erikvb

PanagiotisCharalampous
13 Mar 2019, 14:05

Hi erikvb,

I did not backtest it. I just fixed the build errors and made sure that it doesn't throw exceptions.

Best Regards,

Panagiotis


@PanagiotisCharalampous

erikvb
18 Mar 2019, 13:51

Hi Panagiotis,

 

I cleanup the code this weekend.

But when i backtesting the bot , he open every minute a new trade. backtest from 06/03/2018 to 09/03/2019

In the events log the bot open more then 390 sell positions .

when the bot get the first buy position ( trade 390) the bot stop with working.

i just need 1 order between both signals.

I need to use max trades ?


760-371Position Closed06/03/2018 08:31:00.000€ 1kSell1.23510--1.234490.546.11148.151159.3

761-372Position Closed06/03/2018 08:31:00.000€ 1kSell1.23522--1.234490.657.31148.741159.3

762-373Position Closed06/03/2018 08:31:00.000€ 1kSell1.23527--1.234490.697.81149.371159.3

763-374Position Closed06/03/2018 08:31:00.000€ 1kSell1.23525--1.234490.677.61149.981159.3

764-375Position Closed06/03/2018 08:31:00.000€ 1kSell1.23518--1.234490.616.91150.531159.3

765-376Position Closed06/03/2018 08:31:00.000€ 1kSell1.23516--1.234490.596.71151.061159.3

766-377Position Closed06/03/2018 08:31:00.000€ 1kSell1.23528--1.234490.77.91151.71159.3

767-378Position Closed06/03/2018 08:31:00.000€ 1kSell1.23546--1.234490.869.71152.51159.3

768-379Position Closed06/03/2018 08:31:00.000€ 1kSell1.23535--1.234490.768.61153.21159.3

769-380Position Closed06/03/2018 08:31:00.000€ 1kSell1.23544--1.234490.849.51153.981159.3

770-381Position Closed06/03/2018 08:31:00.000€ 1kSell1.23538--1.234490.798.91154.711159.3

771-382Position Closed06/03/2018 08:31:00.000€ 1kSell1.23535--1.234490.768.61155.411159.3

772-383Position Closed06/03/2018 08:31:00.000€ 1kSell1.23527--1.234490.697.81156.041159.3

773-384Position Closed06/03/2018 08:31:00.000€ 1kSell1.23541--1.234490.819.21156.791159.3

774-385Position Closed06/03/2018 08:31:00.000€ 1kSell1.23552--1.234490.9110.31157.641159.3

775-386Position Closed06/03/2018 08:31:00.000€ 1kSell1.23542--1.234490.829.31158.41159.3

776-387Position Closed06/03/2018 08:31:00.000€ 1kSell1.23523--1.234490.657.41158.991159.3

777-388Position Closed06/03/2018 08:31:00.000€ 1kSell1.23475--1.234490.232.61159.161159.3

778-389Position Closed06/03/2018 08:31:00.000€ 1kSell1.23472--1.234490.22.31159.31159.3

779-390Create Position06/03/2018 08:31:00.000€ 1kBuy1.23449------1159.15
 

 

 

 

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

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class HMABOT : Robot
    {

        [Parameter("Label", DefaultValue = "HMA")]
        public string Label { get; set; }

        [Parameter("Volume", DefaultValue = 1000)]
        public int Volume { get; set; }

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

        internal HMASignals HmaSignals { get; set; }

        private MarketSeries HmaDaySeries;
        private HMASignals _hmaSignal;



        protected override void OnStart()
        {
            var index = MarketSeries.Close.Count - 1;

            // Put your initialization logic here
            HmaDaySeries = MarketData.GetSeries(TimeFrame.Hour);
            //_hmaSignal = Indicators.GetIndicator<HMASignals>(Periods, Source, false, false, 1, false, 1);
            //_hmaSignal = Indicators.GetIndicator<HMASignals>(Periods, true, true, 5, true, 25);
            // _hmaSignal = Indicators.GetIndicator(HmaDaySeries, 21, false, false, 3, false, 50);
            _hmaSignal = Indicators.GetIndicator<HMASignals>(HmaDaySeries, 21, falsefalse, 3, false, 24);

        }

        protected override void OnTick()
        {
            // Put your core logic here
            // BEARISH
            double i = _hmaSignal.hma.LastValue;

            if (_hmaSignal.IsBearish && Positions.FindAll(Label, Symbol, TradeType.Buy).Length == 0)
            {
                close(TradeType.Sell);
                trade(TradeType.Buy);

            }

            // BULLISH

            if (_hmaSignal.IsBullish && Positions.FindAll(Label, Symbol, TradeType.Buy).Length == 0)
            {

                close(TradeType.Buy);
                trade(TradeType.Sell);
            }
        }

        private void close(TradeType tradeType)
        {
            foreach (var position in Positions.FindAll(Label, Symbol, tradeType))
                ClosePosition(position);
        }

        private void trade(TradeType tradetype)
        {
            ExecuteMarketOrder(tradetype, Symbol, Volume, Label);
        }
        protected override void OnStop()
        {
            // Put your deinitialization logic here
        }
    }
}


@erikvb

PanagiotisCharalampous
18 Mar 2019, 17:53

Hi erikvb,

Try these conditions

            if (_hmaSignal.IsBearish && Positions.Count(x => x.Label == Label && x.TradeType == TradeType.Buy) == 0)
            {
                close(TradeType.Sell);
                trade(TradeType.Buy);

            }

            // BULLISH

            if (_hmaSignal.IsBullish && Positions.Count(x => x.Label == Label && x.TradeType == TradeType.Sell) == 0)
            {

                close(TradeType.Buy);
                trade(TradeType.Sell);
            }

Best Regards,

Panagiotis


@PanagiotisCharalampous

erikvb
19 Mar 2019, 10:52 ( Updated at: 21 Dec 2023, 09:21 )

Hi Panagiotis,

thanks again for  the input.

but when i backtest i get 3900 trades

there are only 9 signals between 6/03/2019 and 9/03/2019

So it seems the bot never pickup the signals correctly.

last code i tested : from 6/03/2019 and 9/03/2019 / timeframe hour / periods 14

thanks again for your time

 

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

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class HMABOT : Robot
    {

        [Parameter("Label", DefaultValue = "HMA")]
        public string Label { get; set; }

        [Parameter("Volume", DefaultValue = 1000)]
        public int Volume { get; set; }

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

        internal HMASignals HmaSignals { get; set; }

        private MarketSeries HmaDaySeries;
        private HMASignals _hmaSignal;

        protected override void OnStart()
        {
            var index = MarketSeries.Close.Count - 1;

            // Put your initialization logic here
            HmaDaySeries = MarketData.GetSeries(TimeFrame.Hour);
            //_hmaSignal = Indicators.GetIndicator<HMASignals>(Periods, Source, false, false, 1, false, 1);
            _hmaSignal = Indicators.GetIndicator<HMASignals>(Periods, truetrue, 5, true, 25);
            // _hmaSignal = Indicators.GetIndicator(HmaDaySeries, 21, false, false, 3, false, 50);
            // _hmaSignal = Indicators.GetIndicator<HMASignals>(HmaDaySeries, 21, false, false, 3, false, 24);

        }


        protected override void OnTick()
        {
            // Put your core logic here
            // BEARISH

            double i = _hmaSignal.hma.LastValue;
            // Print("{0}", _hmaSignal.hma.LastValue);
            //if (_hmaSignal.IsBearish && Positions.FindAll(Label, Symbol, TradeType.Buy).Length == 0)
            if (_hmaSignal.IsBearish && Positions.Count(x => x.Label == Label && x.TradeType == TradeType.Buy) == 0)
            {
                close(TradeType.Sell);
                trade(TradeType.Buy);
            }

            // BULLISH

            //if (_hmaSignal.IsBullish && Positions.FindAll(Label, Symbol, TradeType.Buy).Length == 0)
            if (_hmaSignal.IsBullish && Positions.Count(x => x.Label == Label && x.TradeType == TradeType.Sell) == 0)
            {
                close(TradeType.Buy);
                trade(TradeType.Sell);
            }
        }

        private void close(TradeType tradeType)
        {
            foreach (var position in Positions.FindAll(Label, Symbol, tradeType))
                ClosePosition(position);
        }

        private void trade(TradeType tradetype)
        {
            ExecuteMarketOrder(tradetype, Symbol, Volume, Label);
        }
        protected override void OnStop()
        {
            // Put your deinitialization logic here
        }
    }
}

 

 




@erikvb

PanagiotisCharalampous
19 Mar 2019, 11:26

Hi erikvb,

Try placing your logic inside OnBar() instead of inside OnTick().

Best Regards,

Panagiotis


@PanagiotisCharalampous

erikvb
19 Mar 2019, 12:51 ( Updated at: 21 Dec 2023, 09:21 )

Hi Panagiotis,

I found out i was backtesting from 2018 and not 2019 :) 

but it not fix the problem compleet.

i backtest now in visual mode , the problem is you get different signals :(

i see the singals change during the last hour candel many times when there is high volatility.

so i think this will never working good.

i need to rethink everything.

backtest2


@erikvb