Fractals custom indicator output to cbot

Created at 14 Jun 2019, 14:59
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!
ZE

zedodia

Joined 30.05.2018

Fractals custom indicator output to cbot
14 Jun 2019, 14:59


First up - I wish Ctrader had a solid Fractals Indicator (maybe in the next version :))

Im using the simple fractals indicator shared in the indicators section. As below

=============================================================

using cAlgo.API;
namespace cAlgo.Indicators
{
    [Indicator(IsOverlay = true, AccessRights = AccessRights.None)]
    public class Fractals : Indicator
    {
        [Parameter(DefaultValue = 5, MinValue = 5)]
        public int Period { get; set; }

        [Output("Up Fractal", Color = Colors.Red, PlotType = PlotType.Points, Thickness = 5)]
        public IndicatorDataSeries UpFractal { get; set; }

        [Output("Down Fractal", Color = Colors.Blue, PlotType = PlotType.Points, Thickness = 5)]
        public IndicatorDataSeries DownFractal { get; set; }

        public override void Calculate(int index)
        {
            if (index < Period)
                return;
            DrawUpFractal(index);
            DrawDownFractal(index);
        }

        private void DrawUpFractal(int index)
        {
            int period = Period % 2 == 0 ? Period - 1 : Period;
            int middleIndex = index - period / 2;
            double middleValue = MarketSeries.High[middleIndex];

            bool up = true;

            for (int i = 0; i < period; i++)
            {
                if (middleValue < MarketSeries.High[index - i])
                {
                    up = false;
                    break;
                }
            }
            if (up)
                UpFractal[middleIndex] = middleValue;
        }
        private void DrawDownFractal(int index)
        {
            int period = Period % 2 == 0 ? Period - 1 : Period;
            int middleIndex = index - period / 2;
            double middleValue = MarketSeries.Low[middleIndex];
            bool down = true;

            for (int i = 0; i < period; i++)
            {
                if (middleValue > MarketSeries.Low[index - i])
                {
                    down = false;
                    break;
                }
            }
            if (down)
                DownFractal[middleIndex] = middleValue;
        }
    }
}

 

Can sombody give me an example of how to pass the outputs to a cBot please.

This is a simple bot to just place a market order OnBar with sl at previous low fractal and tp at previous high fractal. But I cant figure out how to pass the inputs correctly. Am i on the right track?

==========================================================

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 NewcBot : Robot
    {
        [Parameter("Fractal Period", DefaultValue = 5)]
        public double _period { get; set; }

        private Fractals _fractal;

        protected override void OnStart()
        {
            _fractal = Indicators.GetIndicator<Fractals>(_period);
        }

        protected override void OnBar()
        {
var _fractaldown = _fractal.UpFractal.('what goes here')
var _fractalup = _fractal.DownFractal.('what goes here')

            ExecuteMarketOrder(TradeType.Buy, Symbol, 1000, null, _fractaldown, _fractalup);
        }
    }
}

 

 

Any help is greatly appreciated.

 

Thankyou


@zedodia
Replies

PanagiotisCharalampous
14 Jun 2019, 15:06

Hi zedodia,

See an example below

            var _fractaldown = _fractal.UpFractal.LastValue;
            var _fractalup = _fractal.DownFractal.LastValue;

However these values will not work as inputs for SL and TP. Try the below instead

            var position = ExecuteMarketOrder(TradeType.Buy, Symbol, 1000).Position;
            position.ModifyTakeProfitPrice(_fractalup);
            position.ModifyTakeProfitPrice(_fractaldown);

Best Regards,

Panagiotis


@PanagiotisCharalampous

zedodia
14 Jun 2019, 16:29

Thanks again. Youve opened a can of worms for me now lol. Im unsure why you make the market order a variable and how to use it that way.

This is what i have now - except it crashes.

=================================

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 NewcBot : Robot
    {
        [Parameter("Fractal Period", DefaultValue = 5)]
        public int _period { get; set; }

        private Fractals _fractal;

        protected override void OnStart()
        {
            _fractal = Indicators.GetIndicator<Fractals>(_period);
        }

        protected override void OnBar()
        {
            var _fractaldn = _fractal.UpFractal.LastValue;
            var _fractalup = _fractal.DownFractal.LastValue;
            var _lPos = Positions.Find(null, Symbol, TradeType.Buy);

            if (_lPos == null)
            {
                ExecuteMarketOrder(TradeType.Buy, Symbol, 1000, null);
                _lPos.ModifyStopLossPrice(_fractaldn);
            }
        }
    }
}


@zedodia

PanagiotisCharalampous
14 Jun 2019, 16:33

Hi zedodia,

I have given you the code above, not sure why you changed it. Here is the complete cBot

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 NewcBot : Robot
    {
        [Parameter("Fractal Period", DefaultValue = 5)]
        public double _period { get; set; }

        private Fractals _fractal;

        protected override void OnStart()
        {
            _fractal = Indicators.GetIndicator<Fractals>(_period);
        }

        protected override void OnBar()
        {
            var _fractaldown = _fractal.UpFractal.LastValue;
            var _fractalup = _fractal.DownFractal.LastValue;

            var position = ExecuteMarketOrder(TradeType.Buy, Symbol, 1000).Position;
            position.ModifyTakeProfitPrice(_fractalup);
            position.ModifyStopLossPrice(_fractaldown);
        }
    }
}

 


@PanagiotisCharalampous

zedodia
15 Jun 2019, 00:37

The code you wrote doesnt alter the SL or TP to the previous fractal positon.It just opened a position and immedianetly closes it at the open price.


@zedodia

PanagiotisCharalampous
18 Jun 2019, 12:18

Hi zedodia,

I accidentally used the opposite values. See below the correct piece of 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 NewcBot : Robot
    {
        [Parameter("Fractal Period", DefaultValue = 5)]
        public int _period { get; set; }

        private Fractals _fractal;

        protected override void OnStart()
        {
            _fractal = Indicators.GetIndicator<Fractals>(_period);
        }

        protected override void OnBar()
        {
            var _fractaldown = Math.Round(_fractal.UpFractal.LastValue, Symbol.Digits);
            var _fractalup = Math.Round(_fractal.DownFractal.LastValue, Symbol.Digits);

            var position = ExecuteMarketOrder(TradeType.Buy, Symbol, 1000).Position;
            position.ModifyTakeProfitPrice(_fractaldown);
            position.ModifyStopLossPrice(_fractalup);
        }
    }
}

Best Regards,

Panagiotis


@PanagiotisCharalampous