Plotting averages on the price chart.

Created at 14 Feb 2021, 17:08
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!
RU

rubensqueiroz.queiroz

Joined 13.02.2021

Plotting averages on the price chart.
14 Feb 2021, 17:08


Hello, I would like help with the following plot:
EMA 21 (close) calculated on price
EMA 3 (close) calculated on EMA 21 (close)

I can build a code but I can't plot.
If anyone can help me I am grateful!

Follow code:

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

namespace cAlgo
{
    [Indicator(override void = true, TimeZone = TimeZones.UTC, AccessRights = AccessRights)]
    public class BOLA DE CRISTAL : Indicator
    {
        private ExponentialMovingAverage _emaFast;
        private ExponentialMovingAverage _emaSlow;
        [Parameter("Data Source")]
        public DataSeries Price { get; set; }
        [Parameter("Slow Periods", DefaultValue = 21)]
        public int SlowPeriods { get; set; }
        [Output("Main", LineColor = "lime")]
        [Parameter("Fast Periods", DefaultValue = 3)]
        public int FastPeriods { get; set; }
        [Output("Main", LineColor = "red")]
        protected override void Initialize()
        {
            // initialize new instances of ExponentialMovingAverage Indicator class 
            _emaFast = Indicators.ExponentialMovingAverage(Price, FastPeriods);
            // _emaSlow is the exponential moving average of the emaFast 
            _emaSlow = Indicators.ExponentialMovingAverage(_emaFast.Result, SlowPeriods);
        }
        public override void Calculate(int index)
        {
            // If the index is less than SlowPeriods don't calculate

            if (index <= SlowPeriods)
            {
                // Print the index at which the fast ema crossed the slow ema
                Print("Fast EMA  index = {get,set}", index);

            }
        }
    }
}


@rubensqueiroz.queiroz
Replies

PanagiotisCharalampous
15 Feb 2021, 09:44

Hi there,

Here is an example of how to plot values on the chart

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

namespace cAlgo
{
    [Indicator(IsOverlay = true, TimeZone = TimeZones.UTC)]
    public class NewIndicator : Indicator
    {
        private ExponentialMovingAverage _emaFast;
        private ExponentialMovingAverage _emaSlow;
        [Parameter("Data Source")]
        public DataSeries Price { get; set; }
        [Parameter("Slow Periods", DefaultValue = 21)]
        public int SlowPeriods { get; set; }
        [Parameter("Fast Periods", DefaultValue = 3)]
        public int FastPeriods { get; set; }

        [Output("Slow")]
        public IndicatorDataSeries Slow { get; set; }

        [Output("Fast")]
        public IndicatorDataSeries Fast { get; set; }


        protected override void Initialize()
        {
            // initialize new instances of ExponentialMovingAverage Indicator class 
            _emaFast = Indicators.ExponentialMovingAverage(Price, FastPeriods);
            // _emaSlow is the exponential moving average of the emaFast 
            _emaSlow = Indicators.ExponentialMovingAverage(_emaFast.Result, SlowPeriods);
        }
        public override void Calculate(int index)
        {

            Fast[index] = _emaFast.Result[index];
            Slow[index] = _emaSlow.Result[index];
        }
    }
}

Best Regards,

Panagiotis 

Join us on Telegram


@PanagiotisCharalampous