Category Trend  Published on 25/07/2024

Luc MAs

Description

Several averages in a single component. For those who, like me, sometimes forget the trend! :-D
There are 5 averages, the first two and the last two form a cloud, the color can't be changed on the 2nd cloud but on the first it depends on curves 1 and 2.
There's also a Belkhayate curve.

 


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

namespace cAlgo
{
    [Cloud ( "MMA 1-Cloud 1", "MMA 2-Cloud 1", Opacity = 0.4)]
    [Cloud ( "MMA 4-Cloud 2", "MMA 5-Cloud 2", Opacity = 0.17, FirstColor = "#8800BF00", SecondColor = "#B9FF3334")]
    [Indicator(IsOverlay = true, TimeZone = TimeZones.UTC, AutoRescale = false, AccessRights = AccessRights.None)]
    public class LucMAs : Indicator
    {
        [Parameter("Source")]
        public DataSeries Source { get; set; }

        [Output("MMA 1-Cloud 1", LineColor = "#8800BF00")]
        public IndicatorDataSeries mma1 { get; set; }
        [Parameter("Period 1", DefaultValue = 21, MinValue = 1, Group="MMA")]
        public int pmma1 { get; set; }
        [Parameter("Type 1", DefaultValue = typeMMA.Exponential, Group = "MMA")]
        public typeMMA mma1_type { get; set; }
        [Output("MMA 2-Cloud 1", LineColor = "#B9FF3334")]
        public IndicatorDataSeries mma2 { get; set; }
        [Parameter("Period 2", DefaultValue = 55, MinValue = 1, Group="MMA")]
        public int pmma2 { get; set; }
        [Parameter("Type 2", DefaultValue = typeMMA.Exponential, Group = "MMA")]
        public typeMMA mma2_type { get; set; }
        [Output("MMA 3", LineColor = "#FF40E0D0")]
        public IndicatorDataSeries mma3 { get; set; }
        [Parameter("Period 3", DefaultValue = 20, MinValue = 1, Group="MMA")]
        public int pmma3 { get; set; }
        [Parameter("Type 3", DefaultValue = typeMMA.Simple, Group = "MMA")]
        public typeMMA mma3_type { get; set; }
        [Output("MMA 4-Cloud 2", LineColor = "#FFFED966", LineStyle = LineStyle.Lines)]
        public IndicatorDataSeries mma4 { get; set; }
        [Parameter("Period 4", DefaultValue = 50, MinValue = 1, Group="MMA")]
        public int pmma4 { get; set; }
        [Parameter("Type 4", DefaultValue = typeMMA.Smoothed, Group = "MMA")]
        public typeMMA mma4_type { get; set; }
        [Output("MMA 5-Cloud 2", LineColor = "#FFFF7600", LineStyle = LineStyle.Lines)]
        public IndicatorDataSeries mma5 { get; set; }
        [Parameter("Period 5", DefaultValue = 200, MinValue = 1, Group="MMA")]
        public int pmma5 { get; set; }
        [Parameter("Type 5", DefaultValue = typeMMA.Smoothed, Group = "MMA")]
        public typeMMA mma5_type { get; set; }
        
        [Parameter("WMA length", DefaultValue = 30, MinValue = 1, Group = "Belkhayate")]
        public int perioddBelkhayate { get; set; }
        
        [Output("Belkhayate Line Up Trend", LineColor = "Green", PlotType = PlotType.DiscontinuousLine, Thickness = 2)]
        public IndicatorDataSeries BelkhayateUp { get; set; }
        [Output("Belkhayate Line Down Trend", LineColor = "Red", PlotType = PlotType.DiscontinuousLine, Thickness = 2)]
        public IndicatorDataSeries BelkhayateDown { get; set; }
        [Output("Belkhayate Line Neutral Trend", LineColor = "Blue", PlotType = PlotType.DiscontinuousLine, Thickness = 2)]
        public IndicatorDataSeries BelkhayateNeutral { get; set; }
        
        private MovingAverage _mma1, _mma2, _mma3, _mma4, _mma5;
        private WeightedMovingAverage _belkhayate;
        
        public enum typeMMA {
            Simple, Smoothed, Exponential, Weighted
        }
        
        public MovingAverageType GetMMAType(typeMMA type)
        {
            switch(type)
            {
                case typeMMA.Simple:
                    return MovingAverageType.Simple;
                case typeMMA.Exponential:
                    return MovingAverageType.Exponential;
                case typeMMA.Weighted:
                    return MovingAverageType.Weighted;
                default:
                    return MovingAverageType.Simple;
            }
        }
        
        protected override void Initialize()
        {
            _belkhayate = Indicators.WeightedMovingAverage(Bars.ClosePrices, perioddBelkhayate);
            
            _mma1 = Indicators.MovingAverage(Bars.ClosePrices, pmma1, GetMMAType(mma1_type));
            _mma2 = Indicators.MovingAverage(Bars.ClosePrices, pmma2, GetMMAType(mma2_type));
            _mma3 = Indicators.MovingAverage(Bars.ClosePrices, pmma3, GetMMAType(mma3_type));
            _mma4 = Indicators.MovingAverage(Bars.ClosePrices, pmma4, GetMMAType(mma4_type));
            _mma5 = Indicators.MovingAverage(Bars.ClosePrices, pmma5, GetMMAType(mma5_type));
        }
        
        public void CalculSMMA(int index, IndicatorDataSeries x, int Periods)
        {
            if (index <= Periods) return;
            
            if (double.IsNaN(x[index-1]))
            {
                var sum = 0.0;
                for (var i = index-Periods; i < index; i++)
                    sum += Source[i];
                x[index-1] = sum / Periods;
            }
            
            x[index] = (x[index-1] * (Periods - 1) + Source[index]) / Periods;
        }
        
        public double getRange()
        {
            double AvgRange = 0.0;
                  
            for(int x = 11; x > 0; x--)
            {
             AvgRange += Math.Abs(Bars.HighPrices.Last(x) - Bars.LowPrices.Last(x));
            }
            
            return AvgRange/10.0;
        }

        public override void Calculate(int index)
        {
                     
            // MMA
            if (mma1_type == typeMMA.Smoothed)
                CalculSMMA(index, mma1, pmma1);
            else
                mma1[index] = _mma1.Result[index];
            if (mma2_type == typeMMA.Smoothed)
                CalculSMMA(index, mma2, pmma2);
            else
                mma2[index] = _mma2.Result[index];
            if (mma3_type == typeMMA.Smoothed)
                CalculSMMA(index, mma3, pmma3);
            else
                mma3[index] = _mma3.Result[index];
            if (mma4_type == typeMMA.Smoothed)
                CalculSMMA(index, mma4, pmma4);
            else
                mma4[index] = _mma4.Result[index];
            if (mma5_type == typeMMA.Smoothed)
                CalculSMMA(index, mma5, pmma5);
            else
                mma5[index] = _mma5.Result[index];
                
            if (index > 1)
            {
                if (_belkhayate.Result[index] > _belkhayate.Result[index-1])
                {
                    BelkhayateUp[index] = _belkhayate.Result[index];
                    if (double.IsNaN(BelkhayateUp[index-1]))
                        if (!double.IsNaN(BelkhayateDown[index-1]))
                            BelkhayateUp[index-1] = BelkhayateDown[index-1];
                        else
                            BelkhayateUp[index-1] = BelkhayateNeutral[index-1];
                }
                else
                if (_belkhayate.Result[index] < _belkhayate.Result[index-1])
                {
                    BelkhayateDown[index] = _belkhayate.Result[index];
                    if (double.IsNaN(BelkhayateDown[index-1]))
                        if (!double.IsNaN(BelkhayateUp[index-1]))
                            BelkhayateDown[index-1] = BelkhayateUp[index-1];
                        else
                            BelkhayateDown[index-1] = BelkhayateNeutral[index-1];
                }
                else
                {
                    BelkhayateNeutral[index] = _belkhayate.Result[index];
                    if (double.IsNaN(BelkhayateNeutral[index-1]))
                        if (!double.IsNaN(BelkhayateUp[index-1]))
                            BelkhayateNeutral[index-1] = BelkhayateUp[index-1];
                        else
                            BelkhayateNeutral[index-1] = BelkhayateDown[index-1];
                }
            }
            
        }
    }
}

LB
lbellego

Joined on 20.06.2024

  • Distribution: Free
  • Language: C#
  • Trading platform: cTrader Automate
  • File name: Luc MAs_withSourceCode.algo
  • Rating: 0
  • Installs: 410
  • Modified: 25/07/2024 14:56
Comments
Log in to add a comment.
No comments found.