Histogram bars painting on top of each other

Created at 03 Feb 2014, 13:13
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!
MRCR's avatar

MRCR

Joined 08.01.2014

Histogram bars painting on top of each other
03 Feb 2014, 13:13


Hi everyone, I'm developing an histogram based indicator, but I'm getting weird results.

What the indicator does:
It calculates the true median price of a candle using the highest price and the lowest. This is an indicator that works as a filter for a bot I'm developing.

The problem:
This indicator has 3 states: Buy, Wait and Sell. When buy sell or wait states are triggered the bars turn blue for buy, yellow for wait and red for sell.

If a bar is blue (buying time), and then the median price drops below the previous median price, it turns yellow(wait), but instead of just changing the color of the current bar being drawn, it draws another bar above or below the current one, with the new color.

In this example the last bar was telling me to wait (yellow), but then the price started to go up and instead of just changing the color from yellow to blue it just painted another blue bar in the background. I've tried to put isRealtime or isLastBar and nothing changes this. Code below.

Bear in mind i'm very new to programming and i'm actually learning by doing, the code below is not good code, yet. :)
 

//#reference: HullMovingAverage.algo
using System;
using cAlgo.API;
using cAlgo.API.Internals;
using cAlgo.API.Indicators;

namespace cAlgo.Indicators
{
    [Indicator(IsOverlay = false, TimeZone = TimeZones.UTC, ScalePrecision = 5)]
    public class MedianHLHMA : Indicator
    {
        [Parameter("Blue Source")]
        public DataSeries blueSource { get; set; }

        [Parameter("Red Source")]
        public DataSeries redSource { get; set; }

        [Parameter("Blue Period", DefaultValue = 6)]
        public int bluePeriod { get; set; }

        [Parameter("Red Period", DefaultValue = 6)]
        public int redPeriod { get; set; }

        [Output("Current HHMA Up", Color = Colors.SteelBlue, PlotType = PlotType.Histogram)]
        public IndicatorDataSeries currentMedianHLUP { get; set; }

        [Output("Current HHMA Wait", Color = Colors.Yellow, PlotType = PlotType.Histogram)]
        public IndicatorDataSeries currentMedianHLWAIT { get; set; }

        [Output("Current HHMA Down", Color = Colors.OrangeRed, PlotType = PlotType.Histogram)]
        public IndicatorDataSeries currentMedianHLDOWN { get; set; }

        private HullMovingAverage blueMA;
        private HullMovingAverage redMA;

        protected override void Initialize()
        {
            blueMA = Indicators.GetIndicator<HullMovingAverage>(blueSource, bluePeriod);
            redMA = Indicators.GetIndicator<HullMovingAverage>(redSource, redPeriod);
        }

        public override void Calculate(int index)
        {
            var currentMedianValue = (blueMA.Result[index] + redMA.Result[index]) / 2;
            var previousMedianValue = (blueMA.Result[index - 1] + redMA.Result[index - 1]) / 2;
            var previous2MedianValues = (blueMA.Result[index - 2] + redMA.Result[index - 2]) / 2;

            // Bolleans to be used by robot to know if should buy or sell
            bool isSellingTime;
            bool isBuyingTime;

            // Calculations to know if last median price is lower or higher than current price
            //
            // Buy
            // If realtime is enabled it means the indicator only works with realtime data.
            if (previous2MedianValues < currentMedianValue && previousMedianValue < currentMedianValue && IsRealTime)
            {
                isBuyingTime = true;
                isSellingTime = false;
                ChartObjects.DrawText("Buy", "BUY", StaticPosition.TopRight, Colors.SteelBlue);
                ChartObjects.RemoveObject("Sell");
                ChartObjects.RemoveObject("Wait");

                currentMedianHLUP[index] = ((blueMA.Result[index] + redMA.Result[index]) / 2);
            }
            // Wait for better Buy signals
            else if (previousMedianValue < currentMedianValue && previous2MedianValues > previousMedianValue && IsRealTime)
            {
                isSellingTime = false;
                isBuyingTime = false;
                ChartObjects.DrawText("Wait", "WAIT", StaticPosition.TopRight, Colors.Yellow);
                ChartObjects.RemoveObject("Buy");
                ChartObjects.RemoveObject("Sell");

                currentMedianHLWAIT[index] = ((blueMA.Result[index] + redMA.Result[index]) / 2);
            }
            // Wait for better Sell signals
            else if (previousMedianValue > currentMedianValue && previous2MedianValues < previousMedianValue && IsRealTime)
            {
                isSellingTime = false;
                isBuyingTime = false;
                ChartObjects.DrawText("Wait", "WAIT", StaticPosition.TopRight, Colors.Yellow);
                ChartObjects.RemoveObject("Buy");
                ChartObjects.RemoveObject("Sell");

                currentMedianHLWAIT[index] = ((blueMA.Result[index] + redMA.Result[index]) / 2);
            }
            // Sell
            else if (previous2MedianValues > currentMedianValue && previousMedianValue > currentMedianValue && IsRealTime)
            {
                isSellingTime = true;
                isBuyingTime = false;
                ChartObjects.DrawText("Sell", "SELL", StaticPosition.TopRight, Colors.OrangeRed);
                ChartObjects.RemoveObject("Buy");
                ChartObjects.RemoveObject("Wait");

                currentMedianHLDOWN[index] = ((blueMA.Result[index] + redMA.Result[index]) / 2);
            }
        }
    }
}

 

 


@MRCR
Replies

MRCR
04 Feb 2014, 14:48

Would it be possible for someone from spotware to comment please? is this a bug from the platform?


@MRCR

Spotware
04 Feb 2014, 15:04

Probably you have an issue with calculations. If you observe problem with platform, it will be better if you show simpler example with the problem.


@Spotware

MRCR
04 Feb 2014, 18:34

RE:

Spotware said:

Probably you have an issue with calculations. If you observe problem with platform, it will be better if you show simpler example with the problem.

I'll try to simplify this indicator and i'm going to read somethings about C# to improve the code in it, if i see it continues doing the same, then i'll update this post. Thanks anyway.


@MRCR