Replies

jeziel.leira
12 Mar 2019, 05:47

The Indicator doesn´t

Hello Panagiotis

¿Could you check this code? I have added a parameter that change the Zoom when I want but if I change the Zoom directly from the chart, the indicator doesn´t update.But when I change the Zoom manually with the parameter that I have added, the indicator updates very well. 

I want that the indicator updates in the same way like the parameter I added

Do you have any idea?

---------------------- Check my code please --------------------------------

using System;
using System.Threading;
using cAlgo.API;
using cAlgo.API.Internals;
using cAlgo.API.Indicators;


namespace cAlgo
{

    [Indicator(IsOverlay = true, TimeZone = TimeZones.UTC, AutoRescale = true, AccessRights = AccessRights.None)]
    public class SistemaImpulsoVersionEstable : Indicator
    {

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

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

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

        [Parameter("Signal Periods", DefaultValue = 9)]
        public int Periods { get; set; }

        // # MEDIA MOVIL EXPONENCIAL
        [Parameter("Ema Period", DefaultValue = 13, MinValue = 1)]
        public int EmaPeriod { get; set; }

        // Change the Chart Zoom
        [Parameter("Zoom", DefaultValue = 2, MaxValue = 5, MinValue = 0)]
        public int Zoom { get; set; }

        [Parameter("Above up color", DefaultValue = "DarkGreen")]
        public string AboveDownColor { get; set; }

        [Parameter("Above up color", DefaultValue = "DarkGreen")]
        public string AboveUpColor { get; set; }

        [Parameter("Above up color 2", DefaultValue = "Lime")]
        public string AboveDownColor2 { get; set; }

        [Parameter("Above up color 2", DefaultValue = "Lime")]
        public string AboveUpColor2 { get; set; }

        [Parameter("Below up color", DefaultValue = "DarkRed")]
        public string BelowUpColor { get; set; }

        [Parameter("Below down color", DefaultValue = "DarkRed")]
        public string BelowDownColor { get; set; }

        [Parameter("Below up color 2", DefaultValue = "OrangeRed")]
        public string BelowUpColor2 { get; set; }

        [Parameter("Below down color 2", DefaultValue = "OrangeRed")]
        public string BelowDownColor2 { get; set; }

        [Parameter("Neutral up color", DefaultValue = "Gray")]
        public string NeutralUpColor { get; set; }

        [Parameter("Neutral down color", DefaultValue = "Gray")]
        public string NeutralDownColor { get; set; }

        [Parameter("Media Movil 1", DefaultValue = 20, MinValue = 1)]
        public int Period1 { get; set; }

        [Parameter("Media Movil 2", DefaultValue = 200, MinValue = 1)]
        public int Period2 { get; set; }

        [Parameter("Tipo", DefaultValue = MovingAverageType.Simple)]
        public MovingAverageType MAType { get; set; }

        [Output("Linea 1 Color", LineColor = "Yellow", LineStyle = LineStyle.Solid, Thickness = 1)]
        public IndicatorDataSeries ma1 { get; set; }

        [Output("Linea 2 Color", LineColor = "Red", LineStyle = LineStyle.Solid, Thickness = 1)]
        public IndicatorDataSeries ma2 { get; set; }

        private MovingAverage MA1;
        private MovingAverage MA2;
        public MacdHistogram MacdHistogram;
        private ExponentialMovingAverage Ema { get; set; }
        private Color color;

        public int CandleWidth
        {
            get { return _CandleWidth(); }
        }

        public int _CandleWidth()
        {
            int a = 0;

            if (Chart.ChartType == ChartType.Candlesticks)
            {
                if (Chart.Zoom == 0)
                {
                    a = 1;
                }
                if (Chart.Zoom == 1)
                {
                    a = 1;
                }
                if (Chart.Zoom == 2)
                {
                    a = 3;
                }
                if (Chart.Zoom == 3)
                {
                    a = 5;
                }
                if (Chart.Zoom == 4)
                {
                    a = 11;
                }
                if (Chart.Zoom == 5)
                {
                    a = 25;
                }
            }

            else
            {
                a = 1;
            }

            return a;
        }

        protected override void Initialize()
        {

            Chart.Zoom = Zoom;
            MacdHistogram = Indicators.MacdHistogram(Source, LongCycle, ShortCycle, Periods);
            Ema = Indicators.ExponentialMovingAverage(Source, EmaPeriod);

            MA1 = Indicators.MovingAverage(Source, Period1, MAType);
            MA2 = Indicators.MovingAverage(Source, Period2, MAType);

            Chart.ZoomChanged += Chart_ZoomChanged;
        }

        private void Chart_ZoomChanged(ChartZoomEventArgs obj)
        {

            Print("Zoom Changed");
            Zoom = Chart.Zoom;

        }

        public override void Calculate(int index)
        {

            ma1[index] = MA1.Result[index];
            ma2[index] = MA2.Result[index];

            double open = MarketSeries.Open[index];
            double high = MarketSeries.High[index];
            double low = MarketSeries.Low[index];
            double close = MarketSeries.Close[index];

            if (MacdHistogram.Histogram.IsRising() && Ema.Result.IsRising() && MacdHistogram.Histogram.LastValue < 0)
            {
                color = open > close ? AboveDownColor : AboveUpColor;
            }

            if (MacdHistogram.Histogram.IsRising() && Ema.Result.IsRising() && MacdHistogram.Histogram.LastValue > 0)
            {
                color = open > close ? AboveDownColor2 : AboveUpColor2;
            }

            if (MacdHistogram.Histogram.IsFalling() && Ema.Result.IsFalling() && MacdHistogram.Histogram.LastValue > 0)
            {
                color = open > close ? BelowDownColor : BelowUpColor;
            }

            if (MacdHistogram.Histogram.IsFalling() && Ema.Result.IsFalling() && MacdHistogram.Histogram.LastValue < 0)
            {
                color = open > close ? BelowDownColor2 : BelowUpColor2;
            }

            if (MacdHistogram.Histogram.IsRising() && Ema.Result.IsFalling() || MacdHistogram.Histogram.IsFalling() && Ema.Result.IsRising())
            {
                color = open > close ? NeutralDownColor : NeutralUpColor;
            }

            //Here is printed on the Chart and the CandleWidth take the last value in the switch

            Chart.DrawTrendLine("candle" + index, index, open, index, close, color, CandleWidth, LineStyle.Solid);
            Chart.DrawTrendLine("line" + index, index, high, index, low, color, 1, LineStyle.Solid);
        }

    }
}


@jeziel.leira

jeziel.leira
08 Mar 2019, 00:08

RE:

Panagiotis Charalampous said:

Hi Jeziel,

Check the code below

using System;
using cAlgo.API;
using cAlgo.API.Internals;
using cAlgo.API.Indicators;
using cAlgo.Indicators;
using System.Linq;

namespace cAlgo
{

    [Indicator(IsOverlay = true, TimeZone = TimeZones.UTC, AutoRescale = true, AccessRights = AccessRights.None)]
    public class SistemaImpulsoVersionEstable : Indicator
    {

        public MacdHistogram MacdHistogram;

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

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

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

        [Parameter("Signal Periods", DefaultValue = 9)]
        public int Periods { get; set; }

        [Parameter("Ema Period", DefaultValue = 13, MinValue = 1)]
        public int EmaPeriod { get; set; }

        private ExponentialMovingAverage Ema { get; set; }

        [Parameter("Above up color", DefaultValue = "DarkGreen")]
        public string AboveDownColor { get; set; }

        [Parameter("Above up color", DefaultValue = "DarkGreen")]
        public string AboveUpColor { get; set; }

        [Parameter("Above up color 2", DefaultValue = "Lime")]
        public string AboveDownColor2 { get; set; }

        [Parameter("Above up color 2", DefaultValue = "Lime")]
        public string AboveUpColor2 { get; set; }

        [Parameter("Below up color", DefaultValue = "DarkRed")]
        public string BelowUpColor { get; set; }

        [Parameter("Below down color", DefaultValue = "DarkRed")]
        public string BelowDownColor { get; set; }

        [Parameter("Below up color 2", DefaultValue = "OrangeRed")]
        public string BelowUpColor2 { get; set; }

        [Parameter("Below down color 2", DefaultValue = "OrangeRed")]
        public string BelowDownColor2 { get; set; }

        [Parameter("Neutral up color", DefaultValue = "Gray")]
        public string NeutralUpColor { get; set; }

        [Parameter("Neutral down color", DefaultValue = "Gray")]
        public string NeutralDownColor { get; set; }

        private Colors _AboveUpColor;
        private Colors _AboveDownColor;
        private Colors _AboveUpColor2;
        private Colors _AboveDownColor2;

        private Colors _BelowUpColor;
        private Colors _BelowDownColor;
        private Colors _BelowUpColor2;
        private Colors _BelowDownColor2;

        private Colors _NeutralUpColor;
        private Colors _NeutralDownColor;
        private Colors color;
        private bool _incorrectColors;
        private Random _random = new Random();


        /*------ SMA 200-----------*/
        public int Ema200 = 200;
        private MovingAverage ma200;

        /*------FIN SMA 200-----------*/


        protected override void Initialize()
        {

            MacdHistogram = Indicators.MacdHistogram(Source, LongCycle, ShortCycle, Periods);
            Ema = Indicators.ExponentialMovingAverage(Source, EmaPeriod);
            ma200 = Indicators.SimpleMovingAverage(Source, Ema200);



            if (!Enum.TryParse<Colors>(AboveUpColor, out _AboveUpColor) || !Enum.TryParse<Colors>(AboveDownColor, out _AboveDownColor) || !Enum.TryParse<Colors>(BelowUpColor, out _BelowUpColor) || !Enum.TryParse<Colors>(BelowDownColor, out _BelowDownColor) || !Enum.TryParse<Colors>(NeutralUpColor, out _NeutralUpColor) || !Enum.TryParse<Colors>(NeutralDownColor, out _NeutralDownColor) || !Enum.TryParse<Colors>(AboveUpColor2, out _AboveUpColor2) || !Enum.TryParse<Colors>(AboveDownColor2, out _AboveDownColor2) || !Enum.TryParse<Colors>(BelowUpColor2, out _BelowUpColor2) || !Enum.TryParse<Colors>(BelowDownColor2, out _BelowDownColor2))
            {
                _incorrectColors = true;
            }
            Chart.ZoomChanged += Chart_ZoomChanged;
        }

        private void Chart_ZoomChanged(ChartZoomEventArgs obj)
        {
          
            Print("Zoom Changed");
            for (int i = 0; i < MarketSeries.Close.Count; i++)
            {
                Refresh(i);
            }
        }
        public void Refresh(int index)
        {
            // This update the Candlewidth Variable according to the ZoomChart Value

            int CandleWidth = 1;

            switch (Chart.Zoom)
            {
                case 0:
                    {
                        CandleWidth = 1;
                        break;
                    }
                case 1:
                    {
                        CandleWidth = 1;
                        break;
                    }
                case 2:
                    {
                        CandleWidth = 3;
                        break;
                    }
                case 3:
                    {
                        CandleWidth = 5;
                        break;
                    }
                case 4:
                    {
                        CandleWidth = 11;
                        break;
                    }
                case 5:
                    {
                        CandleWidth = 25;
                        break;
                    }
            }

            // This update de color of the color variable according to its condition 

            var open = MarketSeries.Open[index];
            var high = MarketSeries.High[index];
            var low = MarketSeries.Low[index];
            var close = MarketSeries.Close[index];

            if (MacdHistogram.Histogram.IsRising() && Ema.Result.IsRising() && MacdHistogram.Histogram.LastValue < 0)
            {
                color = open > close ? _AboveDownColor : _AboveUpColor;
            }

            if (MacdHistogram.Histogram.IsRising() && Ema.Result.IsRising() && MacdHistogram.Histogram.LastValue > 0)
            {
                color = open > close ? _AboveDownColor2 : _AboveUpColor2;
            }

            if (MacdHistogram.Histogram.IsFalling() && Ema.Result.IsFalling() && MacdHistogram.Histogram.LastValue > 0)
            {
                color = open > close ? _BelowDownColor : _BelowUpColor;
            }

            if (MacdHistogram.Histogram.IsFalling() && Ema.Result.IsFalling() && MacdHistogram.Histogram.LastValue < 0)
            {
                color = open > close ? _BelowDownColor2 : _BelowUpColor2;
            }

            if (MacdHistogram.Histogram.IsRising() && Ema.Result.IsFalling() || MacdHistogram.Histogram.IsFalling() && Ema.Result.IsRising())
            {
                color = open > close ? _NeutralDownColor : _NeutralUpColor;
            }

            //Here is printed on the Chart and the CandleWidth take the last value in the switch
            Chart.DrawTrendLine("candle" + index, index, open, index, close, (Chart.Objects.First(x => x.Name == "candle" + index) as ChartTrendLine).Color, CandleWidth, LineStyle.Solid);
            Chart.DrawTrendLine("line" + index, index, high, index, low, (Chart.Objects.First(x => x.Name == "line" + index) as ChartTrendLine).Color, 1, LineStyle.Solid);
        }
        public override void Calculate(int index)
        {
            // This update the Candlewidth Variable according to the ZoomChart Value

            int CandleWidth = 1;

            switch (Chart.Zoom)
            {
                case 0:
                    {
                        CandleWidth = 1;
                        break;
                    }
                case 1:
                    {
                        CandleWidth = 1;
                        break;
                    }
                case 2:
                    {
                        CandleWidth = 3;
                        break;
                    }
                case 3:
                    {
                        CandleWidth = 5;
                        break;
                    }
                case 4:
                    {
                        CandleWidth = 11;
                        break;
                    }
                case 5:
                    {
                        CandleWidth = 25;
                        break;
                    }
            }

            // This update de color of the color variable according to its condition 

            var open = MarketSeries.Open[index];
            var high = MarketSeries.High[index];
            var low = MarketSeries.Low[index];
            var close = MarketSeries.Close[index];

            if (MacdHistogram.Histogram.IsRising() && Ema.Result.IsRising() && MacdHistogram.Histogram.LastValue < 0)
            {
                color = open > close ? _AboveDownColor : _AboveUpColor;
            }

            if (MacdHistogram.Histogram.IsRising() && Ema.Result.IsRising() && MacdHistogram.Histogram.LastValue > 0)
            {
                color = open > close ? _AboveDownColor2 : _AboveUpColor2;
            }

            if (MacdHistogram.Histogram.IsFalling() && Ema.Result.IsFalling() && MacdHistogram.Histogram.LastValue > 0)
            {
                color = open > close ? _BelowDownColor : _BelowUpColor;
            }

            if (MacdHistogram.Histogram.IsFalling() && Ema.Result.IsFalling() && MacdHistogram.Histogram.LastValue < 0)
            {
                color = open > close ? _BelowDownColor2 : _BelowUpColor2;
            }

            if (MacdHistogram.Histogram.IsRising() && Ema.Result.IsFalling() || MacdHistogram.Histogram.IsFalling() && Ema.Result.IsRising())
            {
                color = open > close ? _NeutralDownColor : _NeutralUpColor;
            }

            //Here is printed on the Chart and the CandleWidth take the last value in the switch

            ChartObjects.DrawLine("candle" + index, index, open, index, close, color, CandleWidth, LineStyle.Solid);
            ChartObjects.DrawLine("line" + index, index, high, index, low, color, 1, LineStyle.Solid);
        }

    }

}

Let me know if this works for you

Best Regards,

Panagiotis

Hi Panagiotis

It works !! But I don´t know why it is very slow when changing the width of the candles after set other zoom. It takes several seconds to change it, up to 15 or 20 second. I thank you a lot for your help. I will try to research the solution jeje. Thanks.


@jeziel.leira

jeziel.leira
01 Mar 2019, 18:43

RE:

Panagiotis Charalampous said:

Hi Jeziel,

Thanks for posting in our forum. To solve the problem, you need to handle the zoom changing event and redraw your indicator. See below

        protected override void Initialize()
        {

            MacdHistogram = Indicators.MacdHistogram(Source, LongCycle, ShortCycle, Periods);
            Ema = Indicators.ExponentialMovingAverage(Source, EmaPeriod);
            ma200 = Indicators.SimpleMovingAverage(Source, Ema200);



            if (!Enum.TryParse<Colors>(AboveUpColor, out _AboveUpColor) || !Enum.TryParse<Colors>(AboveDownColor, out _AboveDownColor) || !Enum.TryParse<Colors>(BelowUpColor, out _BelowUpColor) || !Enum.TryParse<Colors>(BelowDownColor, out _BelowDownColor) || !Enum.TryParse<Colors>(NeutralUpColor, out _NeutralUpColor) || !Enum.TryParse<Colors>(NeutralDownColor, out _NeutralDownColor) || !Enum.TryParse<Colors>(AboveUpColor2, out _AboveUpColor2) || !Enum.TryParse<Colors>(AboveDownColor2, out _AboveDownColor2) || !Enum.TryParse<Colors>(BelowUpColor2, out _BelowUpColor2) || !Enum.TryParse<Colors>(BelowDownColor2, out _BelowDownColor2))
            {
                _incorrectColors = true;
            }
            Chart.ZoomChanged += Chart_ZoomChanged;
        }

        private void Chart_ZoomChanged(ChartZoomEventArgs obj)
        {
           //Redraw your indicator           
        }

Let me know if this helps

Best Regards.

Panagiotis

------------------------------------------------------------------------------------------------------------------------------

Hi Panagiotis

Thanks. I really don´t know how to redraw the indicator... ¿Could you please give me an example? I am not a programmer but I am trying to learn little by little. I would be very grateful with you. 

I have modified the code to be a little more short: 

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

namespace cAlgo
{

    [Indicator(IsOverlay = true, TimeZone = TimeZones.UTC, AutoRescale = true, AccessRights = AccessRights.None)]
    public class SistemaImpulsoVersionEstable : Indicator
    {

        public MacdHistogram MacdHistogram;

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

        [Parameter("CandleWidth", DefaultValue = 2)]
        public int CandleWidth { get; set; }

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

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

        [Parameter("Signal Periods", DefaultValue = 9)]
        public int Periods { get; set; }

        [Parameter("Ema Period", DefaultValue = 13, MinValue = 1)]
        public int EmaPeriod { get; set; }

        [Parameter("Above up color", DefaultValue = "DarkGreen")]
        public string AboveDownColor { get; set; }

        [Parameter("Above up color", DefaultValue = "DarkGreen")]
        public string AboveUpColor { get; set; }

        [Parameter("Above up color 2", DefaultValue = "Lime")]
        public string AboveDownColor2 { get; set; }

        [Parameter("Above up color 2", DefaultValue = "Lime")]
        public string AboveUpColor2 { get; set; }

        [Parameter("Below up color", DefaultValue = "DarkRed")]
        public string BelowUpColor { get; set; }

        [Parameter("Below down color", DefaultValue = "DarkRed")]
        public string BelowDownColor { get; set; }

        [Parameter("Below up color 2", DefaultValue = "OrangeRed")]
        public string BelowUpColor2 { get; set; }

        [Parameter("Below down color 2", DefaultValue = "OrangeRed")]
        public string BelowDownColor2 { get; set; }

        [Parameter("Neutral up color", DefaultValue = "Gray")]
        public string NeutralUpColor { get; set; }

        [Parameter("Neutral down color", DefaultValue = "Gray")]
        public string NeutralDownColor { get; set; }
        private ExponentialMovingAverage Ema { get; set; }

        private Color color;

        protected override void Initialize()
        {

            MacdHistogram = Indicators.MacdHistogram(Source, LongCycle, ShortCycle, Periods);
            Ema = Indicators.ExponentialMovingAverage(Source, EmaPeriod);

            Chart.ZoomChanged += Chart_ZoomChanged;
        }

        private void Chart_ZoomChanged(ChartZoomEventArgs obj)
        {
            //I don´t know how to redraw the indicator here
        }
        public override void Calculate(int index)
        {

            double open = MarketSeries.Open[index];
            double high = MarketSeries.High[index];
            double low = MarketSeries.Low[index];
            double close = MarketSeries.Close[index];

            if (MacdHistogram.Histogram.IsRising() && Ema.Result.IsRising() && MacdHistogram.Histogram.LastValue < 0)
            {
                color = open > close ? AboveDownColor : AboveUpColor;

            }

            if (MacdHistogram.Histogram.IsRising() && Ema.Result.IsRising() && MacdHistogram.Histogram.LastValue > 0)
            {
                color = open > close ? AboveDownColor2 : AboveUpColor2;
            }

            if (MacdHistogram.Histogram.IsFalling() && Ema.Result.IsFalling() && MacdHistogram.Histogram.LastValue > 0)
            {
                color = open > close ? BelowDownColor : BelowUpColor;
            }

            if (MacdHistogram.Histogram.IsFalling() && Ema.Result.IsFalling() && MacdHistogram.Histogram.LastValue < 0)
            {
                color = open > close ? BelowDownColor2 : BelowUpColor2;
            }

            if (MacdHistogram.Histogram.IsRising() && Ema.Result.IsFalling() || MacdHistogram.Histogram.IsFalling() && Ema.Result.IsRising())
            {
                color = open > close ? NeutralDownColor : NeutralUpColor;
            }

            //Here is printed on the Chart and the CandleWidth take the last value in the switch

            Chart.DrawTrendLine("Body" + index, index, open, index, close, color, CandleWidth, LineStyle.Solid);
            Chart.DrawTrendLine("high" + index, index, high, index, Math.Max(open, close), color, 1);
            Chart.DrawTrendLine("low" + index, index, low, index, Math.Min(open, close), color, 1);

            switch (Chart.Zoom)
            {
                case 0:
                    {
                        CandleWidth = 1;
                        ChartObjects.DrawText("Position", "Zoom está en cero", StaticPosition.BottomCenter, Colors.Green);
                        break;
                    }
                case 1:
                    {
                        CandleWidth = 1;
                        ChartObjects.DrawText("Position", "Zoom está en 1", StaticPosition.BottomCenter, Colors.Lime);
                        break;
                    }
                case 2:
                    {
                        CandleWidth = 3;
                        ChartObjects.DrawText("Position", "Zoom está en 2", StaticPosition.BottomCenter, Colors.Blue);
                        break;
                    }
                case 3:
                    {
                        CandleWidth = 5;
                        ChartObjects.DrawText("Position", "Zoom está en 3", StaticPosition.BottomCenter, Colors.Red);
                        break;
                    }
                case 4:
                    {
                        CandleWidth = 11;
                        ChartObjects.DrawText("Position", "Zoom está en 4", StaticPosition.BottomCenter, Colors.Yellow);
                        break;
                    }
                case 5:
                    {
                        CandleWidth = 25;
                        ChartObjects.DrawText("Position", "Zoom está en 5", StaticPosition.BottomCenter, Colors.Pink);
                        break;
                    }
            }

        }

    }

}


@jeziel.leira