Category Oscilators  Published on 19/05/2023

Parabolic SAR oscillator

Description

The Parabolic SAR indicator is transformed into an oscillator by displaying the simple smoothed difference between the PSAR indicator and price.

In this version, a positive difference value in the indicator indicates a bullish market sentiment, while a negative difference value indicates a bearish market sentiment.


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 mPSARosc : Indicator
    {
        [Parameter("PSAR Step (0.02)", DefaultValue = 0.02)]
        public double inpStep { get; set; }
        [Parameter("PSAR Max (0.2)", DefaultValue = 0.2)]
        public double inpMax { get; set; }
        [Parameter("Smooth Period (7)", DefaultValue = 7)]
        public int inpSmoothPeriod { get; set; }

        [Output("PSAR Osc Positive", LineColor = "Green", PlotType = PlotType.Histogram, Thickness = 3, LineStyle = LineStyle.Solid)]
        public IndicatorDataSeries outPSARoscPositive { get; set; }
        [Output("PSAR Osc Negative", LineColor = "Red", PlotType = PlotType.Histogram, Thickness = 3, LineStyle = LineStyle.Solid)]
        public IndicatorDataSeries outPSARoscNegative { get; set; }
        
        private ParabolicSAR _psar;
        private IndicatorDataSeries _raw, _psaroscpos, _psaroscneg;
        private MovingAverage _smoothaverage;

        protected override void Initialize()
        {
            _psar = Indicators.ParabolicSAR(inpStep, inpMax);
            _raw = CreateDataSeries();
            _psaroscpos = CreateDataSeries();
            _psaroscneg = CreateDataSeries();
            _smoothaverage = Indicators.MovingAverage(_raw, inpSmoothPeriod, MovingAverageType.Simple);
        }

        public override void Calculate(int i)
        {
            _raw[i] = Bars.ClosePrices[i] - _psar.Result[i];
            _psaroscpos[i] = i>inpSmoothPeriod && _smoothaverage.Result[i] > _smoothaverage.Result[i-1] ? _smoothaverage.Result[i] : 0;
            _psaroscneg[i] = i>inpSmoothPeriod && _smoothaverage.Result[i] < _smoothaverage.Result[i-1] ? _smoothaverage.Result[i] : 0;
            
            outPSARoscPositive[i] = _psaroscpos[i];
            outPSARoscNegative[i] = _psaroscneg[i];
        }
    }
}

mfejza's avatar
mfejza

Joined on 25.01.2022

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