Swing high code

Created at 02 Sep 2021, 22:48
How’s your experience with the cTrader Platform?
Your feedback is crucial to cTrader's development. Please take a few seconds to share your opinion and help us improve your trading experience. Thanks!
Nickachino's avatar

Nickachino

Joined 03.04.2017

Swing high code
02 Sep 2021, 22:48


Hi,

 

Anyone that can help me code the following:

 

I want to store the last two swing highs and compare it to each other. A swing high is defined as a 3 bar high. For example if the candle high is higher than the previous three bar highs and higher than the next bar's high then it is regarded as a swing high. What I want is two have the last two swing high to compare them to each other to see if the last swing high is higher or lower than the previous one.

 

Below is a screen shot of the last two swing highs as defined above:

 


@Nickachino
Replies

amusleh
03 Sep 2021, 10:31

Hi,

Try these extension methods (Copy this and paste it at the bottom of your indicator class):

public static class DataSeriesExtensions
    {

        /// <summary>
        /// Returns the maximum value between start and end (inclusive) index in a dataseries
        /// </summary>
        /// <param name="dataSeries"></param>
        /// <param name="startIndex">Start index (Ex: 1)</param>
        /// <param name="endIndex">End index (Ex: 10)</param>
        /// <returns>double</returns>
        public static double Maximum(this DataSeries dataSeries, int startIndex, int endIndex)
        {
            var max = double.NegativeInfinity;

            for (var i = startIndex; i <= endIndex; i++)
            {
                max = Math.Max(dataSeries[i], max);
            }

            return max;
        }
		
		/// <summary>
        /// Checks if the index value is higher than x previous and future values in a data series
        /// </summary>
        /// <param name="dataSeries"></param>
        /// <param name="index">Dataseries value index</param>
        /// <param name="previousValues">The number of index previous values to check</param>
        /// <param name="futureValues">The number of index future values to check</param>
        /// <param name="equal">Check for equality</param>
        /// <returns>bool</returns>
        public static bool IsHigher(
            this DataSeries dataSeries, int index, int previousValues = 0, int futureValues = 0, bool equal = true)
        {
            var previousBarsHighest = previousValues > 0 ? dataSeries.Maximum(index - previousValues, index - 1) : double.NegativeInfinity;
            var futureBarsHighest = futureValues > 0 ? dataSeries.Maximum(index + 1, index + futureValues) : double.NegativeInfinity;

            if (equal)
            {
                return dataSeries[index] >= previousBarsHighest && dataSeries[index] >= futureBarsHighest;
            }
            else
            {
                return dataSeries[index] > previousBarsHighest && dataSeries[index] > futureBarsHighest;
            }
        }
	}

Usage:

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 NewIndicator : Indicator
    {

        protected override void Initialize()
        {
        }

        public override void Calculate(int index)
        {
            bool isSwingHigh = Bars.ClosePrices.IsHigher(index, 3, 3, false);
        }
    }
}

 


@amusleh