how to Reference custom indicator & multi timeframe

Created at 05 Mar 2019, 19:28
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!
PS

Psak

Joined 05.03.2019

how to Reference custom indicator & multi timeframe
05 Mar 2019, 19:28


I just start C#

but the code have error

pls help me , thank you

 

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

namespace cAlgo.Indicators
{
    [Indicator(IsOverlay = true, TimeZone = TimeZones.UTC)]
    public class MultiTF : Indicator
    {
        [Parameter(DefaultValue = 50)]
        public int Period { get; set; }

        [Output("MA", Color = Colors.Yellow)]
        public IndicatorDataSeries MA { get; set; }

        [Output("MA5", Color = Colors.Orange)]
        public IndicatorDataSeries MA5 { get; set; }

        [Output("MA10", Color = Colors.Red)]
        public IndicatorDataSeries MA10 { get; set; }

        private MarketSeries series5;
        private MarketSeries series10;

        private MovingAverage ma;
        private MovingAverage ma5;
        private MovingAverage ma10;

        private DX dxx;

        protected override void Initialize()
        {
            series5 = MarketData.GetSeries(TimeFrame.Hour);
            series10 = MarketData.GetSeries(TimeFrame.Hour4);

            ma = dxx.A(MarketSeries.Close, 27, MovingAverageType.Exponential);
            ma5 = dxx.A(series5.Close, 27, MovingAverageType.Exponential);
            ma10 = dxx.A(series10.Close, 27, MovingAverageType.Exponential);

            dxx = Indicators.GetIndicator<DX>(27, MovingAverageType.Exponential);
        }

        public override void Calculate(int index)
        {
            MA[index] = ma.Result[index];

            var index5 = series5.OpenTime.GetIndexByTime(MarketSeries.OpenTime[index]);
            if (index5 != -1)
                MA5[index] = ma5.Result[index5];

            var index10 = series10.OpenTime.GetIndexByTime(MarketSeries.OpenTime[index]);

            if (index10 != -1)
                MA10[index] = ma10.Result[index10];
        }
    }
}

 


@Psak
Replies

Psak
05 Mar 2019, 19:31

error show in row 36-38

            ma = dxx.A(MarketSeries.Close, 27, MovingAverageType.Exponential);

            ma5 = dxx.A(series5.Close, 27, MovingAverageType.Exponential);

            ma10 = dxx.A(series10.Close, 27, MovingAverageType.Exponential);

show

Error CS1955: Non-invocable member 'cAlgo.Indicators.DX.A' cannot be used like a method.

 

 


@Psak

PanagiotisCharalampous
06 Mar 2019, 10:02

Hi Psak,

Thanks for posting in our forum. What is DX? A custom indicator? Can you please provide it as well?

Best Regards,

Panagiotis


@PanagiotisCharalampous

Psak
06 Mar 2019, 10:43

RE:

Panagiotis Charalampous said:

Hi Psak,

Thanks for posting in our forum. What is DX? A custom indicator? Can you please provide it as well?

Best Regards,

Panagiotis

Yes DX is custom indicator

the indicator is ok

 

but i need build it for multi TF

 

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

namespace cAlgo.Indicators
{
    [Indicator("DMS", IsOverlay = false, ScalePrecision = 0, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class DX : Indicator
    {
        [Parameter("Period", DefaultValue = 14, MinValue = 1)]
        public int Period { get; set; }

        [Parameter("MA Type", DefaultValue = MovingAverageType.Exponential)]
        public MovingAverageType MaType { get; set; }

        [Output("ADS", Color = Colors.Blue)]
        public IndicatorDataSeries A { get; set; }

        [Output("EADS", Color = Colors.Cyan)]
        public IndicatorDataSeries EMAA { get; set; }

 


@Psak

PanagiotisCharalampous
06 Mar 2019, 10:52

Hi Psak,

You did not provide the complete indicator code. Also, this part of the code does not make sense.

            ma = dxx.A(MarketSeries.Close, 27, MovingAverageType.Exponential);
            ma5 = dxx.A(series5.Close, 27, MovingAverageType.Exponential);
            ma10 = dxx.A(series10.Close, 27, MovingAverageType.Exponential);

Would you like to explain to what are you trying to do here?

Best Regards,

Panagiotis


@PanagiotisCharalampous

Psak
06 Mar 2019, 15:07

RE:

Panagiotis Charalampous said:

Hi Psak,

You did not provide the complete indicator code. Also, this part of the code does not make sense.

            ma = dxx.A(MarketSeries.Close, 27, MovingAverageType.Exponential);
            ma5 = dxx.A(series5.Close, 27, MovingAverageType.Exponential);
            ma10 = dxx.A(series10.Close, 27, MovingAverageType.Exponential);

Would you like to explain to what are you trying to do here?

Best Regards,

Panagiotis

sorry panagiotis

I think my custom indicators have many bugs

this is complete code

base on DirectionalMovementSystem

 

plese help me

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

namespace cAlgo.Indicators
{
    [Indicator("DMS", IsOverlay = false, ScalePrecision = 0, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class DX : Indicator
    {
        [Parameter("Period", DefaultValue = 14, MinValue = 1)]
        public int Period { get; set; }

        [Parameter("MA Type", DefaultValue = MovingAverageType.Exponential)]
        public MovingAverageType MaType { get; set; }

        [Output("ADS", Color = Colors.Blue)]
        public IndicatorDataSeries A { get; set; }

        [Output("EADS", Color = Colors.Cyan)]
        public IndicatorDataSeries EMAA { get; set; }
        //


        //[Output("ADXR", Color = Colors.Yellow)]
        public IndicatorDataSeries ADXR { get; set; }
        //
        [Output("DI+", Color = Colors.Green)]
        public IndicatorDataSeries DiPlus { get; set; }

        [Output("Di-", Color = Colors.Red)]
        public IndicatorDataSeries DiMinus { get; set; }

        private DataSeries high, low, close;
        private IndicatorDataSeries tr, dmPlus, dmMinus, adx, dx;
        private MovingAverage maTr, maDmPlus, maDmMinus, maAdx;

        protected override void Initialize()
        {
            high = MarketSeries.High;
            low = MarketSeries.Low;
            close = MarketSeries.Close;

            tr = CreateDataSeries();
            dmPlus = CreateDataSeries();
            dmMinus = CreateDataSeries();
            adx = CreateDataSeries();
            dx = CreateDataSeries();

            maTr = Indicators.MovingAverage(tr, Period, MaType);
            maDmPlus = Indicators.MovingAverage(dmPlus, Period, MaType);
            maDmMinus = Indicators.MovingAverage(dmMinus, Period, MaType);
            maAdx = Indicators.MovingAverage(adx, Period, MaType);

        }

        public override void Calculate(int index)
        {
            if (index == 0)
            {
                tr[0] = high[0] - low[0];
                dmPlus[0] = 0;
                dmMinus[0] = 0;
            }
            else
            {
                tr[index] = Math.Max(Math.Abs(low[index] - close[index - 1]), Math.Max(Math.Abs(high[index] - close[index - 1]), high[index] - low[index]));
                dmPlus[index] = high[index] - high[index - 1] > low[index - 1] - low[index] ? Math.Max(high[index] - high[index - 1], 0) : 0;
                dmMinus[index] = low[index - 1] - low[index] > high[index] - high[index - 1] ? Math.Max(low[index - 1] - low[index], 0) : 0;

                DiPlus[index] = 100 * (maTr.Result[index] == 0 ? 0 : maDmPlus.Result[index] / maTr.Result[index]);
                DiMinus[index] = 100 * (maTr.Result[index] == 0 ? 0 : maDmMinus.Result[index] / maTr.Result[index]);

                adx[index] = ((DiPlus[index] - DiMinus[index]) / (DiPlus[index] + DiMinus[index]));
                A[index] = ((DiPlus[index] - DiMinus[index]) / (DiPlus[index] + DiMinus[index]) * 100);
                EMAA[index] = adx[index] == 0 ? 50 : 100 * maAdx.Result[index];
            }
        }
    }
}

 


@Psak

... Deleted by UFO ...

PanagiotisCharalampous
06 Mar 2019, 15:36

Hi Psak,

Thanks but you did not answer my question. What are you trying to do in the lines Ι quoted above? Are you trying to initialize the indicator? If yes, then this is wrong. You need to use Indicators.GetIndicator<>() function. You can read more about it here, in the Referencing Custom Indicators section.

Best Regards,

Panagiotis


@PanagiotisCharalampous

Psak
06 Mar 2019, 16:00

RE:

Panagiotis Charalampous said:

Hi Psak,

Thanks but you did not answer my question. What are you trying to do in the lines Ι quoted above? Are you trying to initialize the indicator? If yes, then this is wrong. You need to use Indicators.GetIndicator<>() function. You can read more about it here, in the Referencing Custom Indicators section.

Best Regards,

Panagiotis

thank you

yes I need try Initialize same as example

Multiple timeframes , Example 1- Using multiple timeframes   https://ctrader.com/api/guides/indicators#el12

I try getindicator but it don't work with

dxx = Indicators.GetIndicator<DX>(27, MovingAverageType.Exponential);

 


@Psak

PanagiotisCharalampous
07 Mar 2019, 09:25

Hi Psak,

The problem is these lines

            ma = dxx.A(MarketSeries.Close, 27, MovingAverageType.Exponential);
            ma5 = dxx.A(series5.Close, 27, MovingAverageType.Exponential);
            ma10 = dxx.A(series10.Close, 27, MovingAverageType.Exponential);

They don't make any sense and I cannot help you if you do not explain to me what are you trying to do here. If you delete them, your indicator will build without problems.

Best Regards,

Panagiotis


@PanagiotisCharalampous

Psak
07 Mar 2019, 12:46

RE:

Panagiotis Charalampous said:

Hi Psak,

The problem is these lines

            ma = dxx.A(MarketSeries.Close, 27, MovingAverageType.Exponential);
            ma5 = dxx.A(series5.Close, 27, MovingAverageType.Exponential);
            ma10 = dxx.A(series10.Close, 27, MovingAverageType.Exponential);

They don't make any sense and I cannot help you if you do not explain to me what are you trying to do here. If you delete them, your indicator will build without problems.

Best Regards,

Panagiotis

thank panagiotis

I need to make new indicator from GetIndicator() in multi timeframe base on with this souce code

https://ctrader.com/forum/whats-new/1463

 

using System;
using cAlgo.API;
using cAlgo.API.Internals;
using cAlgo.API.Indicators;
 
namespace cAlgo.Indicators
{
    [Indicator(IsOverlay = true, TimeZone = TimeZones.UTC)]
    public class MultiTF_MA : Indicator
    {
        [Parameter(DefaultValue = 50)]
        public int Period { get; set; }
 
        [Output("MA", Color = Colors.Yellow)]
        public IndicatorDataSeries MA { get; set; }
 
        [Output("MA5", Color = Colors.Orange)]
        public IndicatorDataSeries MA5 { get; set; }
 
        [Output("MA10", Color = Colors.Red)]
        public IndicatorDataSeries MA10 { get; set; }
 
        private MarketSeries series5;
        private MarketSeries series10;
 
        private MovingAverage ma;
        private MovingAverage ma5;
        private MovingAverage ma10;
 
        protected override void Initialize()
        {
            series5 = MarketData.GetSeries(TimeFrame.Minute5);
            series10 = MarketData.GetSeries(TimeFrame.Minute10);
 
            ma = Indicators.MovingAverage(MarketSeries.Close, Period, MovingAverageType.Triangular);
            ma5 = Indicators.MovingAverage(series5.Close, Period, MovingAverageType.Triangular);
            ma10 = Indicators.MovingAverage(series10.Close, Period, MovingAverageType.Triangular);
        }
 
        public override void Calculate(int index)
        {
            MA[index] = ma.Result[index];
 
            var index5 = GetIndexByDate(series5, MarketSeries.OpenTime[index]);
            if (index5 != -1)
                MA5[index] = ma5.Result[index5];
 
            var index10 = GetIndexByDate(series10, MarketSeries.OpenTime[index]);
            if (index10 != -1)
                MA10[index] = ma10.Result[index10];
        }
 
 
        private int GetIndexByDate(MarketSeries series, DateTime time)
        {
            for (int i = series.Close.Count - 1; i > 0; i--)
            {
                if (time == series.OpenTime[i])
                    return i;
            }
            return -1;
        }
    }
}

 


@Psak

Psak
07 Mar 2019, 12:46

RE:

Panagiotis Charalampous said:

Hi Psak,

The problem is these lines

            ma = dxx.A(MarketSeries.Close, 27, MovingAverageType.Exponential);
            ma5 = dxx.A(series5.Close, 27, MovingAverageType.Exponential);
            ma10 = dxx.A(series10.Close, 27, MovingAverageType.Exponential);

They don't make any sense and I cannot help you if you do not explain to me what are you trying to do here. If you delete them, your indicator will build without problems.

Best Regards,

Panagiotis

thank panagiotis

I need to make new indicator from GetIndicator() in multi timeframe base on with this souce code

https://ctrader.com/forum/whats-new/1463

 

using System;
using cAlgo.API;
using cAlgo.API.Internals;
using cAlgo.API.Indicators;
 
namespace cAlgo.Indicators
{
    [Indicator(IsOverlay = true, TimeZone = TimeZones.UTC)]
    public class MultiTF_MA : Indicator
    {
        [Parameter(DefaultValue = 50)]
        public int Period { get; set; }
 
        [Output("MA", Color = Colors.Yellow)]
        public IndicatorDataSeries MA { get; set; }
 
        [Output("MA5", Color = Colors.Orange)]
        public IndicatorDataSeries MA5 { get; set; }
 
        [Output("MA10", Color = Colors.Red)]
        public IndicatorDataSeries MA10 { get; set; }
 
        private MarketSeries series5;
        private MarketSeries series10;
 
        private MovingAverage ma;
        private MovingAverage ma5;
        private MovingAverage ma10;
 
        protected override void Initialize()
        {
            series5 = MarketData.GetSeries(TimeFrame.Minute5);
            series10 = MarketData.GetSeries(TimeFrame.Minute10);
 
            ma = Indicators.MovingAverage(MarketSeries.Close, Period, MovingAverageType.Triangular);
            ma5 = Indicators.MovingAverage(series5.Close, Period, MovingAverageType.Triangular);
            ma10 = Indicators.MovingAverage(series10.Close, Period, MovingAverageType.Triangular);
        }
 
        public override void Calculate(int index)
        {
            MA[index] = ma.Result[index];
 
            var index5 = GetIndexByDate(series5, MarketSeries.OpenTime[index]);
            if (index5 != -1)
                MA5[index] = ma5.Result[index5];
 
            var index10 = GetIndexByDate(series10, MarketSeries.OpenTime[index]);
            if (index10 != -1)
                MA10[index] = ma10.Result[index10];
        }
 
 
        private int GetIndexByDate(MarketSeries series, DateTime time)
        {
            for (int i = series.Close.Count - 1; i > 0; i--)
            {
                if (time == series.OpenTime[i])
                    return i;
            }
            return -1;
        }
    }
}

 


@Psak