HLAveragesTF free

by srm_bcn in category Trend at 02/08/2020
Description

Notification Publishing copyrighted material is strictly prohibited. If you believe there is copyrighted material in this section you may use the Copyright Infringement Notification form to submit a claim.
Formula / Source Code
Language: C#
Trading Platform: cAlgocTrader
´╗┐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 HLAveragesTF : Indicator
    {
        [Parameter("Custom TF")]
        public TimeFrame customTimeFrame { get; set; }

        [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 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];
            }

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

            previousIdx = idx;
        }
    }
}
Comments