Category Trend  Published on 04/07/2024

Multi Ma Trend v1.1

Description

This indicator allows you to add 18 MAs to the chart of a different timeframe. The periods of each MA depend on the MA Period Start * Period Multiplier.

Each MA has specific sources to help determine important pivots.

A sensitivity parameter is added to filter strong trends from weak trends.

This version is a update of : https://ctrader.com/algos/indicators/show/4354

Enjoy for Free =) 


Previous account here : https://ctrader.com/users/profile/70920
Contact telegram :  https://t.me/nimi012 

 

m :  https://t.me/nimi012 


using System;
using System.Linq;
using System.Reflection;
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 MaMultiTrendv11 : Indicator
    {
        [Parameter("TimeFrame", Group = "Initialization")]
        public TimeFrame TF1 { get; set; }
        [Parameter("High Source", DefaultValue = EnumSourceHigh.Low, Group = "Initialization")]
        public EnumSourceHigh SourceHigh { get; set; }
        public enum EnumSourceHigh
        {
            Open,
            Close,
            High,
            Low,
            Median_Price,
            Typical_Price,
            Weighted_Price,
        }
        [Parameter("Low Source", DefaultValue = EnumSourceLow.High, Group = "Initialization")]
        public EnumSourceLow SourceLow { get; set; }
        public enum EnumSourceLow
        {
            Open,
            Close,
            High,
            Low,
            Median_Price,
            Typical_Price,
            Weighted_Price,
        }

        [Parameter("Midd Source", DefaultValue = EnumSourceMidd.None, Group = "Initialization")]
        public EnumSourceMidd SourceMidd { get; set; }
        public enum EnumSourceMidd
        {
            Open,
            Close,
            High,
            Low,
            Median_Price,
            Typical_Price,
            Weighted_Price,
            None,
        }
        [Parameter("MA Period Start", DefaultValue = 13, Group = "Moving Average Setting")]
        public double MAPeriodStart { get; set; }
        [Parameter("Period Multiplicator", DefaultValue = 1.34, Group = "Moving Average Setting")]
        public double MAPeriodMulti { get; set; }
        [Parameter("MA Type", DefaultValue = MovingAverageType.Exponential, Group = "Moving Average Setting")]
        public MovingAverageType MaType { get; set; }
        [Parameter("Sensibility Ma Trend ", DefaultValue = 100, Group = "Moving Average Setting")]
        public double SensibilityMaTrend { get; set; }

        [Output("MA 1", LineColor = "Gray", PlotType = PlotType.DiscontinuousLine, LineStyle = LineStyle.Solid, Thickness = 1)]
        public IndicatorDataSeries Ma1 { get; set; }
        [Output("MA 2", LineColor = "Gray", PlotType = PlotType.DiscontinuousLine, LineStyle = LineStyle.Solid, Thickness = 1)]
        public IndicatorDataSeries Ma2 { get; set; }
        [Output("MA 3", LineColor = "Gray", PlotType = PlotType.DiscontinuousLine, LineStyle = LineStyle.Solid, Thickness = 1)]
        public IndicatorDataSeries Ma3 { get; set; }
        [Output("MA 4", LineColor = "Gray", PlotType = PlotType.DiscontinuousLine, LineStyle = LineStyle.Solid, Thickness = 1)]
        public IndicatorDataSeries Ma4 { get; set; }
        [Output("MA 5", LineColor = "Gray", PlotType = PlotType.DiscontinuousLine, LineStyle = LineStyle.Solid, Thickness = 1)]
        public IndicatorDataSeries Ma5 { get; set; }
        [Output("MA 6", LineColor = "Gray", PlotType = PlotType.DiscontinuousLine, LineStyle = LineStyle.Solid, Thickness = 1)]
        public IndicatorDataSeries Ma6 { get; set; }

        [Output("MA 1High", LineColor = "Lime", PlotType = PlotType.DiscontinuousLine, LineStyle = LineStyle.Solid, Thickness = 1)]
        public IndicatorDataSeries Ma1High { get; set; }
        [Output("MA 2High", LineColor = "Lime", PlotType = PlotType.DiscontinuousLine, LineStyle = LineStyle.Solid, Thickness = 1)]
        public IndicatorDataSeries Ma2High { get; set; }
        [Output("MA 3High", LineColor = "Lime", PlotType = PlotType.DiscontinuousLine, LineStyle = LineStyle.Solid, Thickness = 1)]
        public IndicatorDataSeries Ma3High { get; set; }
        [Output("MA 4High", LineColor = "Lime", PlotType = PlotType.DiscontinuousLine, LineStyle = LineStyle.Solid, Thickness = 1)]
        public IndicatorDataSeries Ma4High { get; set; }
        [Output("MA 5High", LineColor = "Lime", PlotType = PlotType.DiscontinuousLine, LineStyle = LineStyle.Solid, Thickness = 1)]
        public IndicatorDataSeries Ma5High { get; set; }
        [Output("MA 6High", LineColor = "Lime", PlotType = PlotType.DiscontinuousLine, LineStyle = LineStyle.Solid, Thickness = 1)]
        public IndicatorDataSeries Ma6High { get; set; }

        [Output("MA 1Low", LineColor = "Red", PlotType = PlotType.DiscontinuousLine, LineStyle = LineStyle.Solid, Thickness = 1)]
        public IndicatorDataSeries Ma1Low { get; set; }
        [Output("MA 2Low", LineColor = "Red", PlotType = PlotType.DiscontinuousLine, LineStyle = LineStyle.Solid, Thickness = 1)]
        public IndicatorDataSeries Ma2Low { get; set; }
        [Output("MA 3Low", LineColor = "Red", PlotType = PlotType.DiscontinuousLine, LineStyle = LineStyle.Solid, Thickness = 1)]
        public IndicatorDataSeries Ma3Low { get; set; }
        [Output("MA 4Low", LineColor = "Red", PlotType = PlotType.DiscontinuousLine, LineStyle = LineStyle.Solid, Thickness = 1)]
        public IndicatorDataSeries Ma4Low { get; set; }
        [Output("MA 5Low", LineColor = "Red", PlotType = PlotType.DiscontinuousLine, LineStyle = LineStyle.Solid, Thickness = 1)]
        public IndicatorDataSeries Ma5Low { get; set; }
        [Output("MA 6Low", LineColor = "Red", PlotType = PlotType.DiscontinuousLine, LineStyle = LineStyle.Solid, Thickness = 1)]
        public IndicatorDataSeries Ma6Low { get; set; }

        private IndicatorDataSeries[] res, resHigh, resLow;
        private DataSeries BarsSourceHigh, BarsSourceLow, BarsSourceMidd;
        private MovingAverage[] maHigh, maLow, maMidd;
        private int[] periods;
        private Bars bars;

        protected override void Initialize()
        {
            bars = MarketData.GetBars(TF1);

            if (!IsBacktesting)
                while (bars.OpenTimes[0] > Bars.OpenTimes[0])
                    bars.LoadMoreHistory();

            switch (SourceHigh)
            {
                case EnumSourceHigh.Open:
                    BarsSourceHigh = bars.OpenPrices;
                    break;
                case EnumSourceHigh.Close:
                    BarsSourceHigh = bars.ClosePrices;
                    break;
                case EnumSourceHigh.High:
                    BarsSourceHigh = bars.HighPrices;
                    break;
                case EnumSourceHigh.Low:
                    BarsSourceHigh = bars.LowPrices;
                    break;
                case EnumSourceHigh.Median_Price:
                    BarsSourceHigh = bars.MedianPrices;
                    break;
                case EnumSourceHigh.Typical_Price:
                    BarsSourceHigh = bars.TypicalPrices;
                    break;
                case EnumSourceHigh.Weighted_Price:
                    BarsSourceHigh = bars.WeightedPrices;
                    break;
            }
            switch (SourceLow)
            {
                case EnumSourceLow.Open:
                    BarsSourceLow = bars.OpenPrices;
                    break;
                case EnumSourceLow.Close:
                    BarsSourceLow = bars.ClosePrices;
                    break;
                case EnumSourceLow.High:
                    BarsSourceLow = bars.HighPrices;
                    break;
                case EnumSourceLow.Low:
                    BarsSourceLow = bars.LowPrices;
                    break;
                case EnumSourceLow.Median_Price:
                    BarsSourceLow = bars.MedianPrices;
                    break;
                case EnumSourceLow.Typical_Price:
                    BarsSourceLow = bars.TypicalPrices;
                    break;
                case EnumSourceLow.Weighted_Price:
                    BarsSourceLow = bars.WeightedPrices;
                    break;
            }
            switch (SourceMidd)
            {
                case EnumSourceMidd.Open:
                    BarsSourceMidd = bars.OpenPrices;
                    break;
                case EnumSourceMidd.Close:
                    BarsSourceMidd = bars.ClosePrices;
                    break;
                case EnumSourceMidd.High:
                    BarsSourceMidd = bars.HighPrices;
                    break;
                case EnumSourceMidd.Low:
                    BarsSourceMidd = bars.LowPrices;
                    break;
                case EnumSourceMidd.Median_Price:
                    BarsSourceMidd = bars.MedianPrices;
                    break;
                case EnumSourceMidd.Typical_Price:
                    BarsSourceMidd = bars.TypicalPrices;
                    break;
                case EnumSourceMidd.Weighted_Price:
                    BarsSourceMidd = bars.WeightedPrices;
                    break;
            }

            resHigh = new IndicatorDataSeries[6];
            resHigh[0] = Ma1High;
            resHigh[1] = Ma2High;
            resHigh[2] = Ma3High;
            resHigh[3] = Ma4High;
            resHigh[4] = Ma5High;
            resHigh[5] = Ma6High;

            resLow = new IndicatorDataSeries[6];
            resLow[0] = Ma1Low;
            resLow[1] = Ma2Low;
            resLow[2] = Ma3Low;
            resLow[3] = Ma4Low;
            resLow[4] = Ma5Low;
            resLow[5] = Ma6Low;

            res = new IndicatorDataSeries[6];
            res[0] = Ma1;
            res[1] = Ma2;
            res[2] = Ma3;
            res[3] = Ma4;
            res[4] = Ma5;
            res[5] = Ma6;

            maHigh = new MovingAverage[6];
            maLow = new MovingAverage[6];
            maMidd = new MovingAverage[6];
            periods = new int[6];

            for (int i = 0; i < maHigh.Length; i++)
            {
                periods[i] = (int)(Math.Round(Math.Pow(MAPeriodMulti, i) * MAPeriodStart));
                maHigh[i] = Indicators.MovingAverage(BarsSourceHigh, periods[i], MaType);
                maLow[i] = Indicators.MovingAverage(BarsSourceLow, periods[i], MaType);
                maMidd[i] = Indicators.MovingAverage(BarsSourceMidd, periods[i], MaType);
            }
        }

        public override void Calculate(int index)
        {
            int maAboveTotalHigh = 0;
            int maAboveTotalMidd = 0;
            int maAboveTotalLow = 0;
            int indexTf = bars.OpenTimes.GetIndexByTime(Bars.OpenTimes.Last(0));
            for (int i = 0; i < maHigh.Length; i++)
            {
                maAboveTotalHigh += GetNumMaAboveHigh(i, indexTf);
                maAboveTotalMidd += GetNumMaAboveMidd(i, indexTf);
                maAboveTotalLow += GetNumMaAboveLow(i, indexTf);
            }

            for (int i = 0; i < maHigh.Length; i++)
            {
                res[i][index] = SourceMidd == EnumSourceMidd.None ? double.NaN : maMidd[i].Result[indexTf];
                resHigh[i][index] = (200.0 * maAboveTotalHigh / (maHigh.Length * (maHigh.Length - 1.0))) < SensibilityMaTrend ? maHigh[i].Result[indexTf] : double.NaN;
                resLow[i][index] = (200.0 * maAboveTotalLow / (maLow.Length * (maLow.Length - 1.0))) > 100 - SensibilityMaTrend ? maLow[i].Result[indexTf] : double.NaN;
            }
        }
        private int GetNumMaAboveHigh(int reference, int indexTf)
        {
            int count = 0;
            for (int i = reference + 1; i < maHigh.Length; i++)
            {
                if (maHigh[i].Result[indexTf] > maHigh[reference].Result[indexTf])
                    count++;
            }

            return count;
        }
        private int GetNumMaAboveMidd(int reference, int indexTf)
        {
            int count = 0;
            for (int i = reference + 1; i < maMidd.Length; i++)
            {
                if (maMidd[i].Result[indexTf] > maMidd[reference].Result[indexTf])
                    count++;
            }

            return count;
        }
        private int GetNumMaAboveLow(int reference, int indexTf)
        {
            int count = 0;
            for (int i = reference + 1; i < maLow.Length; i++)
            {
                if (maLow[i].Result[indexTf] > maLow[reference].Result[indexTf])
                    count++;
            }

            return count;
        }

    }
}


YE
YesOrNot2

Joined on 17.05.2024

  • Distribution: Free
  • Language: C#
  • Trading platform: cTrader Automate
  • File name: Multi Ma Trend v1.1.algo
  • Rating: 5
  • Installs: 530
  • Modified: 04/07/2024 15:28
Comments
Log in to add a comment.
No comments found.