Category Oscilators  Published on 30/06/2020

HL Averages TF Stoch

Description


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

namespace cAlgo
{
    [Indicator(IsOverlay = false, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class HLAveragesTFStoch : Indicator
    {
        [Parameter("Custom TF")]
        public TimeFrame customTimeFrame { get; set; }

        [Output("Result1", LineColor = "Green")]
        public IndicatorDataSeries Result { get; set; }

        private Bars customBars;

        private int idx;
        private int previousIdx;
        private int buyPeriod;
        private int sellPeriod;
        private double buyAverage;
        private double sellAverage;

        protected override void Initialize()
        {
            customBars = MarketData.GetBars(customTimeFrame);
        }

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

            buyAverage = 0;
            for (int i = idx - buyPeriod + 1; i <= idx; i++)
                buyAverage += customBars.HighPrices[i];
            buyAverage = buyAverage / buyPeriod;

            sellAverage = 0;
            for (int i = idx - sellPeriod + 1; i <= idx; i++)
                sellAverage += customBars.LowPrices[i];
            sellAverage = sellAverage / sellPeriod;

            if (Bars.HighPrices[index] >= buyAverage)
            {
                buyPeriod = 1;
                buyAverage = Bars.HighPrices[index];
            }

            if (Bars.LowPrices[index] <= sellAverage)
            {
                sellPeriod = 1;
                sellAverage = Bars.LowPrices[index];
            }

            Result[index] = (Bars.ClosePrices[index] - sellAverage) / (buyAverage - sellAverage) * 100;

            previousIdx = idx;
        }
    }
}


srm_bcn's avatar
srm_bcn

Joined on 01.09.2019

  • Distribution: Free
  • Language: C#
  • Trading platform: cTrader Automate
  • File name: HLAveragesTFStoch.algo
  • Rating: 0
  • Installs: 1268
  • Modified: 13/10/2021 09:54
Comments
Log in to add a comment.
No comments found.