Category Oscilators  Published on 03/04/2023

TOSC oscillator

Description

The TOSC oscillator shows the difference between the exponential moving average and the recursive trendline.


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

namespace cAlgo
{
    [Levels(0)]
    [Indicator(IsOverlay = false, AccessRights = AccessRights.None)]
    public class mTOSC : Indicator
    {
        [Parameter("Period (20)", DefaultValue = 20, MinValue = 1)]
        public int inpPeriod { get; set; }
        [Parameter("Smooth Period (7)", DefaultValue = 7, MinValue = 2)]
        public int inpPeriodSmooth { get; set; }
        [Parameter("Data Source (weighted)", DefaultValue = enumPriceTypes.Weighted)]
        public enumPriceTypes inpPriceType { get; set; }

        [Output("TOSC", LineColor = "Black", PlotType = PlotType.Line, Thickness = 1)]
        public IndicatorDataSeries outTOSC { get; set; }
        [Output("FIR", LineColor = "Red", PlotType = PlotType.Line, Thickness = 1)]
        public IndicatorDataSeries outFIR { get; set; }

        private MovingAverage _sma, _ema;
        private IndicatorDataSeries _price, _tmp0, _tmp1, _tosc, _fir;
        private double alpha;


        protected override void Initialize()
        {
            alpha = (2.0/((double)inpPeriod + 1));
            _price = CreateDataSeries();
            _sma = Indicators.MovingAverage(_price, 1, MovingAverageType.Simple);
            _ema = Indicators.MovingAverage(_price, inpPeriod, MovingAverageType.Exponential);
            _tmp0 = CreateDataSeries();
            _tmp1 = CreateDataSeries();
            _tosc = CreateDataSeries();
            _fir = CreateDataSeries();
        }

        public override void Calculate(int i)
        {
            switch (inpPriceType)
            {
                case enumPriceTypes.Open:
                    _price[i] = Bars.OpenPrices[i];
                    break;
                case enumPriceTypes.Close:
                    _price[i] = Bars.ClosePrices[i];
                    break;
                case enumPriceTypes.High:
                    _price[i] = Bars.HighPrices[i];
                    break;
                case enumPriceTypes.Low:
                    _price[i] = Bars.LowPrices[i];
                    break;
                case enumPriceTypes.Median:
                    _price[i] = Bars.MedianPrices[i];
                    break;
                case enumPriceTypes.Typical:
                    _price[i] = Bars.TypicalPrices[i];
                    break;
                case enumPriceTypes.Weighted:
                    _price[i] = Bars.WeightedPrices[i];
                    break;
                default:
                    _price[i] = Bars.ClosePrices[i];
                    break;
            }
        
            _tmp0[i] = i>inpPeriod ? (1 - alpha) * _tmp0[i-1] + _sma.Result[i] : _sma.Result[i];
            _tmp1[i] = i>inpPeriod ? (1 - alpha) * _tmp1[i-1] + alpha * (_sma.Result[i] + _tmp0[i] - _tmp0[i-1]) : alpha * (_sma.Result[i] + _tmp0[i]);
            _tosc[i] = (_tmp1[i] - _ema.Result[i]) / Symbol.TickSize;
            _fir[i] = i>4 ? (_tosc[i] + 2.0 * _tosc[i-1] + 2.0 * _tosc[i-2] + _tosc[i-3]) / 6.0 : _tosc[i];
            
            outTOSC[i] = _tosc[i];
            outFIR[i] = _fir[i];
        }
    }
    
    public enum enumPriceTypes
    {
        Open,
        Close,
        High,
        Low,
        Median,
        Typical,
        Weighted
    }
}


mfejza's avatar
mfejza

Joined on 25.01.2022

  • Distribution: Free
  • Language: C#
  • Trading platform: cTrader Automate
  • File name: mTOSC.algo
  • Rating: 5
  • Installs: 387
Comments
Log in to add a comment.
No comments found.