Category Trend  Published on 30/06/2020

HL Averages

Description


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 HLAverages : Indicator
    {
        [Output("Result1", LineColor = "Green")]
        public IndicatorDataSeries Result1 { get; set; }

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

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

        private Bars tf;

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

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

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

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

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

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

            Result1[index] = sellAverage;
            Result2[index] = buyAverage;
            Result3[index] = (buyAverage + sellAverage) / 2;

            previousIdx = idx;
        }
    }
}


srm_bcn's avatar
srm_bcn

Joined on 01.09.2019

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