inport an indicator

Created at 15 Jun 2018, 02:16
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!
CA

carlosdrcunha

Joined 23.04.2018

inport an indicator
15 Jun 2018, 02:16


hello,

 

i made an indicator, how can i import it to make an cBot?

 

 

Thank you.

 

Carlos Cunha


@carlosdrcunha
Replies

PanagiotisCharalampous
15 Jun 2018, 09:35

Hi Carlos,

See below how to reference an indicator in a cBot

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 SamplecBotReferenceSMA : Robot
    {
        [Parameter("Source")]
        public DataSeries Source { get; set; }

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

        private SampleSMA sma;

        protected override void OnStart()
        {
            sma = Indicators.GetIndicator<SampleSMA>(Source, SmaPeriod);
        }

        protected override void OnTick()
        {
            Print("{0}", sma.Result.LastValue);
        }
    }
}

Best Regards,

Panagiotis


@PanagiotisCharalampous

carlosdrcunha
15 Jun 2018, 16:47

Hello,

 

in first i want to Thank you for ur help.

 

 

lets look at the 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 WedgeVolume : Robot
    {
        [Parameter("Source")]
        public DataSeries Source { get; set; }

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

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


        private WedgeVolume WV;

        protected override void OnStart()
        {

            WV = Indicators.GetIndicator<WedgeVolume>(Source, Period);




            // Put your initialization logic here
        }

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

        protected override void OnStop()
        {
            // Put your deinitialization logic here
        }
    }
}

 

 

 

 

 

 

it gives me an error, wht do i need to change?

 

 

 

Thank you,

Sincerly,

 

Carlos Cunha.


@carlosdrcunha

PanagiotisCharalampous
15 Jun 2018, 16:54

Hi Carlos,

WedgeVolume is not an indicator, it is the robot itself. What are you trying to do with the above code?

Best Regards,

Panagiotis


@PanagiotisCharalampous

carlosdrcunha
15 Jun 2018, 16:57

wedgevolume

WedgeVolume is an indicator i created, and i want to do an cBot with it.


@carlosdrcunha

PanagiotisCharalampous
15 Jun 2018, 17:01

Hi Carlos,

Please send me the indicator if possible and rename the cBot to something else. Also make sure that you reference the custom indicator.

Best Regards,

Panagiotis


@PanagiotisCharalampous

carlosdrcunha
15 Jun 2018, 18:38

RE:

hi again,

 

 

there is no WedgeVolume custom indicator, because Wedge is my name.

 

i just created this indicator, in the indicators part, now i just want to import it to use in a cBot,

 

 

 

Thank you,

Sincerly,

Carlos Cunha.

 

 

 

 

 

 

 

Panagiotis Charalampous said:

Hi Carlos,

Please send me the indicator if possible and rename the cBot to something else. Also make sure that you reference the custom indicator.

Best Regards,

Panagiotis

 


@carlosdrcunha

cwik_m
26 Jun 2018, 12:15

Hello cAlgo fans,

I just want to continue this topic since its the latest one that I have found - I am having the same problems as other traders with importing a custom indicator.

I have started to experiment with using custom indicators from ctdn.com and I have been doing exactly according to this link https://ctdn.com/api/guides/indicators#el8

Apparently my cAlgo cannot Build successfully ANY robot with ANY custom indicator. Somehow maybe problem is in the code 'GetIndicator'.

I have searched it thoroughly and also checked on other cBots - for example the one RsiAtrII  https://ctdn.com/algos/cbots/show/551  which is based on a custom indicator PipsAtr https://ctdn.com/algos/indicators/show/549.

Now If I add the robot and indicator to my cTrader and try to 'Build' the robot, it cannot be built because cannot find any PipsAtr indicator... I mean it cannot 'Get' it onto the cBot script. I am sure it was well tested by the author so the problem is not in the code... Its the same with other custom indicators.

Recently there has been and update of cTrader which merged cAlgo together with cTrader into one tool. Maybe this caused some bug while invoking custom indicators..? Pls note I have done everything according to the instruction for a proper indicator reference as it says here - ctdn.com/api/guides/indicators#el8

Anyone found a reason for that... ?


@cwik_m

PanagiotisCharalampous
26 Jun 2018, 12:28

Hi cwik_m,

Thanks for posting in our forum. What is the error you receive? Can you please post a screenshot?

Best Regards,

Panagiotis


@PanagiotisCharalampous

cwik_m
26 Jun 2018, 14:58 ( Updated at: 21 Dec 2023, 09:20 )

RE:

Panagiotis Charalampous said:

Hi cwik_m,

Thanks for posting in our forum. What is the error you receive? Can you please post a screenshot?

Best Regards,

Panagiotis

Dear Mr Panagiotis,

Thank you for a quick reply. I have tried to include the Hull MA (https://ctdn.com/algos/indicators/show/144) to the Sample Trend cBot. Problem is the cBot cannot build itself because the HMA indicator does not have the 'Result' definition (pls forgive me the error is not in english, although I have set the cTrader language to ENG...)

 

Below is the cBot full code for your reference

//#reference: C:\Users\HP\Documents\cAlgo\Sources\Indicators\HMA.algo

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 SampleTrend : 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("Fast Periods", DefaultValue = 5)]
        public int FastPeriods { get; set; }

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

        private MovingAverage slowMa;
        private HMA fastMa;
        private const string label = "Sample Trend";

        protected override void OnStart()
        {
            fastMa = Indicators.GetIndicator<HMA>(21);
            slowMa = Indicators.MovingAverage(SourceSeries, SlowPeriods, MAType);
        }

        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);

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

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

 


@cwik_m

PanagiotisCharalampous
26 Jun 2018, 15:32

Hi cwik_m,

You are using an obsolete method of referencing indicators. After correcting it, cBot seems to be building fine. See below my version


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 SampleTrend : 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("Fast Periods", DefaultValue = 5)]
        public int FastPeriods { get; set; }

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

        private MovingAverage slowMa;
        private HullMovingAverage fastMa;
        private const string label = "Sample Trend";

        protected override void OnStart()
        {
            fastMa = Indicators.GetIndicator<HullMovingAverage>(21);
            slowMa = Indicators.MovingAverage(SourceSeries, SlowPeriods, MAType);
        }

        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);

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

        private double VolumeInUnits
        {
            get { return Symbol.QuantityToVolumeInUnits(Quantity); }
        }
    }
}

Best Regards,

Panagiotis


@PanagiotisCharalampous

cwik_m
26 Jun 2018, 16:40

Thank you Mr Panagiotis,

I am surprised it worked now (according to your version of the code and the HullMovingAverage https://ctdn.com/algos/indicators/show/144). I am very thankful for such quick help.

Unfortunatelly while doing the backtesting - this cBot code you posted makes no trades. Seems that it cannot retrieve the Hull MA values from the custom indicator into the cBot.

Could you advise please how code the cBot to get access to these list of values or dataseries (like from any Moving Average or Indicator - I mean these references https://ctdn.com/api/reference/dataseries and https://ctdn.com/api/reference/functions) ?


@cwik_m

PanagiotisCharalampous
26 Jun 2018, 16:45

Hi cwik_m.

The reason is that the cBot seems to crash due to a wrong number of parameters when initializing the indicator. Change your initialization code to the following

 fastMa = Indicators.GetIndicator<HullMovingAverage>(MarketSeries.Close, 21);

Best Regards,

Panagiotis


@PanagiotisCharalampous

cwik_m
26 Jun 2018, 17:28

RE:

Panagiotis Charalampous said:

Hi cwik_m.

The reason is that the cBot seems to crash due to a wrong number of parameters when initializing the indicator. Change your initialization code to the following

 fastMa = Indicators.GetIndicator<HullMovingAverage>(MarketSeries.Close, 21);

Best Regards,

Panagiotis

Thank you Mr Panagiotis! It seems to work properly now!


@cwik_m

jani
28 Oct 2019, 14:20

RE:

Panagiotis Charalampous said:

Hi cwik_m.

The reason is that the cBot seems to crash due to a wrong number of parameters when initializing the indicator. Change your initialization code to the following

 fastMa = Indicators.GetIndicator<HullMovingAverage>(MarketSeries.Close, 21);

Best Regards,

Panagiotis

Thank you for the explanation. Some custom indicators may have a lot of parameters (sometimes most of them are not needed), do we always have to call all of them? Is there any way to define how many parameters are called? I remember MQL doing a pretty good job regarding this issue...


@jani

PanagiotisCharalampous
29 Oct 2019, 09:14

Hi Jani,

Yes you always need to pass all the parameters to a custom indicator.

Best Regards,

Panagiotis


@PanagiotisCharalampous