Moving Average that change color as the candles change ?

Created at 04 Mar 2019, 05:06
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!
fernandopaivabr's avatar

fernandopaivabr

Joined 04.03.2019

Moving Average that change color as the candles change ?
04 Mar 2019, 05:06


Hi everyone

I want to create an indicator using Moving Average(MA) where it will change color as candle change showing a possible tendance. 

Example: If candle is green(buy) it will change color to green and if candle is red it will change for red. 

I'm using something like that in MT5 but I do not find it in cTrader. Please, if something like that exists in cTrader I want to know to use.

Note, in the picture below I am using a MA that change color as the candle changes. In the picture the MA is Blue for Buy and Red for Sell.

How could I to create it ?


@fernandopaivabr
Replies

PanagiotisCharalampous
04 Mar 2019, 11:34

Hi fernandopaivabr,

Thanks for posting in our forum. This is possible but it requires some effort. You could always ask for professional assistance by posting a Job or getting in touch with a Consultant.

Best Regards,

Panagiotis


@PanagiotisCharalampous

fernandopaivabr
05 Mar 2019, 02:54

RE:

Panagiotis Charalampous said:

Hi fernandopaivabr,

Thanks for posting in our forum. This is possible but it requires some effort. You could always ask for professional assistance by posting a Job or getting in touch with a Consultant.

Best Regards,

Panagiotis

I've already posted there but I can't get a solution. Could you help me ?

I'm trying to create a script to make it but I don't know how do I to calculate the MA

Please, look at my code below and if you can help me I'll be glad.

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


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

        private MovingAverage MA { get; set; }

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

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

        [Output("Default", LineColor = "#6E6E6E")]//default
        public IndicatorDataSeries Result { get; set; }

        [Output("Buy", LineColor = "#3ADF00")]//green
        public IndicatorDataSeries ResultBuy { get; set; }

        [Output("Sell", LineColor = "#FF0000")]//red
        public IndicatorDataSeries ResultSell { get; set; }

        [Parameter("MA Type", DefaultValue = MovingAverageType.Simple)]
        public MovingAverageType MaType { get; set; }

        
        protected override void Initialize()
        {
            MA = Indicators.MovingAverage(Source, Periods, MaType);

        }

        public override void Calculate(int index)
        {
            //MA default
            Result[index] = MA.Result[index];


            //MA buy green


            //MA sell red


        }

    }

}

 


@fernandopaivabr

guillermo
03 Oct 2020, 23:26

RE: RE:

fernandopaivabr said:

I've already posted there but I can't get a solution. Could you help me ?

 

Hi, 

this is the way I do it. I do not know if there is a better way, but this works for me.

 

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 = false, AccessRights = AccessRights.None)]
    public class Trinity : Indicator
    {
        [Parameter("Thickness", DefaultValue = 3, MinValue = 1, MaxValue = 5)]
        public int Thickness { get; set; }

        private WeightedMovingAverage trendWma;
        private WeightedMovingAverage slowWma;
        private HullMovingAverage fastHma;

        protected override void Initialize()
        {
            this.trendWma = Indicators.WeightedMovingAverage(Bars.ClosePrices, 500);
            this.slowWma = Indicators.WeightedMovingAverage(Bars.ClosePrices, 100);
            this.fastHma = Indicators.HullMovingAverage(Bars.ClosePrices, 30);

            for (int index = 0; index < Bars.Count; index++)
            {
                Chart.RemoveObject("trendup" + index);
                Chart.RemoveObject("trenddown" + index);

                Chart.RemoveObject("slowup" + index);
                Chart.RemoveObject("slowdown" + index);

                Chart.RemoveObject("fastup" + index);
                Chart.RemoveObject("fastdown" + index);
            }
        }

        public override void Calculate(int index)
        {
            if (index > 0)
            {
                if (this.trendWma.Result[index] > this.trendWma.Result[index - 1])
                {
                    Chart.RemoveObject("trendup" + index);
                    Chart.DrawTrendLine("trendup" + index, index - 1, this.trendWma.Result[index - 1], index, this.trendWma.Result[index], Color.Lime, this.Thickness);
                }
                if (this.trendWma.Result[index] < this.trendWma.Result[index - 1])
                {
                    Chart.RemoveObject("trenddown" + index);
                    Chart.DrawTrendLine("trenddown" + index, index - 1, this.trendWma.Result[index - 1], index, this.trendWma.Result[index], Color.Red, this.Thickness);
                }

                if (this.slowWma.Result[index] > this.slowWma.Result[index - 1])
                {
                    Chart.RemoveObject("slowup" + index);
                    Chart.DrawTrendLine("slowup" + index, index - 1, this.slowWma.Result[index - 1], index, this.slowWma.Result[index], Color.Lime, this.Thickness);
                }
                if (this.slowWma.Result[index] < this.slowWma.Result[index - 1])
                {
                    Chart.RemoveObject("slowdown" + index);
                    Chart.DrawTrendLine("slowdown" + index, index - 1, this.slowWma.Result[index - 1], index, this.slowWma.Result[index], Color.Red, this.Thickness);
                }

                if (this.fastHma.Result[index] > this.fastHma.Result[index - 1])
                {
                    Chart.RemoveObject("fastup" + index);
                    Chart.DrawTrendLine("fastup" + index, index - 1, this.fastHma.Result[index - 1], index, this.fastHma.Result[index], Color.Lime, this.Thickness);
                }
                if (this.fastHma.Result[index] < this.fastHma.Result[index - 1])
                {
                    Chart.RemoveObject("fastdown" + index);
                    Chart.DrawTrendLine("fastdown" + index, index - 1, this.fastHma.Result[index - 1], index, this.fastHma.Result[index], Color.Red, this.Thickness);
                }
            }
        }
    }
}

 


@guillermo