Category Trend  Published on 04/03/2021

SpongeBob

Description

Jellyfishing


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

namespace cAlgo
{
    [Indicator(IsOverlay = true, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class SpongeBob : Indicator
    {
        [Output("Result1", LineColor = "lightGreen")]
        public IndicatorDataSeries Result1 { get; set; }

        [Output("Result2", LineColor = "Red")]
        public IndicatorDataSeries Result2 { get; set; }

        [Output("Result3", LineColor = "DodgerBlue")]
        public IndicatorDataSeries Result3 { get; set; }

        [Output("Result4", LineColor = "DodgerBlue")]
        public IndicatorDataSeries Result4 { get; set; }

        [Output("Result5", LineColor = "DodgerBlue")]
        public IndicatorDataSeries Result5 { get; set; }

        [Output("Result6", LineColor = "Yelow")]
        public IndicatorDataSeries Result6 { get; set; }

        private Bars tf;

        private int idx;
        private int previousIdx;
        private int buyPeriod;
        private int sellPeriod;

        private double buyAverage;
        private double sellAverage;
        private int savedbp;
        private int savedsp;
        private double CenterMean, UpperMean, LowerMean;

        private double price, level, previousLevel, result;
        private bool bullish, bearish;

        protected override void Initialize()
        {
            tf = MarketData.GetBars(Bars.TimeFrame);

            previousLevel = Math.Round(Bars.ClosePrices.LastValue, 4);
            bullish = true;
            bearish = false;
        }

        public override void Calculate(int index)
        {
            idx = tf.OpenTimes.GetIndexByTime(Bars.OpenTimes[index]);
            if (idx > previousIdx)
            {
                buyPeriod++;
                savedbp = buyPeriod;
                sellPeriod++;
                savedsp = sellPeriod;
            }

            if (buyPeriod == 0)
                buyPeriod = savedbp;

            if (sellPeriod == 0)
                sellPeriod = savedsp;

            buyAverage = Bars.OpenPrices.Sum(buyPeriod) / buyPeriod;
            sellAverage = Bars.OpenPrices.Sum(sellPeriod) / sellPeriod;

            if (Bars.ClosePrices[index] > buyAverage)
            {
                buyAverage = Bars.ClosePrices[index];
                buyPeriod = 0;
            }

            if (Bars.ClosePrices[index] < sellAverage)
            {
                sellAverage = Bars.ClosePrices[index];
                sellPeriod = 0;
            }

            price = Bars.ClosePrices[index];
            level = Math.Round(price, 4);

            if (level > previousLevel)
                if (price < level)
                    level = level - 0.0001;

            if (level < previousLevel)
                if (price > level)
                    level = level + 0.0001;

            result = Math.Round(level - previousLevel, 4);

            if (bullish)
            {
                if (result == -0.0001)
                    level = level + 0.0001;
                if (result == -0.0002 || result < -0.0002)
                {
                    bullish = false;
                    bearish = true;
                }
            }

            if (bearish)
            {
                if (result == 0.0001)
                    level = level - 0.0001;
                if (result == 0.0002 || result > 0.0002)
                {
                    bullish = true;
                    bearish = false;
                }
            }

            CenterMean = (buyAverage + sellAverage) / 2;
            UpperMean = (buyAverage + CenterMean) / 2;
            LowerMean = (CenterMean + sellAverage) / 2;

            Result1[index] = sellAverage;
            Result2[index] = buyAverage;
            Result3[index] = CenterMean;
            Result4[index] = UpperMean;
            Result5[index] = LowerMean;
            Result6[index] = level;

            previousIdx = idx;
            previousLevel = level;
        }
    }
}


srm_bcn's avatar
srm_bcn

Joined on 01.09.2019

  • Distribution: Free
  • Language: C#
  • Trading platform: cTrader Automate
  • File name: SpongeBob.algo
  • Rating: 5
  • Installs: 1081
Comments
Log in to add a comment.
juanbertrading's avatar
juanbertrading · 3 years ago

How can I exactly use this?