Last three bars indicator

Created at 08 Sep 2021, 11:12
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

ctid4633759

Joined 25.08.2021

Last three bars indicator
08 Sep 2021, 11:12


Hi, I am attempting to create an indicator that will tell me if the last three bars are in the same direction. I want to display this as a chart object, but the logic regarding color changes confuses me.

using System;
using cAlgo.API;
using cAlgo.API.Internals;
using cAlgo.API.Indicators;
using cAlgo.Indicators;
using System.Text;
using System.Collections.Generic;

namespace cAlgo
{
    [Indicator(IsOverlay = true, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class ThreeStrike : Indicator
    {
        [Parameter("Vertical Alignment", Group = "Position", DefaultValue = VerticalAlignment.Top)]
        public VerticalAlignment vAlignment { get; set; }

        [Parameter("Horizontal Alignment", Group = "Position", DefaultValue = HorizontalAlignment.Right)]
        public HorizontalAlignment hAlignment { get; set; }

        private List<Box> list;
        private string lastResult;
        private bool threestrike;

        protected override void Initialize()
        {
            this.list = new List<Box>();
            list.Add(new Box("Three Strike", "     "));

            this.lastResult = "";
        }

        public override void Calculate(int index)
        {
            string sb_green = "", sb_white = "", sb_red = "";
            double price = (Symbol.Ask + Symbol.Bid) * 0.5;

            foreach (Box box in this.list)
            {
                if (Bars.ClosePrices.Last(1) > Bars.ClosePrices.Last(2) && Bars.ClosePrices.Last(2) > Bars.ClosePrices.Last(3) && Bars.ClosePrices.Last(3) > Bars.ClosePrices.Last(4))
                {
                    threestrike = true;
                }

                if (Bars.ClosePrices.Last(1) < Bars.ClosePrices.Last(2) && Bars.ClosePrices.Last(2) < Bars.ClosePrices.Last(3) && Bars.ClosePrices.Last(3) < Bars.ClosePrices.Last(4))
                {
                    threestrike = false;
                }

                if (threestrike)
                {
                    sb_green += "  " + box.label;
                    sb_red += "  " + box.empty;
                    sb_white += "  " + box.empty;
                }
                else if (!threestrike)
                {
                    sb_green += "  " + box.empty;
                    sb_red += "  " + box.label;
                    sb_white += "  " + box.empty;
                }
                else
                {
                    sb_green += "  " + box.empty;
                    sb_red += "  " + box.empty;
                    sb_white += "  " + box.label;
                }
            }

            string result = sb_green + sb_white + sb_red;
            if (!result.Equals(this.lastResult))
            {
                Chart.DrawStaticText("idtext_green", "\n" + sb_green + ".", this.vAlignment, this.hAlignment, Color.Green);
                Chart.DrawStaticText("idtext_red", "\n" + sb_red + ".", this.vAlignment, this.hAlignment, Color.Chocolate);
                Chart.DrawStaticText("idtext_white", "\n" + sb_white + ".", this.vAlignment, this.hAlignment, Color.Gray);
                this.lastResult = result;
            }
        }
    }

    public class Box
    {
        public Box(string l, string e)
        {
            this.label = l;
            this.empty = e;
        }
        public string label { get; set; }
        public string empty { get; set; }
    }
}

 

Any help would be appreciated, thanks.


@ctid4633759
Replies

amusleh
08 Sep 2021, 11:58

Hi,

Not sure what exactly you want to do with all those extra code, but if you want to show a text based on type of last three bar you can do it like this:

using cAlgo.API;
using System.Linq;

namespace cAlgo
{
    [Indicator(IsOverlay = true, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class ThreeStrike : Indicator
    {
        [Parameter("Vertical Alignment", Group = "Position", DefaultValue = VerticalAlignment.Top)]
        public VerticalAlignment vAlignment { get; set; }

        [Parameter("Horizontal Alignment", Group = "Position", DefaultValue = HorizontalAlignment.Right)]
        public HorizontalAlignment hAlignment { get; set; }

        private ChartStaticText _text;

        protected override void Initialize()
        {
            _text = Chart.DrawStaticText("idtext_red", string.Empty, this.vAlignment, this.hAlignment, Color.Chocolate);
        }

        public override void Calculate(int index)
        {
            if (index < 2) return;

            var lastThreeBars = new Bar[] { Bars.Last(0), Bars.Last(1), Bars.Last(2) };

            if (lastThreeBars.All(bar => bar.Close > bar.Open))
            {
                _text.Text = "Three Bull";
                _text.Color = Color.Green;
            }
            else if (lastThreeBars.All(bar => bar.Close < bar.Open))
            {
                _text.Text = "Three Bear";
                _text.Color = Color.Red;
            }
            else
            {
                _text.Text = "Neutral";
                _text.Color = Color.Gray;
            }
        }
    }
}

 


@amusleh

ctid4633759
09 Sep 2021, 13:22

RE:

amusleh said:

Hi,

Not sure what exactly you want to do with all those extra code, but if you want to show a text based on type of last three bar you can do it like this:

using cAlgo.API;
using System.Linq;

namespace cAlgo
{
    [Indicator(IsOverlay = true, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class ThreeStrike : Indicator
    {
        [Parameter("Vertical Alignment", Group = "Position", DefaultValue = VerticalAlignment.Top)]
        public VerticalAlignment vAlignment { get; set; }

        [Parameter("Horizontal Alignment", Group = "Position", DefaultValue = HorizontalAlignment.Right)]
        public HorizontalAlignment hAlignment { get; set; }

        private ChartStaticText _text;

        protected override void Initialize()
        {
            _text = Chart.DrawStaticText("idtext_red", string.Empty, this.vAlignment, this.hAlignment, Color.Chocolate);
        }

        public override void Calculate(int index)
        {
            if (index < 2) return;

            var lastThreeBars = new Bar[] { Bars.Last(0), Bars.Last(1), Bars.Last(2) };

            if (lastThreeBars.All(bar => bar.Close > bar.Open))
            {
                _text.Text = "Three Bull";
                _text.Color = Color.Green;
            }
            else if (lastThreeBars.All(bar => bar.Close < bar.Open))
            {
                _text.Text = "Three Bear";
                _text.Color = Color.Red;
            }
            else
            {
                _text.Text = "Neutral";
                _text.Color = Color.Gray;
            }
        }
    }
}

 

Hi amusleh, thanks for the help.

I have added another step where the next bar should be an engulfing bar, which works, but I'm having a bit of trouble creating a vertical line as an entry signal where the conditions are met.

using cAlgo.API;
using System.Linq;

namespace cAlgo
{
    [Indicator(IsOverlay = true, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class ThreeStrike : Indicator
    {
        [Parameter("Vertical Alignment", Group = "Position", DefaultValue = VerticalAlignment.Top)]
        public VerticalAlignment vAlignment { get; set; }

        [Parameter("Horizontal Alignment", Group = "Position", DefaultValue = HorizontalAlignment.Right)]
        public HorizontalAlignment hAlignment { get; set; }

        private ChartStaticText _text;

        protected override void Initialize()
        {
            _text = Chart.DrawStaticText("idtext_red", string.Empty, this.vAlignment, this.hAlignment, Color.Chocolate);
        }

        public override void Calculate(int index)
        {
            if (index < 2)
                return;

            var lastThreeBars = new Bar[] 
            {
                Bars.Last(1),
                Bars.Last(2),
                Bars.Last(3)
            };

            if (lastThreeBars.All(bar => bar.Close > bar.Open))
            {
                _text.Text = "Wait For Red Engulfing";
                _text.Color = Color.Red;
                if (Bars.ClosePrices.Last(0) < Bars.OpenPrices.Last(1))
                {
                    _text.Text = "Sell";
                    _text.Color = Color.Red;
                    Chart.DrawVerticalLine("Sell", System.DateTime.Now, Color.OrangeRed);
                }
            }
            else if (lastThreeBars.All(bar => bar.Close < bar.Open))
            {
                _text.Text = "Wait For Green Engulfing";
                _text.Color = Color.Green;

                if (Bars.ClosePrices.Last(0) > Bars.OpenPrices.Last(1))
                {
                    _text.Text = "Buy";
                    _text.Color = Color.Green;
                    Chart.DrawVerticalLine("Buy", System.DateTime.Now, Color.LimeGreen);
                }
            }
            else
            {
                _text.Text = "Do Not Trade";
                _text.Color = Color.Gray;
            }
        }

    }
}

 


@ctid4633759

... Deleted by UFO ...

amusleh
13 Sep 2021, 11:07

Hi,

You can't use System date time for drawing objects on your chart, instead you should use the Bars OpenTimes:

using cAlgo.API;
using System.Linq;

namespace cAlgo
{
    [Indicator(IsOverlay = true, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class ThreeStrike : Indicator
    {
        [Parameter("Vertical Alignment", Group = "Position", DefaultValue = VerticalAlignment.Top)]
        public VerticalAlignment vAlignment { get; set; }

        [Parameter("Horizontal Alignment", Group = "Position", DefaultValue = HorizontalAlignment.Right)]
        public HorizontalAlignment hAlignment { get; set; }

        private ChartStaticText _text;

        protected override void Initialize()
        {
            _text = Chart.DrawStaticText("idtext_red", string.Empty, this.vAlignment, this.hAlignment, Color.Chocolate);
        }

        public override void Calculate(int index)
        {
            if (index < 3)
                return;

            var lastThreeBars = new Bar[]
            {
                Bars.Last(1),
                Bars.Last(2),
                Bars.Last(3)
            };

            if (lastThreeBars.All(bar => bar.Close > bar.Open))
            {
                _text.Text = "Wait For Red Engulfing";
                _text.Color = Color.Red;
                if (Bars.ClosePrices.Last(0) < Bars.OpenPrices.Last(1))
                {
                    _text.Text = "Sell";
                    _text.Color = Color.Red;
                    Chart.DrawVerticalLine("Sell", Bars.OpenTimes.LastValue, Color.OrangeRed);
                }
            }
            else if (lastThreeBars.All(bar => bar.Close < bar.Open))
            {
                _text.Text = "Wait For Green Engulfing";
                _text.Color = Color.Green;

                if (Bars.ClosePrices.Last(0) > Bars.OpenPrices.Last(1))
                {
                    _text.Text = "Buy";
                    _text.Color = Color.Green;
                    Chart.DrawVerticalLine("Buy", Bars.OpenTimes.LastValue, Color.LimeGreen);
                }
            }
            else
            {
                _text.Text = "Do Not Trade";
                _text.Color = Color.Gray;
            }
        }
    }
}

@amusleh