how can i convert an indicator to cAlgo ?

Created at 13 Feb 2018, 00:03
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!
WA

waisuklondon@gmail.com

Joined 12.02.2018

how can i convert an indicator to cAlgo ?
13 Feb 2018, 00:03


Hi

 

I am totally new in here ,just wondering how can I convert an indicator into cAlgo ?

 

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

namespace cAlgo.Robots
{
    (Robot IsOverlay = false, TimeZone = TimeZones.UTC+1, ScalePrecision = 5 , AccessRights = AccessRights.None)
    public class MACD : Indicator
    {
        private ExponentialMovingAverage _emaLong;
        private ExponentialMovingAverage _emaShort;
        private ExponentialMovingAverage _emaSignal;
        private ExponentialMovingAverage _emaLong2;
        private ExponentialMovingAverage _emaShort2;

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

        [Parameter(DefaultValue = 5000, MinValue = 400)]
        public int StopLoss { get; set; }

        [Parameter(DefaultValue = 5000, MinValue = 400)]
        public int TakeProfit { get; set; }


        [Parameter("MACD LongCycle", DefaultValue = 8, MinValue = 1)]
        public int LongCycle { get; set; }

        [Parameter("MACD ShortCycle", DefaultValue = 5, MinValue = 1)]
        public int ShortCycle { get; set; }

        [Parameter("MACD Period", DefaultValue = 9, MinValue = 1)]
        public int MACDPeriod { get; set; }
        
        [Output("Histogram", Color = Colors.Turquoise, PlotType = PlotType.Histogram)]
        public IndicatorDataSeries Histogram { get; set; }

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

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

        private IndicatorDataSeries _zeroLagEmaShort;
        private IndicatorDataSeries _zeroLagEmaLong;

        protected override void Onstart()
        {
            _EmaShort = CreateDataSeries();
            _EmaLong = CreateDataSeries();

            _emaLong = Indicators.ExponentialMovingAverage(MarketSeries.Close, LongCycle);
            _emaLong2 = Indicators.ExponentialMovingAverage(_emaLong.Result, LongCycle);

            _emaShort = Indicators.ExponentialMovingAverage(MarketSeries.Close, ShortCycle);
            _emaShort2 = Indicators.ExponentialMovingAverage(_emaShort.Result, ShortCycle);

            _emaSignal = Indicators.ExponentialMovingAverage(MACD, SignalPeriods);
        }

        public override void OnBar()
        {
            _EmaShort[index] = _emaShort.Result[index] * 2 - _emaShort2.Result[index];
            _EmaLong[index] = _emaLong.Result[index] * 2 - _emaLong2.Result[index];

            MACD[index] = _EmaShort[index] - _EmaLong[index];

            Signal[index] = _emaSignal.Result[index];
            Histogram[index] = MACD[index] - Signal[index];
        }
    }
}

 

Thank you so much


@waisuklondon@gmail.com
Replies

PanagiotisCharalampous
13 Feb 2018, 09:33

Hi waisuklondon@gmail.com,

Thanks for posting in our forum. What do you mean to convert an indicator to a cBot? Do you mean to access the indicator through a cBot, read its values and trade based on them?

Best Regards,

Panagiotis


@PanagiotisCharalampous

waisuklondon@gmail.com
13 Feb 2018, 12:35

hi Panagiotis

 

Thank you for your swift reply . Yeah precisely  i would like to access the indicator through Cbot.  It should take trade according to MACD crossover. 

I really appreciate if you could help me how  convert it to Cbot .

 

Warm regards, 

 

Elias


@waisuklondon@gmail.com

PanagiotisCharalampous
13 Feb 2018, 12:51

Hi Elias,

It is not very clear what you are trying to do since the code you have posted seems to be a mixup of a cBot and an Indicator. If you want to call the MACD Crossover indicator in a cBot, see a simple example below.

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 NewcBot : Robot
    {
        [Parameter(DefaultValue = 0.0)]
        public double Parameter { get; set; }
        private MacdCrossOver _macdCrossOver;
        
        protected override void OnStart()
        {
            _macdCrossOver = Indicators.MacdCrossOver(28,12,9);
        }

        protected override void OnTick()
        {
            
        }

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

Let me know if this is what you are looking for.

Best Regards,

Panagiotis


@PanagiotisCharalampous

waisuklondon@gmail.com
13 Feb 2018, 13:04

RE:

Panagiotis Charalampous said:

Hi Elias,

It is not very clear what you are trying to do since the code you have posted seems to be a mixup of a cBot and an Indicator. If you want to call the MACD Crossover indicator in a cBot, see a simple example below.

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 NewcBot : Robot
    {
        [Parameter(DefaultValue = 0.0)]
        public double Parameter { get; set; }
        private MacdCrossOver _macdCrossOver;
        
        protected override void OnStart()
        {
            _macdCrossOver = Indicators.MacdCrossOver(28,12,9);
        }

        protected override void OnTick()
        {
            
        }

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

Let me know if this is what you are looking for.

Best Regards,

Panagiotis

Hi Panagiotis 

 

Thank you for your quick reply Yeah that s what i am looking for  and it should be including the EMA ,however it should take the trade once MACD crosses ( not the first candle , it should take on second candle.) by the way it should be on different time frame as well . 

 

Warm regards, 

 

Elias


@waisuklondon@gmail.com

PanagiotisCharalampous
13 Feb 2018, 15:52

Hi Elias,

You can develop your cBot based on the example above. If you need professional help developing your cBot you can contact a Consultant or post a Job.

Best Regards,

Panagiotis


@PanagiotisCharalampous