How to reference custom indicator in Visual Studio

Created at 28 Jan 2020, 04:06
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!
HU

hung

Joined 28.01.2020

How to reference custom indicator in Visual Studio
28 Jan 2020, 04:06


I followed the tutorial in the below page to reference the custom Indicator. I could do that within the cTrader editor but not in Visual Studio. Take a look at the screenshot for more info.

https://help.ctrader.com/ctrader-automate/indicator_code_samples#custom-indicators

Pls help. Thanks

 


@hung
Replies

ClickAlgo
28 Jan 2020, 13:14

You added a reference to the indicator, that's fine, but you did not include the scope of use into your class using the using statement.

Just hover your mouse under the red squiggly line and you will see a yellow light bulb, click on this and an option to auto-include the statement will show, or you can just do it manually, use the namespace of the indicator.


@ClickAlgo

hung
28 Jan 2020, 13:52 ( Updated at: 21 Dec 2023, 09:21 )

RE:

ClickAlgo said:

You added a reference to the indicator, that's fine, but you did not include the scope of use into your class using the using statement.

Just hover your mouse under the red squiggly line and you will see a yellow light bulb, click on this and an option to auto-include the statement will show, or you can just do it manually, use the namespace of the indicator.

Here's what I've got. Not sure which option should i use


@hung

ClickAlgo
28 Jan 2020, 16:03

Can you post the class code showing its name, it can be two things a corrupt class or the name of the class you are attempting to define is incorrectly spelt.

Without seeing the complete code it is difficult to diagnose.


@ClickAlgo

hung
28 Jan 2020, 19:41

RE:

ClickAlgo said:

Can you post the class code showing its name, it can be two things a corrupt class or the name of the class you are attempting to define is incorrectly spelt.

Without seeing the complete code it is difficult to diagnose.

I posted here source code of 2 files. I created the first one. The other one was the ctrader built-in sample. Mine was built successfully in cTrader but not in Visual Studio.

1) tutorial demarker.cs

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

namespace cAlgo
{
    [Indicator("deMarker", IsOverlay = false, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class tutorialdemarker : Indicator
    {
        [Parameter(DefaultValue = 14)]
        public int Periods { get; set; }

        [Output("DMark", LineColor = "Turquoise")]
        public IndicatorDataSeries DMark { get; set; }

        [Output("DMark compare", LineColor = "Red")]
        public IndicatorDataSeries DMark2 { get; set; }

        private IndicatorDataSeries deMin;
        private IndicatorDataSeries deMax;
        private MovingAverage deMinMA;
        private MovingAverage deMaxMA;

        private SampleDeMarker sde;


        protected override void Initialize()
        {
            // Initialize and create nested indicators
            deMin = CreateDataSeries();
            deMax = CreateDataSeries();
            deMinMA = Indicators.MovingAverage(deMin, Periods, MovingAverageType.Simple);
            deMaxMA = Indicators.MovingAverage(deMax, Periods, MovingAverageType.Simple);
            sde = Indicators.GetIndicator<SampleDeMarker>(Periods);

        }

        public override void Calculate(int index)
        {
            // Calculate value at specified index
            // Result[index] = ...
            deMin[index] = Math.Max(MarketSeries.Low[index - 1] - MarketSeries.Low[index], 0);
            deMax[index] = Math.Max(MarketSeries.High[index] - MarketSeries.High[index - 1], 0);
            var min = deMinMA.Result[index];
            var max = deMaxMA.Result[index];

            DMark[index] = min + max == 0 ? 0 : max / (min + max);
            DMark2[index] = sde.DMark[index];
            if (sde.DMark[index] != DMark[index])
            {
                Print("different at index {0}, {1}, {2}", index, sde.DMark[index], DMark[index]);

            }
        }
    }
}

 

2) Sample Demarker (This is the ctrader built-in sample )

// -------------------------------------------------------------------------------------------------
//
//    This code is a cTrader Automate API example.
//    
//    All changes to this file might be lost on the next application update.
//    If you are going to modify this file please make a copy using the "Duplicate" command.
//
// -------------------------------------------------------------------------------------------------

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

namespace cAlgo
{
    [Indicator(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class SampleDeMarker : Indicator
    {
        [Parameter(DefaultValue = 14)]
        public int Periods { get; set; }

        [Output("DMark", LineColor = "Turquoise")]
        public IndicatorDataSeries DMark { get; set; }

        private IndicatorDataSeries deMin;
        private IndicatorDataSeries deMax;
        private MovingAverage deMinMA;
        private MovingAverage deMaxMA;

        protected override void Initialize()
        {
            deMin = CreateDataSeries();
            deMax = CreateDataSeries();
            deMinMA = Indicators.MovingAverage(deMin, Periods, MovingAverageType.Simple);
            deMaxMA = Indicators.MovingAverage(deMax, Periods, MovingAverageType.Simple);
        }

        public override void Calculate(int index)
        {
            deMin[index] = Math.Max(MarketSeries.Low[index - 1] - MarketSeries.Low[index], 0);
            deMax[index] = Math.Max(MarketSeries.High[index] - MarketSeries.High[index - 1], 0);

            var min = deMinMA.Result[index];
            var max = deMaxMA.Result[index];

            DMark[index] = max / (min + max);
        }
    }
}

 


@hung

ClickAlgo
28 Jan 2020, 21:27

I can see you have created an indicator template and used this as a cBot, this is your problem, start again and create a blank cBot and then add your code for automated trading, pretty much all the code in the cBot is the logic for an indicator.

If you need our help further just drop us an email at development@clickalgo.com, please note our services are fee-based.


@ClickAlgo

hung
28 Jan 2020, 22:05

RE:

ClickAlgo said:

I can see you have created an indicator template and used this as a cBot, this is your problem, start again and create a blank cBot and then add your code for automated trading, pretty much all the code in the cBot is the logic for an indicator.

If you need our help further just drop us an email at development@clickalgo.com, please note our services are fee-based.

Hi, I used this as an indicator, not a cBot. 


@hung

ClickAlgo
29 Jan 2020, 08:57 ( Updated at: 21 Dec 2023, 09:21 )

My mistake, sorry, I see you are attempting nested indicators, so one indicator calling another.

I created both the indicators in cTrader editor and they both built ok, I then opened the tutorialdemarker in Visual Studio and the SampleDemarker indicator has missing references, which is odd, this is your problem.

I closed VS and returned to CT and opened the SampleDeMarker indicator in cTrader on its own without the tutorialdemarker, the references were corrected, so I built the project and closed VS.

Again, from CT open the tutorialdemarker with VS and you should see that everything is ok.

This is strange behaviour that is not normal.


@ClickAlgo

hung
29 Jan 2020, 13:26 ( Updated at: 21 Dec 2023, 09:21 )

RE:

ClickAlgo said:

My mistake, sorry, I see you are attempting nested indicators, so one indicator calling another.

I created both the indicators in cTrader editor and they both built ok, I then opened the tutorialdemarker in Visual Studio and the SampleDemarker indicator has missing references, which is odd, this is your problem.

I closed VS and returned to CT and opened the SampleDeMarker indicator in cTrader on its own without the tutorialdemarker, the references were corrected, so I built the project and closed VS.

Again, from CT open the tutorialdemarker with VS and you should see that everything is ok.

This is strange behaviour that is not normal.

Thank you for pointing out the problem of missing references in Sample Demarker. I got the problem solved by right clicking on the missing references and choose Properties. It's weird. 

 


@hung

dave.anderson.consulting
02 Apr 2022, 12:14

I have the exact same problem. In Visual Studio I get the same error.

Has anyone figured out what could be going on here?


@dave.anderson.consulting

hung
03 Apr 2022, 21:33

RE:

dave.anderson.consulting said:

I have the exact same problem. In Visual Studio I get the same error.

Has anyone figured out what could be going on here?

See my comment and the video posted earlier

I got the problem solved by right clicking on the missing references and choose Properties.


@hung