Information

Username: evanhallen
Member since: 12 Oct 2024
Last login: 12 Oct 2024
Status: Active

Activity

Where Created Comments
Algorithms 0 1
Forum Topics 0 0
Jobs 0 0

Last Algorithm Comments

EV
evanhallen · 1 month ago

i did use chatgpt to update this one

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

namespace cAlgo
{
    [Cloud("Upper Deviation 1", "Lower Deviation 1")]
    [Indicator(IsOverlay = true, AutoRescale = false, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class VWAPCloud : Indicator
    {
        [Parameter("TimeFrame", DefaultValue = "Daily")]
        public TimeFrame TF { get; set; }

        [Parameter("Show Interruptions", DefaultValue = false)]
        public bool ShowInterruptions { get; set; }

        [Parameter("Show Standard Deviations", Group = "Standard Deviations", DefaultValue = false)]
        public bool ShowSTDs { get; set; }

        [Parameter("First Multiplier", Group = "Standard Deviations", DefaultValue = 1)]
        public double STD1M { get; set; }

        [Parameter("Second Multiplier", Group = "Standard Deviations", DefaultValue = 2)]
        public double STD2M { get; set; }

        [Parameter("Third Multiplier", Group = "Standard Deviations", DefaultValue = 3)]
        public double STD3M { get; set; }

        [Output("Main", LineColor = "Orange", Thickness = 2, PlotType = PlotType.DiscontinuousLine)]
        public IndicatorDataSeries Result { get; set; }

        [Output("Upper Deviation 1", LineColor = "RoyalBlue", Thickness = 2, PlotType = PlotType.DiscontinuousLine)]
        public IndicatorDataSeries STD1U { get; set; }

        [Output("Lower Deviation 1", LineColor = "RoyalBlue", Thickness = 2, PlotType = PlotType.DiscontinuousLine)]
        public IndicatorDataSeries STD1D { get; set; }

        [Output("Upper Deviation 2", LineColor = "RoyalBlue", Thickness = 2, PlotType = PlotType.DiscontinuousLine)]
        public IndicatorDataSeries STD2U { get; set; }

        [Output("Lower Deviation 2", LineColor = "RoyalBlue", Thickness = 2, PlotType = PlotType.DiscontinuousLine)]
        public IndicatorDataSeries STD2D { get; set; }

        [Output("Upper Deviation 3", LineColor = "RoyalBlue", Thickness = 2, PlotType = PlotType.DiscontinuousLine)]
        public IndicatorDataSeries STD3U { get; set; }

        [Output("Lower Deviation 3", LineColor = "RoyalBlue", Thickness = 2, PlotType = PlotType.DiscontinuousLine)]
        public IndicatorDataSeries STD3D { get; set; }

        [Parameter("Build Lines", Group = "Lines", DefaultValue = false)]
        public bool BuildLines { get; set; }

        private IndicatorDataSeries tpv;
        private int lastStartIndex = 0;

        protected override void Initialize()
        {
            // Create the typical price volume series
            tpv = CreateDataSeries();
        }

        public override void Calculate(int index)
        {
            // Calculate TPV (typical price volume)
            tpv[index] = Bars.ClosePrices[index] * Bars.TickVolumes[index];

            // Determine the starting index of the new session based on timeframes
            int startIndex = GetStartIndex(index);

            // Handle interruptions between different periods
            if (startIndex > lastStartIndex && ShowInterruptions)
            {
                Chart.DrawIcon("Interruption " + startIndex, ChartIconType.Diamond, startIndex, Result[startIndex - 1], Color.Yellow);
            }

            // Calculate cumulative price-volume and volume within the session
            double cumulativeTPV = 0, cumulativeVolume = 0;

            for (int i = index; i >= startIndex; i--)
            {
                cumulativeTPV += tpv[i];
                cumulativeVolume += Bars.TickVolumes[i];
            }

            // VWAP calculation (typical price weighted by volume)
            Result[index] = cumulativeTPV / cumulativeVolume;

            // Standard deviation calculations (if enabled)
            if (ShowSTDs)
            {
                double squaredErrors = 0;

                for (int i = index; i >= startIndex; i--)
                {
                    squaredErrors += Math.Pow(Bars.ClosePrices[i] - Result[index], 2);
                }

                squaredErrors /= (index - startIndex + 1);
                squaredErrors = Math.Sqrt(squaredErrors);

                // Setting upper and lower bands for each deviation level
                STD1U[index] = squaredErrors * STD1M + Result[index];
                STD1D[index] = Result[index] - squaredErrors * STD1M;
                STD2U[index] = squaredErrors * STD2M + Result[index];
                STD2D[index] = Result[index] - squaredErrors * STD2M;
                STD3U[index] = squaredErrors * STD3M + Result[index];
                STD3D[index] = Result[index] - squaredErrors * STD3M;
            }

            // Drawing fixed lines if the option is enabled
            if (BuildLines)
            {
                Chart.DrawTrendLine("Fixed " + startIndex, startIndex, Result[startIndex - 1], index, Result[startIndex - 1], Color.Yellow);
            }
        }

        private int GetStartIndex(int index)
        {
            // Retrieve the start index based on the chosen time frame
            DateTime currentOpenTime = Bars.OpenTimes[index];

            for (int i = index; i >= 0; i--)
            {
                if (Bars.OpenTimes[i].Day != currentOpenTime.Day)
                {
                    return i + 1;
                }
            }

            return 0; // Default to zero if no change found
        }
    }
}