Category Oscilators  Published on 04/01/2024

ESPF

Description

ESPF


using cAlgo.API;
using System;
using System.Linq;

namespace cAlgo
{
    [Indicator(IsOverlay = false, TimeZone = TimeZones.UTC, AccessRights = AccessRights.FullAccess)]
    public class ESPF : Indicator
    {
        [Parameter("Source")]
        public DataSeries Source { get; set; }

        [Parameter("Fast Length", DefaultValue = 40)]
        public double FastLength { get; set; }

        [Parameter("Slow Length", DefaultValue = 60)]
        public double SlowLength { get; set; }

        [Parameter("Load Data", DefaultValue = false)]
        public bool LoadData { get; set; }

        [Output("RMS+")]
        public IndicatorDataSeries RMSPlus { get; set; }

        [Output("RMS-")]
        public IndicatorDataSeries RMSMinus { get; set; }

        [Output("ESPF")]
        public IndicatorDataSeries ESPFResult { get; set; }

        protected override void Initialize()
        {
           // if (Account.BrokerName == "Skilling")
            {
                if (RunningMode == RunningMode.RealTime)
                    while (Bars.Count < SlowLength && LoadData)
                        Bars.LoadMoreHistory();
                Print("Bars Count: " + Bars.Count);
            }
            //else
            //{
            //    Chart.DrawStaticText("Text", "This indicator works only with our preferred broker. Please click to open an account on the indicator's page in ctrader.com", VerticalAlignment.Center,HorizontalAlignment.Center, Color.Red);
            // //   System.Diagnostics.Process.Start("https://bit.ly/3Cppal6");
            //}
            // Initialize and create nested indicators
        }

        public override void Calculate(int index)
        {
        //    if (Account.BrokerName == "Skilling")
            {
                // Calculate value at specified index
                var a1 = 5.0 / FastLength;
                var a2 = 5.0 / SlowLength;
                if (index > 1)
                {
                    ESPFResult[index] = (a1 - a2) * Source[index] + (a2 * (1 - a1) - a1 * (1 - a2)) * Source[index - 1] + ((1 - a1) + (1 - a2)) * (ESPFResult[index - 1]) - ((1 - a1) * (1 - a2)) * (ESPFResult[index - 2]);
                }
                else
                {
                    ESPFResult[index] = 0;
                }
                if (ESPFResult.Count > 1)
                {
                    RMSPlus[index] = RootMeanSquare(ESPFResult);
                    RMSMinus[index] = -RMSPlus[index];
                }
                if (!IsLastBar)
                    Print("ESPF : " + ESPFResult[index]);
            }
        }

        public double RootMeanSquare(IndicatorDataSeries source)
        {
            if (source.Count() < 2)
                throw new InvalidOperationException("Source must have at least 2 elements");

            double s = source.Skip(Math.Max(0, source.Count() - 50)).Take(50).Aggregate(0.0, (x, d) => x += Math.Pow(d, 2));

            return Math.Sqrt(s / 50);
        }
    }
}


PanagiotisChar's avatar
PanagiotisChar

Joined on 15.09.2022

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