Replies

3010996
05 Mar 2019, 16:49

I think I succeeded. Panagiotis Charalampous, could you look at the correct code?

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


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

        private bool showSMA;
        //private int i;

        public int redPeriod = 12;
        [Output("Red SMA", LineColor = "Red", Thickness = 1)]
        public IndicatorDataSeries RedSMA { get; set; }

        [Output("Red SMA M15", LineColor = "Red", Thickness = 3)]
        public IndicatorDataSeries RedSMA15 { get; set; }

        private MovingAverage _redSMA;
        private MarketSeries seriesM15;
        private MovingAverage _redSMA15;
        private int indexM15;


        protected override void Initialize()
        {
            Timer.TimerTick += OnTimerTick;
            Timer.Start(2);

            _redSMA = Indicators.MovingAverage(MarketSeries.Close, redPeriod, MovingAverageType.Simple);

            seriesM15 = MarketData.GetSeries(TimeFrame.Minute15);
            _redSMA15 = Indicators.MovingAverage(seriesM15.Close, redPeriod, MovingAverageType.Simple);
        }

        public override void Calculate(int index)
        {
            RedSMA[index] = _redSMA.Result[index];

            indexM15 = GetIndexByDate(seriesM15, MarketSeries.OpenTime[index]);
            if (indexM15 != -1)
            {
                RedSMA15[index] = _redSMA15.Result[indexM15];
            }

            //
            // Current timeframe
            //
            if (showSMA)
            {
                for (int i = 0; i < RedSMA.Count; i++)
                {
                    RedSMA[i] = _redSMA.Result[i];
                }
            }
            else
            {
                for (int i = 0; i < RedSMA.Count; i++)
                {
                    RedSMA[i] = double.NaN;
                }
            }

            //
            // SMA - 15min timeframe
            //
            if (showSMA)
            {
                for (int i = 0; i < RedSMA15.Count; i++)
                {
                    indexM15 = GetIndexByDate(seriesM15, MarketSeries.OpenTime[i]);
                    if (indexM15 != -1)
                        RedSMA15[i] = _redSMA15.Result[indexM15];
                }
            }
            else
            {
                for (int i = 0; i < RedSMA15.Count; i++)
                {
                    RedSMA15[i] = double.NaN;
                }
            }
        }

        private void OnTimerTick()
        {            
            showSMA = !showSMA;
        }

        private int GetIndexByDate(MarketSeries series, DateTime time)
        {
            for (int i = series.Close.Count - 1; i > 0; i--)
            {
                if (time == series.OpenTime[i])
                    return i;
            }

            return -1;
        }
    }
}

 


@3010996

3010996
05 Mar 2019, 16:23

Exactly. Unfortunately, I can not understand how to calculate the indexes correctly. Erasing the graph works. However, I can not draw it correctly again. I have been struggling with it for several days.

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

namespace cAlgo
{
    [Indicator(IsOverlay = true, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None, AutoRescale = false)]
    public class TEST2 : Indicator
    {
        private bool showSMA;       
        public int redPeriod = 12;
        [Output("Red SMA", LineColor = "Red", Thickness = 1)]
        public IndicatorDataSeries RedSMA { get; set; }

        [Output("Red SMA M15", LineColor = "Red", Thickness = 3)]
        public IndicatorDataSeries RedSMA15 { get; set; }

        private MovingAverage _redSMA;
        private MarketSeries seriesM15;
        private MovingAverage _redSMA15;
        private int indexM15;

        protected override void Initialize()
        {
            Timer.TimerTick += OnTimerTick;
            Timer.Start(2);
            _redSMA = Indicators.MovingAverage(MarketSeries.Close, redPeriod, MovingAverageType.Simple);
            seriesM15 = MarketData.GetSeries(TimeFrame.Minute15);
            _redSMA15 = Indicators.MovingAverage(seriesM15.Close, redPeriod, MovingAverageType.Simple);
       }

        public override void Calculate(int index)
        {
            RedSMA[index] = _redSMA.Result[index];

            indexM15 = GetIndexByDate(seriesM15, MarketSeries.OpenTime[index]);
            if (indexM15 != -1)
            {
                RedSMA15[index] = _redSMA15.Result[indexM15];
            }
        }

        private void OnTimerTick()
        {
            //
            // SMA - current timeframe
            //
            showSMA = !showSMA;
            if (showSMA)
            {
                for (int i = 0; i < RedSMA.Count; i++)
                {
                    RedSMA[i] = _redSMA.Result[i];
                }
            }
            else
            {
                for (int i = 0; i < RedSMA.Count; i++)
                {
                    RedSMA[i] = double.NaN;
                }
            }
            //
            // SMA - 15min timeframe
            //
            if (showSMA)
            {
                for (int i = 0; i < RedSMA15.Count; i++)
                {                    
                        RedSMA15[i] = _redSMA15.Result[indexM15];
                }
            }
            else
            {
                for (int i = 0; i < RedSMA15.Count; i++)
                {
                    RedSMA15[i] = double.NaN;
                }
            }
        }

        private int GetIndexByDate(MarketSeries series, DateTime time)
        {
            for (int i = series.Close.Count - 1; i > 0; i--)
            {
                if (time == series.OpenTime[i])
                    return i;
            }

            return -1;
        }
    }
}

 


@3010996

3010996
05 Mar 2019, 13:08

I have one more question. I'm trying to rewrite the code to show and hide on average from a different time frame. Unfortunately, the average I display shows bad values.

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


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

        private bool showSMA;
        //private int i;

        public int redPeriod = 12;
        [Output("Red SMA", LineColor = "Red", Thickness = 1)]
        public IndicatorDataSeries RedSMA { get; set; }

        [Output("Red SMA M15", LineColor = "Red", Thickness = 3)]
        public IndicatorDataSeries RedSMA15 { get; set; }

        private MovingAverage _redSMA;
        private MarketSeries seriesM15;
        private MovingAverage _redSMA15;
        private int indexM15;


        protected override void Initialize()
        {
            Timer.TimerTick += OnTimerTick;
            Timer.Start(2);

            _redSMA = Indicators.MovingAverage(MarketSeries.Close, redPeriod, MovingAverageType.Simple);

            seriesM15 = MarketData.GetSeries(TimeFrame.Minute15);
            _redSMA15 = Indicators.MovingAverage(seriesM15.Close, redPeriod, MovingAverageType.Simple);

        }

        public override void Calculate(int index)
        {
            RedSMA[index] = _redSMA.Result[index];

            indexM15 = GetIndexByDate(seriesM15, MarketSeries.OpenTime[index]);
            if (indexM15 != -1)
            {
                RedSMA15[index] = _redSMA15.Result[indexM15];
            }

        }

        private void OnTimerTick()
        {
            //
            // SMA - current timeframe
            //
            showSMA = !showSMA;
            if (showSMA)
            {
                for (int i = 0; i < RedSMA.Count; i++)
                {
                    RedSMA[i] = _redSMA.Result[i];
                }
            }
            else
            {
                for (int i = 0; i < RedSMA.Count; i++)
                {
                    RedSMA[i] = double.NaN;
                }
            }
            //
            // SMA - timeframe 15 min
            //
            if (showSMA)
            {
                for (int i = 0; i < RedSMA15.Count; i++)
                {
                    RedSMA15[i] = _redSMA15.Result[i];
                }
            }
            else
            {
                for (int i = 0; i < RedSMA15.Count; i++)
                {
                    RedSMA15[i] = double.NaN;
                }
            }
        }

        private int GetIndexByDate(MarketSeries series, DateTime time)
        {
            for (int i = series.Close.Count - 1; i > 0; i--)
            {
                if (time == series.OpenTime[i])
                    return i;
            }

            return -1;
        }
    }
}

 


@3010996

3010996
26 Feb 2019, 22:51

RE:

Panagiotis Charalampous said:

Hi lw3010996,

Here is an example

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


namespace cAlgo
{
    [Indicator(IsOverlay = true, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class TEST2 : Indicator
    {

        private bool showSMA;
        private int i;

        public int redPeriod = 12;
        [Output("Red SMA", LineColor = "Red", Thickness = 1)]
        public IndicatorDataSeries redSMA { get; set; }


        private MovingAverage _redSMA;


        protected override void Initialize()
        {
            Timer.TimerTick += OnTimerTick;
            Timer.Start(2);
            _redSMA = Indicators.MovingAverage(MarketSeries.Close, redPeriod, MovingAverageType.Simple);
        }

        public override void Calculate(int index)
        {

            redSMA[index] = _redSMA.Result[index];


        }

        private void OnTimerTick()
        {
            showSMA = !showSMA;
            if (showSMA)
            {
                for (int i = 0; i < redSMA.Count; i++)
                {
                    redSMA[i] = _redSMA.Result[i];
                }
            }
            else
            {
                for (int i = 0; i < redSMA.Count; i++)
                {
                    redSMA[i] = double.NaN;
                }
            }
        }
    }
}

Best Regards,

Panagiotis

Thank you, exactly the effect I tried to achieve. Unfortunately, my way was wrong.


@3010996

3010996
26 Feb 2019, 12:22

It seems that the code works, however, not as I wanted. The SMA indicator is drawn in the chart only from the moment when the "showSMA" condition is true, not in the entire chart.

Is there any way to show and hide the working indicator with a code?


@3010996

3010996
26 Feb 2019, 10:44

RE:

Panagiotis Charalampous said:

Hi lw3010996,

The reason you see nothing is because showSMA is always false.

Best Regards,

Panagiotis

After 5 seconds I want to display Clock and SMA for 15 seconds. The clock displays correctly the SMA does not. The "showSMA" condition is true for 15 seconds.

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


namespace cAlgo
{
    [Indicator(IsOverlay = true, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class TEST2 : Indicator
    {

        private bool showSMA;
        private int i;

        public int redPeriod = 12;
        [Output("Red SMA", LineColor = "Red", Thickness = 1)]
        public IndicatorDataSeries redSMA { get; set; }


        private MovingAverage _redSMA;


        protected override void Initialize()
        {
            Timer.TimerTick += OnTimerTick;
            Timer.Start(1);

            _redSMA = Indicators.MovingAverage(MarketSeries.Close, redPeriod, MovingAverageType.Simple);
        }

        public override void Calculate(int index)
        {
            if (showSMA)
            {
                ChartObjects.DrawText("Clock", Time.ToString("HH:mm:ss"), StaticPosition.TopLeft);
                redSMA[index] = _redSMA.Result[index];

            }
            else
            {
                Chart.RemoveObject("Clock");
            }
        }

        private void OnTimerTick()
        {
            i++;

            if (i > 5 && i < 20)
            {
                showSMA = true;
            }
            else
            {
                showSMA = false;
            }
        }
    }
}

 


@3010996

3010996
20 Feb 2019, 17:43

RE:

Panagiotis Charalampous said:

Hi lw3010996,

Yes it is. Just set IsFilled property to true. See below

  rectangle.IsFilled = true;

Best Regards,

Panagiotis

It works great, thank you. I could not find an answer anywhere.


@3010996

3010996
20 Feb 2019, 12:50

That's right, thank you.


@3010996

3010996
30 Jan 2019, 12:19

RE:

Panagiotis Charalampous said:

Hi lw3010996,

if you remove this piece of code

             thread.Join();
             while (thread.IsAlive)
               System.Windows.Forms.Application.DoEvents();

It should work fine.

Best Regards,

Panagiotis

Thank you very much. Problem solved :)


@3010996