Fractals cBot

Created at 11 Sep 2021, 14: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!
CT

cTraderX

Joined 03.06.2020

Fractals cBot
11 Sep 2021, 14:30


Hi,

I am trying to build a fractals cbot, which executes orders when fractals appear in the previous candle, but it does not work. It gives the previous fractal instead of the current fractal.

 

___________________

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; }

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

        private Fractals _fractal;

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

        protected override void OnBar()
        {

            if (Bars.HighPrices.Last(1) == _fractal.UpFractal.Last(0))
            {

                foreach (var position in Positions)
                {
                    if (position.SymbolName == SymbolName && position.Label == "Fractal")
                        ClosePosition(position);
                }
                ExecuteMarketOrder(TradeType.Sell, SymbolName, FirstVolume, "Fractal", null, null, null, false);
            }

            if (Bars.LowPrices.Last(1) == _fractal.DownFractal.Last(0))
            {
                foreach (var position in Positions)
                {
                    if (position.SymbolName == SymbolName && position.Label == "Fractal")
                        ClosePosition(position);
                }
                ExecuteMarketOrder(TradeType.Buy, SymbolName, FirstVolume, "Fractal", null, null, null, false);
            }

        }
    }
}


@cTraderX
Replies

amusleh
13 Sep 2021, 11:10

Hi,

Try this:

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; }

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

        private Fractals _fractal;

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

        protected override void OnBar()
        {
            if (double.IsNaN(_fractal.UpFractal.Last(1)) == false)
            {
                foreach (var position in Positions)
                {
                    if (position.SymbolName == SymbolName && position.Label == "Fractal")
                        ClosePosition(position);
                }
                ExecuteMarketOrder(TradeType.Sell, SymbolName, FirstVolume, "Fractal", null, null, null, false);
            }

            if (double.IsNaN(_fractal.DownFractal.Last(1)) == false)
            {
                foreach (var position in Positions)
                {
                    if (position.SymbolName == SymbolName && position.Label == "Fractal")
                        ClosePosition(position);
                }
                ExecuteMarketOrder(TradeType.Buy, SymbolName, FirstVolume, "Fractal", null, null, null, false);
            }
        }
    }
}

 


@amusleh

cTraderX
13 Sep 2021, 13:23 ( Updated at: 21 Dec 2023, 09:22 )

Thank you for replying. However, it does not work, and it gives the following result. 

 


@cTraderX

PanagiotisCharalampous
14 Sep 2021, 08:43

Hi cTraderX,

Please note that Fractals indicator is a redrawing indicator. Fractals do not appear immediately but only some candles after. So you cannot expect trades to be placed on the bar immediately after the fractals.

Best Regards,

Panagiotis 

Join us on Telegram and Facebook


@PanagiotisCharalampous

jaydcrowe1989
10 Sep 2022, 13:24

RE:

PanagiotisCharalampous said:

Hi cTraderX,

Please note that Fractals indicator is a redrawing indicator. Fractals do not appear immediately but only some candles after. So you cannot expect trades to be placed on the bar immediately after the fractals.

Best Regards,

Panagiotis 

Join us on Telegram and Facebook

How do you determine which arrow was drawn last? I am trying to use the fractals indicator within a bot and I need the latest arrow that was drawn? I seem to be able to get the last up arrow and the last arrow but I have no way of determining which arrow was drawn last or a way of matching an arrow to a bar


@jaydcrowe1989