turn indicator into a robot

Created at 04 May 2022, 10:30
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!
HA

hamirady60

Joined 25.04.2022

turn indicator into a robot
04 May 2022, 10:30


hi

I want to this indicator into a robot with a slight difference. We have two green and red lines here. I want the sell position to open whenever the red line closes one candle lower than the previous red line.
And whenever the green line goes one candle higher than the previous green line, the buy position will open

In fact, I want to get a breakout from the Ranko chart
And I think it is good to use this indicator
Thank you very much for your help

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

namespace cAlgo
{
    [Indicator(IsOverlay = true, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class TrendZonePrice : Indicator
    {
        ////////////////////////////////////////////////////////
        ////////////////////////////////////////////////////////
        ////////////////  Variables ...........................!
        ////////////////////////////////////////////////////////


        [Parameter("Show High,Low", DefaultValue = true)]
        public bool showHL { get; set; }


        private IndicatorDataSeries _haOpen;
        private IndicatorDataSeries _haClose;
        private IndicatorDataSeries _haHigh;
        private IndicatorDataSeries _haLow;






        ////////////////////////////////////////////////////////////////////////
        ////////// initializer ................................................!
        ////////////////////////////////////////////////////////////////////////

        protected override void Initialize()
        {
            _haOpen = CreateDataSeries();
            _haClose = CreateDataSeries();
            _haHigh = CreateDataSeries();
            _haLow = CreateDataSeries();
        }




        /////////////////////////////////////////////////////////////////////////
        /////// Calculation ....................................................!
        /////////////////////////////////////////////////////////////////////////


        public override void Calculate(int index)
        {
            var open = Bars.OpenPrices[index];
            var high = Bars.HighPrices[index];
            var low = Bars.LowPrices[index];
            var close = Bars.ClosePrices[index];
            var time = Bars.OpenTimes[index];

            var haClose = (open + high + low + close) / 4;
            var haOpen = (index > 0) ? (_haOpen[index - 1] + _haClose[index - 1]) / 2 : (open + close) / 2;
            var haHigh = Math.Max(Math.Max(high, haOpen), haClose);
            var haLow = Math.Min(Math.Min(low, haOpen), haClose);

            _haOpen[index] = haOpen;
            _haHigh[index] = haHigh;
            _haLow[index] = haLow;
            _haClose[index] = haClose;

            Chart.ChartType = ChartType.Line;
            var clr = haClose > haOpen ? "green" : "red";
            Chart.DrawTrendLine("line" + index, Bars.OpenTimes[index - 1], Bars.ClosePrices[index - 1], time, close, clr, 3);

            if (showHL)
            {
                Chart.DrawEllipse("high" + index, time, high, time, high, clr, 3);
                Chart.DrawEllipse("low" + index, time, low, time, low, clr, 3);

                Chart.DrawTrendLine("verticle" + index, time, high, time, low, clr, 1, LineStyle.DotsVeryRare);
                Chart.DrawTrendLine("upperline" + index, Bars.OpenTimes[index - 1], Bars.HighPrices[index - 1], time, high, clr, 1, LineStyle.DotsVeryRare);
                Chart.DrawTrendLine("lowerline" + index, Bars.OpenTimes[index - 1], Bars.LowPrices[index - 1], time, low, clr, 1, LineStyle.DotsVeryRare);
            }
        }




    }
}

 


@hamirady60
Replies

amusleh
05 May 2022, 10:24

Hi,

You have to use outputs instead of IndicatorDataSeries fields, then you will be able to use your indicator on a cBot by referencing.

Also you can put the whole indicator code on a cBot, example:

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

namespace NewcBot2
{
    [Robot(AccessRights = AccessRights.None)]
    public class NewcBot2 : Robot
    {
        [Parameter("Show High,Low", DefaultValue = true)]
        public bool showHL { get; set; }

        private IndicatorDataSeries _haOpen;
        private IndicatorDataSeries _haClose;
        private IndicatorDataSeries _haHigh;
        private IndicatorDataSeries _haLow;

        protected override void OnStart()
        {
            _haOpen = CreateDataSeries();
            _haClose = CreateDataSeries();
            _haHigh = CreateDataSeries();
            _haLow = CreateDataSeries();
        }

        protected override void OnBar()
        {
            var index = Bars.Count - 2;

            var open = Bars.OpenPrices[index];
            var high = Bars.HighPrices[index];
            var low = Bars.LowPrices[index];
            var close = Bars.ClosePrices[index];

            var haClose = (open + high + low + close) / 4;

            if (double.IsNaN(_haOpen[index - 1]))
            {
                _haOpen[index - 1] = Bars.OpenPrices[index - 1];
            }

            var haOpen = (index > 0) ? (_haOpen[index - 1] + _haClose[index - 1]) / 2 : (open + close) / 2;
            var haHigh = Math.Max(Math.Max(high, haOpen), haClose);
            var haLow = Math.Min(Math.Min(low, haOpen), haClose);

            _haOpen[index] = haOpen;
            _haHigh[index] = haHigh;
            _haLow[index] = haLow;
            _haClose[index] = haClose;

            if (index < 4) return;

            var position = Positions.Find("MyBot");

            if (_haClose[index] > _haOpen[index] && _haClose[index - 1] < _haOpen[index - 1])
            {
                if (position != null && position.TradeType == TradeType.Sell) ClosePosition(position);

                ExecuteMarketOrder(TradeType.Buy, SymbolName, Symbol.VolumeInUnitsMin, "MyBot");
            }
            else if (_haClose[index] < _haOpen[index] && _haClose[index - 1] > _haOpen[index - 1])
            {
                if (position != null && position.TradeType == TradeType.Buy) ClosePosition(position);

                ExecuteMarketOrder(TradeType.Sell, SymbolName, Symbol.VolumeInUnitsMin, "MyBot");
            }
        }
    }
}

The above cBot opens a buy position on start of green line and a sell position on start of red line, it closes the buy position on start of red line and the sell position on start of green line.

 


@amusleh

steel.export
15 May 2022, 07:18

RE:

amusleh said:

Hi,

You have to use outputs instead of IndicatorDataSeries fields, then you will be able to use your indicator on a cBot by referencing.

Also you can put the whole indicator code on a cBot, example:

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

namespace NewcBot2
{
    [Robot(AccessRights = AccessRights.None)]
    public class NewcBot2 : Robot
    {
        [Parameter("Show High,Low", DefaultValue = true)]
        public bool showHL { get; set; }

        private IndicatorDataSeries _haOpen;
        private IndicatorDataSeries _haClose;
        private IndicatorDataSeries _haHigh;
        private IndicatorDataSeries _haLow;

        protected override void OnStart()
        {
            _haOpen = CreateDataSeries();
            _haClose = CreateDataSeries();
            _haHigh = CreateDataSeries();
            _haLow = CreateDataSeries();
        }

        protected override void OnBar()
        {
            var index = Bars.Count - 2;

            var open = Bars.OpenPrices[index];
            var high = Bars.HighPrices[index];
            var low = Bars.LowPrices[index];
            var close = Bars.ClosePrices[index];

            var haClose = (open + high + low + close) / 4;

            if (double.IsNaN(_haOpen[index - 1]))
            {
                _haOpen[index - 1] = Bars.OpenPrices[index - 1];
            }

            var haOpen = (index > 0) ? (_haOpen[index - 1] + _haClose[index - 1]) / 2 : (open + close) / 2;
            var haHigh = Math.Max(Math.Max(high, haOpen), haClose);
            var haLow = Math.Min(Math.Min(low, haOpen), haClose);

            _haOpen[index] = haOpen;
            _haHigh[index] = haHigh;
            _haLow[index] = haLow;
            _haClose[index] = haClose;

            if (index < 4) return;

            var position = Positions.Find("MyBot");

            if (_haClose[index] > _haOpen[index] && _haClose[index - 1] < _haOpen[index - 1])
            {
                if (position != null && position.TradeType == TradeType.Sell) ClosePosition(position);

                ExecuteMarketOrder(TradeType.Buy, SymbolName, Symbol.VolumeInUnitsMin, "MyBot");
            }
            else if (_haClose[index] < _haOpen[index] && _haClose[index - 1] > _haOpen[index - 1])
            {
                if (position != null && position.TradeType == TradeType.Buy) ClosePosition(position);

                ExecuteMarketOrder(TradeType.Sell, SymbolName, Symbol.VolumeInUnitsMin, "MyBot");
            }
        }
    }
}

The above cBot opens a buy position on start of green line and a sell position on start of red line, it closes the buy position on start of red line and the sell position on start of green line.

 

Dear Mr. Ahmed,

Thanks for posting the above Sample cBot that can be used by Referencing an Indicator, and it opens a buy position on start of green line and a sell position on start of red line, it closes the buy position on start of red line and the sell position on start of green line.

Kindly also advice how to Add Parameters such as Volume/Lot Size,  ATR Period, and  ATR Multiplier to this cBot, if we are Referencing it with SuperTrend Indicator.

Thanks & Best Regards,

Altaf


@steel.export

amusleh
16 May 2022, 10:22

RE: RE:

steel.export said:

Dear Mr. Ahmed,

Thanks for posting the above Sample cBot that can be used by Referencing an Indicator, and it opens a buy position on start of green line and a sell position on start of red line, it closes the buy position on start of red line and the sell position on start of green line.

Kindly also advice how to Add Parameters such as Volume/Lot Size,  ATR Period, and  ATR Multiplier to this cBot, if we are Referencing it with SuperTrend Indicator.

Thanks & Best Regards,

Altaf

Hi,

I recommend you to read the cTrader automate API user guide.


@amusleh