Category Oscilators  Published on 03/07/2024

Scored Ma MultiTimeFrame

Description

This indicator uses the scoring strategy to define a signal. If a bar is above a moving average (MA), the score is +1, and if a bar is below a moving average (MA), the score is -1. This analysis is applied to 3 different moving averages and 7 different timeframes.

Output:

  • 3 different results, each result corresponding to a specific moving average across 7 different timeframes.
  • 1 average of the 3 different results.
  • 1 trend indicator that determines if all results are above or below 0. 


Enjoy for Free =) 

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

 


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
{
    [Indicator(AccessRights = AccessRights.None)]
    public class ScoredMa : Indicator
    {
        [Parameter("Timeframe 1", DefaultValue = "Minute", Group = "TimeFrame Setting")]
        public TimeFrame Timeframe1 { get; set; }
        [Parameter("Timeframe 1", DefaultValue = "Minute5", Group = "TimeFrame Setting")]
        public TimeFrame Timeframe2 { get; set; }
        [Parameter("Timeframe 1", DefaultValue = "Minute10", Group = "TimeFrame Setting")]
        public TimeFrame Timeframe3 { get; set; }
        [Parameter("Timeframe 1", DefaultValue = "Minute15", Group = "TimeFrame Setting")]
        public TimeFrame Timeframe4 { get; set; }
        [Parameter("Timeframe 1", DefaultValue = "Minute30", Group = "TimeFrame Setting")]
        public TimeFrame Timeframe5 { get; set; }
        [Parameter("Timeframe 1", DefaultValue = "Hour", Group = "TimeFrame Setting")]
        public TimeFrame Timeframe6 { get; set; }
        [Parameter("Timeframe 1", DefaultValue = "Hour4", Group = "TimeFrame Setting")]
        public TimeFrame Timeframe7 { get; set; }

        [Parameter("Period", DefaultValue = 21, Group = "Ma 1 Setting")]
        public int PeriodMa1 { get; set; }
        [Parameter("Ma Type", DefaultValue = 21, Group = "Ma 1 Setting")]
        public MovingAverageType MaTypeMa1 { get; set; }
        [Parameter("Period ", DefaultValue = 50, Group = "Ma 2 Setting")]
        public int PeriodMa2 { get; set; }
        [Parameter("Ma Type", DefaultValue = 21, Group = "Ma 2 Setting")]
        public MovingAverageType MaTypeMa2 { get; set; }
        [Parameter("Period", DefaultValue = 200, Group = "Ma 3 Setting")]
        public int PeriodMa3 { get; set; }
        [Parameter("Ma Type", DefaultValue = 21, Group = "Ma 3 Setting")]
        public MovingAverageType MaTypeMa3 { get; set; }

        [Output("Result Ma 1", LineColor = "Lime")]
        public IndicatorDataSeries ResultMa1 { get; set; }
        [Output("Result Ma 2", LineColor = "DeepSkyBlue")]
        public IndicatorDataSeries ResultMa2 { get; set; }
        [Output("Result Ma 3", LineColor = "Red")]
        public IndicatorDataSeries ResultMa3 { get; set; }

        [Output("Average", LineColor = "White")]
        public IndicatorDataSeries Average { get; set; }

        [Output("Trend Mid", PlotType = PlotType.DiscontinuousLine, LineColor = "Gray")]
        public IndicatorDataSeries TrendMid { get; set; }
        [Output("Trend Up", PlotType = PlotType.DiscontinuousLine, LineColor = "Lime")]
        public IndicatorDataSeries TrendUp { get; set; }
        [Output("Trend Down", PlotType = PlotType.DiscontinuousLine, LineColor = "Red")]
        public IndicatorDataSeries TrendDown { get; set; }


        private MovingAverage[] ema1, ema2, ema3;
        private Bars[] barsTF;
        private int[] indexTF;
        private IndicatorDataSeries[] mas1, mas2, mas3;

        protected override void Initialize()
        {
            ema1 = new MovingAverage[7];
            ema2 = new MovingAverage[7];
            ema3 = new MovingAverage[7];

            barsTF = new Bars[7];
            barsTF[0] = MarketData.GetBars(Timeframe1);
            barsTF[1] = MarketData.GetBars(Timeframe2);
            barsTF[2] = MarketData.GetBars(Timeframe3);
            barsTF[3] = MarketData.GetBars(Timeframe4);
            barsTF[4] = MarketData.GetBars(Timeframe5);
            barsTF[5] = MarketData.GetBars(Timeframe6);
            barsTF[6] = MarketData.GetBars(Timeframe7);

            indexTF = new int[7];
            mas1 = new IndicatorDataSeries[7];
            mas2 = new IndicatorDataSeries[7];
            mas3 = new IndicatorDataSeries[7];

            for (int i = 0; i < 7; i++)
            {
                if (!IsBacktesting)
                {

                    while (barsTF[i].OpenTimes[0] > Bars.OpenTimes[0])
                        barsTF[i].LoadMoreHistory();
                }
                ema1[i] = Indicators.MovingAverage(barsTF[i].ClosePrices, PeriodMa1, MaTypeMa1);
                ema2[i] = Indicators.MovingAverage(barsTF[i].ClosePrices, PeriodMa2, MaTypeMa2);
                ema3[i] = Indicators.MovingAverage(barsTF[i].ClosePrices, PeriodMa3, MaTypeMa3);

                mas1[i] = CreateDataSeries();
                mas2[i] = CreateDataSeries();
                mas3[i] = CreateDataSeries();
            }
        }

        public override void Calculate(int index)
        {
            double count1 = 0;
            double count2 = 0;
            double count3 = 0;
            for (int i = 0; i < 7; i++)
            {
                indexTF[i] = barsTF[i].OpenTimes.GetIndexByTime(Bars.OpenTimes.Last(0));
                mas1[i][index] = Bars.ClosePrices.Last(0) > ema1[i].Result[indexTF[i]] ? 1
                            : Bars.ClosePrices.Last(0) < ema1[i].Result[indexTF[i]] ? -1 : 0;
                mas2[i][index] = Bars.ClosePrices.Last(0) > ema2[i].Result[indexTF[i]] ? 1
                            : Bars.ClosePrices.Last(0) < ema2[i].Result[indexTF[i]] ? -1 : 0;
                mas3[i][index] = Bars.ClosePrices.Last(0) > ema3[i].Result[indexTF[i]] ? 1
                            : Bars.ClosePrices.Last(0) < ema3[i].Result[indexTF[i]] ? -1 : 0;

                count1 += mas1[i][index];
                count2 += mas2[i][index];
                count3 += mas3[i][index];
            }
            
            ResultMa1[index] = count1;
            ResultMa2[index] = count2;
            ResultMa3[index] = count3;

            Average[index] = (ResultMa1[index] + ResultMa2[index] + ResultMa3[index]) / 3;

            TrendMid[index] = 0;
            TrendUp[index] = ResultMa1[index] > 0 && ResultMa2[index] > 0 && ResultMa3[index] > 0 ? 0 : double.NaN;
            TrendDown[index] = ResultMa1[index] < 0 && ResultMa2[index] < 0 && ResultMa3[index] < 0 ? 0 : double.NaN;
        }
    }
}

YE
YesOrNot2

Joined on 17.05.2024

  • Distribution: Free
  • Language: C#
  • Trading platform: cTrader Automate
  • File name: Scored Ma.algo
  • Rating: 5
  • Installs: 460
  • Modified: 03/07/2024 22:02
Comments
Log in to add a comment.
No comments found.