Category Trend  Published on 04/09/2021

TX Color

Description

This is a generalized version of the T3 indciator by Tim Tillson. It is based on an indicator published by user jani:

In this version it is possible to set any order since the coefficients are caluculated based on the binomial formula. Setting the order to 6 will produce the same result as the orginal indicator by Tim Tillson.


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

using System.IO;
namespace cAlgo.Indicators
{

    [Indicator(IsOverlay = true, AccessRights = AccessRights.None)]

    public class TXMA : Indicator
    {

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

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

        [Parameter("Order ", DefaultValue = 6, MinValue = 3)]
        public int Order { get; set; }

        [Parameter("Volume Factor ", DefaultValue = 0.7)]
        public double Volume_Factor { get; set; }

        [Parameter("Show Direction ", DefaultValue = true)]
        public bool showDirection { get; set; }


        [Parameter("Min Threshold ", DefaultValue = 0.0, Step = 0.001, MinValue = 0)]
        public double minthr { get; set; }

        [Parameter("Start Bar ", DefaultValue = 1, MinValue = 0)]
        public int startbar { get; set; }

        [Parameter("Stop Bar ", DefaultValue = 2, MinValue = 0)]
        public int stopbar { get; set; }


        [Output("TXMA steady", LineColor = "Gray", PlotType = PlotType.DiscontinuousLine, Thickness = 3)]
        public IndicatorDataSeries steady { get; set; }

        [Output("TXMA rise", LineColor = "Green", PlotType = PlotType.DiscontinuousLine, Thickness = 3)]
        public IndicatorDataSeries rise { get; set; }

        [Output("TXMA fall", LineColor = "Red", PlotType = PlotType.DiscontinuousLine, Thickness = 3)]
        public IndicatorDataSeries fall { get; set; }

        private ExponentialMovingAverage[] ema;
        private double[] coeff;


        protected override void Initialize()
        {
            ema = new ExponentialMovingAverage[Order];

            ema[0] = Indicators.ExponentialMovingAverage(Source, Period);

            for (int i = 1; i < Order; i++)
            {
                ema[i] = Indicators.ExponentialMovingAverage(ema[i - 1].Result, Period);
            }

            double a, b;

            b = Volume_Factor;
            a = 1.0 + b;

            coeff = new double[Order - 2];

            for (int i = 0; i < Order - 2; i++)
            {
                coeff[i] = binomialCoeff(Order - 3, i) * Math.Pow(a, Order - 3 - i) * Math.Pow(-b, i);
            }
        }


        public override void Calculate(int index)
        {
            double val = maketema(index);

            if (showDirection)
            {
                double avg = averageslope(startbar, stopbar, index);

                int mode = (avg > minthr) ? 1 : (avg < -minthr) ? -1 : 0;

                rise[index] = (mode == 1) ? val : double.NaN;
                fall[index] = (mode == -1) ? val : double.NaN;
            }

            steady[index] = val;
        }


        double averageslope(int start, int end, int index)
        {

            var sum = 0.0;
            var count = 0;

            for (var i = index - end; i < index - start; i++)
            {
                var p0 = maketema(i + 1);
                var p1 = maketema(i);
                var per = (p0 - p1) / p0 * 100;

                sum += per;
                count++;
            }
            return sum / count;
        }


        double maketema(int index)
        {
            double result = 0.0;

            for (int i = 0; i < Order - 2; i++)
            {
                result += coeff[i] * ema[i + 2].Result[index];
            }

            return result;

        }

        // Returns value of Binomial Coefficient C(n, k)
        int binomialCoeff(int n, int k)
        {
            // Base Cases
            if (k > n)
                return 0;
            if (k == 0 || k == n)
                return 1;

            // Recur
            return binomialCoeff(n - 1, k - 1) + binomialCoeff(n - 1, k);
        }
    }
}


CW
cW22Trader

Joined on 16.03.2021

  • Distribution: Free
  • Language: C#
  • Trading platform: cTrader Automate
  • File name: TX Colors.algo
  • Rating: 0
  • Installs: 923
Comments
Log in to add a comment.
AL
aloisioroq · 5 months ago

Estou necessitando de um seguinte código  para colorir as velas do gráfico nas cores verde ou vermelho, de acordo com determinadas condições. Para estabelcer estas condições vou utilizar três médias Tilson de períodos diferentes. 
Vou utilizar trêsmédias Tilson de períodos diferentes, com os seguintes parâmetros:
T3MAfast: Período 6, fator 0.70
T3MAmid: Período 9, fator 0.70
T3MAslow: Período 12, fator 0.70
Quero que estes parâmetros possam ser modificados pelo usuário quando o indicador for adicionado ao gráfico. As condições para a coloração das velas do gráfico são as seguintes:
Velas na cor verde:
1ª condição: T3MAfast, T3MAmid e T3MAslow, todas subindo, ou seja, o valor atual das três médicas devem ser maiores que os seus valores anteriores;
2ª condição: T3MAfast subindo e T3MAmid e T3MAslow descendo;
3ª condição: T3MAfast e T3MAmid subindo e T3MAslow descendo;
4ª condição: T3MAfast e T3MAslow subindo e T3MAmid descendo;
Velas na cor vermelha:
1ª condição: T3MAfast, T3MAmid e T3MAslow, todas descendo, ou seja, o valor atual das três médicas devem ser nenores que os seus valores anteriores;
2ª condição: T3MAfast descendo e T3MAmid e T3MAslow subindo;
3ª condição: T3MAfast e T3MAmid descendo e T3MAslow subindo;
4ª condição: T3MAfast e T3MAslow descendo e T3MAmid subindo;

Por fim, quero que o código permita que as cores com as quais as velas serão coloridas possam ser modificadas pelo usuário quando o indicador for adicionado ao gráfico.