Category Trend  Published on 18/05/2023

Moving Average Cross Over

Description

This is a simple Indicator based on two Moving Averages, that you can choose between many of them made available natively from Ctrader.

The Indicator changes the colors of the Clouds depending on the Trend and it paints the Candles as well based on the Trend.

There was a little error in my code, and i just uploaded it without noticing it. The indicator was asked from some members our group

and i did it fast without having a better look into it. Here please delete the old one and install this one.

 

You can find me by joyining this Telegram Group http://t.me/cTraderCoders

Grupo de Telegram Para Brasileiros, Portugueses e todos aqueles que falam portugues:http://t.me/ComunidadeCtrader

Grupo CTrader en Español para Latinos:  http://t.me/ComunidadCtrader

 

 


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

namespace cAlgo
{
    [Cloud("Fast MA", "Slow MA",  FirstColor = "LawnGreen")]

    [Indicator(AccessRights = AccessRights.None, IsOverlay = true)]
    public class MovingAverageCrossOverExperto : Indicator
    {
        [Parameter("Fast Period", DefaultValue = "45")]
        public int fastPeriod{ get; set; }
        
        [Parameter("Slow Period", DefaultValue = "100")]
        public int slowPeriod{ get; set; }
        
        [Parameter("Moving Average Type", DefaultValue = MovingAverageType.WilderSmoothing)]
        public MovingAverageType MaType { get; set; }

        [Output("Fast MA", LineColor = "LawnGreen")]
        public IndicatorDataSeries Fast_MA { get; set; }
        
        [Output("Slow MA", LineColor = "Coral")]
        public IndicatorDataSeries Slow_MA { get; set; }
        
        public MovingAverage SlowMa;
        
        public MovingAverage FastMa;

        protected override void Initialize()
        {
            FastMa = Indicators.MovingAverage(Bars.ClosePrices, fastPeriod, MaType);
            
            SlowMa = Indicators.MovingAverage(Bars.ClosePrices, slowPeriod, MaType);
        }

        public override void Calculate(int index)
        {
            Fast_MA[index] = FastMa.Result[index];
            Slow_MA[index] = SlowMa.Result[index];
            
            if(Bars.ClosePrices[index] >= FastMa.Result[index])
            {
                if(Bars.ClosePrices[index] >= Bars.OpenPrices[index])
                    Chart.SetBarColor(index, Color.LawnGreen);
                else if(Bars.ClosePrices[index] < Bars.OpenPrices[index])
                    Chart.SetBarColor(index, Color.Green);
            }
             else if(Bars.ClosePrices[index] < FastMa.Result[index])
            {
                if(Bars.ClosePrices[index] <= Bars.OpenPrices[index])
                    Chart.SetBarColor(index, Color.Red);
                else if(Bars.ClosePrices[index] > Bars.OpenPrices[index])
                    Chart.SetBarColor(index, Color.Coral);
            }
            
            
        }
    }
}

TraderExperto's avatar
TraderExperto

Joined on 07.06.2019

  • Distribution: Free
  • Language: C#
  • Trading platform: cTrader Automate
  • File name: Moving Average Cross Over Experto.algo
  • Rating: 5
  • Installs: 2073
  • Modified: 25/10/2022 19:12
Comments
Log in to add a comment.
WE
weelmedia · 1 year ago

I think this is a fantastic article, and I really appreciate you letting me know about it stumble guys. That is precisely what I had hoped to find, and I really hope that you will keep sharing content that is of such high quality in the years to come.

TraderExperto's avatar
TraderExperto · 2 years ago

Sorry for the error in the Code! Made it in a hurry some members of one of our groups were asking it.

So i just uploaded it without ckecking if it was everything all right

 

mfejza's avatar
mfejza · 2 years ago

change rows 38 and 40, for full functionality

            FastMa = Indicators.MovingAverage(Bars.ClosePrices, fastPeriod, MaType);
             
            SlowMa = Indicators.MovingAverage(Bars.ClosePrices, slowPeriod, MaType);