HL Averages TF Stoch free

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 = 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;
        }
    }
}
Comments
0