When Indicator is an Overlay=false and it scales, lines get misdrawn.

Created at 27 Nov 2019, 16:22
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!
FI

firemyst

Joined 26.03.2019

When Indicator is an Overlay=false and it scales, lines get misdrawn.
27 Nov 2019, 16:22


HI everyone:

I have an indicator which draws discontinuous lines and is set as IsOverlay=false.

I've noticed today that when there's big moves in the indicator value and it scales/adjusts its y-axis, the lines get disconnected as everything scales.

Perfect example from today:

What happened here is the y-axis squished down a bit on its own, and then expanded again. You can see with the discontinuous lines, they get drawn as shown above.

If I force the indicator to be redrawn, it redraws correctly as shown below:

Is there a way around this issue? Or is this something Spotware needs to fix?

Thank you.


@firemyst
Replies

PanagiotisCharalampous
27 Nov 2019, 16:24

Hi FireMyst,

Any chance we can get the indicator code?

Best Regards,

Panagiotis


@PanagiotisCharalampous

firemyst
27 Nov 2019, 16:55

RE:

Panagiotis Charalampous said:

Hi FireMyst,

Any chance we can get the indicator code?

Best Regards,

Panagiotis

Yup. Here it is. Looking at it further now, it seems like the values might be changing slightly for the lines to disconnect, but then I don't understand why the lines reconnect when it's redrawn.

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

namespace cAlgo.Indicators
{
    [Levels(0.0)]
    [Indicator("OSMA (DaveLoz)", IsOverlay = false, AccessRights = AccessRights.None)]
    public class OSMA : Indicator
    {
        [Parameter()]
        public TimeFrame SourceTimeFrame { get; set; }

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

        [Parameter(DefaultValue = 26)]
        public int LongCycle { get; set; }

        [Parameter(DefaultValue = 9)]
        public int Period { get; set; }

        [Output("UpTrend", LineColor = "Green", PlotType = PlotType.DiscontinuousLine, LineStyle = LineStyle.Solid, Thickness = 2)]
        public IndicatorDataSeries UpTrend { get; set; }

        [Output("DownTrend", LineColor = "Red", PlotType = PlotType.DiscontinuousLine, LineStyle = LineStyle.Solid, Thickness = 2)]
        public IndicatorDataSeries DownTrend { get; set; }

        [Output("Main", LineColor = "Cyan", PlotType = PlotType.Line, LineStyle = LineStyle.Solid, Thickness = 2)]
        public IndicatorDataSeries Result { get; set; }

        MacdHistogram macd;
        private MarketSeries _marketSeries;
        private DataSeries _dataSeries;
        private double _result = 0;
        private double _resultIndexMinus1 = 0;
        private int _previousIndex = Int32.MaxValue;
        private int _trend = 0;

        protected override void Initialize()
        {
            if (SourceTimeFrame != null)
                _marketSeries = MarketData.GetSeries(Symbol.Name, SourceTimeFrame);
            else
                _marketSeries = MarketData.GetSeries(Symbol.Name, MarketSeries.TimeFrame);

            _dataSeries = _marketSeries.Close;
            macd = Indicators.MacdHistogram(_dataSeries, ShortCycle, LongCycle, Period);
        }

        public override void Calculate(int index)
        {
            int altIndex = index;

            if (MarketSeries.TimeFrame != _marketSeries.TimeFrame)
            {
                altIndex = _marketSeries.OpenTime.GetIndexByTime(MarketSeries.OpenTime[index]);
            }

            if (altIndex < 2)
                return;

            _result = macd.Signal[altIndex] - macd.Histogram[altIndex];
            Result[index] = _result;
            UpTrend[index] = Double.NaN;
            DownTrend[index] = Double.NaN;

            _resultIndexMinus1 = Result[index - 1];

            if (_previousIndex < altIndex)
            {
                DownTrend[index] = Double.NaN;
                UpTrend[index] = Double.NaN;
                if (_result > _resultIndexMinus1)
                {
                    UpTrend[index] = _result;
                    UpTrend[index - 1] = _resultIndexMinus1;
                    if (UpTrend[index - 1] == DownTrend[index - 1] && UpTrend[index - 2] == DownTrend[index - 2])
                    {
                        DownTrend[index - 1] = Double.NaN;
                    }
                    _trend = 1;
                }
                else if (_result < _resultIndexMinus1)
                {
                    DownTrend[index] = _result;
                    DownTrend[index - 1] = _resultIndexMinus1;
                    if (UpTrend[index - 1] == DownTrend[index - 1] && UpTrend[index - 2] == DownTrend[index - 2])
                    {
                        UpTrend[index - 1] = Double.NaN;
                    }
                    _trend = -1;
                }
                else
                {
                    DownTrend[index] = Double.NaN;
                    UpTrend[index] = Double.NaN;
                    if (_trend == 1)
                    {
                        UpTrend[index] = _resultIndexMinus1;
                    }
                    else if (_trend == -1)
                    {
                        DownTrend[index] = _resultIndexMinus1;
                    }
                }
            }
            else
            {
                UpTrend[index] = Double.NaN;
                DownTrend[index] = Double.NaN;
                if (_trend == 1)
                {
                    UpTrend[index] = _resultIndexMinus1;
                }
                else if (_trend == -1)
                {
                    DownTrend[index] = _resultIndexMinus1;
                }
            }
            _previousIndex = altIndex;
        }
    }
}

 


@firemyst