Chaikin Oscillator free

by gorin in category Oscilator at 05/09/2013
Description

Based on the definition of Accumulation/Distribution Index
Chaikin oscillator is formed by subtracting a 10-day exponential moving average from a 3-day exponential moving average of the accumulation/distribution index.
The formula for Accumulation/Distribution Index is:

 CLV = { (close - low) - (high - close) \over high - low }

 accdist = accdist_{prev} + volume \times CLV

Note:
This implementation may not be entirely correct. Any comments are appreciated.

Notification Publishing copyrighted material is strictly prohibited. If you believe there is copyrighted material in this section you may use the Copyright Infringement Notification form to submit a claim.
Formula / Source Code
Language: C#
Trading Platform: cAlgocTrader
// -------------------------------------------------------------------------------
//
//      The Chaikin oscillator is formed by subtracting a 10-day exponential moving average 
//      from a 3-day exponential moving average of the accumulation/distribution index.
//
// -------------------------------------------------------------------------------

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

namespace cAlgo.Indicators
{
    [Levels(0.0)]
    [Indicator("Chaikin Oscillator", IsOverlay = false, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class ChaikinOscillator : Indicator
    {
        private IndicatorDataSeries accDist;
        private ExponentialMovingAverage ema10;
        private ExponentialMovingAverage ema3;
        private MarketSeries marketSeriesDaily;


        [Output("Chaikin Oscillator")]
        public IndicatorDataSeries Result { get; set; }


        protected override void Initialize()
        {
            // Initialize and create nested indicators
            accDist = CreateDataSeries();
            ema3 = Indicators.ExponentialMovingAverage(accDist, 3);
            ema10 = Indicators.ExponentialMovingAverage(accDist, 10);

            marketSeriesDaily = MarketData.GetSeries(TimeFrame.Daily);

            

        }

        public override void Calculate(int index)
        {
            // Calculate value at specified index

            if (index < 10)
            {
                accDist[index] = 0;
                return;
            }

            if(TimeFrame != TimeFrame.Daily)
            {
                DisplayTFMessage();

                CalculateAccumulationDistribution(marketSeriesDaily, index);
                DisplayInCorrectIndex(index);

                return;
            }

            // Timeframe Daily
            CalculateAccumulationDistribution(MarketSeries, index);
            
            Result[index] = ema3.Result[index] - ema10.Result[index];

        }


        private void DisplayTFMessage()
        {
            const string errorMsg = "Please choose Daily Timeframe";
            ChartObjects.DrawText("ErrorMessage", errorMsg, StaticPosition.TopLeft, Colors.Red);
        }


        private void CalculateAccumulationDistribution(MarketSeries series, int index)
        {
            double close = series.Close[index];
            double low = series.Low[index];
            double high = series.High[index];
            double volume = marketSeriesDaily.TickVolume[index];

            if (!high.Equals(low))
            {
                double clv = ((close - low) - (high - close)) / (high - low);
                accDist[index] = accDist[index - 1] + volume * clv;
            }
            else
                accDist[index] = accDist[index - 1];

           
        }

        /// <summary>
        /// Display in correct index
        /// </summary>
        /// <param name="index"></param>
        private void DisplayInCorrectIndex(int index)
        {
            
            var indexDaily = GetIndexByDate(marketSeriesDaily, MarketSeries.OpenTime[index]);
            Print(index);
            Print(MarketSeries.OpenTime[index]);
            Print(indexDaily);

            if (indexDaily != -1)
                Result[index] = ema3.Result[indexDaily] - ema10.Result[indexDaily];
        }

        private int GetIndexByDate(MarketSeries series, DateTime time)
        {
            for (int i = series.Close.Count - 1; i > 0; i--)
            {
                if (time == series.OpenTime[i])
                    return i;
            }
            return -1;
        }

    }
}
Comments

khashayar09 - September 13, 2019 @ 21:47

hi, why ctrader have not  Accumulation/Distribution Index . please guide me.

thanks.

elucom10 - February 24, 2020 @ 16:45

I can only use this indicator on 1Day time frame, isn´t it posible to use this indicator on other time frames? Thank you.

0