Category Oscilators  Published on 22/05/2024

Multi Timeframe Stochastic Oscillator

Description

Indicator that calculates the Stochastic Oscillator for specified timeframe (it can be different from chart's timeframe).


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

namespace MultiTimeframeStoch;

[Indicator(IsOverlay = false, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
public class MultiTimeframeStoch : Indicator
{
    [Parameter("%K Periods", DefaultValue = 14, MinValue = 1)]
    public int KPeriods { get; set; }

    [Parameter("%K Slowing", DefaultValue = 3, MinValue = 1)]
    public int KSlowing { get; set; }

    [Parameter("%D Periods", DefaultValue = 14, MinValue = 1)]
    public int DPeriods { get; set; }

    [Parameter("MA Type", DefaultValue = MovingAverageType.WilderSmoothing)]
    public MovingAverageType MaType { get; set; }

    [Parameter("Timeframe", DefaultValue = "Daily")]
    public TimeFrame Timeframe { get; set; }

    [Output("%D", LineColor = "Red", LineStyle = LineStyle.Lines)]
    public IndicatorDataSeries PercentD { get; set; }

    [Output("%K", LineColor = "Green")]
    public IndicatorDataSeries PercentK { get; set; }

    private StochasticOscillator _stochasticOscillator;

    protected override void Initialize()
    {
        var bars = MarketData.GetBars(Timeframe);
        _stochasticOscillator = Indicators.StochasticOscillator(
            bars,
            KPeriods,
            KSlowing,
            DPeriods,
            MaType);
    }

    public override void Calculate(int index)
    {
        var stochIndex = GetIndexByDate(Bars.OpenTimes[index]);
        if (stochIndex is not -1)
        {
            PercentD[index] = _stochasticOscillator.PercentD[stochIndex];
            PercentK[index] = _stochasticOscillator.PercentK[stochIndex];
        }
    }

    private int GetIndexByDate(DateTime date)
    {
        var bars = MarketData.GetBars(Timeframe);
        for (var i = bars.OpenTimes.Count - 1; i >= 0; i--)
        {
            if (bars.OpenTimes[i] <= date)
            {
                return i;
            }
        }

        return -1;
    }
}

MA
matturczynski

Joined on 28.05.2023

  • Distribution: Free
  • Language: C#
  • Trading platform: cTrader Automate
  • File name: MultiTimeframeStoch.algo
  • Rating: 5
  • Installs: 273
Comments
Log in to add a comment.
SO
souksanhcopter · 1 month ago

Thank you so much