Category Trend  Published on 19/03/2021

ADXW

Description

ADX Wilder, an ADX Variation, created by Welles Wilder. 

 

 

 


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

namespace cAlgo
{
    [Indicator(IsOverlay = false, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class ADXW : Indicator
    {
        [Parameter("Periods", DefaultValue = 14)]
        public int ExtADXWPeriod { get; set; }


        [Output("Main")]


        public IndicatorDataSeries Result { get; set; }



        private IndicatorDataSeries ExtPDBuffer;

        private IndicatorDataSeries ExtNDBuffer;

        private IndicatorDataSeries ExtATRBuffer;

        private IndicatorDataSeries ExtPDIBuffer;

        private IndicatorDataSeries ExtNDIBuffer;

        private IndicatorDataSeries ExtPDSBuffer;

        private IndicatorDataSeries ExtNDSBuffer;

        private IndicatorDataSeries ExtDXBuffer;

        private IndicatorDataSeries ExtTRBuffer;


        protected override void Initialize()
        {
            // Initialize and create nested indicators

            ExtPDBuffer = CreateDataSeries();
            ExtNDBuffer = CreateDataSeries();
            ExtATRBuffer = CreateDataSeries();
            ExtPDIBuffer = CreateDataSeries();
            ExtNDIBuffer = CreateDataSeries();
            ExtPDSBuffer = CreateDataSeries();
            ExtNDSBuffer = CreateDataSeries();
            ExtDXBuffer = CreateDataSeries();
            ExtTRBuffer = CreateDataSeries();




        }

        public override void Calculate(int index)
        {
            // Calculate value at specified index
            // Result[index] = ...

            int i = index;

            if (Bars.Count < ExtADXWPeriod)
            {
                //ExtADXWBuffer[i] = 0;
                Result[i] = 0;
                ExtPDIBuffer[i] = 0;
                ExtNDIBuffer[i] = 0;
                ExtPDSBuffer[i] = 0;
                ExtNDSBuffer[i] = 0;
                ExtPDBuffer[i] = 0;
                ExtNDBuffer[i] = 0;
                ExtTRBuffer[i] = 0;
                ExtATRBuffer[i] = 0;
                ExtDXBuffer[i] = 0;
                return;

            }


            ExtPDIBuffer[i] = Bars.ClosePrices[i];

            DataSeries high = Bars.HighPrices;
            DataSeries low = Bars.LowPrices;
            DataSeries close = Bars.ClosePrices;

            double high_price = high[i];
            double prev_high = high[i - 1];
            double low_price = low[i];
            double prev_low = low[i - 1];
            double prev_close = close[i - 1];


            //--- fill main positive and main negative buffers
            double tmp_pos = high_price - prev_high;
            double tmp_neg = prev_low - low_price;


            if (tmp_pos < 0.0)
                tmp_pos = 0.0;
            if (tmp_neg < 0.0)
                tmp_neg = 0.0;
            if (tmp_neg == tmp_pos)
            {
                tmp_neg = 0.0;
                tmp_pos = 0.0;
            }
            else
            {
                if (tmp_pos < tmp_neg)
                    tmp_pos = 0.0;
                else
                    tmp_neg = 0.0;
            }


            ExtPDBuffer[i] = tmp_pos;
            ExtNDBuffer[i] = tmp_neg;
            //--- define TR
            double tr = Math.Max(Math.Max(Math.Abs(high_price - low_price), Math.Abs(high_price - prev_close)), Math.Abs(low_price - prev_close));

            ExtTRBuffer[i] = tr;
            // write down TR to TR buffer
            //--- fill smoothed positive and negative buffers and TR buffer




            if (i < ExtADXWPeriod)
            {
                ExtATRBuffer[i] = 0.0;
                ExtPDIBuffer[i] = 0.0;
                ExtNDIBuffer[i] = 0.0;
            }


            else
            {
                ExtATRBuffer[i] = double.IsNaN(ExtATRBuffer[i - 1]) == false ? SmoothedMA(i, ExtADXWPeriod, ExtATRBuffer[i - 1], ExtTRBuffer) : SmoothedMA(i, ExtADXWPeriod, 0, ExtTRBuffer);
                ExtPDSBuffer[i] = double.IsNaN(ExtPDSBuffer[i - 1]) == false ? SmoothedMA(i, ExtADXWPeriod, ExtPDSBuffer[i - 1], ExtPDBuffer) : SmoothedMA(i, ExtADXWPeriod, 0, ExtPDBuffer);
                ExtNDSBuffer[i] = double.IsNaN(ExtNDSBuffer[i - 1]) == false ? SmoothedMA(i, ExtADXWPeriod, ExtNDSBuffer[i - 1], ExtNDBuffer) : SmoothedMA(i, ExtADXWPeriod, 0, ExtNDBuffer);
            }
            //--- calculate PDI and NDI buffers

            //Result[i] = ExtNDSBuffer[i];

            if (ExtATRBuffer[i] != 0.0)
            {
                ExtPDIBuffer[i] = 100.0 * ExtPDSBuffer[i] / ExtATRBuffer[i];
                ExtNDIBuffer[i] = 100.0 * ExtNDSBuffer[i] / ExtATRBuffer[i];
            }
            else
            {
                ExtPDIBuffer[i] = 0.0;
                ExtNDIBuffer[i] = 0.0;
            }





            //--- Calculate DX buffer
            double dTmp = ExtPDIBuffer[i] + ExtNDIBuffer[i];
            if (dTmp != 0.0)
                dTmp = 100.0 * Math.Abs((ExtPDIBuffer[i] - ExtNDIBuffer[i]) / dTmp);
            else
                dTmp = 0.0;
            ExtDXBuffer[i] = dTmp;
            //--- fill ADXW buffer as smoothed DX buffer


            Result[i] = double.IsNaN(Result[i - 1]) == false ? SmoothedMA(i, ExtADXWPeriod, Result[i - 1], ExtDXBuffer) : SmoothedMA(i, ExtADXWPeriod, 0, ExtDXBuffer);







        }

        double SmoothedMA(int position, int period, double prev_value, IndicatorDataSeries price)
        {
            double result = 0.0;
            //--- check period
            if (period > 0 && period <= (position + 1))
            {
                if (position == period - 1)
                {
                    for (int i = 0; i < period; i++)
                        result += price[position - i];

                    result /= period;
                }

                result = (prev_value * (period - 1) + price[position]) / period;
            }

            return (result);
        }


    }
}


AN
andurei

Joined on 30.09.2020

  • Distribution: Free
  • Language: C#
  • Trading platform: cTrader Automate
  • File name: ADXW.algo
  • Rating: 0
  • Installs: 1737
  • Modified: 13/10/2021 09:55
Comments
Log in to add a comment.
No comments found.