Replies

heforus
05 Oct 2020, 18:26

RE:

Thank you so much Panagiotis,

I will work with this. I am so grateful.

 

PanagiotisCharalampous said:

Hi herofus,

Here you go

Indicator Code

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


namespace cAlgo.Indicators
{
    [Indicator(IsOverlay = true, AccessRights = AccessRights.None)]
    public class ZigZag : Indicator
    {

        [Parameter(DefaultValue = 12)]
        public int Depth { get; set; }

        [Parameter(DefaultValue = 5)]
        public int Deviation { get; set; }

        [Parameter(DefaultValue = 3)]
        public int BackStep { get; set; }

        [Output("ZigZag", Color = Colors.OrangeRed)]
        public IndicatorDataSeries Result { get; set; }

        #region Private fields

        private double _lastLow;
        private double _lastHigh;
        private double _low;
        private double _high;
        private int _lastHighIndex;
        private int _lastLowIndex;
        private int _type;
        private double _point;
        private double _currentLow;
        private double _currentHigh;

        public IndicatorDataSeries HighZigZags;
        public IndicatorDataSeries LowZigZags;

        #endregion

        protected override void Initialize()
        {
            HighZigZags = CreateDataSeries();
            LowZigZags = CreateDataSeries();
            _point = Symbol.PointSize;
        }

        public override void Calculate(int index)
        {

            if (index < Depth)
            {
                Result[index] = 0;
                HighZigZags[index] = 0;
                LowZigZags[index] = 0;
                return;
            }

            _currentLow = Functions.Minimum(MarketSeries.Low, Depth);
            if (Math.Abs(_currentLow - _lastLow) < double.Epsilon)
                _currentLow = 0.0;
            else
            {
                _lastLow = _currentLow;

                if ((MarketSeries.Low[index] - _currentLow) > (Deviation * _point))
                    _currentLow = 0.0;
                else
                {
                    for (int i = 1; i <= BackStep; i++)
                    {
                        if (Math.Abs(LowZigZags[index - i]) > double.Epsilon && LowZigZags[index - i] > _currentLow)
                            LowZigZags[index - i] = 0.0;
                    }
                }
            }
            if (Math.Abs(MarketSeries.Low[index] - _currentLow) < double.Epsilon)
                LowZigZags[index] = _currentLow;
            else
                LowZigZags[index] = 0.0;

            _currentHigh = MarketSeries.High.Maximum(Depth);

            if (Math.Abs(_currentHigh - _lastHigh) < double.Epsilon)
                _currentHigh = 0.0;
            else
            {
                _lastHigh = _currentHigh;

                if ((_currentHigh - MarketSeries.High[index]) > (Deviation * _point))
                    _currentHigh = 0.0;
                else
                {
                    for (int i = 1; i <= BackStep; i++)
                    {
                        if (Math.Abs(HighZigZags[index - i]) > double.Epsilon && HighZigZags[index - i] < _currentHigh)
                            HighZigZags[index - i] = 0.0;
                    }
                }
            }

            if (Math.Abs(MarketSeries.High[index] - _currentHigh) < double.Epsilon)
                HighZigZags[index] = _currentHigh;
            else
                HighZigZags[index] = 0.0;


            switch (_type)
            {
                case 0:
                    if (Math.Abs(_low - 0) < double.Epsilon && Math.Abs(_high - 0) < double.Epsilon)
                    {
                        if (Math.Abs(HighZigZags[index]) > double.Epsilon)
                        {
                            _high = MarketSeries.High[index];
                            _lastHighIndex = index;
                            _type = -1;
                            Result[index] = _high;
                        }
                        if (Math.Abs(LowZigZags[index]) > double.Epsilon)
                        {
                            _low = MarketSeries.Low[index];
                            _lastLowIndex = index;
                            _type = 1;
                            Result[index] = _low;
                        }
                    }
                    break;
                case 1:
                    if (Math.Abs(LowZigZags[index]) > double.Epsilon && LowZigZags[index] < _low && Math.Abs(HighZigZags[index] - 0.0) < double.Epsilon)
                    {
                        Result[_lastLowIndex] = double.NaN;
                        _lastLowIndex = index;
                        _low = LowZigZags[index];
                        Result[index] = _low;
                    }
                    if (Math.Abs(HighZigZags[index] - 0.0) > double.Epsilon && Math.Abs(LowZigZags[index] - 0.0) < double.Epsilon)
                    {
                        _high = HighZigZags[index];
                        _lastHighIndex = index;
                        Result[index] = _high;
                        _type = -1;
                    }
                    break;
                case -1:
                    if (Math.Abs(HighZigZags[index]) > double.Epsilon && HighZigZags[index] > _high && Math.Abs(LowZigZags[index] - 0.0) < double.Epsilon)
                    {
                        Result[_lastHighIndex] = double.NaN;
                        _lastHighIndex = index;
                        _high = HighZigZags[index];
                        Result[index] = _high;
                    }
                    if (Math.Abs(LowZigZags[index]) > double.Epsilon && Math.Abs(HighZigZags[index]) <= double.Epsilon)
                    {
                        _low = LowZigZags[index];
                        _lastLowIndex = index;
                        Result[index] = _low;
                        _type = 1;
                    }
                    break;
                default:
                    return;
            }

        }
    }
}

cBot code

using cAlgo.API;
using cAlgo.Indicators;

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class NewcBot : Robot
    {
        [Parameter(DefaultValue = 12)]
        public int Depth { get; set; }

        [Parameter(DefaultValue = 5)]
        public int Deviation { get; set; }

        [Parameter(DefaultValue = 3)]
        public int BackStep { get; set; }

        private ZigZag _zigzag;

        protected override void OnStart()
        {
            _zigzag = Indicators.GetIndicator<ZigZag>(Depth, Deviation, BackStep);
        }

        protected override void OnBar()
        {
            var result = _zigzag.Result.Last(1);
            Print("High: " + _zigzag.HighZigZags.Last(1));
            Print("Low: " + _zigzag.LowZigZags.LastValue);
        }

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

Best Regards,

Panagiotis 

Join us on Telegram

 


@heforus

heforus
04 Oct 2020, 17:07

RE:

Hello Panagiotis,

Thank you for your support. I must confess that i don't yet get how to retrieve the values I wanted to get from zigzag indicator inside the robot im building i.e the last low point of the zigzag and the last high point of the zigzag.

I code in c# asp.net, but i just started looking at ctrader about 2 weeks.

Please help me with an exact code sample to solve this problem. You said the last code i wrote doesn't make sense and the truth is i'm new to this and sure i'm mixing things up.

Kindly assist me. Thank you for all your efforts. I'm really grateful.

 

 

PanagiotisCharalampous said:

Hi herofus,

This change needs to take place in the indicator, not in the cBot. The code you have written makes no sense at all.

Best Regards,

Panagiotis 

Join us on Telegram

 


@heforus

heforus
30 Sep 2020, 16:54

RE:

Thank you @PanagiotisCharalampous 

Below are my Robot Codes where I call the Zigzag parameters. I have made them public and tried to retrieve the high and low values but it returns NaN when i run the Robot.
Kindly help correct me where I am making the error.

 

using System;
using System.Linq;
using System.Text;
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 ZigZagRobot : Robot
    {
        [Parameter(DefaultValue = 12)]
        public int Depth { get; set; }

        [Parameter(DefaultValue = 5)]
        public int Deviation { get; set; }

        [Parameter(DefaultValue = 3)]
        public int BackStep { get; set; }

        [Output("ZigZag", Color = Colors.OrangeRed)]
        public IndicatorDataSeries Result { get; set; }

        #region Private fields

        private double _lastLow = 0;
        private double _lastHigh = 0;
        private double _low;
        private double _high;
        private int _lastHighIndex;
        private int _lastLowIndex;
        private int _type;
        private double _point;
        private double _currentLow;
        private double _currentHigh;

        public IndicatorDataSeries _highZigZags;
        public IndicatorDataSeries _lowZigZags;

        private ZigZag _zigzag;

        #endregion


        protected override void OnStart()
        {
            // Put your initialization logic here
            _zigzag = Indicators.GetIndicator<ZigZag>(Depth, Deviation, BackStep);

            _highZigZags = CreateDataSeries();
            _lowZigZags = CreateDataSeries();
            _point = Symbol.PointSize;

            _lastHigh = _highZigZags.LastValue.;
            _lastLow = _lowZigZags.LastValue;

            Print("Last High is {0}", _lastHigh);
            Print("Last Low is {0}", _lastLow);
            _iniCurrentPrice = MarketSeries.Close.LastValue;
            Print("Current Price is {0}", _iniCurrentPrice);

        }
Log Result:
Last High is NaN
Last Low is NaN

 

Thanks for your time.

 

Hi herofus,

This indicator does not expose this information. The information is stored in the following private variables

        private IndicatorDataSeries _highZigZags;
        private IndicatorDataSeries _lowZigZags;

You need to make them public and read the information from there.

Best Regards,

Panagiotis 

Join us on Telegram

 


@heforus

heforus
30 Sep 2020, 13:44

RE: Here is the Zigzag Indicator Code, Thank you

heforus said:

I downloaded it and added it as a reference to my cbot instance. Running the zigzag indicator on a chat, it shows all the points highs and lows but while coding the cbot, i want to know how to programatically detect the last zigzag low price, last zigzag high price and which of the price points was created last before the current market price(the high point or low point).

 

Thank you

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


namespace cAlgo.Indicators
{
    [Indicator(IsOverlay = true, AccessRights = AccessRights.None)]
    public class ZigZag : Indicator
    {

        [Parameter(DefaultValue = 12)]
        public int Depth { get; set; }

        [Parameter(DefaultValue = 5)]
        public int Deviation { get; set; }

        [Parameter(DefaultValue = 3)]
        public int BackStep { get; set; }

        [Output("ZigZag", Color = Colors.OrangeRed)]
        public IndicatorDataSeries Result { get; set; }

        #region Private fields

        private double _lastLow;
        private double _lastHigh;
        private double _low;
        private double _high;
        private int _lastHighIndex;
        private int _lastLowIndex;
        private int _type;
        private double _point;
        private double _currentLow;
        private double _currentHigh;

        private IndicatorDataSeries _highZigZags;
        private IndicatorDataSeries _lowZigZags;

        #endregion

        protected override void Initialize()
        {
            _highZigZags = CreateDataSeries();
            _lowZigZags = CreateDataSeries();
            _point = Symbol.PointSize;
        }

        public override void Calculate(int index)
        {

            if (index < Depth)
            {
                Result[index] = 0;
                _highZigZags[index] = 0;
                _lowZigZags[index] = 0;
                return;
            }

            _currentLow = Functions.Minimum(MarketSeries.Low, Depth);
            if (Math.Abs(_currentLow - _lastLow) < double.Epsilon)
                _currentLow = 0.0;
            else
            {
                _lastLow = _currentLow;

                if ((MarketSeries.Low[index] - _currentLow) > (Deviation * _point))
                    _currentLow = 0.0;
                else
                {
                    for (int i = 1; i <= BackStep; i++)
                    {
                        if (Math.Abs(_lowZigZags[index - i]) > double.Epsilon && _lowZigZags[index - i] > _currentLow)
                            _lowZigZags[index - i] = 0.0;
                    }
                }
            }
            if (Math.Abs(MarketSeries.Low[index] - _currentLow) < double.Epsilon)
                _lowZigZags[index] = _currentLow;
            else
                _lowZigZags[index] = 0.0;

            _currentHigh = MarketSeries.High.Maximum(Depth);

            if (Math.Abs(_currentHigh - _lastHigh) < double.Epsilon)
                _currentHigh = 0.0;
            else
            {
                _lastHigh = _currentHigh;

                if ((_currentHigh - MarketSeries.High[index]) > (Deviation * _point))
                    _currentHigh = 0.0;
                else
                {
                    for (int i = 1; i <= BackStep; i++)
                    {
                        if (Math.Abs(_highZigZags[index - i]) > double.Epsilon && _highZigZags[index - i] < _currentHigh)
                            _highZigZags[index - i] = 0.0;
                    }
                }
            }

            if (Math.Abs(MarketSeries.High[index] - _currentHigh) < double.Epsilon)
                _highZigZags[index] = _currentHigh;
            else
                _highZigZags[index] = 0.0;


            switch (_type)
            {
                case 0:
                    if (Math.Abs(_low - 0) < double.Epsilon && Math.Abs(_high - 0) < double.Epsilon)
                    {
                        if (Math.Abs(_highZigZags[index]) > double.Epsilon)
                        {
                            _high = MarketSeries.High[index];
                            _lastHighIndex = index;
                            _type = -1;
                            Result[index] = _high;
                        }
                        if (Math.Abs(_lowZigZags[index]) > double.Epsilon)
                        {
                            _low = MarketSeries.Low[index];
                            _lastLowIndex = index;
                            _type = 1;
                            Result[index] = _low;
                        }
                    }
                    break;
                case 1:
                    if (Math.Abs(_lowZigZags[index]) > double.Epsilon && _lowZigZags[index] < _low && Math.Abs(_highZigZags[index] - 0.0) < double.Epsilon)
                    {
                        Result[_lastLowIndex] = double.NaN;
                        _lastLowIndex = index;
                        _low = _lowZigZags[index];
                        Result[index] = _low;
                    }
                    if (Math.Abs(_highZigZags[index] - 0.0) > double.Epsilon && Math.Abs(_lowZigZags[index] - 0.0) < double.Epsilon)
                    {
                        _high = _highZigZags[index];
                        _lastHighIndex = index;
                        Result[index] = _high;
                        _type = -1;
                    }
                    break;
                case -1:
                    if (Math.Abs(_highZigZags[index]) > double.Epsilon && _highZigZags[index] > _high && Math.Abs(_lowZigZags[index] - 0.0) < double.Epsilon)
                    {
                        Result[_lastHighIndex] = double.NaN;
                        _lastHighIndex = index;
                        _high = _highZigZags[index];
                        Result[index] = _high;
                    }
                    if (Math.Abs(_lowZigZags[index]) > double.Epsilon && Math.Abs(_highZigZags[index]) <= double.Epsilon)
                    {
                        _low = _lowZigZags[index];
                        _lastLowIndex = index;
                        Result[index] = _low;
                        _type = 1;
                    }
                    break;
                default:
                    return;
            }

        }
    }
}

 


@heforus

heforus
30 Sep 2020, 13:40

Here is the Zigzag Indicator Code, Thank you

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


namespace cAlgo.Indicators
{
    [Indicator(IsOverlay = true, AccessRights = AccessRights.None)]
    public class ZigZag : Indicator
    {

        [Parameter(DefaultValue = 12)]
        public int Depth { get; set; }

        [Parameter(DefaultValue = 5)]
        public int Deviation { get; set; }

        [Parameter(DefaultValue = 3)]
        public int BackStep { get; set; }

        [Output("ZigZag", Color = Colors.OrangeRed)]
        public IndicatorDataSeries Result { get; set; }

        #region Private fields

        private double _lastLow;
        private double _lastHigh;
        private double _low;
        private double _high;
        private int _lastHighIndex;
        private int _lastLowIndex;
        private int _type;
        private double _point;
        private double _currentLow;
        private double _currentHigh;

        private IndicatorDataSeries _highZigZags;
        private IndicatorDataSeries _lowZigZags;

        #endregion

        protected override void Initialize()
        {
            _highZigZags = CreateDataSeries();
            _lowZigZags = CreateDataSeries();
            _point = Symbol.PointSize;
        }

        public override void Calculate(int index)
        {

            if (index < Depth)
            {
                Result[index] = 0;
                _highZigZags[index] = 0;
                _lowZigZags[index] = 0;
                return;
            }

            _currentLow = Functions.Minimum(MarketSeries.Low, Depth);
            if (Math.Abs(_currentLow - _lastLow) < double.Epsilon)
                _currentLow = 0.0;
            else
            {
                _lastLow = _currentLow;

                if ((MarketSeries.Low[index] - _currentLow) > (Deviation * _point))
                    _currentLow = 0.0;
                else
                {
                    for (int i = 1; i <= BackStep; i++)
                    {
                        if (Math.Abs(_lowZigZags[index - i]) > double.Epsilon && _lowZigZags[index - i] > _currentLow)
                            _lowZigZags[index - i] = 0.0;
                    }
                }
            }
            if (Math.Abs(MarketSeries.Low[index] - _currentLow) < double.Epsilon)
                _lowZigZags[index] = _currentLow;
            else
                _lowZigZags[index] = 0.0;

            _currentHigh = MarketSeries.High.Maximum(Depth);

            if (Math.Abs(_currentHigh - _lastHigh) < double.Epsilon)
                _currentHigh = 0.0;
            else
            {
                _lastHigh = _currentHigh;

                if ((_currentHigh - MarketSeries.High[index]) > (Deviation * _point))
                    _currentHigh = 0.0;
                else
                {
                    for (int i = 1; i <= BackStep; i++)
                    {
                        if (Math.Abs(_highZigZags[index - i]) > double.Epsilon && _highZigZags[index - i] < _currentHigh)
                            _highZigZags[index - i] = 0.0;
                    }
                }
            }

            if (Math.Abs(MarketSeries.High[index] - _currentHigh) < double.Epsilon)
                _highZigZags[index] = _currentHigh;
            else
                _highZigZags[index] = 0.0;


            switch (_type)
            {
                case 0:
                    if (Math.Abs(_low - 0) < double.Epsilon && Math.Abs(_high - 0) < double.Epsilon)
                    {
                        if (Math.Abs(_highZigZags[index]) > double.Epsilon)
                        {
                            _high = MarketSeries.High[index];
                            _lastHighIndex = index;
                            _type = -1;
                            Result[index] = _high;
                        }
                        if (Math.Abs(_lowZigZags[index]) > double.Epsilon)
                        {
                            _low = MarketSeries.Low[index];
                            _lastLowIndex = index;
                            _type = 1;
                            Result[index] = _low;
                        }
                    }
                    break;
                case 1:
                    if (Math.Abs(_lowZigZags[index]) > double.Epsilon && _lowZigZags[index] < _low && Math.Abs(_highZigZags[index] - 0.0) < double.Epsilon)
                    {
                        Result[_lastLowIndex] = double.NaN;
                        _lastLowIndex = index;
                        _low = _lowZigZags[index];
                        Result[index] = _low;
                    }
                    if (Math.Abs(_highZigZags[index] - 0.0) > double.Epsilon && Math.Abs(_lowZigZags[index] - 0.0) < double.Epsilon)
                    {
                        _high = _highZigZags[index];
                        _lastHighIndex = index;
                        Result[index] = _high;
                        _type = -1;
                    }
                    break;
                case -1:
                    if (Math.Abs(_highZigZags[index]) > double.Epsilon && _highZigZags[index] > _high && Math.Abs(_lowZigZags[index] - 0.0) < double.Epsilon)
                    {
                        Result[_lastHighIndex] = double.NaN;
                        _lastHighIndex = index;
                        _high = _highZigZags[index];
                        Result[index] = _high;
                    }
                    if (Math.Abs(_lowZigZags[index]) > double.Epsilon && Math.Abs(_highZigZags[index]) <= double.Epsilon)
                    {
                        _low = _lowZigZags[index];
                        _lastLowIndex = index;
                        Result[index] = _low;
                        _type = 1;
                    }
                    break;
                default:
                    return;
            }

        }
    }
}


@heforus